Merge branch 'ic/maint-rebase-i-abort'
[git/git.git] / t / t3404-rebase-interactive.sh
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
13 . "$TEST_DIRECTORY"/lib-rebase.sh
14
15 set_fake_editor
16
17 # Set up the repository like this:
18 #
19 # one - two - three - four (conflict-branch)
20 # /
21 # A - B - C - D - E (master)
22 # | \
23 # | F - G - H (branch1)
24 # | \
25 # |\ I (branch2)
26 # | \
27 # | J - K - L - M (no-conflict-branch)
28 # \
29 # N - O - P (no-ff-branch)
30 #
31 # where A, B, D and G all touch file1, and one, two, three, four all
32 # touch file "conflict".
33 #
34 # WARNING: Modifications to the initial repository can change the SHA ID used
35 # in the expect2 file for the 'stop on conflicting pick' test.
36
37
38 test_expect_success 'setup' '
39 test_commit A file1 &&
40 test_commit B file1 &&
41 test_commit C file2 &&
42 test_commit D file1 &&
43 test_commit E file3 &&
44 git checkout -b branch1 A &&
45 test_commit F file4 &&
46 test_commit G file1 &&
47 test_commit H file5 &&
48 git checkout -b branch2 F &&
49 test_commit I file6
50 git checkout -b conflict-branch A &&
51 for n in one two three four
52 do
53 test_commit $n conflict
54 done &&
55 git checkout -b no-conflict-branch A &&
56 for n in J K L M
57 do
58 test_commit $n file$n
59 done &&
60 git checkout -b no-ff-branch A &&
61 for n in N O P
62 do
63 test_commit $n file$n
64 done
65 '
66
67 test_expect_success 'no changes are a nop' '
68 git checkout branch2 &&
69 git rebase -i F &&
70 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
71 test $(git rev-parse I) = $(git rev-parse HEAD)
72 '
73
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 &&
79 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
80 test $(git rev-parse I) = $(git rev-parse branch2) &&
81 test $(git rev-parse I) = $(git rev-parse HEAD)
82 '
83
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
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) &&
97 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
98 test $(git rev-parse I) = $(git rev-parse branch2) &&
99 test $(git rev-parse I) = $(git rev-parse HEAD~2)
100 '
101
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
106 test_expect_success 'exchange two commits' '
107 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
108 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
109 test G = $(git cat-file commit HEAD | sed -ne \$p)
110 '
111
112 cat > expect << EOF
113 diff --git a/file1 b/file1
114 index f70f10e..fd79235 100644
115 --- a/file1
116 +++ b/file1
117 @@ -1 +1 @@
118 -A
119 +G
120 EOF
121
122 cat > expect2 << EOF
123 <<<<<<< HEAD
124 D
125 =======
126 G
127 >>>>>>> 5d18e54... G
128 EOF
129
130 test_expect_success 'stop on conflicting pick' '
131 git tag new-branch1 &&
132 test_must_fail git rebase -i master &&
133 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
134 test_cmp expect .git/rebase-merge/patch &&
135 test_cmp expect2 file1 &&
136 test "$(git diff --name-status |
137 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
138 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
139 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
140 '
141
142 test_expect_success 'abort' '
143 git rebase --abort &&
144 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
145 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
146 ! test -d .git/rebase-merge
147 '
148
149 test_expect_success 'abort with error when new base cannot be checked out' '
150 git rm --cached file1 &&
151 git commit -m "remove file in base" &&
152 test_must_fail git rebase -i master > output 2>&1 &&
153 grep "Untracked working tree file .file1. would be overwritten" \
154 output &&
155 ! test -d .git/rebase-merge &&
156 git reset --hard HEAD^
157 '
158
159 test_expect_success 'retain authorship' '
160 echo A > file7 &&
161 git add file7 &&
162 test_tick &&
163 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
164 git tag twerp &&
165 git rebase -i --onto master HEAD^ &&
166 git show HEAD | grep "^Author: Twerp Snog"
167 '
168
169 test_expect_success 'squash' '
170 git reset --hard twerp &&
171 echo B > file7 &&
172 test_tick &&
173 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
174 echo "******************************" &&
175 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
176 git rebase -i --onto master HEAD~2 &&
177 test B = $(cat file7) &&
178 test $(git rev-parse HEAD^) = $(git rev-parse master)
179 '
180
181 test_expect_success 'retain authorship when squashing' '
182 git show HEAD | grep "^Author: Twerp Snog"
183 '
184
185 test_expect_success '-p handles "no changes" gracefully' '
186 HEAD=$(git rev-parse HEAD) &&
187 git rebase -i -p HEAD^ &&
188 git update-index --refresh &&
189 git diff-files --quiet &&
190 git diff-index --quiet --cached HEAD -- &&
191 test $HEAD = $(git rev-parse HEAD)
192 '
193
194 test_expect_failure 'exchange two commits with -p' '
195 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
196 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
197 test G = $(git cat-file commit HEAD | sed -ne \$p)
198 '
199
200 test_expect_success 'preserve merges with -p' '
201 git checkout -b to-be-preserved master^ &&
202 : > unrelated-file &&
203 git add unrelated-file &&
204 test_tick &&
205 git commit -m "unrelated" &&
206 git checkout -b another-branch master &&
207 echo B > file1 &&
208 test_tick &&
209 git commit -m J file1 &&
210 test_tick &&
211 git merge to-be-preserved &&
212 echo C > file1 &&
213 test_tick &&
214 git commit -m K file1 &&
215 echo D > file1 &&
216 test_tick &&
217 git commit -m L1 file1 &&
218 git checkout HEAD^ &&
219 echo 1 > unrelated-file &&
220 test_tick &&
221 git commit -m L2 unrelated-file &&
222 test_tick &&
223 git merge another-branch &&
224 echo E > file1 &&
225 test_tick &&
226 git commit -m M file1 &&
227 git checkout -b to-be-rebased &&
228 test_tick &&
229 git rebase -i -p --onto branch1 master &&
230 git update-index --refresh &&
231 git diff-files --quiet &&
232 git diff-index --quiet --cached HEAD -- &&
233 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
234 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
235 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
236 test $(git show HEAD~5:file1) = B &&
237 test $(git show HEAD~3:file1) = C &&
238 test $(git show HEAD:file1) = E &&
239 test $(git show HEAD:unrelated-file) = 1
240 '
241
242 test_expect_success 'edit ancestor with -p' '
243 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
244 echo 2 > unrelated-file &&
245 test_tick &&
246 git commit -m L2-modified --amend unrelated-file &&
247 git rebase --continue &&
248 git update-index --refresh &&
249 git diff-files --quiet &&
250 git diff-index --quiet --cached HEAD -- &&
251 test $(git show HEAD:unrelated-file) = 2
252 '
253
254 test_expect_success '--continue tries to commit' '
255 test_tick &&
256 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
257 echo resolved > file1 &&
258 git add file1 &&
259 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
260 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
261 git show HEAD | grep chouette
262 '
263
264 test_expect_success 'verbose flag is heeded, even after --continue' '
265 git reset --hard HEAD@{1} &&
266 test_tick &&
267 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
268 echo resolved > file1 &&
269 git add file1 &&
270 git rebase --continue > output &&
271 grep "^ file1 | 2 +-$" output
272 '
273
274 test_expect_success 'multi-squash only fires up editor once' '
275 base=$(git rev-parse HEAD~4) &&
276 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
277 EXPECT_HEADER_COUNT=4 \
278 git rebase -i $base &&
279 test $base = $(git rev-parse HEAD^) &&
280 test 1 = $(git show | grep ONCE | wc -l)
281 '
282
283 test_expect_success 'multi-fixup does not fire up editor' '
284 git checkout -b multi-fixup E &&
285 base=$(git rev-parse HEAD~4) &&
286 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
287 git rebase -i $base &&
288 test $base = $(git rev-parse HEAD^) &&
289 test 0 = $(git show | grep NEVER | wc -l) &&
290 git checkout to-be-rebased &&
291 git branch -D multi-fixup
292 '
293
294 test_expect_success 'commit message used after conflict' '
295 git checkout -b conflict-fixup conflict-branch &&
296 base=$(git rev-parse HEAD~4) &&
297 (
298 FAKE_LINES="1 fixup 3 fixup 4" &&
299 export FAKE_LINES &&
300 test_must_fail git rebase -i $base
301 ) &&
302 echo three > conflict &&
303 git add conflict &&
304 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
305 git rebase --continue &&
306 test $base = $(git rev-parse HEAD^) &&
307 test 1 = $(git show | grep ONCE | wc -l) &&
308 git checkout to-be-rebased &&
309 git branch -D conflict-fixup
310 '
311
312 test_expect_success 'commit message retained after conflict' '
313 git checkout -b conflict-squash conflict-branch &&
314 base=$(git rev-parse HEAD~4) &&
315 (
316 FAKE_LINES="1 fixup 3 squash 4" &&
317 export FAKE_LINES &&
318 test_must_fail git rebase -i $base
319 ) &&
320 echo three > conflict &&
321 git add conflict &&
322 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
323 git rebase --continue &&
324 test $base = $(git rev-parse HEAD^) &&
325 test 2 = $(git show | grep TWICE | wc -l) &&
326 git checkout to-be-rebased &&
327 git branch -D conflict-squash
328 '
329
330 cat > expect-squash-fixup << EOF
331 B
332
333 D
334
335 ONCE
336 EOF
337
338 test_expect_success 'squash and fixup generate correct log messages' '
339 git checkout -b squash-fixup E &&
340 base=$(git rev-parse HEAD~4) &&
341 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
342 EXPECT_HEADER_COUNT=4 \
343 git rebase -i $base &&
344 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
345 test_cmp expect-squash-fixup actual-squash-fixup &&
346 git checkout to-be-rebased &&
347 git branch -D squash-fixup
348 '
349
350 test_expect_success 'squash ignores comments' '
351 git checkout -b skip-comments E &&
352 base=$(git rev-parse HEAD~4) &&
353 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
354 EXPECT_HEADER_COUNT=4 \
355 git rebase -i $base &&
356 test $base = $(git rev-parse HEAD^) &&
357 test 1 = $(git show | grep ONCE | wc -l) &&
358 git checkout to-be-rebased &&
359 git branch -D skip-comments
360 '
361
362 test_expect_success 'squash ignores blank lines' '
363 git checkout -b skip-blank-lines E &&
364 base=$(git rev-parse HEAD~4) &&
365 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
366 EXPECT_HEADER_COUNT=4 \
367 git rebase -i $base &&
368 test $base = $(git rev-parse HEAD^) &&
369 test 1 = $(git show | grep ONCE | wc -l) &&
370 git checkout to-be-rebased &&
371 git branch -D skip-blank-lines
372 '
373
374 test_expect_success 'squash works as expected' '
375 git checkout -b squash-works no-conflict-branch &&
376 one=$(git rev-parse HEAD~3) &&
377 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
378 git rebase -i HEAD~3 &&
379 test $one = $(git rev-parse HEAD~2)
380 '
381
382 test_expect_success 'interrupted squash works as expected' '
383 git checkout -b interrupted-squash conflict-branch &&
384 one=$(git rev-parse HEAD~3) &&
385 (
386 FAKE_LINES="1 squash 3 2" &&
387 export FAKE_LINES &&
388 test_must_fail git rebase -i HEAD~3
389 ) &&
390 (echo one; echo two; echo four) > conflict &&
391 git add conflict &&
392 test_must_fail git rebase --continue &&
393 echo resolved > conflict &&
394 git add conflict &&
395 git rebase --continue &&
396 test $one = $(git rev-parse HEAD~2)
397 '
398
399 test_expect_success 'interrupted squash works as expected (case 2)' '
400 git checkout -b interrupted-squash2 conflict-branch &&
401 one=$(git rev-parse HEAD~3) &&
402 (
403 FAKE_LINES="3 squash 1 2" &&
404 export FAKE_LINES &&
405 test_must_fail git rebase -i HEAD~3
406 ) &&
407 (echo one; echo four) > conflict &&
408 git add conflict &&
409 test_must_fail git rebase --continue &&
410 (echo one; echo two; echo four) > conflict &&
411 git add conflict &&
412 test_must_fail git rebase --continue &&
413 echo resolved > conflict &&
414 git add conflict &&
415 git rebase --continue &&
416 test $one = $(git rev-parse HEAD~2)
417 '
418
419 test_expect_success 'ignore patch if in upstream' '
420 HEAD=$(git rev-parse HEAD) &&
421 git checkout -b has-cherry-picked HEAD^ &&
422 echo unrelated > file7 &&
423 git add file7 &&
424 test_tick &&
425 git commit -m "unrelated change" &&
426 git cherry-pick $HEAD &&
427 EXPECT_COUNT=1 git rebase -i $HEAD &&
428 test $HEAD = $(git rev-parse HEAD^)
429 '
430
431 test_expect_success '--continue tries to commit, even for "edit"' '
432 parent=$(git rev-parse HEAD^) &&
433 test_tick &&
434 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
435 echo edited > file7 &&
436 git add file7 &&
437 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
438 test edited = $(git show HEAD:file7) &&
439 git show HEAD | grep chouette &&
440 test $parent = $(git rev-parse HEAD^)
441 '
442
443 test_expect_success 'aborted --continue does not squash commits after "edit"' '
444 old=$(git rev-parse HEAD) &&
445 test_tick &&
446 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
447 echo "edited again" > file7 &&
448 git add file7 &&
449 (
450 FAKE_COMMIT_MESSAGE=" " &&
451 export FAKE_COMMIT_MESSAGE &&
452 test_must_fail git rebase --continue
453 ) &&
454 test $old = $(git rev-parse HEAD) &&
455 git rebase --abort
456 '
457
458 test_expect_success 'auto-amend only edited commits after "edit"' '
459 test_tick &&
460 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
461 echo "edited again" > file7 &&
462 git add file7 &&
463 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
464 echo "and again" > file7 &&
465 git add file7 &&
466 test_tick &&
467 (
468 FAKE_COMMIT_MESSAGE="and again" &&
469 export FAKE_COMMIT_MESSAGE &&
470 test_must_fail git rebase --continue
471 ) &&
472 git rebase --abort
473 '
474
475 test_expect_success 'rebase a detached HEAD' '
476 grandparent=$(git rev-parse HEAD~2) &&
477 git checkout $(git rev-parse HEAD) &&
478 test_tick &&
479 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
480 test $grandparent = $(git rev-parse HEAD~2)
481 '
482
483 test_expect_success 'rebase a commit violating pre-commit' '
484
485 mkdir -p .git/hooks &&
486 PRE_COMMIT=.git/hooks/pre-commit &&
487 echo "#!/bin/sh" > $PRE_COMMIT &&
488 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
489 chmod a+x $PRE_COMMIT &&
490 echo "monde! " >> file1 &&
491 test_tick &&
492 test_must_fail git commit -m doesnt-verify file1 &&
493 git commit -m doesnt-verify --no-verify file1 &&
494 test_tick &&
495 FAKE_LINES=2 git rebase -i HEAD~2
496
497 '
498
499 test_expect_success 'rebase with a file named HEAD in worktree' '
500
501 rm -fr .git/hooks &&
502 git reset --hard &&
503 git checkout -b branch3 A &&
504
505 (
506 GIT_AUTHOR_NAME="Squashed Away" &&
507 export GIT_AUTHOR_NAME &&
508 >HEAD &&
509 git add HEAD &&
510 git commit -m "Add head" &&
511 >BODY &&
512 git add BODY &&
513 git commit -m "Add body"
514 ) &&
515
516 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
517 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
518
519 '
520
521 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
522
523 git checkout -b branch4 HEAD &&
524 GIT_EDITOR=: git commit --amend \
525 --author="Somebody else <somebody@else.com>"
526 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
527 git rebase -i branch3 &&
528 test $(git rev-parse branch3) = $(git rev-parse branch4)
529
530 '
531
532 test_expect_success 'submodule rebase setup' '
533 git checkout A &&
534 mkdir sub &&
535 (
536 cd sub && git init && >elif &&
537 git add elif && git commit -m "submodule initial"
538 ) &&
539 echo 1 >file1 &&
540 git add file1 sub
541 test_tick &&
542 git commit -m "One" &&
543 echo 2 >file1 &&
544 test_tick &&
545 git commit -a -m "Two" &&
546 (
547 cd sub && echo 3 >elif &&
548 git commit -a -m "submodule second"
549 ) &&
550 test_tick &&
551 git commit -a -m "Three changes submodule"
552 '
553
554 test_expect_success 'submodule rebase -i' '
555 FAKE_LINES="1 squash 2 3" git rebase -i A
556 '
557
558 test_expect_success 'avoid unnecessary reset' '
559 git checkout master &&
560 test-chmtime =123456789 file3 &&
561 git update-index --refresh &&
562 HEAD=$(git rev-parse HEAD) &&
563 git rebase -i HEAD~4 &&
564 test $HEAD = $(git rev-parse HEAD) &&
565 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
566 test 123456789 = $MTIME
567 '
568
569 test_expect_success 'reword' '
570 git checkout -b reword-branch master &&
571 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
572 git show HEAD | grep "E changed" &&
573 test $(git rev-parse master) != $(git rev-parse HEAD) &&
574 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
575 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
576 git show HEAD^ | grep "D changed" &&
577 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
578 git show HEAD~3 | grep "B changed" &&
579 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
580 git show HEAD~2 | grep "C changed"
581 '
582
583 test_expect_success 'rebase -i can copy notes' '
584 git config notes.rewrite.rebase true &&
585 git config notes.rewriteRef "refs/notes/*" &&
586 test_commit n1 &&
587 test_commit n2 &&
588 test_commit n3 &&
589 git notes add -m"a note" n3 &&
590 git rebase --onto n1 n2 &&
591 test "a note" = "$(git notes show HEAD)"
592 '
593
594 cat >expect <<EOF
595 an earlier note
596 a note
597 EOF
598
599 test_expect_success 'rebase -i can copy notes over a fixup' '
600 git reset --hard n3 &&
601 git notes add -m"an earlier note" n2 &&
602 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
603 git notes show > output &&
604 test_cmp expect output
605 '
606
607 test_expect_success 'rebase while detaching HEAD' '
608 git symbolic-ref HEAD &&
609 grandparent=$(git rev-parse HEAD~2) &&
610 test_tick &&
611 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
612 test $grandparent = $(git rev-parse HEAD~2) &&
613 test_must_fail git symbolic-ref HEAD
614 '
615
616 test_tick # Ensure that the rebased commits get a different timestamp.
617 test_expect_success 'always cherry-pick with --no-ff' '
618 git checkout no-ff-branch &&
619 git tag original-no-ff-branch &&
620 git rebase -i --no-ff A &&
621 touch empty &&
622 for p in 0 1 2
623 do
624 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
625 git diff HEAD~$p original-no-ff-branch~$p > out &&
626 test_cmp empty out
627 done &&
628 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
629 git diff HEAD~3 original-no-ff-branch~3 > out &&
630 test_cmp empty out
631 '
632
633 test_done