]>
Commit | Line | Data |
---|---|---|
1b1dce4b JS |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Johannes E. Schindelin | |
4 | # | |
5 | ||
6 | test_description='git rebase interactive | |
7 | ||
8 | This test runs git rebase "interactively", by faking an edit, and verifies | |
9 | that the result still makes sense. | |
10 | ' | |
11 | . ./test-lib.sh | |
12 | ||
29a03348 JS |
13 | . ../lib-rebase.sh |
14 | ||
15 | set_fake_editor | |
16 | ||
1b1dce4b JS |
17 | # set up two branches like this: |
18 | # | |
19 | # A - B - C - D - E | |
20 | # \ | |
21 | # F - G - H | |
22 | # \ | |
23 | # I | |
24 | # | |
25 | # where B, D and G touch the same file. | |
26 | ||
27 | test_expect_success 'setup' ' | |
28 | : > file1 && | |
29 | git add file1 && | |
30 | test_tick && | |
31 | git commit -m A && | |
32 | git tag A && | |
33 | echo 1 > file1 && | |
34 | test_tick && | |
35 | git commit -m B file1 && | |
36 | : > file2 && | |
37 | git add file2 && | |
38 | test_tick && | |
39 | git commit -m C && | |
40 | echo 2 > file1 && | |
41 | test_tick && | |
42 | git commit -m D file1 && | |
43 | : > file3 && | |
44 | git add file3 && | |
45 | test_tick && | |
46 | git commit -m E && | |
47 | git checkout -b branch1 A && | |
48 | : > file4 && | |
49 | git add file4 && | |
50 | test_tick && | |
51 | git commit -m F && | |
52 | git tag F && | |
53 | echo 3 > file1 && | |
54 | test_tick && | |
55 | git commit -m G file1 && | |
56 | : > file5 && | |
57 | git add file5 && | |
58 | test_tick && | |
59 | git commit -m 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 | ||
1b1dce4b JS |
68 | test_expect_success 'no changes are a nop' ' |
69 | git rebase -i F && | |
ab736792 | 70 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" && |
1b1dce4b JS |
71 | test $(git rev-parse I) = $(git rev-parse HEAD) |
72 | ' | |
73 | ||
c9e65892 JS |
74 | test_expect_success 'test the [branch] option' ' |
75 | git checkout -b dead-end && | |
76 | git rm file6 && | |
77 | git commit -m "stop here" && | |
78 | git rebase -i F branch2 && | |
ab736792 SB |
79 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" && |
80 | test $(git rev-parse I) = $(git rev-parse branch2) && | |
c9e65892 JS |
81 | test $(git rev-parse I) = $(git rev-parse HEAD) |
82 | ' | |
83 | ||
ab736792 SB |
84 | test_expect_success 'test --onto <branch>' ' |
85 | git checkout -b test-onto branch2 && | |
86 | git rebase -i --onto branch1 F && | |
87 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" && | |
88 | test $(git rev-parse HEAD^) = $(git rev-parse branch1) && | |
89 | test $(git rev-parse I) = $(git rev-parse branch2) | |
90 | ' | |
91 | ||
1b1dce4b JS |
92 | test_expect_success 'rebase on top of a non-conflicting commit' ' |
93 | git checkout branch1 && | |
94 | git tag original-branch1 && | |
95 | git rebase -i branch2 && | |
96 | test file6 = $(git diff --name-only original-branch1) && | |
ab736792 SB |
97 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" && |
98 | test $(git rev-parse I) = $(git rev-parse branch2) && | |
1b1dce4b JS |
99 | test $(git rev-parse I) = $(git rev-parse HEAD~2) |
100 | ' | |
101 | ||
68a163c9 JS |
102 | test_expect_success 'reflog for the branch shows state before rebase' ' |
103 | test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1) | |
104 | ' | |
105 | ||
1b1dce4b JS |
106 | test_expect_success 'exchange two commits' ' |
107 | FAKE_LINES="2 1" git rebase -i HEAD~2 && | |
b4ce54fc JK |
108 | test H = $(git cat-file commit HEAD^ | sed -ne \$p) && |
109 | test G = $(git cat-file commit HEAD | sed -ne \$p) | |
1b1dce4b JS |
110 | ' |
111 | ||
112 | cat > expect << EOF | |
113 | diff --git a/file1 b/file1 | |
114 | index e69de29..00750ed 100644 | |
115 | --- a/file1 | |
116 | +++ b/file1 | |
117 | @@ -0,0 +1 @@ | |
118 | +3 | |
119 | EOF | |
120 | ||
121 | cat > expect2 << EOF | |
122 | <<<<<<< HEAD:file1 | |
123 | 2 | |
124 | ======= | |
125 | 3 | |
126 | >>>>>>> b7ca976... G:file1 | |
127 | EOF | |
128 | ||
129 | test_expect_success 'stop on conflicting pick' ' | |
130 | git tag new-branch1 && | |
ab736792 SB |
131 | test_must_fail git rebase -i master && |
132 | test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" && | |
28ed6e7b | 133 | test_cmp expect .git/rebase-merge/patch && |
82ebb0b6 | 134 | test_cmp expect2 file1 && |
0cb0e143 | 135 | test "$(git diff --name-status | |
ab736792 | 136 | sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 && |
28ed6e7b JS |
137 | test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) && |
138 | test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo) | |
1b1dce4b JS |
139 | ' |
140 | ||
141 | test_expect_success 'abort' ' | |
142 | git rebase --abort && | |
143 | test $(git rev-parse new-branch1) = $(git rev-parse HEAD) && | |
ab736792 | 144 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" && |
28ed6e7b | 145 | ! test -d .git/rebase-merge |
1b1dce4b JS |
146 | ' |
147 | ||
148 | test_expect_success 'retain authorship' ' | |
149 | echo A > file7 && | |
150 | git add file7 && | |
c54b7817 | 151 | test_tick && |
1b1dce4b JS |
152 | GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" && |
153 | git tag twerp && | |
154 | git rebase -i --onto master HEAD^ && | |
155 | git show HEAD | grep "^Author: Twerp Snog" | |
156 | ' | |
157 | ||
158 | test_expect_success 'squash' ' | |
159 | git reset --hard twerp && | |
160 | echo B > file7 && | |
c54b7817 | 161 | test_tick && |
1b1dce4b JS |
162 | GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 && |
163 | echo "******************************" && | |
164 | FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 && | |
165 | test B = $(cat file7) && | |
166 | test $(git rev-parse HEAD^) = $(git rev-parse master) | |
167 | ' | |
168 | ||
169 | test_expect_success 'retain authorship when squashing' ' | |
81ab1cb4 | 170 | git show HEAD | grep "^Author: Twerp Snog" |
1b1dce4b JS |
171 | ' |
172 | ||
34454e85 JS |
173 | test_expect_success '-p handles "no changes" gracefully' ' |
174 | HEAD=$(git rev-parse HEAD) && | |
175 | git rebase -i -p HEAD^ && | |
71d9451e TR |
176 | git update-index --refresh && |
177 | git diff-files --quiet && | |
178 | git diff-index --quiet --cached HEAD -- && | |
34454e85 JS |
179 | test $HEAD = $(git rev-parse HEAD) |
180 | ' | |
181 | ||
f09c9b8c JS |
182 | test_expect_success 'preserve merges with -p' ' |
183 | git checkout -b to-be-preserved master^ && | |
184 | : > unrelated-file && | |
185 | git add unrelated-file && | |
186 | test_tick && | |
187 | git commit -m "unrelated" && | |
5352a82b | 188 | git checkout -b another-branch master && |
f09c9b8c JS |
189 | echo B > file1 && |
190 | test_tick && | |
191 | git commit -m J file1 && | |
192 | test_tick && | |
193 | git merge to-be-preserved && | |
194 | echo C > file1 && | |
195 | test_tick && | |
196 | git commit -m K file1 && | |
5352a82b SB |
197 | echo D > file1 && |
198 | test_tick && | |
199 | git commit -m L1 file1 && | |
200 | git checkout HEAD^ && | |
201 | echo 1 > unrelated-file && | |
202 | test_tick && | |
203 | git commit -m L2 unrelated-file && | |
204 | test_tick && | |
205 | git merge another-branch && | |
206 | echo E > file1 && | |
207 | test_tick && | |
208 | git commit -m M file1 && | |
209 | git checkout -b to-be-rebased && | |
18640d99 | 210 | test_tick && |
f09c9b8c | 211 | git rebase -i -p --onto branch1 master && |
71d9451e TR |
212 | git update-index --refresh && |
213 | git diff-files --quiet && | |
214 | git diff-index --quiet --cached HEAD -- && | |
5352a82b SB |
215 | test $(git rev-parse HEAD~6) = $(git rev-parse branch1) && |
216 | test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) && | |
217 | test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) && | |
218 | test $(git show HEAD~5:file1) = B && | |
219 | test $(git show HEAD~3:file1) = C && | |
220 | test $(git show HEAD:file1) = E && | |
221 | test $(git show HEAD:unrelated-file) = 1 | |
f09c9b8c JS |
222 | ' |
223 | ||
a96dc01e TR |
224 | test_expect_success 'edit ancestor with -p' ' |
225 | FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 && | |
226 | echo 2 > unrelated-file && | |
227 | test_tick && | |
228 | git commit -m L2-modified --amend unrelated-file && | |
229 | git rebase --continue && | |
230 | git update-index --refresh && | |
231 | git diff-files --quiet && | |
232 | git diff-index --quiet --cached HEAD -- && | |
233 | test $(git show HEAD:unrelated-file) = 2 | |
234 | ' | |
235 | ||
18640d99 JS |
236 | test_expect_success '--continue tries to commit' ' |
237 | test_tick && | |
ab736792 | 238 | test_must_fail git rebase -i --onto new-branch1 HEAD^ && |
18640d99 JS |
239 | echo resolved > file1 && |
240 | git add file1 && | |
241 | FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue && | |
242 | test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) && | |
243 | git show HEAD | grep chouette | |
244 | ' | |
245 | ||
8e4a91bd JS |
246 | test_expect_success 'verbose flag is heeded, even after --continue' ' |
247 | git reset --hard HEAD@{1} && | |
248 | test_tick && | |
ab736792 | 249 | test_must_fail git rebase -v -i --onto new-branch1 HEAD^ && |
8e4a91bd JS |
250 | echo resolved > file1 && |
251 | git add file1 && | |
252 | git rebase --continue > output && | |
253 | grep "^ file1 | 2 +-$" output | |
254 | ' | |
255 | ||
6368f3f8 JS |
256 | test_expect_success 'multi-squash only fires up editor once' ' |
257 | base=$(git rev-parse HEAD~4) && | |
258 | FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \ | |
259 | git rebase -i $base && | |
260 | test $base = $(git rev-parse HEAD^) && | |
261 | test 1 = $(git show | grep ONCE | wc -l) | |
262 | ' | |
263 | ||
fb47cfbd JS |
264 | test_expect_success 'squash works as expected' ' |
265 | for n in one two three four | |
266 | do | |
267 | echo $n >> file$n && | |
268 | git add file$n && | |
269 | git commit -m $n | |
270 | done && | |
271 | one=$(git rev-parse HEAD~3) && | |
272 | FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 && | |
273 | test $one = $(git rev-parse HEAD~2) | |
274 | ' | |
275 | ||
276 | test_expect_success 'interrupted squash works as expected' ' | |
277 | for n in one two three four | |
278 | do | |
279 | echo $n >> conflict && | |
280 | git add conflict && | |
281 | git commit -m $n | |
282 | done && | |
283 | one=$(git rev-parse HEAD~3) && | |
ab736792 SB |
284 | ( |
285 | FAKE_LINES="1 squash 3 2" && | |
286 | export FAKE_LINES && | |
287 | test_must_fail git rebase -i HEAD~3 | |
288 | ) && | |
fb47cfbd JS |
289 | (echo one; echo two; echo four) > conflict && |
290 | git add conflict && | |
ab736792 | 291 | test_must_fail git rebase --continue && |
fb47cfbd JS |
292 | echo resolved > conflict && |
293 | git add conflict && | |
294 | git rebase --continue && | |
295 | test $one = $(git rev-parse HEAD~2) | |
296 | ' | |
297 | ||
1d25c8cf JS |
298 | test_expect_success 'interrupted squash works as expected (case 2)' ' |
299 | for n in one two three four | |
300 | do | |
301 | echo $n >> conflict && | |
302 | git add conflict && | |
303 | git commit -m $n | |
304 | done && | |
305 | one=$(git rev-parse HEAD~3) && | |
ab736792 SB |
306 | ( |
307 | FAKE_LINES="3 squash 1 2" && | |
308 | export FAKE_LINES && | |
309 | test_must_fail git rebase -i HEAD~3 | |
310 | ) && | |
1d25c8cf JS |
311 | (echo one; echo four) > conflict && |
312 | git add conflict && | |
ab736792 | 313 | test_must_fail git rebase --continue && |
1d25c8cf JS |
314 | (echo one; echo two; echo four) > conflict && |
315 | git add conflict && | |
ab736792 | 316 | test_must_fail git rebase --continue && |
1d25c8cf JS |
317 | echo resolved > conflict && |
318 | git add conflict && | |
319 | git rebase --continue && | |
320 | test $one = $(git rev-parse HEAD~2) | |
321 | ' | |
322 | ||
96ffe892 JS |
323 | test_expect_success 'ignore patch if in upstream' ' |
324 | HEAD=$(git rev-parse HEAD) && | |
325 | git checkout -b has-cherry-picked HEAD^ && | |
326 | echo unrelated > file7 && | |
327 | git add file7 && | |
328 | test_tick && | |
329 | git commit -m "unrelated change" && | |
330 | git cherry-pick $HEAD && | |
331 | EXPECT_COUNT=1 git rebase -i $HEAD && | |
332 | test $HEAD = $(git rev-parse HEAD^) | |
333 | ' | |
334 | ||
be6ff208 JS |
335 | test_expect_success '--continue tries to commit, even for "edit"' ' |
336 | parent=$(git rev-parse HEAD^) && | |
337 | test_tick && | |
338 | FAKE_LINES="edit 1" git rebase -i HEAD^ && | |
339 | echo edited > file7 && | |
340 | git add file7 && | |
341 | FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue && | |
342 | test edited = $(git show HEAD:file7) && | |
343 | git show HEAD | grep chouette && | |
344 | test $parent = $(git rev-parse HEAD^) | |
345 | ' | |
346 | ||
dc7f55cb SB |
347 | test_expect_success 'aborted --continue does not squash commits after "edit"' ' |
348 | old=$(git rev-parse HEAD) && | |
349 | test_tick && | |
350 | FAKE_LINES="edit 1" git rebase -i HEAD^ && | |
351 | echo "edited again" > file7 && | |
352 | git add file7 && | |
353 | ( | |
354 | FAKE_COMMIT_MESSAGE=" " && | |
355 | export FAKE_COMMIT_MESSAGE && | |
356 | test_must_fail git rebase --continue | |
357 | ) && | |
358 | test $old = $(git rev-parse HEAD) && | |
359 | git rebase --abort | |
360 | ' | |
361 | ||
f8aa1b69 SB |
362 | test_expect_success 'auto-amend only edited commits after "edit"' ' |
363 | test_tick && | |
364 | FAKE_LINES="edit 1" git rebase -i HEAD^ && | |
365 | echo "edited again" > file7 && | |
366 | git add file7 && | |
367 | FAKE_COMMIT_MESSAGE="edited file7 again" git commit && | |
368 | echo "and again" > file7 && | |
369 | git add file7 && | |
370 | test_tick && | |
371 | ( | |
372 | FAKE_COMMIT_MESSAGE="and again" && | |
373 | export FAKE_COMMIT_MESSAGE && | |
374 | test_must_fail git rebase --continue | |
375 | ) && | |
376 | git rebase --abort | |
377 | ' | |
378 | ||
73697a0b JS |
379 | test_expect_success 'rebase a detached HEAD' ' |
380 | grandparent=$(git rev-parse HEAD~2) && | |
381 | git checkout $(git rev-parse HEAD) && | |
382 | test_tick && | |
383 | FAKE_LINES="2 1" git rebase -i HEAD~2 && | |
384 | test $grandparent = $(git rev-parse HEAD~2) | |
385 | ' | |
386 | ||
752527f5 JS |
387 | test_expect_success 'rebase a commit violating pre-commit' ' |
388 | ||
389 | mkdir -p .git/hooks && | |
390 | PRE_COMMIT=.git/hooks/pre-commit && | |
391 | echo "#!/bin/sh" > $PRE_COMMIT && | |
392 | echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT && | |
393 | chmod a+x $PRE_COMMIT && | |
394 | echo "monde! " >> file1 && | |
395 | test_tick && | |
ab736792 | 396 | test_must_fail git commit -m doesnt-verify file1 && |
752527f5 JS |
397 | git commit -m doesnt-verify --no-verify file1 && |
398 | test_tick && | |
399 | FAKE_LINES=2 git rebase -i HEAD~2 | |
400 | ||
401 | ' | |
402 | ||
077b725f JH |
403 | test_expect_success 'rebase with a file named HEAD in worktree' ' |
404 | ||
405 | rm -fr .git/hooks && | |
406 | git reset --hard && | |
407 | git checkout -b branch3 A && | |
408 | ||
409 | ( | |
410 | GIT_AUTHOR_NAME="Squashed Away" && | |
411 | export GIT_AUTHOR_NAME && | |
412 | >HEAD && | |
413 | git add HEAD && | |
414 | git commit -m "Add head" && | |
415 | >BODY && | |
416 | git add BODY && | |
417 | git commit -m "Add body" | |
418 | ) && | |
419 | ||
420 | FAKE_LINES="1 squash 2" git rebase -i to-be-rebased && | |
421 | test "$(git show -s --pretty=format:%an)" = "Squashed Away" | |
422 | ||
423 | ' | |
424 | ||
ff74126c JS |
425 | test_expect_success 'do "noop" when there is nothing to cherry-pick' ' |
426 | ||
427 | git checkout -b branch4 HEAD && | |
428 | GIT_EDITOR=: git commit --amend \ | |
429 | --author="Somebody else <somebody@else.com>" | |
430 | test $(git rev-parse branch3) != $(git rev-parse branch4) && | |
431 | git rebase -i branch3 && | |
432 | test $(git rev-parse branch3) = $(git rev-parse branch4) | |
433 | ||
434 | ' | |
435 | ||
96747696 JH |
436 | test_expect_success 'submodule rebase setup' ' |
437 | git checkout A && | |
438 | mkdir sub && | |
439 | ( | |
440 | cd sub && git init && >elif && | |
441 | git add elif && git commit -m "submodule initial" | |
442 | ) && | |
443 | echo 1 >file1 && | |
444 | git add file1 sub | |
445 | test_tick && | |
446 | git commit -m "One" && | |
447 | echo 2 >file1 && | |
448 | test_tick && | |
449 | git commit -a -m "Two" && | |
450 | ( | |
451 | cd sub && echo 3 >elif && | |
452 | git commit -a -m "submodule second" | |
453 | ) && | |
454 | test_tick && | |
455 | git commit -a -m "Three changes submodule" | |
456 | ' | |
457 | ||
94c88ede | 458 | test_expect_success 'submodule rebase -i' ' |
96747696 JH |
459 | FAKE_LINES="1 squash 2 3" git rebase -i A |
460 | ' | |
461 | ||
1b1dce4b | 462 | test_done |