Skip to content

Commit 1e0dacd

Browse files
johnkeepinggitster
authored andcommitted
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 <[email protected]> Signed-off-by: John Keeping <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b6266dc commit 1e0dacd

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

git-rebase--am.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ then
4545
# itself well to recording empty patches. fortunately, cherry-pick
4646
# makes this easy
4747
git cherry-pick ${gpg_sign_opt:+"$gpg_sign_opt"} --allow-empty \
48-
--right-only "$revisions"
48+
--right-only "$revisions" \
49+
${restrict_revision+^$restrict_revision}
4950
ret=$?
5051
else
5152
rm -f "$GIT_DIR/rebased-patches"
5253

5354
git format-patch -k --stdout --full-index --cherry-pick --right-only \
5455
--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"
5658
ret=$?
5759

5860
if test 0 != $ret

git-rebase--interactive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ else
963963
fi
964964
git rev-list $merges_option --pretty=oneline --abbrev-commit \
965965
--abbrev=7 --reverse --left-right --topo-order \
966-
$revisions | \
966+
$revisions ${restrict_revision+^$restrict_revision} | \
967967
sed -n "s/^>//p" |
968968
while read -r shortsha1 rest
969969
do

git-rebase.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ If you prefer to skip this patch, run "git rebase --skip" instead.
5959
To check out the original branch and stop rebasing, run "git rebase --abort".')
6060
"
6161
unset onto
62+
unset restrict_revision
6263
cmd=
6364
strategy=
6465
strategy_opts=
@@ -546,7 +547,7 @@ then
546547
"${switch_to:-HEAD}")
547548
if test -n "$new_upstream"
548549
then
549-
upstream=$new_upstream
550+
restrict_revision=$new_upstream
550551
fi
551552
fi
552553

@@ -572,7 +573,7 @@ require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
572573
# and if this is not an interactive rebase.
573574
mb=$(git merge-base "$onto" "$orig_head")
574575
if test "$type" != interactive && test "$upstream" = "$onto" &&
575-
test "$mb" = "$onto" &&
576+
test "$mb" = "$onto" && test -z "$restrict_revision" &&
576577
# linear history?
577578
! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
578579
then
@@ -626,7 +627,7 @@ if test -n "$rebase_root"
626627
then
627628
revisions="$onto..$orig_head"
628629
else
629-
revisions="$upstream..$orig_head"
630+
revisions="${restrict_revision-$upstream}..$orig_head"
630631
fi
631632

632633
run_specific_rebase

t/t3400-rebase.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,29 @@ test_expect_success 'default to common base in @{upstream}s reflog if no upstrea
169169
test_cmp expect actual
170170
'
171171

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+
172195
test_expect_success 'rebase -q is quiet' '
173196
git checkout -b quiet topic &&
174197
git rebase -q master >output.out 2>&1 &&

0 commit comments

Comments
 (0)