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
13log_with_names () {
14 git rev-list --topo-order --parents --pretty="tformat:%s" HEAD |
15 git name-rev --stdin --name-only --refs=refs/heads/$1
16}
17
18
19test_expect_success 'prepare repository' '
20 test_commit 1 A &&
21 test_commit 2 A &&
22 git symbolic-ref HEAD refs/heads/other &&
23 rm .git/index &&
24 test_commit 3 B &&
25 test_commit 1b A 1 &&
26 test_commit 4 B
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 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 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 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 test_commit 5 C &&
107 git checkout other &&
108 git merge side
109'
110
111cat > expect-side <<'EOF'
112commit work6 work6~1 work6^2
113Merge branch 'side' into other
114commit work6^2 work6~2
1155
116commit work6~1 work6~2
1174
118commit work6~2 work6~3
1193
120commit work6~3 work6~4
1212
122commit work6~4
1231
124EOF
125
126test_expect_success 'rebase -i -p with merge' '
127 git checkout -b work6 other &&
128 git rebase -i -p --root --onto master &&
129 log_with_names work6 > rebased6 &&
130 test_cmp expect-side rebased6
131'
132
133test_expect_success 'set up second root and merge' '
134 git symbolic-ref HEAD refs/heads/third &&
135 rm .git/index &&
136 rm A B C &&
137 test_commit 6 D &&
138 git checkout other &&
139 git merge third
140'
141
142cat > expect-third <<'EOF'
143commit work7 work7~1 work7^2
144Merge branch 'third' into other
145commit work7^2 work7~4
1466
147commit work7~1 work7~2 work7~1^2
148Merge branch 'side' into other
149commit work7~1^2 work7~3
1505
151commit work7~2 work7~3
1524
153commit work7~3 work7~4
1543
155commit work7~4 work7~5
1562
157commit work7~5
1581
159EOF
160
161test_expect_success 'rebase -i -p with two roots' '
162 git checkout -b work7 other &&
163 git rebase -i -p --root --onto master &&
164 log_with_names work7 > rebased7 &&
165 test_cmp expect-third rebased7
166'
167
168test_expect_success 'setup pre-rebase hook that fails' '
169 mkdir -p .git/hooks &&
170 cat >.git/hooks/pre-rebase <<EOF &&
171#!$SHELL_PATH
172false
173EOF
174 chmod +x .git/hooks/pre-rebase
175'
176
177test_expect_success 'pre-rebase hook stops rebase' '
178 git checkout -b stops1 other &&
179 test_must_fail git rebase --root --onto master &&
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 test_must_fail git rebase --root --onto master &&
187 test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2
188 test 0 = $(git rev-list other...stops2 | wc -l)
189'
190
191test_expect_success 'remove pre-rebase hook' '
192 rm -f .git/hooks/pre-rebase
193'
194
195test_expect_success 'set up a conflict' '
196 git checkout master &&
197 echo conflict > B &&
198 git add B &&
199 git commit -m conflict
200'
201
202test_expect_success 'rebase --root with conflict (first part)' '
203 git checkout -b conflict1 other &&
204 test_must_fail git rebase --root --onto master &&
205 git ls-files -u | grep "B$"
206'
207
208test_expect_success 'fix the conflict' '
209 echo 3 > B &&
210 git add B
211'
212
213cat > expect-conflict <<EOF
2146
2155
2164
2173
218conflict
2192
2201
221EOF
222
223test_expect_success 'rebase --root with conflict (second part)' '
224 git rebase --continue &&
225 git log --pretty=tformat:"%s" > conflict1 &&
226 test_cmp expect-conflict conflict1
227'
228
229test_expect_success 'rebase -i --root with conflict (first part)' '
230 git checkout -b conflict2 other &&
231 test_must_fail git rebase -i --root --onto master &&
232 git ls-files -u | grep "B$"
233'
234
235test_expect_success 'fix the conflict' '
236 echo 3 > B &&
237 git add B
238'
239
240test_expect_success 'rebase -i --root with conflict (second part)' '
241 git rebase --continue &&
242 git log --pretty=tformat:"%s" > conflict2 &&
243 test_cmp expect-conflict conflict2
244'
245
246cat >expect-conflict-p <<\EOF
247commit conflict3 conflict3~1 conflict3^2
248Merge branch 'third' into other
249commit conflict3^2 conflict3~4
2506
251commit conflict3~1 conflict3~2 conflict3~1^2
252Merge branch 'side' into other
253commit conflict3~1^2 conflict3~3
2545
255commit conflict3~2 conflict3~3
2564
257commit conflict3~3 conflict3~4
2583
259commit conflict3~4 conflict3~5
260conflict
261commit conflict3~5 conflict3~6
2622
263commit conflict3~6
2641
265EOF
266
267test_expect_success 'rebase -i -p --root with conflict (first part)' '
268 git checkout -b conflict3 other &&
269 test_must_fail git rebase -i -p --root --onto master &&
270 git ls-files -u | grep "B$"
271'
272
273test_expect_success 'fix the conflict' '
274 echo 3 > B &&
275 git add B
276'
277
278test_expect_success 'rebase -i -p --root with conflict (second part)' '
279 git rebase --continue &&
280 log_with_names conflict3 >out &&
281 test_cmp expect-conflict-p out
282'
283
284test_done