checkout: inform the user when removing branch state
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Fri, 29 Mar 2019 10:38:59 +0000 (17:38 +0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 2 Apr 2019 04:56:59 +0000 (13:56 +0900)
After a successful switch, if a merge, cherry-pick or revert is ongoing,
it is canceled. This behavior has been with us from the very early
beginning, soon after git-merge was created but never actually
documented [1]. It may be a good idea to be transparent and tell the
user if some operation is canceled.

I consider this a better way of telling the user than just adding a
sentence or two in git-checkout.txt, which will be mostly ignored
anyway.

PS. Originally I wanted to print more details like

warning: cancelling an in-progress merge from <SHA-1>

which may allow some level of undo if the user wants to. But that seems
a lot more work. Perhaps it can be improved later if people still want
that.

[1] ... and I will try not to argue whether it is a sensible behavior.
There is some more discussion here if people are interested:
CACsJy8Axa5WsLSjiscjnxVK6jQHkfs-gH959=YtUvQkWriAk5w@mail.gmail.com

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
branch.c
branch.h
builtin/am.c
builtin/checkout.c
builtin/rebase.c
builtin/reset.c
builtin/revert.c
index 28b81a7e0256b2923c0fe296d9618d130821cdc4..8dd5bb9f1c38ac5bf94b9cb845b3dba0daceac10 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -337,11 +337,14 @@ void create_branch(struct repository *r,
        free(real_ref);
 }
 
-void remove_branch_state(struct repository *r)
+void remove_branch_state(struct repository *r, int verbose)
 {
-       unlink(git_path_cherry_pick_head(r));
-       unlink(git_path_revert_head(r));
-       unlink(git_path_merge_head(r));
+       if (!unlink(git_path_cherry_pick_head(r)) && verbose)
+               warning(_("cancelling a cherry picking in progress"));
+       if (!unlink(git_path_revert_head(r)) && verbose)
+               warning(_("cancelling a revert in progress"));
+       if (!unlink(git_path_merge_head(r)) && verbose)
+               warning(_("cancelling a merge in progress"));
        unlink(git_path_merge_rr(r));
        unlink(git_path_merge_msg(r));
        unlink(git_path_merge_mode(r));
index 29c1afa4d0ce5d1639b81d3d652d5b16085218e9..aed045901eb9cfea5ea6348dc8229a2e7af24ff9 100644 (file)
--- a/branch.h
+++ b/branch.h
@@ -64,7 +64,7 @@ extern int validate_new_branchname(const char *name, struct strbuf *ref, int for
  * Remove information about the state of working on the current
  * branch. (E.g., MERGE_HEAD)
  */
-void remove_branch_state(struct repository *r);
+void remove_branch_state(struct repository *r, int verbose);
 
 /*
  * Configure local branch "local" as downstream to branch "remote"
index 4fb107a9d1bdf36ff4c71d761abdda4d50d0e24b..99b66508fdb8f2e7fa5409a7d5f9d9f2901dae5f 100644 (file)
@@ -1957,7 +1957,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
        if (merge_tree(remote_tree))
                return -1;
 
-       remove_branch_state(the_repository);
+       remove_branch_state(the_repository, 0);
 
        return 0;
 }
index 0e6037b2968f326ca2781fa141d74683e52c983c..f66bd2f56db77eadfe3c4f430bb4ca869e1fd1e1 100644 (file)
@@ -899,7 +899,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
                                delete_reflog(old_branch_info->path);
                }
        }
-       remove_branch_state(the_repository);
+       remove_branch_state(the_repository, !opts->quiet);
        strbuf_release(&msg);
        if (!opts->quiet &&
            (new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD"))))
index 52114cbf0d9f98d52a5b48d8806019d14b1c7029..646d0f9fb1c2ef9995b77feb0a8013acde73a37e 100644 (file)
@@ -1272,7 +1272,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
                if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
                               NULL, NULL) < 0)
                        die(_("could not discard worktree changes"));
-               remove_branch_state(the_repository);
+               remove_branch_state(the_repository, 0);
                if (read_basic_state(&options))
                        exit(1);
                goto run_rebase;
@@ -1292,7 +1292,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
                               NULL, NULL) < 0)
                        die(_("could not move back to %s"),
                            oid_to_hex(&options.orig_head));
-               remove_branch_state(the_repository);
+               remove_branch_state(the_repository, 0);
                ret = finish_rebase(&options);
                goto cleanup;
        }
index 7882829a95d8294ea5bb5bcba3335e651233d79a..6d9397c84434c4ba09874a1146095d3d396e045c 100644 (file)
@@ -420,7 +420,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
                        print_new_head_line(lookup_commit_reference(the_repository, &oid));
        }
        if (!pathspec.nr)
-               remove_branch_state(the_repository);
+               remove_branch_state(the_repository, 0);
 
        return update_ref_status;
 }
index a47b53ceaff92d4079b4ea73a61ce1552ad66b05..ebf2789225bf4973d03302dc58ce7fddd76c4eeb 100644 (file)
@@ -196,7 +196,7 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
        if (cmd == 'q') {
                int ret = sequencer_remove_state(opts);
                if (!ret)
-                       remove_branch_state(the_repository);
+                       remove_branch_state(the_repository, 0);
                return ret;
        }
        if (cmd == 'c')