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
9test_expect_success 'prepare repository' '
10 echo 1 > A &&
11 git add A &&
12 git commit -m 1 &&
13 echo 2 > A &&
14 git add A &&
15 git commit -m 2 &&
16 git symbolic-ref HEAD refs/heads/other &&
17 rm .git/index &&
18 echo 3 > B &&
19 git add B &&
20 git commit -m 3 &&
21 echo 1 > A &&
22 git add A &&
23 git commit -m 1b &&
24 echo 4 > B &&
25 git add B &&
26 git commit -m 4
27'
28
29test_expect_success 'rebase --root expects --onto' '
30 test_must_fail git rebase --root
31'
32
33test_expect_success 'setup pre-rebase hook' '
34 mkdir -p .git/hooks &&
35 cat >.git/hooks/pre-rebase <<EOF &&
36#!$SHELL_PATH
37echo "\$1,\$2" >.git/PRE-REBASE-INPUT
38EOF
39 chmod +x .git/hooks/pre-rebase
40'
41cat > expect <<EOF
424
433
442
451
46EOF
47
48test_expect_success 'rebase --root --onto <newbase>' '
49 git checkout -b work &&
50 git rebase --root --onto master &&
51 git log --pretty=tformat:"%s" > rebased &&
52 test_cmp expect rebased
53'
54
55test_expect_success 'pre-rebase got correct input (1)' '
56 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
57'
58
59test_expect_success 'rebase --root --onto <newbase> <branch>' '
60 git branch work2 other &&
61 git rebase --root --onto master work2 &&
62 git log --pretty=tformat:"%s" > rebased2 &&
63 test_cmp expect rebased2
64'
65
66test_expect_success 'pre-rebase got correct input (2)' '
67 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work2
68'
69
70test_expect_success 'rebase -i --root --onto <newbase>' '
71 git checkout -b work3 other &&
72 GIT_EDITOR=: git rebase -i --root --onto master &&
73 git log --pretty=tformat:"%s" > rebased3 &&
74 test_cmp expect rebased3
75'
76
77test_expect_success 'pre-rebase got correct input (3)' '
78 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
79'
80
81test_expect_success 'rebase -i --root --onto <newbase> <branch>' '
82 git branch work4 other &&
83 GIT_EDITOR=: git rebase -i --root --onto master work4 &&
84 git log --pretty=tformat:"%s" > rebased4 &&
85 test_cmp expect rebased4
86'
87
88test_expect_success 'pre-rebase got correct input (4)' '
89 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work4
90'
91
92test_expect_success 'rebase -i -p with linear history' '
93 git checkout -b work5 other &&
94 GIT_EDITOR=: git rebase -i -p --root --onto master &&
95 git log --pretty=tformat:"%s" > rebased5 &&
96 test_cmp expect rebased5
97'
98
99test_expect_success 'pre-rebase got correct input (5)' '
100 test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
101'
102
103test_expect_success 'set up merge history' '
104 git checkout other^ &&
105 git checkout -b side &&
106 echo 5 > C &&
107 git add C &&
108 git commit -m 5 &&
109 git checkout other &&
110 git merge side
111'
112
113sed 's/#/ /g' > expect-side <<'EOF'
114* Merge branch 'side' into other
115|\##
116| * 5
117* | 4
118|/##
119* 3
120* 2
121* 1
122EOF
123
124test_expect_success 'rebase -i -p with merge' '
125 git checkout -b work6 other &&
126 GIT_EDITOR=: git rebase -i -p --root --onto master &&
127 git log --graph --topo-order --pretty=tformat:"%s" > rebased6 &&
128 test_cmp expect-side rebased6
129'
130
131test_expect_success 'set up second root and merge' '
132 git symbolic-ref HEAD refs/heads/third &&
133 rm .git/index &&
134 rm A B C &&
135 echo 6 > D &&
136 git add D &&
137 git commit -m 6 &&
138 git checkout other &&
139 git merge third
140'
141
142sed 's/#/ /g' > expect-third <<'EOF'
143* Merge branch 'third' into other
144|\##
145| * 6
146* | Merge branch 'side' into other
147|\ \##
148| * | 5
149* | | 4
150|/ /##
151* | 3
152|/##
153* 2
154* 1
155EOF
156
157test_expect_success 'rebase -i -p with two roots' '
158 git checkout -b work7 other &&
159 GIT_EDITOR=: git rebase -i -p --root --onto master &&
160 git log --graph --topo-order --pretty=tformat:"%s" > rebased7 &&
161 test_cmp expect-third rebased7
162'
163
164test_expect_success 'setup pre-rebase hook that fails' '
165 mkdir -p .git/hooks &&
166 cat >.git/hooks/pre-rebase <<EOF &&
167#!$SHELL_PATH
168false
169EOF
170 chmod +x .git/hooks/pre-rebase
171'
172
173test_expect_success 'pre-rebase hook stops rebase' '
174 git checkout -b stops1 other &&
175 (
176 GIT_EDITOR=:
177 export GIT_EDITOR
178 test_must_fail git rebase --root --onto master
179 ) &&
180 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops1
181 test 0 = $(git rev-list other...stops1 | wc -l)
182'
183
184test_expect_success 'pre-rebase hook stops rebase -i' '
185 git checkout -b stops2 other &&
186 (
187 GIT_EDITOR=:
188 export GIT_EDITOR
189 test_must_fail git rebase --root --onto master
190 ) &&
191 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2
192 test 0 = $(git rev-list other...stops2 | wc -l)
193'
194
195test_expect_success 'remove pre-rebase hook' '
196 rm -f .git/hooks/pre-rebase
197'
198
199test_expect_success 'set up a conflict' '
200 git checkout master &&
201 echo conflict > B &&
202 git add B &&
203 git commit -m conflict
204'
205
206test_expect_success 'rebase --root with conflict (first part)' '
207 git checkout -b conflict1 other &&
208 test_must_fail git rebase --root --onto master &&
209 git ls-files -u | grep "B$"
210'
211
212test_expect_success 'fix the conflict' '
213 echo 3 > B &&
214 git add B
215'
216
217cat > expect-conflict <<EOF
2186
2195
2204
2213
222conflict
2232
2241
225EOF
226
227test_expect_success 'rebase --root with conflict (second part)' '
228 git rebase --continue &&
229 git log --pretty=tformat:"%s" > conflict1 &&
230 test_cmp expect-conflict conflict1
231'
232
233test_expect_success 'rebase -i --root with conflict (first part)' '
234 git checkout -b conflict2 other &&
235 (
236 GIT_EDITOR=:
237 export GIT_EDITOR
238 test_must_fail git rebase -i --root --onto master
239 ) &&
240 git ls-files -u | grep "B$"
241'
242
243test_expect_success 'fix the conflict' '
244 echo 3 > B &&
245 git add B
246'
247
248test_expect_success 'rebase -i --root with conflict (second part)' '
249 git rebase --continue &&
250 git log --pretty=tformat:"%s" > conflict2 &&
251 test_cmp expect-conflict conflict2
252'
253
254cat >expect-conflict-p <<\EOF
255commit conflict3 conflict3~1 conflict3^2
256Merge branch 'third' into other
257commit conflict3^2 conflict3~4
2586
259commit conflict3~1 conflict3~2 conflict3~1^2
260Merge branch 'side' into other
261commit conflict3~1^2 conflict3~3
2625
263commit conflict3~2 conflict3~3
2644
265commit conflict3~3 conflict3~4
2663
267commit conflict3~4 conflict3~5
268conflict
269commit conflict3~5 conflict3~6
2702
271commit conflict3~6
2721
273EOF
274
275test_expect_success 'rebase -i -p --root with conflict (first part)' '
276 git checkout -b conflict3 other &&
277 (
278 GIT_EDITOR=:
279 export GIT_EDITOR
280 test_must_fail git rebase -i -p --root --onto master
281 ) &&
282 git ls-files -u | grep "B$"
283'
284
285test_expect_success 'fix the conflict' '
286 echo 3 > B &&
287 git add B
288'
289
290test_expect_success 'rebase -i -p --root with conflict (second part)' '
291 git rebase --continue &&
292 git rev-list --topo-order --parents --pretty="tformat:%s" HEAD |
293 git name-rev --stdin --name-only --refs=refs/heads/conflict3 >out &&
294 test_cmp expect-conflict-p out
295'
296
297test_done