]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3404-rebase-interactive.sh
tests: remove leftover untracked files
[thirdparty/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 Initial setup:
12
13 one - two - three - four (conflict-branch)
14 /
15 A - B - C - D - E (primary)
16 | \
17 | F - G - H (branch1)
18 | \
19 |\ I (branch2)
20 | \
21 | J - K - L - M (no-conflict-branch)
22 \
23 N - O - P (no-ff-branch)
24
25 where A, B, D and G all touch file1, and one, two, three, four all
26 touch file "conflict".
27 '
28 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
29 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
30
31 . ./test-lib.sh
32
33 . "$TEST_DIRECTORY"/lib-rebase.sh
34
35 test_expect_success 'setup' '
36 git switch -C primary &&
37 test_commit A file1 &&
38 test_commit B file1 &&
39 test_commit C file2 &&
40 test_commit D file1 &&
41 test_commit E file3 &&
42 git checkout -b branch1 A &&
43 test_commit F file4 &&
44 test_commit G file1 &&
45 test_commit H file5 &&
46 git checkout -b branch2 F &&
47 test_commit I file6 &&
48 git checkout -b conflict-branch A &&
49 test_commit one conflict &&
50 test_commit two conflict &&
51 test_commit three conflict &&
52 test_commit four conflict &&
53 git checkout -b no-conflict-branch A &&
54 test_commit J fileJ &&
55 test_commit K fileK &&
56 test_commit L fileL &&
57 test_commit M fileM &&
58 git checkout -b no-ff-branch A &&
59 test_commit N fileN &&
60 test_commit O fileO &&
61 test_commit P fileP
62 '
63
64 # "exec" commands are run with the user shell by default, but this may
65 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
66 # to create a file. Unsetting SHELL avoids such non-portable behavior
67 # in tests. It must be exported for it to take effect where needed.
68 SHELL=
69 export SHELL
70
71 test_expect_success 'rebase --keep-empty' '
72 git checkout -b emptybranch primary &&
73 git commit --allow-empty -m "empty" &&
74 git rebase --keep-empty -i HEAD~2 &&
75 git log --oneline >actual &&
76 test_line_count = 6 actual
77 '
78
79 test_expect_success 'rebase -i with empty todo list' '
80 cat >expect <<-\EOF &&
81 error: nothing to do
82 EOF
83 (
84 set_fake_editor &&
85 test_must_fail env FAKE_LINES="#" \
86 git rebase -i HEAD^ >output 2>&1
87 ) &&
88 tail -n 1 output >actual && # Ignore output about changing todo list
89 test_cmp expect actual
90 '
91
92 test_expect_success 'rebase -i with the exec command' '
93 git checkout primary &&
94 (
95 set_fake_editor &&
96 FAKE_LINES="1 exec_>touch-one
97 2 exec_>touch-two exec_false exec_>touch-three
98 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
99 export FAKE_LINES &&
100 test_must_fail git rebase -i A
101 ) &&
102 test_path_is_file touch-one &&
103 test_path_is_file touch-two &&
104 # Missing because we should have stopped by now.
105 test_path_is_missing touch-three &&
106 test_cmp_rev C HEAD &&
107 git rebase --continue &&
108 test_path_is_file touch-three &&
109 test_path_is_file "touch-file name with spaces" &&
110 test_path_is_file touch-after-semicolon &&
111 test_cmp_rev primary HEAD &&
112 rm -f touch-*
113 '
114
115 test_expect_success 'rebase -i with the exec command runs from tree root' '
116 git checkout primary &&
117 mkdir subdir && (cd subdir &&
118 set_fake_editor &&
119 FAKE_LINES="1 exec_>touch-subdir" \
120 git rebase -i HEAD^
121 ) &&
122 test_path_is_file touch-subdir &&
123 rm -fr subdir
124 '
125
126 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
127 test_when_finished "rm -rf subdir" &&
128 test_when_finished "git rebase --abort ||:" &&
129 git checkout primary &&
130 mkdir subdir && (cd subdir &&
131 set_fake_editor &&
132 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
133 git rebase -i HEAD^
134 )
135 '
136
137 test_expect_success 'rebase -i sets work tree properly' '
138 test_when_finished "rm -rf subdir" &&
139 test_when_finished "test_might_fail git rebase --abort" &&
140 mkdir subdir &&
141 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
142 >actual &&
143 ! grep "/subdir$" actual
144 '
145
146 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
147 git checkout primary &&
148 (
149 set_fake_editor &&
150 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
151 git rebase -i HEAD^
152 ) &&
153 test_cmp_rev primary^ HEAD &&
154 git reset --hard &&
155 git rebase --continue
156 '
157
158 test_expect_success 'rebase -x with empty command fails' '
159 test_when_finished "git rebase --abort ||:" &&
160 test_must_fail env git rebase -x "" @ 2>actual &&
161 test_write_lines "error: empty exec command" >expected &&
162 test_cmp expected actual &&
163 test_must_fail env git rebase -x " " @ 2>actual &&
164 test_cmp expected actual
165 '
166
167 test_expect_success 'rebase -x with newline in command fails' '
168 test_when_finished "git rebase --abort ||:" &&
169 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
170 test_write_lines "error: exec commands cannot contain newlines" \
171 >expected &&
172 test_cmp expected actual
173 '
174
175 test_expect_success 'rebase -i with exec of inexistent command' '
176 git checkout primary &&
177 test_when_finished "git rebase --abort" &&
178 (
179 set_fake_editor &&
180 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
181 git rebase -i HEAD^ >actual 2>&1
182 ) &&
183 ! grep "Maybe git-rebase is broken" actual
184 '
185
186 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
187 test_when_finished "git rebase --abort ||:" &&
188 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
189 '
190
191 test_expect_success 'no changes are a nop' '
192 git checkout branch2 &&
193 git rebase -i F &&
194 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
195 test_cmp_rev I HEAD
196 '
197
198 test_expect_success 'test the [branch] option' '
199 git checkout -b dead-end &&
200 git rm file6 &&
201 git commit -m "stop here" &&
202 git rebase -i F branch2 &&
203 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
204 test_cmp_rev I branch2 &&
205 test_cmp_rev I HEAD
206 '
207
208 test_expect_success 'test --onto <branch>' '
209 git checkout -b test-onto branch2 &&
210 git rebase -i --onto branch1 F &&
211 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
212 test_cmp_rev HEAD^ branch1 &&
213 test_cmp_rev I branch2
214 '
215
216 test_expect_success 'rebase on top of a non-conflicting commit' '
217 git checkout branch1 &&
218 git tag original-branch1 &&
219 git rebase -i branch2 &&
220 test file6 = $(git diff --name-only original-branch1) &&
221 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
222 test_cmp_rev I branch2 &&
223 test_cmp_rev I HEAD~2
224 '
225
226 test_expect_success 'reflog for the branch shows state before rebase' '
227 test_cmp_rev branch1@{1} original-branch1
228 '
229
230 test_expect_success 'reflog for the branch shows correct finish message' '
231 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
232 "$(git rev-parse branch2)" >expected &&
233 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
234 test_cmp expected actual
235 '
236
237 test_expect_success 'exchange two commits' '
238 (
239 set_fake_editor &&
240 FAKE_LINES="2 1" git rebase -i HEAD~2
241 ) &&
242 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
243 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
244 blob1=$(git rev-parse --short HEAD^:file1) &&
245 blob2=$(git rev-parse --short HEAD:file1) &&
246 commit=$(git rev-parse --short HEAD)
247 '
248
249 test_expect_success 'stop on conflicting pick' '
250 cat >expect <<-EOF &&
251 diff --git a/file1 b/file1
252 index $blob1..$blob2 100644
253 --- a/file1
254 +++ b/file1
255 @@ -1 +1 @@
256 -A
257 +G
258 EOF
259 cat >expect2 <<-EOF &&
260 <<<<<<< HEAD
261 D
262 =======
263 G
264 >>>>>>> $commit (G)
265 EOF
266 git tag new-branch1 &&
267 test_must_fail git rebase -i primary &&
268 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
269 test_cmp expect .git/rebase-merge/patch &&
270 test_cmp expect2 file1 &&
271 test "$(git diff --name-status |
272 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
273 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
274 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
275 '
276
277 test_expect_success 'show conflicted patch' '
278 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
279 grep "show.*REBASE_HEAD" stderr &&
280 # the original stopped-sha1 is abbreviated
281 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
282 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
283 '
284
285 test_expect_success 'abort' '
286 git rebase --abort &&
287 test_cmp_rev new-branch1 HEAD &&
288 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
289 test_path_is_missing .git/rebase-merge
290 '
291
292 test_expect_success 'abort with error when new base cannot be checked out' '
293 git rm --cached file1 &&
294 git commit -m "remove file in base" &&
295 test_must_fail git rebase -i primary > output 2>&1 &&
296 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
297 output &&
298 test_i18ngrep "file1" output &&
299 test_path_is_missing .git/rebase-merge &&
300 rm file1 &&
301 git reset --hard HEAD^
302 '
303
304 test_expect_success 'retain authorship' '
305 echo A > file7 &&
306 git add file7 &&
307 test_tick &&
308 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
309 git tag twerp &&
310 git rebase -i --onto primary HEAD^ &&
311 git show HEAD | grep "^Author: Twerp Snog"
312 '
313
314 test_expect_success 'retain authorship w/ conflicts' '
315 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
316 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
317
318 git reset --hard twerp &&
319 test_commit a conflict a conflict-a &&
320 git reset --hard twerp &&
321
322 GIT_AUTHOR_NAME=AttributeMe &&
323 export GIT_AUTHOR_NAME &&
324 test_commit b conflict b conflict-b &&
325 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
326
327 test_must_fail git rebase -i conflict-a &&
328 echo resolved >conflict &&
329 git add conflict &&
330 git rebase --continue &&
331 test_cmp_rev conflict-a^0 HEAD^ &&
332 git show >out &&
333 grep AttributeMe out
334 '
335
336 test_expect_success 'squash' '
337 git reset --hard twerp &&
338 echo B > file7 &&
339 test_tick &&
340 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
341 echo "******************************" &&
342 (
343 set_fake_editor &&
344 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
345 git rebase -i --onto primary HEAD~2
346 ) &&
347 test B = $(cat file7) &&
348 test_cmp_rev HEAD^ primary
349 '
350
351 test_expect_success 'retain authorship when squashing' '
352 git show HEAD | grep "^Author: Twerp Snog"
353 '
354
355 test_expect_success REBASE_P '-p handles "no changes" gracefully' '
356 HEAD=$(git rev-parse HEAD) &&
357 git rebase -i -p HEAD^ &&
358 git update-index --refresh &&
359 git diff-files --quiet &&
360 git diff-index --quiet --cached HEAD -- &&
361 test $HEAD = $(git rev-parse HEAD)
362 '
363
364 test_expect_failure REBASE_P 'exchange two commits with -p' '
365 git checkout H &&
366 (
367 set_fake_editor &&
368 FAKE_LINES="2 1" git rebase -i -p HEAD~2
369 ) &&
370 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
371 test G = $(git cat-file commit HEAD | sed -ne \$p)
372 '
373
374 test_expect_success REBASE_P 'preserve merges with -p' '
375 git checkout -b to-be-preserved primary^ &&
376 : > unrelated-file &&
377 git add unrelated-file &&
378 test_tick &&
379 git commit -m "unrelated" &&
380 git checkout -b another-branch primary &&
381 echo B > file1 &&
382 test_tick &&
383 git commit -m J file1 &&
384 test_tick &&
385 git merge to-be-preserved &&
386 echo C > file1 &&
387 test_tick &&
388 git commit -m K file1 &&
389 echo D > file1 &&
390 test_tick &&
391 git commit -m L1 file1 &&
392 git checkout HEAD^ &&
393 echo 1 > unrelated-file &&
394 test_tick &&
395 git commit -m L2 unrelated-file &&
396 test_tick &&
397 git merge another-branch &&
398 echo E > file1 &&
399 test_tick &&
400 git commit -m M file1 &&
401 git checkout -b to-be-rebased &&
402 test_tick &&
403 git rebase -i -p --onto branch1 primary &&
404 git update-index --refresh &&
405 git diff-files --quiet &&
406 git diff-index --quiet --cached HEAD -- &&
407 test_cmp_rev HEAD~6 branch1 &&
408 test_cmp_rev HEAD~4^2 to-be-preserved &&
409 test_cmp_rev HEAD^^2^ HEAD^^^ &&
410 test $(git show HEAD~5:file1) = B &&
411 test $(git show HEAD~3:file1) = C &&
412 test $(git show HEAD:file1) = E &&
413 test $(git show HEAD:unrelated-file) = 1
414 '
415
416 test_expect_success REBASE_P 'edit ancestor with -p' '
417 (
418 set_fake_editor &&
419 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3
420 ) &&
421 echo 2 > unrelated-file &&
422 test_tick &&
423 git commit -m L2-modified --amend unrelated-file &&
424 git rebase --continue &&
425 git update-index --refresh &&
426 git diff-files --quiet &&
427 git diff-index --quiet --cached HEAD -- &&
428 test $(git show HEAD:unrelated-file) = 2
429 '
430
431 test_expect_success '--continue tries to commit' '
432 git reset --hard D &&
433 test_tick &&
434 (
435 set_fake_editor &&
436 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
437 echo resolved > file1 &&
438 git add file1 &&
439 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
440 ) &&
441 test_cmp_rev HEAD^ new-branch1 &&
442 git show HEAD | grep chouette
443 '
444
445 test_expect_success 'verbose flag is heeded, even after --continue' '
446 git reset --hard primary@{1} &&
447 test_tick &&
448 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
449 echo resolved > file1 &&
450 git add file1 &&
451 git rebase --continue > output &&
452 grep "^ file1 | 2 +-$" output
453 '
454
455 test_expect_success 'multi-squash only fires up editor once' '
456 base=$(git rev-parse HEAD~4) &&
457 (
458 set_fake_editor &&
459 FAKE_COMMIT_AMEND="ONCE" \
460 FAKE_LINES="1 squash 2 squash 3 squash 4" \
461 EXPECT_HEADER_COUNT=4 \
462 git rebase -i $base
463 ) &&
464 test $base = $(git rev-parse HEAD^) &&
465 test 1 = $(git show | grep ONCE | wc -l)
466 '
467
468 test_expect_success 'multi-fixup does not fire up editor' '
469 git checkout -b multi-fixup E &&
470 base=$(git rev-parse HEAD~4) &&
471 (
472 set_fake_editor &&
473 FAKE_COMMIT_AMEND="NEVER" \
474 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
475 git rebase -i $base
476 ) &&
477 test $base = $(git rev-parse HEAD^) &&
478 test 0 = $(git show | grep NEVER | wc -l) &&
479 git checkout @{-1} &&
480 git branch -D multi-fixup
481 '
482
483 test_expect_success 'commit message used after conflict' '
484 git checkout -b conflict-fixup conflict-branch &&
485 base=$(git rev-parse HEAD~4) &&
486 (
487 set_fake_editor &&
488 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
489 git rebase -i $base &&
490 echo three > conflict &&
491 git add conflict &&
492 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
493 git rebase --continue
494 ) &&
495 test $base = $(git rev-parse HEAD^) &&
496 test 1 = $(git show | grep ONCE | wc -l) &&
497 git checkout @{-1} &&
498 git branch -D conflict-fixup
499 '
500
501 test_expect_success 'commit message retained after conflict' '
502 git checkout -b conflict-squash conflict-branch &&
503 base=$(git rev-parse HEAD~4) &&
504 (
505 set_fake_editor &&
506 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
507 git rebase -i $base &&
508 echo three > conflict &&
509 git add conflict &&
510 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
511 git rebase --continue
512 ) &&
513 test $base = $(git rev-parse HEAD^) &&
514 test 2 = $(git show | grep TWICE | wc -l) &&
515 git checkout @{-1} &&
516 git branch -D conflict-squash
517 '
518
519 test_expect_success 'squash and fixup generate correct log messages' '
520 cat >expect-squash-fixup <<-\EOF &&
521 B
522
523 D
524
525 ONCE
526 EOF
527 git checkout -b squash-fixup E &&
528 base=$(git rev-parse HEAD~4) &&
529 (
530 set_fake_editor &&
531 FAKE_COMMIT_AMEND="ONCE" \
532 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
533 EXPECT_HEADER_COUNT=4 \
534 git rebase -i $base
535 ) &&
536 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
537 test_cmp expect-squash-fixup actual-squash-fixup &&
538 git cat-file commit HEAD@{2} |
539 grep "^# This is a combination of 3 commits\." &&
540 git cat-file commit HEAD@{3} |
541 grep "^# This is a combination of 2 commits\." &&
542 git checkout @{-1} &&
543 git branch -D squash-fixup
544 '
545
546 test_expect_success 'squash ignores comments' '
547 git checkout -b skip-comments E &&
548 base=$(git rev-parse HEAD~4) &&
549 (
550 set_fake_editor &&
551 FAKE_COMMIT_AMEND="ONCE" \
552 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
553 EXPECT_HEADER_COUNT=4 \
554 git rebase -i $base
555 ) &&
556 test $base = $(git rev-parse HEAD^) &&
557 test 1 = $(git show | grep ONCE | wc -l) &&
558 git checkout @{-1} &&
559 git branch -D skip-comments
560 '
561
562 test_expect_success 'squash ignores blank lines' '
563 git checkout -b skip-blank-lines E &&
564 base=$(git rev-parse HEAD~4) &&
565 (
566 set_fake_editor &&
567 FAKE_COMMIT_AMEND="ONCE" \
568 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
569 EXPECT_HEADER_COUNT=4 \
570 git rebase -i $base
571 ) &&
572 test $base = $(git rev-parse HEAD^) &&
573 test 1 = $(git show | grep ONCE | wc -l) &&
574 git checkout @{-1} &&
575 git branch -D skip-blank-lines
576 '
577
578 test_expect_success 'squash works as expected' '
579 git checkout -b squash-works no-conflict-branch &&
580 one=$(git rev-parse HEAD~3) &&
581 (
582 set_fake_editor &&
583 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
584 ) &&
585 test $one = $(git rev-parse HEAD~2)
586 '
587
588 test_expect_success 'interrupted squash works as expected' '
589 git checkout -b interrupted-squash conflict-branch &&
590 one=$(git rev-parse HEAD~3) &&
591 (
592 set_fake_editor &&
593 test_must_fail env FAKE_LINES="1 squash 3 2" \
594 git rebase -i HEAD~3
595 ) &&
596 test_write_lines one two four > conflict &&
597 git add conflict &&
598 test_must_fail git rebase --continue &&
599 echo resolved > conflict &&
600 git add conflict &&
601 git rebase --continue &&
602 test $one = $(git rev-parse HEAD~2)
603 '
604
605 test_expect_success 'interrupted squash works as expected (case 2)' '
606 git checkout -b interrupted-squash2 conflict-branch &&
607 one=$(git rev-parse HEAD~3) &&
608 (
609 set_fake_editor &&
610 test_must_fail env FAKE_LINES="3 squash 1 2" \
611 git rebase -i HEAD~3
612 ) &&
613 test_write_lines one four > conflict &&
614 git add conflict &&
615 test_must_fail git rebase --continue &&
616 test_write_lines one two four > conflict &&
617 git add conflict &&
618 test_must_fail git rebase --continue &&
619 echo resolved > conflict &&
620 git add conflict &&
621 git rebase --continue &&
622 test $one = $(git rev-parse HEAD~2)
623 '
624
625 test_expect_success '--continue tries to commit, even for "edit"' '
626 echo unrelated > file7 &&
627 git add file7 &&
628 test_tick &&
629 git commit -m "unrelated change" &&
630 parent=$(git rev-parse HEAD^) &&
631 test_tick &&
632 (
633 set_fake_editor &&
634 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
635 echo edited > file7 &&
636 git add file7 &&
637 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
638 ) &&
639 test edited = $(git show HEAD:file7) &&
640 git show HEAD | grep chouette &&
641 test $parent = $(git rev-parse HEAD^)
642 '
643
644 test_expect_success 'aborted --continue does not squash commits after "edit"' '
645 old=$(git rev-parse HEAD) &&
646 test_tick &&
647 (
648 set_fake_editor &&
649 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
650 echo "edited again" > file7 &&
651 git add file7 &&
652 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
653 ) &&
654 test $old = $(git rev-parse HEAD) &&
655 git rebase --abort
656 '
657
658 test_expect_success 'auto-amend only edited commits after "edit"' '
659 test_tick &&
660 (
661 set_fake_editor &&
662 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
663 echo "edited again" > file7 &&
664 git add file7 &&
665 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
666 echo "and again" > file7 &&
667 git add file7 &&
668 test_tick &&
669 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
670 git rebase --continue
671 ) &&
672 git rebase --abort
673 '
674
675 test_expect_success 'clean error after failed "exec"' '
676 test_tick &&
677 test_when_finished "git rebase --abort || :" &&
678 (
679 set_fake_editor &&
680 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
681 ) &&
682 echo "edited again" > file7 &&
683 git add file7 &&
684 test_must_fail git rebase --continue 2>error &&
685 test_i18ngrep "you have staged changes in your working tree" error
686 '
687
688 test_expect_success 'rebase a detached HEAD' '
689 grandparent=$(git rev-parse HEAD~2) &&
690 git checkout $(git rev-parse HEAD) &&
691 test_tick &&
692 (
693 set_fake_editor &&
694 FAKE_LINES="2 1" git rebase -i HEAD~2
695 ) &&
696 test $grandparent = $(git rev-parse HEAD~2)
697 '
698
699 test_expect_success 'rebase a commit violating pre-commit' '
700
701 mkdir -p .git/hooks &&
702 write_script .git/hooks/pre-commit <<-\EOF &&
703 test -z "$(git diff --cached --check)"
704 EOF
705 echo "monde! " >> file1 &&
706 test_tick &&
707 test_must_fail git commit -m doesnt-verify file1 &&
708 git commit -m doesnt-verify --no-verify file1 &&
709 test_tick &&
710 (
711 set_fake_editor &&
712 FAKE_LINES=2 git rebase -i HEAD~2
713 )
714 '
715
716 test_expect_success 'rebase with a file named HEAD in worktree' '
717
718 rm -fr .git/hooks &&
719 git reset --hard &&
720 git checkout -b branch3 A &&
721
722 (
723 GIT_AUTHOR_NAME="Squashed Away" &&
724 export GIT_AUTHOR_NAME &&
725 >HEAD &&
726 git add HEAD &&
727 git commit -m "Add head" &&
728 >BODY &&
729 git add BODY &&
730 git commit -m "Add body"
731 ) &&
732
733 (
734 set_fake_editor &&
735 FAKE_LINES="1 squash 2" git rebase -i @{-1}
736 ) &&
737 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
738
739 '
740
741 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
742
743 git checkout -b branch4 HEAD &&
744 GIT_EDITOR=: git commit --amend \
745 --author="Somebody else <somebody@else.com>" &&
746 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
747 git rebase -i branch3 &&
748 test_cmp_rev branch3 branch4
749
750 '
751
752 test_expect_success 'submodule rebase setup' '
753 git checkout A &&
754 mkdir sub &&
755 (
756 cd sub && git init && >elif &&
757 git add elif && git commit -m "submodule initial"
758 ) &&
759 echo 1 >file1 &&
760 git add file1 sub &&
761 test_tick &&
762 git commit -m "One" &&
763 echo 2 >file1 &&
764 test_tick &&
765 git commit -a -m "Two" &&
766 (
767 cd sub && echo 3 >elif &&
768 git commit -a -m "submodule second"
769 ) &&
770 test_tick &&
771 git commit -a -m "Three changes submodule"
772 '
773
774 test_expect_success 'submodule rebase -i' '
775 (
776 set_fake_editor &&
777 FAKE_LINES="1 squash 2 3" git rebase -i A
778 )
779 '
780
781 test_expect_success 'submodule conflict setup' '
782 git tag submodule-base &&
783 git checkout HEAD^ &&
784 (
785 cd sub && git checkout HEAD^ && echo 4 >elif &&
786 git add elif && git commit -m "submodule conflict"
787 ) &&
788 git add sub &&
789 test_tick &&
790 git commit -m "Conflict in submodule" &&
791 git tag submodule-topic
792 '
793
794 test_expect_success 'rebase -i continue with only submodule staged' '
795 test_must_fail git rebase -i submodule-base &&
796 git add sub &&
797 git rebase --continue &&
798 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
799 '
800
801 test_expect_success 'rebase -i continue with unstaged submodule' '
802 git checkout submodule-topic &&
803 git reset --hard &&
804 test_must_fail git rebase -i submodule-base &&
805 git reset &&
806 git rebase --continue &&
807 test_cmp_rev submodule-base HEAD
808 '
809
810 test_expect_success 'avoid unnecessary reset' '
811 git checkout primary &&
812 git reset --hard &&
813 test-tool chmtime =123456789 file3 &&
814 git update-index --refresh &&
815 HEAD=$(git rev-parse HEAD) &&
816 git rebase -i HEAD~4 &&
817 test $HEAD = $(git rev-parse HEAD) &&
818 MTIME=$(test-tool chmtime --get file3) &&
819 test 123456789 = $MTIME
820 '
821
822 test_expect_success 'reword' '
823 git checkout -b reword-branch primary &&
824 (
825 set_fake_editor &&
826 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
827 git rebase -i A &&
828 git show HEAD | grep "E changed" &&
829 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
830 test_cmp_rev primary^ HEAD^ &&
831 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
832 git rebase -i A &&
833 git show HEAD^ | grep "D changed" &&
834 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
835 git rebase -i A &&
836 git show HEAD~3 | grep "B changed" &&
837 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
838 git rebase -i A
839 ) &&
840 git show HEAD~2 | grep "C changed"
841 '
842
843 test_expect_success 'no uncommited changes when rewording the todo list is reloaded' '
844 git checkout E &&
845 test_when_finished "git checkout @{-1}" &&
846 (
847 set_fake_editor &&
848 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
849 export GIT_SEQUENCE_EDITOR &&
850 set_reword_editor &&
851 FAKE_LINES="reword 1 reword 2" git rebase -i C
852 ) &&
853 check_reworded_commits D E
854 '
855
856 test_expect_success 'rebase -i can copy notes' '
857 git config notes.rewrite.rebase true &&
858 git config notes.rewriteRef "refs/notes/*" &&
859 test_commit n1 &&
860 test_commit n2 &&
861 test_commit n3 &&
862 git notes add -m"a note" n3 &&
863 git rebase -i --onto n1 n2 &&
864 test "a note" = "$(git notes show HEAD)"
865 '
866
867 test_expect_success 'rebase -i can copy notes over a fixup' '
868 cat >expect <<-\EOF &&
869 an earlier note
870
871 a note
872 EOF
873 git reset --hard n3 &&
874 git notes add -m"an earlier note" n2 &&
875 (
876 set_fake_editor &&
877 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
878 git rebase -i n1
879 ) &&
880 git notes show > output &&
881 test_cmp expect output
882 '
883
884 test_expect_success 'rebase while detaching HEAD' '
885 git symbolic-ref HEAD &&
886 grandparent=$(git rev-parse HEAD~2) &&
887 test_tick &&
888 (
889 set_fake_editor &&
890 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
891 ) &&
892 test $grandparent = $(git rev-parse HEAD~2) &&
893 test_must_fail git symbolic-ref HEAD
894 '
895
896 test_tick # Ensure that the rebased commits get a different timestamp.
897 test_expect_success 'always cherry-pick with --no-ff' '
898 git checkout no-ff-branch &&
899 git tag original-no-ff-branch &&
900 git rebase -i --no-ff A &&
901 for p in 0 1 2
902 do
903 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
904 git diff HEAD~$p original-no-ff-branch~$p > out &&
905 test_must_be_empty out
906 done &&
907 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
908 git diff HEAD~3 original-no-ff-branch~3 > out &&
909 test_must_be_empty out
910 '
911
912 test_expect_success 'set up commits with funny messages' '
913 git checkout -b funny A &&
914 echo >>file1 &&
915 test_tick &&
916 git commit -a -m "end with slash\\" &&
917 echo >>file1 &&
918 test_tick &&
919 git commit -a -m "something (\000) that looks like octal" &&
920 echo >>file1 &&
921 test_tick &&
922 git commit -a -m "something (\n) that looks like a newline" &&
923 echo >>file1 &&
924 test_tick &&
925 git commit -a -m "another commit"
926 '
927
928 test_expect_success 'rebase-i history with funny messages' '
929 git rev-list A..funny >expect &&
930 test_tick &&
931 (
932 set_fake_editor &&
933 FAKE_LINES="1 2 3 4" git rebase -i A
934 ) &&
935 git rev-list A.. >actual &&
936 test_cmp expect actual
937 '
938
939 test_expect_success 'prepare for rebase -i --exec' '
940 git checkout primary &&
941 git checkout -b execute &&
942 test_commit one_exec main.txt one_exec &&
943 test_commit two_exec main.txt two_exec &&
944 test_commit three_exec main.txt three_exec
945 '
946
947 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
948 (
949 set_fake_editor &&
950 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
951 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
952 export FAKE_LINES &&
953 git rebase -i HEAD~2 >expect
954 ) &&
955 sed -e "1,9d" expect >expected &&
956 test_cmp expected actual
957 '
958
959 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
960 git reset --hard execute &&
961 (
962 set_fake_editor &&
963 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
964 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
965 export FAKE_LINES &&
966 git rebase -i HEAD~2 >expect
967 ) &&
968 sed -e "1,9d" expect >expected &&
969 test_cmp expected actual
970 '
971
972 test_expect_success 'running "git rebase -ix git show HEAD"' '
973 git reset --hard execute &&
974 (
975 set_fake_editor &&
976 git rebase -ix "git show HEAD" HEAD~2 >actual &&
977 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
978 export FAKE_LINES &&
979 git rebase -i HEAD~2 >expect
980 ) &&
981 sed -e "1,9d" expect >expected &&
982 test_cmp expected actual
983 '
984
985
986 test_expect_success 'rebase -ix with several <CMD>' '
987 git reset --hard execute &&
988 (
989 set_fake_editor &&
990 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
991 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
992 export FAKE_LINES &&
993 git rebase -i HEAD~2 >expect
994 ) &&
995 sed -e "1,9d" expect >expected &&
996 test_cmp expected actual
997 '
998
999 test_expect_success 'rebase -ix with several instances of --exec' '
1000 git reset --hard execute &&
1001 (
1002 set_fake_editor &&
1003 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
1004 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
1005 exec_git_show_HEAD exec_pwd" &&
1006 export FAKE_LINES &&
1007 git rebase -i HEAD~2 >expect
1008 ) &&
1009 sed -e "1,11d" expect >expected &&
1010 test_cmp expected actual
1011 '
1012
1013 test_expect_success 'rebase -ix with --autosquash' '
1014 git reset --hard execute &&
1015 git checkout -b autosquash &&
1016 echo second >second.txt &&
1017 git add second.txt &&
1018 git commit -m "fixup! two_exec" &&
1019 echo bis >bis.txt &&
1020 git add bis.txt &&
1021 git commit -m "fixup! two_exec" &&
1022 git checkout -b autosquash_actual &&
1023 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
1024 git checkout autosquash &&
1025 (
1026 set_fake_editor &&
1027 git checkout -b autosquash_expected &&
1028 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
1029 export FAKE_LINES &&
1030 git rebase -i HEAD~4 >expect
1031 ) &&
1032 sed -e "1,13d" expect >expected &&
1033 test_cmp expected actual
1034 '
1035
1036 test_expect_success 'rebase --exec works without -i ' '
1037 git reset --hard execute &&
1038 rm -rf exec_output &&
1039 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
1040 test_i18ngrep "Successfully rebased and updated" actual &&
1041 test_line_count = 2 exec_output &&
1042 test_path_is_missing invoked_editor
1043 '
1044
1045 test_expect_success 'rebase -i --exec without <CMD>' '
1046 git reset --hard execute &&
1047 test_must_fail git rebase -i --exec 2>actual &&
1048 test_i18ngrep "requires a value" actual &&
1049 git checkout primary
1050 '
1051
1052 test_expect_success 'rebase -i --root re-order and drop commits' '
1053 git checkout E &&
1054 (
1055 set_fake_editor &&
1056 FAKE_LINES="3 1 2 5" git rebase -i --root
1057 ) &&
1058 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1059 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1060 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
1061 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
1062 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
1063 '
1064
1065 test_expect_success 'rebase -i --root retain root commit author and message' '
1066 git checkout A &&
1067 echo B >file7 &&
1068 git add file7 &&
1069 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1070 (
1071 set_fake_editor &&
1072 FAKE_LINES="2" git rebase -i --root
1073 ) &&
1074 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1075 git cat-file commit HEAD | grep -q "^different author$"
1076 '
1077
1078 test_expect_success 'rebase -i --root temporary sentinel commit' '
1079 git checkout B &&
1080 (
1081 set_fake_editor &&
1082 test_must_fail env FAKE_LINES="2" git rebase -i --root
1083 ) &&
1084 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1085 git rebase --abort
1086 '
1087
1088 test_expect_success 'rebase -i --root fixup root commit' '
1089 git checkout B &&
1090 (
1091 set_fake_editor &&
1092 FAKE_LINES="1 fixup 2" git rebase -i --root
1093 ) &&
1094 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1095 test B = $(git show HEAD:file1) &&
1096 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1097 '
1098
1099 test_expect_success 'rebase -i --root reword original root commit' '
1100 test_when_finished "test_might_fail git rebase --abort" &&
1101 git checkout -b reword-original-root-branch primary &&
1102 (
1103 set_fake_editor &&
1104 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1105 git rebase -i --root
1106 ) &&
1107 git show HEAD^ | grep "A changed" &&
1108 test -z "$(git show -s --format=%p HEAD^)"
1109 '
1110
1111 test_expect_success 'rebase -i --root reword new root commit' '
1112 test_when_finished "test_might_fail git rebase --abort" &&
1113 git checkout -b reword-now-root-branch primary &&
1114 (
1115 set_fake_editor &&
1116 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1117 git rebase -i --root
1118 ) &&
1119 git show HEAD^ | grep "C changed" &&
1120 test -z "$(git show -s --format=%p HEAD^)"
1121 '
1122
1123 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1124 test_when_finished "reset_rebase" &&
1125 git checkout -b failing-root-pick A &&
1126 echo x >file2 &&
1127 git rm file1 &&
1128 git commit -m "remove file 1 add file 2" &&
1129 echo z >file1 &&
1130 (
1131 set_fake_editor &&
1132 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1133 ) &&
1134 rm file1 &&
1135 git rebase --continue &&
1136 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1137 test "$(git rev-list --count HEAD)" = 2
1138 '
1139
1140 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1141 test_when_finished "reset_rebase" &&
1142 echo z>file1 &&
1143 (
1144 set_fake_editor &&
1145 test_must_fail env FAKE_LINES="reword 1 2" \
1146 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1147 rm file1 &&
1148 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1149 ) &&
1150 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1151 test "$(git rev-list --count HEAD)" = 2
1152 '
1153
1154 test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
1155 git checkout reword-original-root-branch &&
1156 git reset --hard &&
1157 git checkout conflict-branch &&
1158 (
1159 set_fake_editor &&
1160 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1161 test_must_fail git rebase --edit-todo
1162 ) &&
1163 git rebase --abort
1164 '
1165
1166 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1167 git reset --hard &&
1168 git checkout no-conflict-branch^0 &&
1169 (
1170 set_fake_editor &&
1171 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1172 FAKE_LINES="2 1" git rebase --edit-todo &&
1173 git rebase --continue
1174 ) &&
1175 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1176 test L = $(git cat-file commit HEAD | sed -ne \$p)
1177 '
1178
1179 test_expect_success 'rebase -i produces readable reflog' '
1180 git reset --hard &&
1181 git branch -f branch-reflog-test H &&
1182 git rebase -i --onto I F branch-reflog-test &&
1183 cat >expect <<-\EOF &&
1184 rebase (finish): returning to refs/heads/branch-reflog-test
1185 rebase (pick): H
1186 rebase (pick): G
1187 rebase (start): checkout I
1188 EOF
1189 git reflog -n4 HEAD |
1190 sed "s/[^:]*: //" >actual &&
1191 test_cmp expect actual
1192 '
1193
1194 test_expect_success 'rebase -i respects core.commentchar' '
1195 git reset --hard &&
1196 git checkout E^0 &&
1197 test_config core.commentchar "\\" &&
1198 write_script remove-all-but-first.sh <<-\EOF &&
1199 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1200 mv "$1.tmp" "$1"
1201 EOF
1202 (
1203 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1204 git rebase -i B
1205 ) &&
1206 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1207 '
1208
1209 test_expect_success 'rebase -i respects core.commentchar=auto' '
1210 test_config core.commentchar auto &&
1211 write_script copy-edit-script.sh <<-\EOF &&
1212 cp "$1" edit-script
1213 EOF
1214 test_when_finished "git rebase --abort || :" &&
1215 (
1216 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1217 git rebase -i HEAD^
1218 ) &&
1219 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1220 '
1221
1222 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1223 test_when_finished "git branch -D torebase" &&
1224 git checkout -b torebase branch1 &&
1225 upstream=$(git rev-parse ":/J") &&
1226 onto=$(git rev-parse ":/A") &&
1227 git rebase --onto $onto $upstream &&
1228 git reset --hard branch1 &&
1229 git rebase --onto ":/A" ":/J" &&
1230 git checkout branch1
1231 '
1232
1233 test_expect_success 'rebase -i with --strategy and -X' '
1234 git checkout -b conflict-merge-use-theirs conflict-branch &&
1235 git reset --hard HEAD^ &&
1236 echo five >conflict &&
1237 echo Z >file1 &&
1238 git commit -a -m "one file conflict" &&
1239 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1240 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1241 test $(cat file1) = Z
1242 '
1243
1244 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1245 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1246 git reset --hard HEAD^ &&
1247 >breakpoint &&
1248 git add breakpoint &&
1249 git commit -m "breakpoint for interactive mode" &&
1250 echo five >conflict &&
1251 echo Z >file1 &&
1252 git commit -a -m "one file conflict" &&
1253 (
1254 set_fake_editor &&
1255 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1256 -Xours conflict-branch
1257 ) &&
1258 git rebase --continue &&
1259 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1260 test $(cat file1) = Z
1261 '
1262
1263 test_expect_success 'rebase -i error on commits with \ in message' '
1264 current_head=$(git rev-parse HEAD) &&
1265 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1266 test_commit TO-REMOVE will-conflict old-content &&
1267 test_commit "\temp" will-conflict new-content dummy &&
1268 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1269 test_expect_code 1 grep " emp" error
1270 '
1271
1272 test_expect_success 'short commit ID setup' '
1273 test_when_finished "git checkout primary" &&
1274 git checkout --orphan collide &&
1275 git rm -rf . &&
1276 (
1277 unset test_tick &&
1278 test_commit collide1 collide &&
1279 test_commit --notick collide2 collide &&
1280 test_commit --notick collide3 collide
1281 )
1282 '
1283
1284 if test -n "$GIT_TEST_FIND_COLLIDER"
1285 then
1286 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1287 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1288 blob="$(git rev-parse collide2:collide)"
1289 from="$(git rev-parse collide1^0)"
1290 repl="commit refs/heads/collider-&\\n"
1291 repl="${repl}author $author\\ncommitter $committer\\n"
1292 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1293 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1294 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1295 git fast-import <script &&
1296 git pack-refs &&
1297 git for-each-ref >refs &&
1298 grep "^$(test_oid t3404_collision)" <refs >matches &&
1299 cat matches &&
1300 test_line_count -gt 2 matches || {
1301 echo "Could not find a collider" >&2
1302 exit 1
1303 }
1304 fi
1305
1306 test_expect_success 'short commit ID collide' '
1307 test_oid_cache <<-EOF &&
1308 # collision-related constants
1309 t3404_collision sha1:6bcd
1310 t3404_collision sha256:0161
1311 t3404_collider sha1:ac4f2ee
1312 t3404_collider sha256:16697
1313 EOF
1314 test_when_finished "reset_rebase && git checkout primary" &&
1315 git checkout collide &&
1316 colliding_id=$(test_oid t3404_collision) &&
1317 hexsz=$(test_oid hexsz) &&
1318 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1319 test_config core.abbrev 4 &&
1320 (
1321 unset test_tick &&
1322 test_tick &&
1323 set_fake_editor &&
1324 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1325 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1326 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1327 grep "^pick $colliding_id " \
1328 .git/rebase-merge/git-rebase-todo.tmp &&
1329 grep "^pick [0-9a-f]\{$hexsz\}" \
1330 .git/rebase-merge/git-rebase-todo &&
1331 grep "^pick [0-9a-f]\{$hexsz\}" \
1332 .git/rebase-merge/git-rebase-todo.backup &&
1333 git rebase --continue
1334 ) &&
1335 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1336 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1337 test "$collide2" = "$collide3"
1338 '
1339
1340 test_expect_success 'respect core.abbrev' '
1341 git config core.abbrev 12 &&
1342 (
1343 set_cat_todo_editor &&
1344 test_must_fail git rebase -i HEAD~4 >todo-list
1345 ) &&
1346 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1347 '
1348
1349 test_expect_success 'todo count' '
1350 write_script dump-raw.sh <<-\EOF &&
1351 cat "$1"
1352 EOF
1353 (
1354 test_set_editor "$(pwd)/dump-raw.sh" &&
1355 git rebase -i HEAD~4 >actual
1356 ) &&
1357 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1358 '
1359
1360 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1361 git checkout --force branch2 &&
1362 git clean -f &&
1363 (
1364 set_fake_editor &&
1365 FAKE_LINES="edit 1 2" git rebase -i A
1366 ) &&
1367 test_cmp_rev HEAD F &&
1368 test_path_is_missing file6 &&
1369 >file6 &&
1370 test_must_fail git rebase --continue &&
1371 test_cmp_rev HEAD F &&
1372 rm file6 &&
1373 git rebase --continue &&
1374 test_cmp_rev HEAD I
1375 '
1376
1377 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1378 git checkout --force branch2 &&
1379 git clean -f &&
1380 git tag original-branch2 &&
1381 (
1382 set_fake_editor &&
1383 FAKE_LINES="edit 1 squash 2" git rebase -i A
1384 ) &&
1385 test_cmp_rev HEAD F &&
1386 test_path_is_missing file6 &&
1387 >file6 &&
1388 test_must_fail git rebase --continue &&
1389 test_cmp_rev HEAD F &&
1390 rm file6 &&
1391 git rebase --continue &&
1392 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1393 git reset --hard original-branch2
1394 '
1395
1396 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1397 git checkout --force branch2 &&
1398 git clean -f &&
1399 (
1400 set_fake_editor &&
1401 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1402 ) &&
1403 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1404 test_path_is_missing file6 &&
1405 >file6 &&
1406 test_must_fail git rebase --continue &&
1407 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1408 rm file6 &&
1409 git rebase --continue &&
1410 test $(git cat-file commit HEAD | sed -ne \$p) = I
1411 '
1412
1413 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1414 git checkout -b commit-to-skip &&
1415 for double in X 3 1
1416 do
1417 test_seq 5 | sed "s/$double/&&/" >seq &&
1418 git add seq &&
1419 test_tick &&
1420 git commit -m seq-$double
1421 done &&
1422 git tag seq-onto &&
1423 git reset --hard HEAD~2 &&
1424 git cherry-pick seq-onto &&
1425 (
1426 set_fake_editor &&
1427 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1428 ) &&
1429 test -d .git/rebase-merge &&
1430 git rebase --continue &&
1431 git diff --exit-code seq-onto &&
1432 test ! -d .git/rebase-merge &&
1433 test ! -f .git/CHERRY_PICK_HEAD
1434 '
1435
1436 rebase_setup_and_clean () {
1437 test_when_finished "
1438 git checkout primary &&
1439 test_might_fail git branch -D $1 &&
1440 test_might_fail git rebase --abort
1441 " &&
1442 git checkout -b $1 ${2:-primary}
1443 }
1444
1445 test_expect_success 'drop' '
1446 rebase_setup_and_clean drop-test &&
1447 (
1448 set_fake_editor &&
1449 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1450 ) &&
1451 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1452 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1453 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1454 '
1455
1456 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1457 test_config rebase.missingCommitsCheck ignore &&
1458 rebase_setup_and_clean missing-commit &&
1459 (
1460 set_fake_editor &&
1461 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1462 ) &&
1463 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1464 test_i18ngrep \
1465 "Successfully rebased and updated refs/heads/missing-commit" \
1466 actual
1467 '
1468
1469 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1470 cat >expect <<-EOF &&
1471 Warning: some commits may have been dropped accidentally.
1472 Dropped commits (newer to older):
1473 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1474 To avoid this message, use "drop" to explicitly remove a commit.
1475 EOF
1476 test_config rebase.missingCommitsCheck warn &&
1477 rebase_setup_and_clean missing-commit &&
1478 (
1479 set_fake_editor &&
1480 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1481 ) &&
1482 head -n4 actual.2 >actual &&
1483 test_cmp expect actual &&
1484 test D = $(git cat-file commit HEAD | sed -ne \$p)
1485 '
1486
1487 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1488 cat >expect <<-EOF &&
1489 Warning: some commits may have been dropped accidentally.
1490 Dropped commits (newer to older):
1491 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1492 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1493 To avoid this message, use "drop" to explicitly remove a commit.
1494
1495 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1496 The possible behaviours are: ignore, warn, error.
1497
1498 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1499 Or you can abort the rebase with '\''git rebase --abort'\''.
1500 EOF
1501 test_config rebase.missingCommitsCheck error &&
1502 rebase_setup_and_clean missing-commit &&
1503 (
1504 set_fake_editor &&
1505 test_must_fail env FAKE_LINES="1 2 4" \
1506 git rebase -i --root 2>actual &&
1507 test_cmp expect actual &&
1508 cp .git/rebase-merge/git-rebase-todo.backup \
1509 .git/rebase-merge/git-rebase-todo &&
1510 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1511 ) &&
1512 git rebase --continue &&
1513 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1514 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1515 '
1516
1517 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1518 test_config rebase.missingCommitsCheck ignore &&
1519 rebase_setup_and_clean missing-commit &&
1520 (
1521 set_fake_editor &&
1522 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1523 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1524 git rebase --continue 2>actual
1525 ) &&
1526 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1527 test_i18ngrep \
1528 "Successfully rebased and updated refs/heads/missing-commit" \
1529 actual
1530 '
1531
1532 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1533 cat >expect <<-EOF &&
1534 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1535 Warning: some commits may have been dropped accidentally.
1536 Dropped commits (newer to older):
1537 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1538 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1539 To avoid this message, use "drop" to explicitly remove a commit.
1540 EOF
1541 head -n4 expect >expect.2 &&
1542 tail -n1 expect >>expect.2 &&
1543 tail -n4 expect.2 >expect.3 &&
1544 test_config rebase.missingCommitsCheck warn &&
1545 rebase_setup_and_clean missing-commit &&
1546 (
1547 set_fake_editor &&
1548 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1549 git rebase -i --root &&
1550 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1551 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1552 head -n6 actual.2 >actual &&
1553 test_cmp expect actual &&
1554 cp orig .git/rebase-merge/git-rebase-todo &&
1555 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1556 head -n4 actual.2 >actual &&
1557 test_cmp expect.3 actual &&
1558 git rebase --continue 2>actual
1559 ) &&
1560 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1561 test_i18ngrep \
1562 "Successfully rebased and updated refs/heads/missing-commit" \
1563 actual
1564 '
1565
1566 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1567 cat >expect <<-EOF &&
1568 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1569 Warning: some commits may have been dropped accidentally.
1570 Dropped commits (newer to older):
1571 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1572 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1573 To avoid this message, use "drop" to explicitly remove a commit.
1574
1575 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1576 The possible behaviours are: ignore, warn, error.
1577
1578 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1579 Or you can abort the rebase with '\''git rebase --abort'\''.
1580 EOF
1581 tail -n11 expect >expect.2 &&
1582 head -n3 expect.2 >expect.3 &&
1583 tail -n7 expect.2 >>expect.3 &&
1584 test_config rebase.missingCommitsCheck error &&
1585 rebase_setup_and_clean missing-commit &&
1586 (
1587 set_fake_editor &&
1588 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1589 git rebase -i --root &&
1590 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1591 test_must_fail env FAKE_LINES="2 3 4" \
1592 git rebase --edit-todo 2>actual &&
1593 test_cmp expect actual &&
1594 test_must_fail git rebase --continue 2>actual &&
1595 test_cmp expect.2 actual &&
1596 test_must_fail git rebase --edit-todo &&
1597 cp orig .git/rebase-merge/git-rebase-todo &&
1598 test_must_fail env FAKE_LINES="1 2 3 4" \
1599 git rebase --edit-todo 2>actual &&
1600 test_cmp expect.3 actual &&
1601 test_must_fail git rebase --continue 2>actual &&
1602 test_cmp expect.3 actual &&
1603 cp orig .git/rebase-merge/git-rebase-todo &&
1604 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1605 git rebase --continue 2>actual
1606 ) &&
1607 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1608 test_i18ngrep \
1609 "Successfully rebased and updated refs/heads/missing-commit" \
1610 actual
1611 '
1612
1613 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1614 test_config rebase.missingCommitsCheck error &&
1615 (
1616 set_fake_editor &&
1617 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1618 ) &&
1619 git rebase --edit-todo &&
1620 test_must_fail git rebase --continue &&
1621 echo x >file1 &&
1622 git add file1 &&
1623 git rebase --continue
1624 '
1625
1626 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1627 test_config rebase.missingCommitsCheck error &&
1628 (
1629 set_fake_editor &&
1630 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1631 cp .git/rebase-merge/git-rebase-todo todo &&
1632 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1633 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1634 git rebase --continue
1635 )
1636 '
1637
1638 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1639 rebase_setup_and_clean abbrevcmd &&
1640 test_commit "first" file1.txt "first line" first &&
1641 test_commit "second" file1.txt "another line" second &&
1642 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1643 test_commit "squash! second" file1.txt "another line here" second_squash &&
1644 cat >expected <<-EOF &&
1645 p $(git rev-list --abbrev-commit -1 first) first
1646 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1647 x git show HEAD
1648 p $(git rev-list --abbrev-commit -1 second) second
1649 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1650 x git show HEAD
1651 EOF
1652 git checkout abbrevcmd &&
1653 test_config rebase.abbreviateCommands true &&
1654 (
1655 set_cat_todo_editor &&
1656 test_must_fail git rebase -i --exec "git show HEAD" \
1657 --autosquash primary >actual
1658 ) &&
1659 test_cmp expected actual
1660 '
1661
1662 test_expect_success 'static check of bad command' '
1663 rebase_setup_and_clean bad-cmd &&
1664 (
1665 set_fake_editor &&
1666 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1667 git rebase -i --root 2>actual &&
1668 test_i18ngrep "badcmd $(git rev-list --oneline -1 primary~1)" \
1669 actual &&
1670 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1671 actual &&
1672 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1673 ) &&
1674 git rebase --continue &&
1675 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1676 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1677 '
1678
1679 test_expect_success 'tabs and spaces are accepted in the todolist' '
1680 rebase_setup_and_clean indented-comment &&
1681 write_script add-indent.sh <<-\EOF &&
1682 (
1683 # Turn single spaces into space/tab mix
1684 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1685 printf "\n\t# comment\n #more\n\t # comment\n"
1686 ) >"$1.new"
1687 mv "$1.new" "$1"
1688 EOF
1689 (
1690 test_set_editor "$(pwd)/add-indent.sh" &&
1691 git rebase -i HEAD^^^
1692 ) &&
1693 test E = $(git cat-file commit HEAD | sed -ne \$p)
1694 '
1695
1696 test_expect_success 'static check of bad SHA-1' '
1697 rebase_setup_and_clean bad-sha &&
1698 (
1699 set_fake_editor &&
1700 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1701 git rebase -i --root 2>actual &&
1702 test_i18ngrep "edit XXXXXXX False commit" actual &&
1703 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1704 actual &&
1705 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1706 ) &&
1707 git rebase --continue &&
1708 test E = $(git cat-file commit HEAD | sed -ne \$p)
1709 '
1710
1711 test_expect_success 'editor saves as CR/LF' '
1712 git checkout -b with-crlf &&
1713 write_script add-crs.sh <<-\EOF &&
1714 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1715 mv -f "$1".new "$1"
1716 EOF
1717 (
1718 test_set_editor "$(pwd)/add-crs.sh" &&
1719 git rebase -i HEAD^
1720 )
1721 '
1722
1723 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1724 test_when_finished "test_might_fail git rebase --abort" &&
1725 (
1726 set_fake_editor &&
1727 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1728 HEAD^ >out 2>err
1729 ) &&
1730 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1731 '
1732
1733 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1734 test_when_finished "test_might_fail git rebase --abort" &&
1735 test_config commit.gpgsign true &&
1736 (
1737 set_fake_editor &&
1738 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1739 HEAD^ >out 2>err
1740 ) &&
1741 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1742 '
1743
1744 test_expect_success 'valid author header after --root swap' '
1745 rebase_setup_and_clean author-header no-conflict-branch &&
1746 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1747 git cat-file commit HEAD | grep ^author >expected &&
1748 (
1749 set_fake_editor &&
1750 FAKE_LINES="5 1" git rebase -i --root
1751 ) &&
1752 git cat-file commit HEAD^ | grep ^author >actual &&
1753 test_cmp expected actual
1754 '
1755
1756 test_expect_success 'valid author header when author contains single quote' '
1757 rebase_setup_and_clean author-header no-conflict-branch &&
1758 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1759 git cat-file commit HEAD | grep ^author >expected &&
1760 (
1761 set_fake_editor &&
1762 FAKE_LINES="2" git rebase -i HEAD~2
1763 ) &&
1764 git cat-file commit HEAD | grep ^author >actual &&
1765 test_cmp expected actual
1766 '
1767
1768 test_expect_success 'post-commit hook is called' '
1769 test_when_finished "rm -f .git/hooks/post-commit" &&
1770 >actual &&
1771 mkdir -p .git/hooks &&
1772 write_script .git/hooks/post-commit <<-\EOS &&
1773 git rev-parse HEAD >>actual
1774 EOS
1775 (
1776 set_fake_editor &&
1777 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1778 echo x>file3 &&
1779 git add file3 &&
1780 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1781 ) &&
1782 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1783 >expect &&
1784 test_cmp expect actual
1785 '
1786
1787 test_expect_success 'correct error message for partial commit after empty pick' '
1788 test_when_finished "git rebase --abort" &&
1789 (
1790 set_fake_editor &&
1791 FAKE_LINES="2 1 1" &&
1792 export FAKE_LINES &&
1793 test_must_fail git rebase -i A D
1794 ) &&
1795 echo x >file1 &&
1796 test_must_fail git commit file1 2>err &&
1797 test_i18ngrep "cannot do a partial commit during a rebase." err
1798 '
1799
1800 test_expect_success 'correct error message for commit --amend after empty pick' '
1801 test_when_finished "git rebase --abort" &&
1802 (
1803 set_fake_editor &&
1804 FAKE_LINES="1 1" &&
1805 export FAKE_LINES &&
1806 test_must_fail git rebase -i A D
1807 ) &&
1808 echo x>file1 &&
1809 test_must_fail git commit -a --amend 2>err &&
1810 test_i18ngrep "middle of a rebase -- cannot amend." err
1811 '
1812
1813 test_expect_success 'todo has correct onto hash' '
1814 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1815 onto=$(git rev-parse --short HEAD~4) &&
1816 test_i18ngrep "^# Rebase ..* onto $onto" actual
1817 '
1818
1819 test_expect_success 'ORIG_HEAD is updated correctly' '
1820 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1821 git checkout -b test-orig-head A &&
1822 git commit --allow-empty -m A1 &&
1823 git commit --allow-empty -m A2 &&
1824 git commit --allow-empty -m A3 &&
1825 git commit --allow-empty -m A4 &&
1826 git rebase primary &&
1827 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1828 '
1829
1830 # This must be the last test in this file
1831 test_expect_success '$EDITOR and friends are unchanged' '
1832 test_editor_unchanged
1833 '
1834
1835 test_done