]>
Commit | Line | Data |
---|---|---|
190f5323 TR |
1 | #!/bin/sh |
2 | ||
3 | test_description='git rebase --root | |
4 | ||
5 | Tests if git rebase --root --onto <newparent> can rebase the root commit. | |
6 | ' | |
d1c02d93 | 7 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
8 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
9 | ||
190f5323 | 10 | . ./test-lib.sh |
21828964 | 11 | |
2ea3c171 TR |
12 | log_with_names () { |
13 | git rev-list --topo-order --parents --pretty="tformat:%s" HEAD | | |
14 | git name-rev --stdin --name-only --refs=refs/heads/$1 | |
15 | } | |
16 | ||
17 | ||
190f5323 | 18 | test_expect_success 'prepare repository' ' |
f7951e1d JS |
19 | test_commit 1 A && |
20 | test_commit 2 A && | |
190f5323 TR |
21 | git symbolic-ref HEAD refs/heads/other && |
22 | rm .git/index && | |
f7951e1d JS |
23 | test_commit 3 B && |
24 | test_commit 1b A 1 && | |
25 | test_commit 4 B | |
190f5323 TR |
26 | ' |
27 | ||
f2b6a199 MZ |
28 | test_expect_success 'rebase --root fails with too many args' ' |
29 | git checkout -B fail other && | |
d1c02d93 | 30 | test_must_fail git rebase --onto main --root fail fail |
f2b6a199 MZ |
31 | ' |
32 | ||
190f5323 TR |
33 | test_expect_success 'setup pre-rebase hook' ' |
34 | mkdir -p .git/hooks && | |
35 | cat >.git/hooks/pre-rebase <<EOF && | |
36 | #!$SHELL_PATH | |
37 | echo "\$1,\$2" >.git/PRE-REBASE-INPUT | |
38 | EOF | |
39 | chmod +x .git/hooks/pre-rebase | |
40 | ' | |
41 | cat > expect <<EOF | |
42 | 4 | |
43 | 3 | |
44 | 2 | |
45 | 1 | |
46 | EOF | |
47 | ||
48 | test_expect_success 'rebase --root --onto <newbase>' ' | |
f2b6a199 | 49 | git checkout -b work other && |
d1c02d93 | 50 | git rebase --root --onto main && |
190f5323 TR |
51 | git log --pretty=tformat:"%s" > rebased && |
52 | test_cmp expect rebased | |
53 | ' | |
54 | ||
55 | test_expect_success 'pre-rebase got correct input (1)' ' | |
56 | test "z$(cat .git/PRE-REBASE-INPUT)" = z--root, | |
57 | ' | |
58 | ||
59 | test_expect_success 'rebase --root --onto <newbase> <branch>' ' | |
60 | git branch work2 other && | |
d1c02d93 | 61 | git rebase --root --onto main work2 && |
190f5323 TR |
62 | git log --pretty=tformat:"%s" > rebased2 && |
63 | test_cmp expect rebased2 | |
64 | ' | |
65 | ||
66 | test_expect_success 'pre-rebase got correct input (2)' ' | |
67 | test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work2 | |
68 | ' | |
69 | ||
d911d146 TR |
70 | test_expect_success 'rebase -i --root --onto <newbase>' ' |
71 | git checkout -b work3 other && | |
d1c02d93 | 72 | git rebase -i --root --onto main && |
d911d146 TR |
73 | git log --pretty=tformat:"%s" > rebased3 && |
74 | test_cmp expect rebased3 | |
75 | ' | |
76 | ||
77 | test_expect_success 'pre-rebase got correct input (3)' ' | |
78 | test "z$(cat .git/PRE-REBASE-INPUT)" = z--root, | |
79 | ' | |
80 | ||
81 | test_expect_success 'rebase -i --root --onto <newbase> <branch>' ' | |
82 | git branch work4 other && | |
d1c02d93 | 83 | git rebase -i --root --onto main work4 && |
d911d146 TR |
84 | git log --pretty=tformat:"%s" > rebased4 && |
85 | test_cmp expect rebased4 | |
86 | ' | |
87 | ||
88 | test_expect_success 'pre-rebase got correct input (4)' ' | |
89 | test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work4 | |
90 | ' | |
91 | ||
11aad464 | 92 | test_expect_success REBASE_P 'rebase -i -p with linear history' ' |
d911d146 | 93 | git checkout -b work5 other && |
d1c02d93 | 94 | git rebase -i -p --root --onto main && |
d911d146 TR |
95 | git log --pretty=tformat:"%s" > rebased5 && |
96 | test_cmp expect rebased5 | |
97 | ' | |
98 | ||
11aad464 | 99 | test_expect_success REBASE_P 'pre-rebase got correct input (5)' ' |
d911d146 TR |
100 | test "z$(cat .git/PRE-REBASE-INPUT)" = z--root, |
101 | ' | |
102 | ||
103 | test_expect_success 'set up merge history' ' | |
104 | git checkout other^ && | |
105 | git checkout -b side && | |
f7951e1d | 106 | test_commit 5 C && |
d911d146 TR |
107 | git checkout other && |
108 | git merge side | |
109 | ' | |
110 | ||
2ea3c171 TR |
111 | cat > expect-side <<'EOF' |
112 | commit work6 work6~1 work6^2 | |
113 | Merge branch 'side' into other | |
114 | commit work6^2 work6~2 | |
115 | 5 | |
116 | commit work6~1 work6~2 | |
117 | 4 | |
118 | commit work6~2 work6~3 | |
119 | 3 | |
120 | commit work6~3 work6~4 | |
121 | 2 | |
122 | commit work6~4 | |
123 | 1 | |
d911d146 TR |
124 | EOF |
125 | ||
11aad464 | 126 | test_expect_success REBASE_P 'rebase -i -p with merge' ' |
d911d146 | 127 | git checkout -b work6 other && |
d1c02d93 | 128 | git rebase -i -p --root --onto main && |
2ea3c171 | 129 | log_with_names work6 > rebased6 && |
d911d146 TR |
130 | test_cmp expect-side rebased6 |
131 | ' | |
132 | ||
133 | test_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 && | |
f7951e1d | 137 | test_commit 6 D && |
d911d146 | 138 | git checkout other && |
e379fdf3 | 139 | git merge --allow-unrelated-histories third |
d911d146 TR |
140 | ' |
141 | ||
2ea3c171 TR |
142 | cat > expect-third <<'EOF' |
143 | commit work7 work7~1 work7^2 | |
144 | Merge branch 'third' into other | |
145 | commit work7^2 work7~4 | |
146 | 6 | |
147 | commit work7~1 work7~2 work7~1^2 | |
148 | Merge branch 'side' into other | |
149 | commit work7~1^2 work7~3 | |
150 | 5 | |
151 | commit work7~2 work7~3 | |
152 | 4 | |
153 | commit work7~3 work7~4 | |
154 | 3 | |
155 | commit work7~4 work7~5 | |
156 | 2 | |
157 | commit work7~5 | |
158 | 1 | |
d911d146 TR |
159 | EOF |
160 | ||
11aad464 | 161 | test_expect_success REBASE_P 'rebase -i -p with two roots' ' |
d911d146 | 162 | git checkout -b work7 other && |
d1c02d93 | 163 | git rebase -i -p --root --onto main && |
2ea3c171 | 164 | log_with_names work7 > rebased7 && |
d911d146 TR |
165 | test_cmp expect-third rebased7 |
166 | ' | |
167 | ||
190f5323 TR |
168 | test_expect_success 'setup pre-rebase hook that fails' ' |
169 | mkdir -p .git/hooks && | |
170 | cat >.git/hooks/pre-rebase <<EOF && | |
171 | #!$SHELL_PATH | |
172 | false | |
173 | EOF | |
174 | chmod +x .git/hooks/pre-rebase | |
175 | ' | |
176 | ||
177 | test_expect_success 'pre-rebase hook stops rebase' ' | |
178 | git checkout -b stops1 other && | |
d1c02d93 | 179 | test_must_fail git rebase --root --onto main && |
a48fcd83 | 180 | test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops1 && |
190f5323 TR |
181 | test 0 = $(git rev-list other...stops1 | wc -l) |
182 | ' | |
183 | ||
d911d146 TR |
184 | test_expect_success 'pre-rebase hook stops rebase -i' ' |
185 | git checkout -b stops2 other && | |
d1c02d93 | 186 | test_must_fail git rebase --root --onto main && |
a48fcd83 | 187 | test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2 && |
d911d146 TR |
188 | test 0 = $(git rev-list other...stops2 | wc -l) |
189 | ' | |
190 | ||
a6c7a276 JH |
191 | test_expect_success 'remove pre-rebase hook' ' |
192 | rm -f .git/hooks/pre-rebase | |
193 | ' | |
194 | ||
195 | test_expect_success 'set up a conflict' ' | |
d1c02d93 | 196 | git checkout main && |
a6c7a276 JH |
197 | echo conflict > B && |
198 | git add B && | |
199 | git commit -m conflict | |
200 | ' | |
201 | ||
202 | test_expect_success 'rebase --root with conflict (first part)' ' | |
203 | git checkout -b conflict1 other && | |
d1c02d93 | 204 | test_must_fail git rebase --root --onto main && |
a6c7a276 JH |
205 | git ls-files -u | grep "B$" |
206 | ' | |
207 | ||
208 | test_expect_success 'fix the conflict' ' | |
209 | echo 3 > B && | |
210 | git add B | |
211 | ' | |
212 | ||
213 | cat > expect-conflict <<EOF | |
214 | 6 | |
215 | 5 | |
216 | 4 | |
217 | 3 | |
218 | conflict | |
219 | 2 | |
220 | 1 | |
221 | EOF | |
222 | ||
223 | test_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 | ||
229 | test_expect_success 'rebase -i --root with conflict (first part)' ' | |
230 | git checkout -b conflict2 other && | |
d1c02d93 | 231 | test_must_fail git rebase -i --root --onto main && |
a6c7a276 JH |
232 | git ls-files -u | grep "B$" |
233 | ' | |
234 | ||
235 | test_expect_success 'fix the conflict' ' | |
236 | echo 3 > B && | |
237 | git add B | |
238 | ' | |
239 | ||
240 | test_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 | ||
246 | cat >expect-conflict-p <<\EOF | |
247 | commit conflict3 conflict3~1 conflict3^2 | |
248 | Merge branch 'third' into other | |
249 | commit conflict3^2 conflict3~4 | |
250 | 6 | |
251 | commit conflict3~1 conflict3~2 conflict3~1^2 | |
252 | Merge branch 'side' into other | |
253 | commit conflict3~1^2 conflict3~3 | |
254 | 5 | |
255 | commit conflict3~2 conflict3~3 | |
256 | 4 | |
257 | commit conflict3~3 conflict3~4 | |
258 | 3 | |
259 | commit conflict3~4 conflict3~5 | |
260 | conflict | |
261 | commit conflict3~5 conflict3~6 | |
262 | 2 | |
263 | commit conflict3~6 | |
264 | 1 | |
265 | EOF | |
266 | ||
11aad464 | 267 | test_expect_success REBASE_P 'rebase -i -p --root with conflict (first part)' ' |
a6c7a276 | 268 | git checkout -b conflict3 other && |
d1c02d93 | 269 | test_must_fail git rebase -i -p --root --onto main && |
a6c7a276 JH |
270 | git ls-files -u | grep "B$" |
271 | ' | |
272 | ||
273 | test_expect_success 'fix the conflict' ' | |
274 | echo 3 > B && | |
275 | git add B | |
276 | ' | |
277 | ||
11aad464 | 278 | test_expect_success REBASE_P 'rebase -i -p --root with conflict (second part)' ' |
a6c7a276 | 279 | git rebase --continue && |
2ea3c171 | 280 | log_with_names conflict3 >out && |
a6c7a276 JH |
281 | test_cmp expect-conflict-p out |
282 | ' | |
283 | ||
190f5323 | 284 | test_done |