Skip to content

Commit

Permalink
git-rebase--interactive.sh: preserve-merges fails on merges created w…
Browse files Browse the repository at this point in the history
…ith 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>
  • Loading branch information
andrewkww authored and gitster committed Apr 28, 2011
1 parent 4fec830 commit c192f9c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion git-rebase--interactive.sh
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pick_one_preserving_merges () {
# No point in merging the first parent, that's HEAD # No point in merging the first parent, that's HEAD
new_parents=${new_parents# $first_parent} new_parents=${new_parents# $first_parent}
if ! do_with_author output \ if ! do_with_author output \
git merge $STRATEGY -m "$msg" $new_parents git merge --no-ff $STRATEGY -m "$msg" $new_parents
then then
printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
die_with_patch $sha1 "Error redoing merge $sha1" die_with_patch $sha1 "Error redoing merge $sha1"
Expand Down
32 changes: 31 additions & 1 deletion t/t3409-rebase-preserve-merges.sh
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ export GIT_AUTHOR_EMAIL
# \ # \
# B2 <-- origin/topic # B2 <-- origin/topic
# #
# In both cases, 'topic' is rebased onto 'origin/topic'. # Clone 3 (no-ff merge):
#
# A1--A2--B3 <-- origin/master
# \
# B1------M <-- topic
# \ /
# \--A3 <-- topic2
# \
# B2 <-- origin/topic
#
# In all cases, 'topic' is rebased onto 'origin/topic'.


test_expect_success 'setup for merge-preserving rebase' \ test_expect_success 'setup for merge-preserving rebase' \
'echo First > A && 'echo First > A &&
Expand Down Expand Up @@ -61,6 +71,16 @@ test_expect_success 'setup for merge-preserving rebase' \
git commit -m "Merge origin/master into topic" git commit -m "Merge origin/master into topic"
) && ) &&
git clone ./. clone3 &&
(
cd clone3 &&
git checkout -b topic2 origin/topic &&
echo Sixth > A &&
git commit -a -m "Modify A3" &&
git checkout -b topic origin/topic &&
git merge --no-ff topic2
) &&
git checkout topic && git checkout topic &&
echo Fourth >> B && echo Fourth >> B &&
git commit -a -m "Modify B2" git commit -a -m "Modify B2"
Expand Down Expand Up @@ -93,4 +113,14 @@ test_expect_success '--continue works after a conflict' '
) )
' '


test_expect_success 'rebase -p preserves no-ff merges' '
(
cd clone3 &&
git fetch &&
git rebase -p origin/topic &&
test 3 = $(git rev-list --all --pretty=oneline | grep "Modify A" | wc -l) &&
test 1 = $(git rev-list --all --pretty=oneline | grep "Merge branch" | wc -l)
)
'

test_done test_done

0 comments on commit c192f9c

Please sign in to comment.