Skip to content

Commit faae853

Browse files
Stephen Habermangitster
authored andcommitted
rebase--interactive: fix parent rewriting for dropped commits
`rebase -i -p` got its rev-list of commits to keep by --left-right and --cherry-pick. Adding --cherry-pick would drop commits that duplicated changes already in the rebase target. The dropped commits were then forgotten about when it came to rewriting the parents of their descendents, so the descendents would get cherry-picked with their old, unwritten parents and essentially make the rebase a no-op. This commit adds a $DOTEST/dropped directory to remember dropped commits and rewrite their children's parent as the dropped commit's possibly-rewritten first-parent. Signed-off-by: Stephen Haberman <stephen@exigencecorp.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent c82efaf commit faae853

File tree

2 files changed

+174
-2
lines changed

2 files changed

+174
-2
lines changed

git-rebase--interactive.sh

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ DONE="$DOTEST"/done
3737
MSG="$DOTEST"/message
3838
SQUASH_MSG="$DOTEST"/message-squash
3939
REWRITTEN="$DOTEST"/rewritten
40+
DROPPED="$DOTEST"/dropped
4041
PRESERVE_MERGES=
4142
STRATEGY=
4243
ONTO=
@@ -179,8 +180,12 @@ pick_one_preserving_merges () {
179180

180181
# rewrite parents; if none were rewritten, we can fast-forward.
181182
new_parents=
182-
for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
183+
pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
184+
while [ "$pend" != "" ]
183185
do
186+
p=$(expr "$pend" : ' \([^ ]*\)')
187+
pend="${pend# $p}"
188+
184189
if test -f "$REWRITTEN"/$p
185190
then
186191
new_p=$(cat "$REWRITTEN"/$p)
@@ -193,7 +198,13 @@ pick_one_preserving_merges () {
193198
;;
194199
esac
195200
else
196-
new_parents="$new_parents $p"
201+
if test -f "$DROPPED"/$p
202+
then
203+
fast_forward=f
204+
pend=" $(cat "$DROPPED"/$p)$pend"
205+
else
206+
new_parents="$new_parents $p"
207+
fi
197208
fi
198209
done
199210
case $fast_forward in
@@ -599,6 +610,28 @@ first and then run 'git rebase --continue' again."
599610
#
600611
EOF
601612

613+
# Watch for commits that been dropped by --cherry-pick
614+
if test t = "$PRESERVE_MERGES"
615+
then
616+
mkdir "$DROPPED"
617+
# drop the --cherry-pick parameter this time
618+
git rev-list $MERGES_OPTION --abbrev-commit \
619+
--abbrev=7 $UPSTREAM...$HEAD --left-right | \
620+
sed -n "s/^>//p" | while read rev
621+
do
622+
grep --quiet "$rev" "$TODO"
623+
if [ $? -ne 0 ]
624+
then
625+
# Use -f2 because if rev-list is telling this commit is not
626+
# worthwhile, we don't want to track its multiple heads,
627+
# just the history of its first-parent for others that will
628+
# be rebasing on top of us
629+
full=$(git rev-parse $rev)
630+
git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$full
631+
fi
632+
done
633+
fi
634+
602635
has_action "$TODO" ||
603636
die_abort "Nothing to do"
604637

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2008 Stephen Haberman
4+
#
5+
6+
test_description='git rebase preserve merges
7+
8+
This test runs git rebase with preserve merges and ensures commits
9+
dropped by the --cherry-pick flag have their childrens parents
10+
rewritten.
11+
'
12+
. ./test-lib.sh
13+
14+
# set up two branches like this:
15+
#
16+
# A - B - C - D - E
17+
# \
18+
# F - G - H
19+
# \
20+
# I
21+
#
22+
# where B, D and G touch the same file.
23+
24+
test_expect_success 'setup' '
25+
: > file1 &&
26+
git add file1 &&
27+
test_tick &&
28+
git commit -m A &&
29+
git tag A &&
30+
echo 1 > file1 &&
31+
test_tick &&
32+
git commit -m B file1 &&
33+
: > file2 &&
34+
git add file2 &&
35+
test_tick &&
36+
git commit -m C &&
37+
echo 2 > file1 &&
38+
test_tick &&
39+
git commit -m D file1 &&
40+
: > file3 &&
41+
git add file3 &&
42+
test_tick &&
43+
git commit -m E &&
44+
git tag E &&
45+
git checkout -b branch1 A &&
46+
: > file4 &&
47+
git add file4 &&
48+
test_tick &&
49+
git commit -m F &&
50+
git tag F &&
51+
echo 3 > file1 &&
52+
test_tick &&
53+
git commit -m G file1 &&
54+
git tag G &&
55+
: > file5 &&
56+
git add file5 &&
57+
test_tick &&
58+
git commit -m H &&
59+
git tag H &&
60+
git checkout -b branch2 F &&
61+
: > file6 &&
62+
git add file6 &&
63+
test_tick &&
64+
git commit -m I &&
65+
git tag I
66+
'
67+
68+
# A - B - C - D - E
69+
# \ \ \
70+
# F - G - H -- L \ --> L
71+
# \ | \
72+
# I -- G2 -- J -- K I -- K
73+
# G2 = same changes as G
74+
test_expect_success 'skip same-resolution merges with -p' '
75+
git checkout branch1 &&
76+
! git merge E &&
77+
echo 23 > file1 &&
78+
git add file1 &&
79+
git commit -m L &&
80+
git checkout branch2 &&
81+
echo 3 > file1 &&
82+
git commit -a -m G2 &&
83+
! git merge E &&
84+
echo 23 > file1 &&
85+
git add file1 &&
86+
git commit -m J &&
87+
echo file7 > file7 &&
88+
git add file7 &&
89+
git commit -m K &&
90+
GIT_EDITOR=: git rebase -i -p branch1 &&
91+
test $(git rev-parse branch2^^) = $(git rev-parse branch1) &&
92+
test "23" = "$(cat file1)" &&
93+
test "" = "$(cat file6)" &&
94+
test "file7" = "$(cat file7)" &&
95+
96+
git checkout branch1 &&
97+
git reset --hard H &&
98+
git checkout branch2 &&
99+
git reset --hard I
100+
'
101+
102+
# A - B - C - D - E
103+
# \ \ \
104+
# F - G - H -- L \ --> L
105+
# \ | \
106+
# I -- G2 -- J -- K I -- G2 -- K
107+
# G2 = different changes as G
108+
test_expect_success 'keep different-resolution merges with -p' '
109+
git checkout branch1 &&
110+
! git merge E &&
111+
echo 23 > file1 &&
112+
git add file1 &&
113+
git commit -m L &&
114+
git checkout branch2 &&
115+
echo 4 > file1 &&
116+
git commit -a -m G2 &&
117+
! git merge E &&
118+
echo 24 > file1 &&
119+
git add file1 &&
120+
git commit -m J &&
121+
echo file7 > file7 &&
122+
git add file7 &&
123+
git commit -m K &&
124+
! GIT_EDITOR=: git rebase -i -p branch1 &&
125+
echo 234 > file1 &&
126+
git add file1 &&
127+
GIT_EDITOR=: git rebase --continue &&
128+
test $(git rev-parse branch2^^^) = $(git rev-parse branch1) &&
129+
test "234" = "$(cat file1)" &&
130+
test "" = "$(cat file6)" &&
131+
test "file7" = "$(cat file7)" &&
132+
133+
git checkout branch1 &&
134+
git reset --hard H &&
135+
git checkout branch2 &&
136+
git reset --hard I
137+
'
138+
139+
test_done

0 commit comments

Comments
 (0)