Skip to content

Commit 7ccdf65

Browse files
dschogitster
authored andcommitted
rebase --rebase-merges: avoid "empty merges"
The `git merge` command does not allow merging commits that are already reachable from HEAD: `git merge HEAD^`, for example, will report that we are already up to date and not change a thing. In an interactive rebase, such a merge could occur previously, e.g. when competing (or slightly modified) versions of a patch series were applied upstream, and the user had to `git rebase --skip` all of the local commits, and the topic branch becomes "empty" as a consequence. Let's teach the todo command `merge` to behave the same as `git merge`. Seeing as it requires some low-level trickery to create such merges with Git's commands in the first place, we do not even have to bother to introduce an option to force `merge` to create such merge commits. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 537e7d6 commit 7ccdf65

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

sequencer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,13 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
28102810
write_message("no-ff", 5, git_path_merge_mode(), 0);
28112811

28122812
bases = get_merge_bases(head_commit, merge_commit);
2813+
if (bases && !oidcmp(&merge_commit->object.oid,
2814+
&bases->item->object.oid)) {
2815+
ret = 0;
2816+
/* skip merging an ancestor of HEAD */
2817+
goto leave_merge;
2818+
}
2819+
28132820
for (j = bases; j; j = j->next)
28142821
commit_list_insert(j->item, &reversed);
28152822
free_commit_list(bases);

t/t3430-rebase-merges.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,12 @@ test_expect_success 'post-rewrite hook and fixups work for merges' '
215215
test_cmp expect actual
216216
'
217217

218+
test_expect_success 'refuse to merge ancestors of HEAD' '
219+
echo "merge HEAD^" >script-from-scratch &&
220+
test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
221+
before="$(git rev-parse HEAD)" &&
222+
git rebase -i HEAD &&
223+
test_cmp_rev HEAD $before
224+
'
225+
218226
test_done

0 commit comments

Comments
 (0)