rebase --continue: remove .git/MERGE_MSG

If the user skips the final commit by removing all the changes from the index and worktree with 'git restore' (or read-tree) and then runs 'git rebase --continue' .git/MERGE_MSG is left behind. This will seed the commit message the next time the user commits which is not what we want to happen. Reported-by: Victor Gambier <vgambier@excilys.com> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Aug 12, 2021 at 13:42 UTC e5ee33e8551fa934862b8cbe50693f4841e7dace
3 files changed +21
sequencer.c
+3
@@ -4717,6 +4717,9 @@ static int commit_staged_changes(struct repository *r,
4717 refs_delete_ref(get_main_ref_store(r), "",
4718 "CHERRY_PICK_HEAD", NULL, 0))
4719 return error(_("could not remove CHERRY_PICK_HEAD"));
4720 + if (unlink(git_path_merge_msg(r)) && errno != ENOENT)
4721 + return error_errno(_("could not remove '%s'"),
4722 + git_path_merge_msg(r));
4723 if (!final_fixup)
4724 return 0;
4725 }
t/t3403-rebase-skip.sh
+8
@@ -20,6 +20,7 @@ test_expect_success setup '
20 git add hello &&
21 git commit -m "hello" &&
22 git branch skip-reference &&
23 + git tag hello &&
24
25 echo world >> hello &&
26 git commit -a -m "hello world" &&
@@ -96,6 +97,13 @@ test_expect_success 'moved back to branch correctly' '
97
98 test_debug 'gitk --all & sleep 1'
99
100 +test_expect_success 'skipping final pick removes .git/MERGE_MSG' '
101 + test_must_fail git rebase --onto hello reverted-goodbye^ \
102 + reverted-goodbye &&
103 + git rebase --skip &&
104 + test_path_is_missing .git/MERGE_MSG
105 +'
106 +
107 test_expect_success 'correct advice upon picking empty commit' '
108 test_when_finished "git rebase --abort" &&
109 test_must_fail git rebase -i --onto goodbye \
t/t3418-rebase-continue.sh
+10
@@ -31,6 +31,16 @@ test_expect_success 'merge based rebase --continue with works with touched file'
31 git rebase --continue
32 '
33
34 +test_expect_success 'merge based rebase --continue removes .git/MERGE_MSG' '
35 + git checkout -f --detach topic &&
36 +
37 + test_must_fail git rebase --onto main HEAD^ &&
38 + git read-tree --reset -u HEAD &&
39 + test_path_is_file .git/MERGE_MSG &&
40 + git rebase --continue &&
41 + test_path_is_missing .git/MERGE_MSG
42 +'
43 +
44 test_expect_success 'apply based rebase --continue works with touched file' '
45 rm -fr .git/rebase-* &&
46 git reset --hard &&