Git 2.23
[gitweb.git] / builtin / checkout.c
index 71e25893408fce5d6992f8535ee7895cf3dfcaa0..6123f732a2c84f975c576e7077a0c6da369e6b31 100644 (file)
@@ -68,6 +68,8 @@ struct checkout_opts {
        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;
@@ -409,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");
@@ -418,8 +421,9 @@ 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."),
@@ -450,9 +454,11 @@ static int checkout_paths(const struct checkout_opts *opts,
                        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
-                       die(_("'%s' with only '%s' is not currently supported"),
-                           "--patch", "--worktree");
+                       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);
        }
 
@@ -479,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;
        }
@@ -495,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) {
@@ -677,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) {
@@ -703,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;
 
@@ -713,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 */
 
@@ -750,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);
@@ -1319,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,
@@ -1414,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)")),
@@ -1433,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)"),
@@ -1502,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"));
@@ -1524,6 +1548,12 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
        }
        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,
@@ -1739,11 +1769,13 @@ int cmd_restore(int argc, const char **argv, const char *prefix)
        struct option *options;
        struct option restore_options[] = {
                OPT_STRING('s', "source", &opts.from_treeish, "<tree-ish>",
-                          N_("where the checkout from")),
+                          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()
        };
@@ -1756,6 +1788,7 @@ int cmd_restore(int argc, const char **argv, const char *prefix)
        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(restore_options);
        options = add_common_options(&opts, options);