Skip to content

Commit 1ab6795

Browse files
phil-blaingitster
authored andcommitted
rebase -r: do create merge commit after empty resolution
When a user runs 'git rebase --continue' to conclude a conflicted merge during a 'git rebase -r' invocation, we do not create a merge commit if the resolution was empty (i.e. if the index and HEAD are identical). We simply continue the rebase as if no 'merge' instruction had been given. This is confusing since all commits from the side branch are absent from the rebased history. What's more, if that 'merge' is the last instruction in the todo list, we fail to remove the merge state, such that running 'git status' shows we are still merging after the rebase has concluded. This happens because in 'sequencer.c::commit_staged_changes', we exit early before calling 'run_git_commit' if 'is_clean' is true, i.e. if nothing is staged. Fix this by also checking for the presence of MERGE_HEAD before exiting early, such that we do call 'run_git_commit' when MERGE_HEAD is present. This also ensures that we unlink git_path_merge_head later in 'commit_staged_changes' to clear the merge state. Make sure to also remove MERGE_HEAD when a merge command fails to start. We already remove MERGE_MSG since e032abd (rebase: fix rewritten list for failed pick, 2023-09-06). Removing MERGE_HEAD ensures that in this situation, upon 'git rebase --continue' we still exit early in 'commit_staged_changes', without calling 'run_git_commit'. This is already covered by t5407.11, which fails without this change because we enter 'run_git_commit' and then fail to find 'rebase_path_message'. Signed-off-by: Philippe Blain <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 683c54c commit 1ab6795

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

sequencer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4349,6 +4349,7 @@ static int do_merge(struct repository *r,
43494349
error(_("could not even attempt to merge '%.*s'"),
43504350
merge_arg_len, arg);
43514351
unlink(git_path_merge_msg(r));
4352+
unlink(git_path_merge_head(r));
43524353
goto leave_merge;
43534354
}
43544355
/*
@@ -5364,7 +5365,7 @@ static int commit_staged_changes(struct repository *r,
53645365
flags |= AMEND_MSG;
53655366
}
53665367

5367-
if (is_clean) {
5368+
if (is_clean && !file_exists(git_path_merge_head(r))) {
53685369
if (refs_ref_exists(get_main_ref_store(r),
53695370
"CHERRY_PICK_HEAD") &&
53705371
refs_delete_ref(get_main_ref_store(r), "",

t/t3418-rebase-continue.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,30 @@ test_expect_success 'rebase -r passes merge strategy options correctly' '
111111
git rebase --continue
112112
'
113113

114+
test_expect_success '--continue creates merge commit after empty resolution' '
115+
git reset --hard main &&
116+
git checkout -b rebase_i_merge &&
117+
test_commit unrelated &&
118+
git checkout -b rebase_i_merge_side &&
119+
test_commit side2 main.txt &&
120+
git checkout rebase_i_merge &&
121+
test_commit side1 main.txt &&
122+
PICK=$(git rev-parse --short rebase_i_merge) &&
123+
test_must_fail git merge rebase_i_merge_side &&
124+
echo side1 >main.txt &&
125+
git add main.txt &&
126+
test_tick &&
127+
git commit --no-edit &&
128+
FAKE_LINES="1 2 3 5 6 7 8 9 10 11" &&
129+
export FAKE_LINES &&
130+
test_must_fail git rebase -ir main &&
131+
echo side1 >main.txt &&
132+
git add main.txt &&
133+
git rebase --continue &&
134+
git log --merges >out &&
135+
test_grep "Merge branch '\''rebase_i_merge_side'\''" out
136+
'
137+
114138
test_expect_success '--skip after failed fixup cleans commit message' '
115139
test_when_finished "test_might_fail git rebase --abort" &&
116140
git checkout -b with-conflicting-fixup &&

0 commit comments

Comments
 (0)