rebase: omit patch-identical commits with --fork-point

When the `--fork-point` argument was added to `git rebase`, we changed the value of $upstream to be the fork point instead of the point from which we want to rebase. When $orig_head..$upstream is empty this does not change the behaviour, but when there are new changes in the upstream we are no longer checking if any of them are patch-identical with changes in $upstream..$orig_head. Fix this by introducing a new variable to hold the fork point and using this to restrict the range as an extra (negative) revision argument so that the set of desired revisions becomes (in fork-point mode): git rev-list --cherry-pick --right-only \ $upstream...$orig_head ^$fork_point This allows us to correctly handle the scenario where we have the following topology: C --- D --- E <- dev / B <- master@{1} / o --- B' --- C* --- D* <- master where: - B' is a fixed-up version of B that is not patch-identical with B; - C* and D* are patch-identical to C and D respectively and conflict textually if applied in the wrong order; - E depends textually on D. The correct result of `git rebase master dev` is that B is identified as the fork-point of dev and master, so that C, D, E are the commits that need to be replayed onto master; but C and D are patch-identical with C* and D* and so can be dropped, so that the end result is: o --- B' --- C* --- D* --- E <- dev If the fork-point is not identified, then picking B onto a branch containing B' results in a conflict and if the patch-identical commits are not correctly identified then picking C onto a branch containing D (or equivalently D*) results in a conflict. This change allows us to handle both of these cases, where previously we either identified the fork-point (with `--fork-point`) but not the patch-identical commits *or* (with `--no-fork-point`) identified the patch-identical commits but not the fact that master had been rewritten. Reported-by: Ted Felix <ted@tedfelix.com> Signed-off-by: John Keeping <john@keeping.me.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

John Keeping committed Jul 16, 2014 at 20:23 UTC 1e0dacdbdb751caa5936b6d1510f5e8db4d1ed5f
4 files changed +32 -6
git-rebase--am.sh
+4 -2
@@ -45,14 +45,16 @@ then
45 # itself well to recording empty patches. fortunately, cherry-pick
46 # makes this easy
47 git cherry-pick ${gpg_sign_opt:+"$gpg_sign_opt"} --allow-empty \
48 - --right-only "$revisions"
48 + --right-only "$revisions" \
49 + ${restrict_revision+^$restrict_revision}
50 ret=$?
51 else
52 rm -f "$GIT_DIR/rebased-patches"
53
54 git format-patch -k --stdout --full-index --cherry-pick --right-only \
55 --src-prefix=a/ --dst-prefix=b/ --no-renames --no-cover-letter \
55 - "$revisions" >"$GIT_DIR/rebased-patches"
56 + "$revisions" ${restrict_revision+^$restrict_revision} \
57 + >"$GIT_DIR/rebased-patches"
58 ret=$?
59
60 if test 0 != $ret
git-rebase--interactive.sh
+1 -1
@@ -963,7 +963,7 @@ else
963 fi
964 git rev-list $merges_option --pretty=oneline --abbrev-commit \
965 --abbrev=7 --reverse --left-right --topo-order \
966 - $revisions | \
966 + $revisions ${restrict_revision+^$restrict_revision} | \
967 sed -n "s/^>//p" |
968 while read -r shortsha1 rest
969 do
git-rebase.sh
+4 -3
@@ -59,6 +59,7 @@ If you prefer to skip this patch, run "git rebase --skip" instead.
59 To check out the original branch and stop rebasing, run "git rebase --abort".')
60 "
61 unset onto
62 +unset restrict_revision
63 cmd=
64 strategy=
65 strategy_opts=
@@ -546,7 +547,7 @@ then
547 "${switch_to:-HEAD}")
548 if test -n "$new_upstream"
549 then
549 - upstream=$new_upstream
550 + restrict_revision=$new_upstream
551 fi
552 fi
553
@@ -572,7 +573,7 @@ require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
573 # and if this is not an interactive rebase.
574 mb=$(git merge-base "$onto" "$orig_head")
575 if test "$type" != interactive && test "$upstream" = "$onto" &&
575 - test "$mb" = "$onto" &&
576 + test "$mb" = "$onto" && test -z "$restrict_revision" &&
577 # linear history?
578 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
579 then
@@ -626,7 +627,7 @@ if test -n "$rebase_root"
627 then
628 revisions="$onto..$orig_head"
629 else
629 - revisions="$upstream..$orig_head"
630 + revisions="${restrict_revision-$upstream}..$orig_head"
631 fi
632
633 run_specific_rebase
t/t3400-rebase.sh
+23
@@ -169,6 +169,29 @@ test_expect_success 'default to common base in @{upstream}s reflog if no upstrea
169 test_cmp expect actual
170 '
171
172 +test_expect_success 'cherry-picked commits and fork-point work together' '
173 + git checkout default-base &&
174 + echo Amended >A &&
175 + git commit -a --no-edit --amend &&
176 + test_commit B B &&
177 + test_commit new_B B "New B" &&
178 + test_commit C C &&
179 + git checkout default &&
180 + git reset --hard default-base@{4} &&
181 + test_commit D D &&
182 + git cherry-pick -2 default-base^ &&
183 + test_commit final_B B "Final B" &&
184 + git rebase &&
185 + echo Amended >expect &&
186 + test_cmp A expect &&
187 + echo "Final B" >expect &&
188 + test_cmp B expect &&
189 + echo C >expect &&
190 + test_cmp C expect &&
191 + echo D >expect &&
192 + test_cmp D expect
193 +'
194 +
195 test_expect_success 'rebase -q is quiet' '
196 git checkout -b quiet topic &&
197 git rebase -q master >output.out 2>&1 &&