1#!/bin/sh
2
3test_description='git rebase --root
4
5Tests if git rebase --root --onto <newparent> can rebase the root commit.
6'
7. ./test-lib.sh
8
9# we always run the interactive rebases unchanged, so just disable the editor
10GIT_EDITOR=:
11export GIT_EDITOR
12
13test_expect_success 'prepare repository' '
14 test_commit 1 A &&
15 test_commit 2 A &&
16 git symbolic-ref HEAD refs/heads/other &&
17 rm .git/index &&
18 test_commit 3 B &&
19 test_commit 1b A 1 &&
20 test_commit 4 B
21'
22
23test_expect_success 'rebase --root expects --onto' '
24 test_must_fail git rebase --root
25'
26
27test_expect_success 'setup pre-rebase hook' '
28 mkdir -p .git/hooks &&
29 cat >.git/hooks/pre-rebase <<EOF &&
30#!$SHELL_PATH
31echo "\$1,\$2" >.git/PRE-REBASE-INPUT
32EOF
33 chmod +x .git/hooks/pre-rebase
34'
35cat > expect <<EOF
364
373
382
391
40EOF
41
42test_expect_success 'rebase --root --onto <newbase>' '
43 git checkout -b work &&
44 git rebase --root --onto master &&
45 git log --pretty=tformat:"%s" > rebased &&
46 test_cmp expect rebased
47'
48
49test_expect_success 'pre-rebase got correct input (1)' '
50 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
51'
52
53test_expect_success 'rebase --root --onto <newbase> <branch>' '
54 git branch work2 other &&
55 git rebase --root --onto master work2 &&
56 git log --pretty=tformat:"%s" > rebased2 &&
57 test_cmp expect rebased2
58'
59
60test_expect_success 'pre-rebase got correct input (2)' '
61 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work2
62'
63
64test_expect_success 'rebase -i --root --onto <newbase>' '
65 git checkout -b work3 other &&
66 git rebase -i --root --onto master &&
67 git log --pretty=tformat:"%s" > rebased3 &&
68 test_cmp expect rebased3
69'
70
71test_expect_success 'pre-rebase got correct input (3)' '
72 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
73'
74
75test_expect_success 'rebase -i --root --onto <newbase> <branch>' '
76 git branch work4 other &&
77 git rebase -i --root --onto master work4 &&
78 git log --pretty=tformat:"%s" > rebased4 &&
79 test_cmp expect rebased4
80'
81
82test_expect_success 'pre-rebase got correct input (4)' '
83 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work4
84'
85
86test_expect_success 'rebase -i -p with linear history' '
87 git checkout -b work5 other &&
88 git rebase -i -p --root --onto master &&
89 git log --pretty=tformat:"%s" > rebased5 &&
90 test_cmp expect rebased5
91'
92
93test_expect_success 'pre-rebase got correct input (5)' '
94 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
95'
96
97test_expect_success 'set up merge history' '
98 git checkout other^ &&
99 git checkout -b side &&
100 test_commit 5 C &&
101 git checkout other &&
102 git merge side
103'
104
105sed 's/#/ /g' > expect-side <<'EOF'
106* Merge branch 'side' into other
107|\##
108| * 5
109* | 4
110|/##
111* 3
112* 2
113* 1
114EOF
115
116test_expect_success 'rebase -i -p with merge' '
117 git checkout -b work6 other &&
118 git rebase -i -p --root --onto master &&
119 git log --graph --topo-order --pretty=tformat:"%s" > rebased6 &&
120 test_cmp expect-side rebased6
121'
122
123test_expect_success 'set up second root and merge' '
124 git symbolic-ref HEAD refs/heads/third &&
125 rm .git/index &&
126 rm A B C &&
127 test_commit 6 D &&
128 git checkout other &&
129 git merge third
130'
131
132sed 's/#/ /g' > expect-third <<'EOF'
133* Merge branch 'third' into other
134|\##
135| * 6
136* | Merge branch 'side' into other
137|\ \##
138| * | 5
139* | | 4
140|/ /##
141* | 3
142|/##
143* 2
144* 1
145EOF
146
147test_expect_success 'rebase -i -p with two roots' '
148 git checkout -b work7 other &&
149 git rebase -i -p --root --onto master &&
150 git log --graph --topo-order --pretty=tformat:"%s" > rebased7 &&
151 test_cmp expect-third rebased7
152'
153
154test_expect_success 'setup pre-rebase hook that fails' '
155 mkdir -p .git/hooks &&
156 cat >.git/hooks/pre-rebase <<EOF &&
157#!$SHELL_PATH
158false
159EOF
160 chmod +x .git/hooks/pre-rebase
161'
162
163test_expect_success 'pre-rebase hook stops rebase' '
164 git checkout -b stops1 other &&
165 test_must_fail git rebase --root --onto master &&
166 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops1
167 test 0 = $(git rev-list other...stops1 | wc -l)
168'
169
170test_expect_success 'pre-rebase hook stops rebase -i' '
171 git checkout -b stops2 other &&
172 test_must_fail git rebase --root --onto master &&
173 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2
174 test 0 = $(git rev-list other...stops2 | wc -l)
175'
176
177test_expect_success 'remove pre-rebase hook' '
178 rm -f .git/hooks/pre-rebase
179'
180
181test_expect_success 'set up a conflict' '
182 git checkout master &&
183 echo conflict > B &&
184 git add B &&
185 git commit -m conflict
186'
187
188test_expect_success 'rebase --root with conflict (first part)' '
189 git checkout -b conflict1 other &&
190 test_must_fail git rebase --root --onto master &&
191 git ls-files -u | grep "B$"
192'
193
194test_expect_success 'fix the conflict' '
195 echo 3 > B &&
196 git add B
197'
198
199cat > expect-conflict <<EOF
2006
2015
2024
2033
204conflict
2052
2061
207EOF
208
209test_expect_success 'rebase --root with conflict (second part)' '
210 git rebase --continue &&
211 git log --pretty=tformat:"%s" > conflict1 &&
212 test_cmp expect-conflict conflict1
213'
214
215test_expect_success 'rebase -i --root with conflict (first part)' '
216 git checkout -b conflict2 other &&
217 test_must_fail git rebase -i --root --onto master &&
218 git ls-files -u | grep "B$"
219'
220
221test_expect_success 'fix the conflict' '
222 echo 3 > B &&
223 git add B
224'
225
226test_expect_success 'rebase -i --root with conflict (second part)' '
227 git rebase --continue &&
228 git log --pretty=tformat:"%s" > conflict2 &&
229 test_cmp expect-conflict conflict2
230'
231
232cat >expect-conflict-p <<\EOF
233commit conflict3 conflict3~1 conflict3^2
234Merge branch 'third' into other
235commit conflict3^2 conflict3~4
2366
237commit conflict3~1 conflict3~2 conflict3~1^2
238Merge branch 'side' into other
239commit conflict3~1^2 conflict3~3
2405
241commit conflict3~2 conflict3~3
2424
243commit conflict3~3 conflict3~4
2443
245commit conflict3~4 conflict3~5
246conflict
247commit conflict3~5 conflict3~6
2482
249commit conflict3~6
2501
251EOF
252
253test_expect_success 'rebase -i -p --root with conflict (first part)' '
254 git checkout -b conflict3 other &&
255 test_must_fail git rebase -i -p --root --onto master &&
256 git ls-files -u | grep "B$"
257'
258
259test_expect_success 'fix the conflict' '
260 echo 3 > B &&
261 git add B
262'
263
264test_expect_success 'rebase -i -p --root with conflict (second part)' '
265 git rebase --continue &&
266 git rev-list --topo-order --parents --pretty="tformat:%s" HEAD |
267 git name-rev --stdin --name-only --refs=refs/heads/conflict3 >out &&
268 test_cmp expect-conflict-p out
269'
270
271test_done