Skip to content

Commit f4a4b9a

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent af9ded5 commit f4a4b9a

File tree

7 files changed

+14
-11
lines changed

7 files changed

+14
-11
lines changed

branch.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,14 @@ void create_branch(struct repository *r,
337337
free(real_ref);
338338
}
339339

340-
void remove_branch_state(struct repository *r)
340+
void remove_branch_state(struct repository *r, int verbose)
341341
{
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"));
345348
unlink(git_path_merge_rr(r));
346349
unlink(git_path_merge_msg(r));
347350
unlink(git_path_merge_mode(r));

branch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extern int validate_new_branchname(const char *name, struct strbuf *ref, int for
6464
* Remove information about the state of working on the current
6565
* branch. (E.g., MERGE_HEAD)
6666
*/
67-
void remove_branch_state(struct repository *r);
67+
void remove_branch_state(struct repository *r, int verbose);
6868

6969
/*
7070
* Configure local branch "local" as downstream to branch "remote"

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ static int clean_index(const struct object_id *head, const struct object_id *rem
19571957
if (merge_tree(remote_tree))
19581958
return -1;
19591959

1960-
remove_branch_state(the_repository);
1960+
remove_branch_state(the_repository, 0);
19611961

19621962
return 0;
19631963
}

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
899899
delete_reflog(old_branch_info->path);
900900
}
901901
}
902-
remove_branch_state(the_repository);
902+
remove_branch_state(the_repository, !opts->quiet);
903903
strbuf_release(&msg);
904904
if (!opts->quiet &&
905905
(new_branch_info->path || (!opts->force_detach && !strcmp(new_branch_info->name, "HEAD"))))

builtin/rebase.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12721272
if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
12731273
NULL, NULL) < 0)
12741274
die(_("could not discard worktree changes"));
1275-
remove_branch_state(the_repository);
1275+
remove_branch_state(the_repository, 0);
12761276
if (read_basic_state(&options))
12771277
exit(1);
12781278
goto run_rebase;
@@ -1292,7 +1292,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
12921292
NULL, NULL) < 0)
12931293
die(_("could not move back to %s"),
12941294
oid_to_hex(&options.orig_head));
1295-
remove_branch_state(the_repository);
1295+
remove_branch_state(the_repository, 0);
12961296
ret = finish_rebase(&options);
12971297
goto cleanup;
12981298
}

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
420420
print_new_head_line(lookup_commit_reference(the_repository, &oid));
421421
}
422422
if (!pathspec.nr)
423-
remove_branch_state(the_repository);
423+
remove_branch_state(the_repository, 0);
424424

425425
return update_ref_status;
426426
}

builtin/revert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
196196
if (cmd == 'q') {
197197
int ret = sequencer_remove_state(opts);
198198
if (!ret)
199-
remove_branch_state(the_repository);
199+
remove_branch_state(the_repository, 0);
200200
return ret;
201201
}
202202
if (cmd == 'c')

0 commit comments

Comments
 (0)