Git 2.23
[gitweb.git] / builtin / checkout.c
index 98dc2ded38fe971ca4e746ffbc94b8a128e0d4d3..6123f732a2c84f975c576e7077a0c6da369e6b31 100644 (file)
@@ -39,7 +39,7 @@ static const char * const switch_branch_usage[] = {
 };
 
 static const char * const restore_usage[] = {
-       N_("git restore [<options>] [<branch>] -- <file>..."),
+       N_("git restore [<options>] [--source=<branch>] <file>..."),
        NULL,
 };
 
@@ -59,11 +59,17 @@ struct checkout_opts {
        int overlay_mode;
        int dwim_new_local_branch;
        int discard_changes;
+       int accept_ref;
        int accept_pathspec;
        int switch_branch_doing_nothing_is_ok;
        int only_merge_on_switching_branches;
        int can_switch_when_in_progress;
        int orphan_from_empty_tree;
+       int empty_pathspec_ok;
+       int checkout_index;
+       int checkout_worktree;
+       const char *ignore_unmerged_opt;
+       int ignore_unmerged;
 
        const char *new_branch;
        const char *new_branch_force;
@@ -76,6 +82,7 @@ struct checkout_opts {
        int branch_exists;
        const char *prefix;
        struct pathspec pathspec;
+       const char *from_treeish;
        struct tree *source_tree;
 };
 
@@ -327,17 +334,74 @@ static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
        }
 }
 
+static int checkout_worktree(const struct checkout_opts *opts)
+{
+       struct checkout state = CHECKOUT_INIT;
+       int nr_checkouts = 0, nr_unmerged = 0;
+       int errs = 0;
+       int pos;
+
+       state.force = 1;
+       state.refresh_cache = 1;
+       state.istate = &the_index;
+
+       enable_delayed_checkout(&state);
+       for (pos = 0; pos < active_nr; pos++) {
+               struct cache_entry *ce = active_cache[pos];
+               if (ce->ce_flags & CE_MATCHED) {
+                       if (!ce_stage(ce)) {
+                               errs |= checkout_entry(ce, &state,
+                                                      NULL, &nr_checkouts);
+                               continue;
+                       }
+                       if (opts->writeout_stage)
+                               errs |= checkout_stage(opts->writeout_stage,
+                                                      ce, pos,
+                                                      &state,
+                                                      &nr_checkouts, opts->overlay_mode);
+                       else if (opts->merge)
+                               errs |= checkout_merged(pos, &state,
+                                                       &nr_unmerged);
+                       pos = skip_same_name(ce, pos) - 1;
+               }
+       }
+       remove_marked_cache_entries(&the_index, 1);
+       remove_scheduled_dirs();
+       errs |= finish_delayed_checkout(&state, &nr_checkouts);
+
+       if (opts->count_checkout_paths) {
+               if (nr_unmerged)
+                       fprintf_ln(stderr, Q_("Recreated %d merge conflict",
+                                             "Recreated %d merge conflicts",
+                                             nr_unmerged),
+                                  nr_unmerged);
+               if (opts->source_tree)
+                       fprintf_ln(stderr, Q_("Updated %d path from %s",
+                                             "Updated %d paths from %s",
+                                             nr_checkouts),
+                                  nr_checkouts,
+                                  find_unique_abbrev(&opts->source_tree->object.oid,
+                                                     DEFAULT_ABBREV));
+               else if (!nr_unmerged || nr_checkouts)
+                       fprintf_ln(stderr, Q_("Updated %d path from the index",
+                                             "Updated %d paths from the index",
+                                             nr_checkouts),
+                                  nr_checkouts);
+       }
+
+       return errs;
+}
+
 static int checkout_paths(const struct checkout_opts *opts,
                          const char *revision)
 {
        int pos;
-       struct checkout state = CHECKOUT_INIT;
        static char *ps_matched;
        struct object_id rev;
        struct commit *head;
        int errs = 0;
        struct lock_file lock_file = LOCK_INIT;
-       int nr_checkouts = 0, nr_unmerged = 0;
+       int checkout_index;
 
        trace2_cmd_mode(opts->patch_mode ? "patch" : "path");
 
@@ -347,8 +411,9 @@ static int checkout_paths(const struct checkout_opts *opts,
        if (opts->new_branch_log)
                die(_("'%s' cannot be used with updating paths"), "-l");
 
-       if (opts->force && opts->patch_mode)
-               die(_("'%s' cannot be used with updating paths"), "-f");
+       if (opts->ignore_unmerged && opts->patch_mode)
+               die(_("'%s' cannot be used with updating paths"),
+                   opts->ignore_unmerged_opt);
 
        if (opts->force_detach)
                die(_("'%s' cannot be used with updating paths"), "--detach");
@@ -356,16 +421,46 @@ static int checkout_paths(const struct checkout_opts *opts,
        if (opts->merge && opts->patch_mode)
                die(_("'%s' cannot be used with %s"), "--merge", "--patch");
 
-       if (opts->force && opts->merge)
-               die(_("'%s' cannot be used with %s"), "-f", "-m");
+       if (opts->ignore_unmerged && opts->merge)
+               die(_("'%s' cannot be used with %s"),
+                   opts->ignore_unmerged_opt, "-m");
 
        if (opts->new_branch)
                die(_("Cannot update paths and switch to branch '%s' at the same time."),
                    opts->new_branch);
 
-       if (opts->patch_mode)
-               return run_add_interactive(revision, "--patch=checkout",
-                                          &opts->pathspec);
+       if (!opts->checkout_worktree && !opts->checkout_index)
+               die(_("neither '%s' or '%s' is specified"),
+                   "--staged", "--worktree");
+
+       if (!opts->checkout_worktree && !opts->from_treeish)
+               die(_("'%s' must be used when '%s' is not specified"),
+                   "--worktree", "--source");
+
+       if (opts->checkout_index && !opts->checkout_worktree &&
+           opts->writeout_stage)
+               die(_("'%s' or '%s' cannot be used with %s"),
+                   "--ours", "--theirs", "--staged");
+
+       if (opts->checkout_index && !opts->checkout_worktree &&
+           opts->merge)
+               die(_("'%s' or '%s' cannot be used with %s"),
+                   "--merge", "--conflict", "--staged");
+
+       if (opts->patch_mode) {
+               const char *patch_mode;
+
+               if (opts->checkout_index && opts->checkout_worktree)
+                       patch_mode = "--patch=checkout";
+               else if (opts->checkout_index && !opts->checkout_worktree)
+                       patch_mode = "--patch=reset";
+               else if (!opts->checkout_index && opts->checkout_worktree)
+                       patch_mode = "--patch=worktree";
+               else
+                       BUG("either flag must have been set, worktree=%d, index=%d",
+                           opts->checkout_worktree, opts->checkout_index);
+               return run_add_interactive(revision, patch_mode, &opts->pathspec);
+       }
 
        repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
        if (read_cache_preload(&opts->pathspec) < 0)
@@ -390,7 +485,7 @@ static int checkout_paths(const struct checkout_opts *opts,
                                                        ps_matched,
                                                        opts);
 
-       if (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {
+       if (report_path_error(ps_matched, &opts->pathspec)) {
                free(ps_matched);
                return 1;
        }
@@ -406,8 +501,9 @@ static int checkout_paths(const struct checkout_opts *opts,
                if (ce->ce_flags & CE_MATCHED) {
                        if (!ce_stage(ce))
                                continue;
-                       if (opts->force) {
-                               warning(_("path '%s' is unmerged"), ce->name);
+                       if (opts->ignore_unmerged) {
+                               if (!opts->quiet)
+                                       warning(_("path '%s' is unmerged"), ce->name);
                        } else if (opts->writeout_stage) {
                                errs |= check_stage(opts->writeout_stage, ce, pos, opts->overlay_mode);
                        } else if (opts->merge) {
@@ -423,57 +519,31 @@ static int checkout_paths(const struct checkout_opts *opts,
                return 1;
 
        /* Now we are committed to check them out */
-       state.force = 1;
-       state.refresh_cache = 1;
-       state.istate = &the_index;
+       if (opts->checkout_worktree)
+               errs |= checkout_worktree(opts);
 
-       enable_delayed_checkout(&state);
-       for (pos = 0; pos < active_nr; pos++) {
-               struct cache_entry *ce = active_cache[pos];
-               if (ce->ce_flags & CE_MATCHED) {
-                       if (!ce_stage(ce)) {
-                               errs |= checkout_entry(ce, &state,
-                                                      NULL, &nr_checkouts);
-                               continue;
-                       }
-                       if (opts->writeout_stage)
-                               errs |= checkout_stage(opts->writeout_stage,
-                                                      ce, pos,
-                                                      &state,
-                                                      &nr_checkouts, opts->overlay_mode);
-                       else if (opts->merge)
-                               errs |= checkout_merged(pos, &state,
-                                                       &nr_unmerged);
-                       pos = skip_same_name(ce, pos) - 1;
-               }
-       }
-       remove_marked_cache_entries(&the_index, 1);
-       remove_scheduled_dirs();
-       errs |= finish_delayed_checkout(&state, &nr_checkouts);
+       /*
+        * Allow updating the index when checking out from the index.
+        * This is to save new stat info.
+        */
+       if (opts->checkout_worktree && !opts->checkout_index && !opts->source_tree)
+               checkout_index = 1;
+       else
+               checkout_index = opts->checkout_index;
 
-       if (opts->count_checkout_paths) {
-               if (nr_unmerged)
-                       fprintf_ln(stderr, Q_("Recreated %d merge conflict",
-                                             "Recreated %d merge conflicts",
-                                             nr_unmerged),
-                                  nr_unmerged);
-               if (opts->source_tree)
-                       fprintf_ln(stderr, Q_("Updated %d path from %s",
-                                             "Updated %d paths from %s",
-                                             nr_checkouts),
-                                  nr_checkouts,
-                                  find_unique_abbrev(&opts->source_tree->object.oid,
-                                                     DEFAULT_ABBREV));
-               else if (!nr_unmerged || nr_checkouts)
-                       fprintf_ln(stderr, Q_("Updated %d path from the index",
-                                             "Updated %d paths from the index",
-                                             nr_checkouts),
-                                  nr_checkouts);
+       if (checkout_index) {
+               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+                       die(_("unable to write new index file"));
+       } else {
+               /*
+                * NEEDSWORK: if --worktree is not specified, we
+                * should save stat info of checked out files in the
+                * index to avoid the next (potentially costly)
+                * refresh. But it's a bit tricker to do...
+                */
+               rollback_lock_file(&lock_file);
        }
 
-       if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
-               die(_("unable to write new index file"));
-
        read_ref_full("HEAD", 0, &rev, NULL);
        head = lookup_commit_reference_gently(the_repository, &rev, 1);
 
@@ -614,7 +684,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
                topts.initial_checkout = is_cache_unborn();
                topts.update = 1;
                topts.merge = 1;
-               topts.gently = opts->merge && old_branch_info->commit;
+               topts.quiet = opts->merge && old_branch_info->commit;
                topts.verbose_update = opts->show_progress;
                topts.fn = twoway_merge;
                if (opts->overwrite_ignore) {
@@ -640,7 +710,10 @@ static int merge_working_tree(const struct checkout_opts *opts,
                         */
                        struct tree *result;
                        struct tree *work;
+                       struct tree *old_tree;
                        struct merge_options o;
+                       struct strbuf sb = STRBUF_INIT;
+
                        if (!opts->merge)
                                return 1;
 
@@ -650,6 +723,19 @@ static int merge_working_tree(const struct checkout_opts *opts,
                         */
                        if (!old_branch_info->commit)
                                return 1;
+                       old_tree = get_commit_tree(old_branch_info->commit);
+
+                       if (repo_index_has_changes(the_repository, old_tree, &sb))
+                               die(_("cannot continue with staged changes in "
+                                     "the following files:\n%s"), sb.buf);
+                       strbuf_release(&sb);
+
+                       if (repo_index_has_changes(the_repository,
+                                                  get_commit_tree(old_branch_info->commit),
+                                                  &sb))
+                               warning(_("staged changes in the following files may be lost: %s"),
+                                       sb.buf);
+                       strbuf_release(&sb);
 
                        /* Do more real merge */
 
@@ -687,7 +773,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
                        ret = merge_trees(&o,
                                          new_tree,
                                          work,
-                                         get_commit_tree(old_branch_info->commit),
+                                         old_tree,
                                          &result);
                        if (ret < 0)
                                exit(128);
@@ -1256,9 +1342,7 @@ static void die_if_some_operation_in_progress(void)
                      "Consider \"git revert --quit\" "
                      "or \"git worktree add\"."));
        if (state.bisect_in_progress)
-               die(_("cannot switch branch while bisecting\n"
-                     "Consider \"git bisect reset HEAD\" "
-                     "or \"git worktree add\"."));
+               warning(_("you are switching branch while bisecting"));
 }
 
 static int checkout_branch(struct checkout_opts *opts,
@@ -1271,9 +1355,9 @@ static int checkout_branch(struct checkout_opts *opts,
                die(_("'%s' cannot be used with switching branches"),
                    "--patch");
 
-       if (!opts->overlay_mode)
+       if (opts->overlay_mode != -1)
                die(_("'%s' cannot be used with switching branches"),
-                   "--no-overlay");
+                   "--[no]-overlay");
 
        if (opts->writeout_stage)
                die(_("'%s' cannot be used with switching branches"),
@@ -1351,8 +1435,6 @@ static struct option *add_common_options(struct checkout_opts *opts,
                            "checkout", "control recursive updating of submodules",
                            PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
                OPT_BOOL(0, "progress", &opts->show_progress, N_("force progress reporting")),
-               OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
-                          PARSE_OPT_NOCOMPLETE),
                OPT_BOOL('m', "merge", &opts->merge, N_("perform a 3-way merge with the new branch")),
                OPT_STRING(0, "conflict", &opts->conflict_style, N_("style"),
                           N_("conflict style (merge or diff3)")),
@@ -1370,6 +1452,8 @@ static struct option *add_common_switch_branch_options(
                OPT_BOOL('d', "detach", &opts->force_detach, N_("detach HEAD at named commit")),
                OPT_SET_INT('t', "track",  &opts->track, N_("set upstream info for new branch"),
                        BRANCH_TRACK_EXPLICIT),
+               OPT__FORCE(&opts->force, N_("force checkout (throw away local modifications)"),
+                          PARSE_OPT_NOCOMPLETE),
                OPT_STRING(0, "orphan", &opts->new_orphan_branch, N_("new-branch"), N_("new unparented branch")),
                OPT_BOOL_F(0, "overwrite-ignore", &opts->overwrite_ignore,
                           N_("update ignored files (default)"),
@@ -1396,7 +1480,6 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,
                OPT_BOOL('p', "patch", &opts->patch_mode, N_("select hunks interactively")),
                OPT_BOOL(0, "ignore-skip-worktree-bits", &opts->ignore_skipworktree,
                         N_("do not limit pathspecs to sparse entries only")),
-               OPT_BOOL(0, "overlay", &opts->overlay_mode, N_("use overlay mode (default)")),
                OPT_END()
        };
        struct option *newopts = parse_options_concat(prevopts, options);
@@ -1410,19 +1493,24 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
 {
        struct branch_info new_branch_info;
        int dwim_remotes_matched = 0;
+       int parseopt_flags = 0;
 
        memset(&new_branch_info, 0, sizeof(new_branch_info));
        opts->overwrite_ignore = 1;
        opts->prefix = prefix;
        opts->show_progress = -1;
-       opts->overlay_mode = -1;
 
        git_config(git_checkout_config, opts);
 
        opts->track = BRANCH_TRACK_UNSPECIFIED;
 
-       argc = parse_options(argc, argv, prefix, options, usagestr,
-                            PARSE_OPT_KEEP_DASHDASH);
+       if (!opts->accept_pathspec && !opts->accept_ref)
+               BUG("make up your mind, you need to take _something_");
+       if (opts->accept_pathspec && opts->accept_ref)
+               parseopt_flags = PARSE_OPT_KEEP_DASHDASH;
+
+       argc = parse_options(argc, argv, prefix, options,
+                            usagestr, parseopt_flags);
 
        if (opts->show_progress < 0) {
                if (opts->quiet)
@@ -1435,8 +1523,11 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
                opts->merge = 1; /* implied */
                git_xmerge_config("merge.conflictstyle", opts->conflict_style, NULL);
        }
-       if (opts->force)
+       if (opts->force) {
                opts->discard_changes = 1;
+               opts->ignore_unmerged_opt = "--force";
+               opts->ignore_unmerged = 1;
+       }
 
        if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)
                die(_("-b, -B and --orphan are mutually exclusive"));
@@ -1444,6 +1535,26 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
        if (opts->overlay_mode == 1 && opts->patch_mode)
                die(_("-p and --overlay are mutually exclusive"));
 
+       if (opts->checkout_index >= 0 || opts->checkout_worktree >= 0) {
+               if (opts->checkout_index < 0)
+                       opts->checkout_index = 0;
+               if (opts->checkout_worktree < 0)
+                       opts->checkout_worktree = 0;
+       } else {
+               if (opts->checkout_index < 0)
+                       opts->checkout_index = -opts->checkout_index - 1;
+               if (opts->checkout_worktree < 0)
+                       opts->checkout_worktree = -opts->checkout_worktree - 1;
+       }
+       if (opts->checkout_index < 0 || opts->checkout_worktree < 0)
+               BUG("these flags should be non-negative by now");
+       /*
+        * convenient shortcut: "git restore --staged" equals
+        * "git restore --staged --source HEAD"
+        */
+       if (!opts->from_treeish && opts->checkout_index && !opts->checkout_worktree)
+               opts->from_treeish = "HEAD";
+
        /*
         * From here on, new_branch will contain the branch to be checked out,
         * and new_branch_force and new_orphan_branch will tell us which one of
@@ -1481,7 +1592,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
         * including "last branch" syntax and DWIM-ery for names of
         * remote branches, erroring out for invalid or ambiguous cases.
         */
-       if (argc) {
+       if (argc && opts->accept_ref) {
                struct object_id rev;
                int dwim_ok =
                        !opts->patch_mode &&
@@ -1493,8 +1604,24 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
                                             &dwim_remotes_matched);
                argv += n;
                argc -= n;
+       } else if (!opts->accept_ref && opts->from_treeish) {
+               struct object_id rev;
+
+               if (get_oid_mb(opts->from_treeish, &rev))
+                       die(_("could not resolve %s"), opts->from_treeish);
+
+               setup_new_branch_info_and_source_tree(&new_branch_info,
+                                                     opts, &rev,
+                                                     opts->from_treeish);
+
+               if (!opts->source_tree)
+                       die(_("reference is not a tree: %s"), opts->from_treeish);
        }
 
+       if (opts->accept_pathspec && !opts->empty_pathspec_ok && !argc &&
+           !opts->patch_mode)  /* patch mode is special */
+               die(_("you must specify path(s) to restore"));
+
        if (argc) {
                parse_pathspec(&opts->pathspec, 0,
                               opts->patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,
@@ -1568,6 +1695,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                OPT_BOOL('l', NULL, &opts.new_branch_log, N_("create reflog for new branch")),
                OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
                         N_("second guess 'git checkout <no-such-branch>' (default)")),
+               OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
                OPT_END()
        };
        int ret;
@@ -1576,10 +1704,15 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
        opts.dwim_new_local_branch = 1;
        opts.switch_branch_doing_nothing_is_ok = 1;
        opts.only_merge_on_switching_branches = 0;
+       opts.accept_ref = 1;
        opts.accept_pathspec = 1;
        opts.implicit_detach = 1;
        opts.can_switch_when_in_progress = 1;
        opts.orphan_from_empty_tree = 0;
+       opts.empty_pathspec_ok = 1;
+       opts.overlay_mode = -1;
+       opts.checkout_index = -2;    /* default on */
+       opts.checkout_worktree = -2; /* default on */
 
        options = parse_options_dup(checkout_options);
        options = add_common_options(&opts, options);
@@ -1611,12 +1744,14 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
 
        memset(&opts, 0, sizeof(opts));
        opts.dwim_new_local_branch = 1;
+       opts.accept_ref = 1;
        opts.accept_pathspec = 0;
        opts.switch_branch_doing_nothing_is_ok = 0;
        opts.only_merge_on_switching_branches = 1;
        opts.implicit_detach = 0;
        opts.can_switch_when_in_progress = 0;
        opts.orphan_from_empty_tree = 1;
+       opts.overlay_mode = -1;
 
        options = parse_options_dup(switch_options);
        options = add_common_options(&opts, options);
@@ -1631,15 +1766,31 @@ int cmd_switch(int argc, const char **argv, const char *prefix)
 int cmd_restore(int argc, const char **argv, const char *prefix)
 {
        struct checkout_opts opts;
-       struct option *options = NULL;
+       struct option *options;
+       struct option restore_options[] = {
+               OPT_STRING('s', "source", &opts.from_treeish, "<tree-ish>",
+                          N_("which tree-ish to checkout from")),
+               OPT_BOOL('S', "staged", &opts.checkout_index,
+                          N_("restore the index")),
+               OPT_BOOL('W', "worktree", &opts.checkout_worktree,
+                          N_("restore the working tree (default)")),
+               OPT_BOOL(0, "ignore-unmerged", &opts.ignore_unmerged,
+                        N_("ignore unmerged entries")),
+               OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode")),
+               OPT_END()
+       };
        int ret;
 
        memset(&opts, 0, sizeof(opts));
-       opts.dwim_new_local_branch = 1;
-       opts.switch_branch_doing_nothing_is_ok = 0;
+       opts.accept_ref = 0;
        opts.accept_pathspec = 1;
+       opts.empty_pathspec_ok = 0;
+       opts.overlay_mode = 0;
+       opts.checkout_index = -1;    /* default off */
+       opts.checkout_worktree = -2; /* default on */
+       opts.ignore_unmerged_opt = "--ignore-unmerged";
 
-       options = parse_options_dup(options);
+       options = parse_options_dup(restore_options);
        options = add_common_options(&opts, options);
        options = add_checkout_path_options(&opts, options);