@matheusbsbs / matheus-bagliano / commits / c192f9c865
Fork 0

git-rebase--interactive.sh: preserve-merges fails on merges created with no-ff

'git rebase' uses 'git merge' to preserve merges (-p). This preserves the original merge commit correctly, except when the original merge commit was created by 'git merge --no-ff'. In this case, 'git rebase' will fail to preserve the merge, because during 'git rebase', 'git merge' will simply fast-forward and skip the commit. For example: B / \ A---M / ---o---O---P---Q If we try to rebase M onto P, we lose the merge commit and this happens: A---B / ---o---O---P---Q To correct this, we simply do a "no fast-forward" on all merge commits when rebasing. Since by the time we decided to do a 'git merge' inside 'git rebase', it means there was a merge originally, so 'git merge' should always create a merge commit regardless of what the merge branches look like. This way, when rebase M onto P from the above example, we get: B / \ A---M / ---o---O---P---Q Signed-off-by: Andrew Wong <andrew.kw.w@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Andrew Wong committed Apr 28, 2011 at 00:35 UTC c192f9c865dbdae48c0400d717581d34cd315fb8
2 files changed +32 -2
git-rebase--interactive.sh
+1 -1
@@ -339,7 +339,7 @@ pick_one_preserving_merges () {
339 # No point in merging the first parent, that's HEAD
340 new_parents=${new_parents# $first_parent}
341 if ! do_with_author output \
342 - git merge $STRATEGY -m "$msg" $new_parents
342 + git merge --no-ff $STRATEGY -m "$msg" $new_parents
343 then
344 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
345 die_with_patch $sha1 "Error redoing merge $sha1"
t/t3409-rebase-preserve-merges.sh
+31 -1
@@ -27,7 +27,17 @@ export GIT_AUTHOR_EMAIL
27 # \
28 # B2 <-- origin/topic
29 #
30 -# In both cases, 'topic' is rebased onto 'origin/topic'.
30 +# Clone 3 (no-ff merge):
31 +#
32 +# A1--A2--B3 <-- origin/master
33 +# \
34 +# B1------M <-- topic
35 +# \ /
36 +# \--A3 <-- topic2
37 +# \
38 +# B2 <-- origin/topic
39 +#
40 +# In all cases, 'topic' is rebased onto 'origin/topic'.
41
42 test_expect_success 'setup for merge-preserving rebase' \
43 'echo First > A &&
@@ -61,6 +71,16 @@ test_expect_success 'setup for merge-preserving rebase' \
71 git commit -m "Merge origin/master into topic"
72 ) &&
73
74 + git clone ./. clone3 &&
75 + (
76 + cd clone3 &&
77 + git checkout -b topic2 origin/topic &&
78 + echo Sixth > A &&
79 + git commit -a -m "Modify A3" &&
80 + git checkout -b topic origin/topic &&
81 + git merge --no-ff topic2
82 + ) &&
83 +
84 git checkout topic &&
85 echo Fourth >> B &&
86 git commit -a -m "Modify B2"
@@ -93,4 +113,14 @@ test_expect_success '--continue works after a conflict' '
113 )
114 '
115
116 +test_expect_success 'rebase -p preserves no-ff merges' '
117 + (
118 + cd clone3 &&
119 + git fetch &&
120 + git rebase -p origin/topic &&
121 + test 3 = $(git rev-list --all --pretty=oneline | grep "Modify A" | wc -l) &&
122 + test 1 = $(git rev-list --all --pretty=oneline | grep "Merge branch" | wc -l)
123 + )
124 +'
125 +
126 test_done