checkout: inform the user when removing branch state

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>

Nguyễn Thái Ngọc Duy committed Mar 29, 2019 at 17:38 UTC f4a4b9aca3cb70fabe3e856fa61ca35a99e2b8eb
7 files changed +14 -11
branch.c
+7 -4
@@ -337,11 +337,14 @@ void create_branch(struct repository *r,
337 free(real_ref);
338 }
339
340 -void remove_branch_state(struct repository *r)
340 +void remove_branch_state(struct repository *r, int verbose)
341 {
342 - unlink(git_path_cherry_pick_head(r));
343 - unlink(git_path_revert_head(r));
344 - unlink(git_path_merge_head(r));
342 + if (!unlink(git_path_cherry_pick_head(r)) && verbose)
343 + warning(_("cancelling a cherry picking in progress"));
344 + if (!unlink(git_path_revert_head(r)) && verbose)
345 + warning(_("cancelling a revert in progress"));
346 + if (!unlink(git_path_merge_head(r)) && verbose)
347 + warning(_("cancelling a merge in progress"));
348 unlink(git_path_merge_rr(r));
349 unlink(git_path_merge_msg(r));
350 unlink(git_path_merge_mode(r));
branch.h
+1 -1
@@ -64,7 +64,7 @@ extern int validate_new_branchname(const char *name, struct strbuf *ref, int for
64 * Remove information about the state of working on the current
65 * branch. (E.g., MERGE_HEAD)
66 */
67 -void remove_branch_state(struct repository *r);
67 +void remove_branch_state(struct repository *r, int verbose);
68
69 /*
70 * Configure local branch "local" as downstream to branch "remote"
builtin/am.c
+1 -1
@@ -1957,7 +1957,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
1957 if (merge_tree(remote_tree))
1958 return -1;
1959
1960 - remove_branch_state(the_repository);
1960 + remove_branch_state(the_repository, 0);
1961
1962 return 0;
1963 }
builtin/checkout.c
+1 -1
@@ -899,7 +899,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
899 delete_reflog(old_branch_info->path);
900 }
901 }
902 - remove_branch_state(the_repository);
902 + remove_branch_state(the_repository, !opts->quiet);
903 strbuf_release(&msg);
904 if (!opts->quiet &&
905 (new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD"))))
builtin/rebase.c
+2 -2
@@ -1272,7 +1272,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1272 if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1273 NULL, NULL) < 0)
1274 die(_("could not discard worktree changes"));
1275 - remove_branch_state(the_repository);
1275 + remove_branch_state(the_repository, 0);
1276 if (read_basic_state(&options))
1277 exit(1);
1278 goto run_rebase;
@@ -1292,7 +1292,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1292 NULL, NULL) < 0)
1293 die(_("could not move back to %s"),
1294 oid_to_hex(&options.orig_head));
1295 - remove_branch_state(the_repository);
1295 + remove_branch_state(the_repository, 0);
1296 ret = finish_rebase(&options);
1297 goto cleanup;
1298 }
builtin/reset.c
+1 -1
@@ -420,7 +420,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
420 print_new_head_line(lookup_commit_reference(the_repository, &oid));
421 }
422 if (!pathspec.nr)
423 - remove_branch_state(the_repository);
423 + remove_branch_state(the_repository, 0);
424
425 return update_ref_status;
426 }
builtin/revert.c
+1 -1
@@ -196,7 +196,7 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
196 if (cmd == 'q') {
197 int ret = sequencer_remove_state(opts);
198 if (!ret)
199 - remove_branch_state(the_repository);
199 + remove_branch_state(the_repository, 0);
200 return ret;
201 }
202 if (cmd == 'c')