rebase -i: re-fix short SHA-1 collision
[alt-git.git] / t / t3404-rebase-interactive.sh
blob1cc9f36bc76657ed8154895f269ac46baa347e23
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes E. Schindelin
6 test_description='git rebase interactive
8 This test runs git rebase "interactively", by faking an edit, and verifies
9 that the result still makes sense.
11 Initial setup:
13 one - two - three - four (conflict-branch)
15 A - B - C - D - E (master)
16 | \
17 | F - G - H (branch1)
18 | \
19 |\ I (branch2)
20 | \
21 | J - K - L - M (no-conflict-branch)
23 N - O - P (no-ff-branch)
25 where A, B, D and G all touch file1, and one, two, three, four all
26 touch file "conflict".
28 . ./test-lib.sh
30 . "$TEST_DIRECTORY"/lib-rebase.sh
32 test_expect_success 'setup' '
33 test_commit A file1 &&
34 test_commit B file1 &&
35 test_commit C file2 &&
36 test_commit D file1 &&
37 test_commit E file3 &&
38 git checkout -b branch1 A &&
39 test_commit F file4 &&
40 test_commit G file1 &&
41 test_commit H file5 &&
42 git checkout -b branch2 F &&
43 test_commit I file6 &&
44 git checkout -b conflict-branch A &&
45 test_commit one conflict &&
46 test_commit two conflict &&
47 test_commit three conflict &&
48 test_commit four conflict &&
49 git checkout -b no-conflict-branch A &&
50 test_commit J fileJ &&
51 test_commit K fileK &&
52 test_commit L fileL &&
53 test_commit M fileM &&
54 git checkout -b no-ff-branch A &&
55 test_commit N fileN &&
56 test_commit O fileO &&
57 test_commit P fileP
60 # "exec" commands are run with the user shell by default, but this may
61 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
62 # to create a file. Unsetting SHELL avoids such non-portable behavior
63 # in tests. It must be exported for it to take effect where needed.
64 SHELL=
65 export SHELL
67 test_expect_success 'rebase --keep-empty' '
68 git checkout -b emptybranch master &&
69 git commit --allow-empty -m "empty" &&
70 git rebase --keep-empty -i HEAD~2 &&
71 git log --oneline >actual &&
72 test_line_count = 6 actual
75 test_expect_success 'rebase -i with empty HEAD' '
76 cat >expect <<-\EOF &&
77 error: nothing to do
78 EOF
80 set_fake_editor &&
81 test_must_fail env FAKE_LINES="1 exec_true" \
82 git rebase -i HEAD^ >actual 2>&1
83 ) &&
84 test_i18ncmp expect actual
87 test_expect_success 'rebase -i with the exec command' '
88 git checkout master &&
90 set_fake_editor &&
91 FAKE_LINES="1 exec_>touch-one
92 2 exec_>touch-two exec_false exec_>touch-three
93 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
94 export FAKE_LINES &&
95 test_must_fail git rebase -i A
96 ) &&
97 test_path_is_file touch-one &&
98 test_path_is_file touch-two &&
99 test_path_is_missing touch-three " (should have stopped before)" &&
100 test_cmp_rev C HEAD &&
101 git rebase --continue &&
102 test_path_is_file touch-three &&
103 test_path_is_file "touch-file name with spaces" &&
104 test_path_is_file touch-after-semicolon &&
105 test_cmp_rev master HEAD &&
106 rm -f touch-*
109 test_expect_success 'rebase -i with the exec command runs from tree root' '
110 git checkout master &&
111 mkdir subdir && (cd subdir &&
112 set_fake_editor &&
113 FAKE_LINES="1 exec_>touch-subdir" \
114 git rebase -i HEAD^
115 ) &&
116 test_path_is_file touch-subdir &&
117 rm -fr subdir
120 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
121 test_when_finished "rm -rf subdir" &&
122 test_when_finished "git rebase --abort ||:" &&
123 git checkout master &&
124 mkdir subdir && (cd subdir &&
125 set_fake_editor &&
126 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
127 git rebase -i HEAD^
131 test_expect_success 'rebase -i sets work tree properly' '
132 test_when_finished "rm -rf subdir" &&
133 test_when_finished "test_might_fail git rebase --abort" &&
134 mkdir subdir &&
135 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
136 >actual &&
137 ! grep "/subdir$" actual
140 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
141 git checkout master &&
143 set_fake_editor &&
144 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
145 git rebase -i HEAD^
146 ) &&
147 test_cmp_rev master^ HEAD &&
148 git reset --hard &&
149 git rebase --continue
152 test_expect_success 'rebase -x with empty command fails' '
153 test_when_finished "git rebase --abort ||:" &&
154 test_must_fail env git rebase -x "" @ 2>actual &&
155 test_write_lines "error: empty exec command" >expected &&
156 test_i18ncmp expected actual &&
157 test_must_fail env git rebase -x " " @ 2>actual &&
158 test_i18ncmp expected actual
161 test_expect_success 'rebase -x with newline in command fails' '
162 test_when_finished "git rebase --abort ||:" &&
163 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
164 test_write_lines "error: exec commands cannot contain newlines" \
165 >expected &&
166 test_i18ncmp expected actual
169 test_expect_success 'rebase -i with exec of inexistent command' '
170 git checkout master &&
171 test_when_finished "git rebase --abort" &&
173 set_fake_editor &&
174 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
175 git rebase -i HEAD^ >actual 2>&1
176 ) &&
177 ! grep "Maybe git-rebase is broken" actual
180 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
181 test_when_finished "git rebase --abort ||:" &&
182 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
185 test_expect_success 'no changes are a nop' '
186 git checkout branch2 &&
187 git rebase -i F &&
188 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
189 test $(git rev-parse I) = $(git rev-parse HEAD)
192 test_expect_success 'test the [branch] option' '
193 git checkout -b dead-end &&
194 git rm file6 &&
195 git commit -m "stop here" &&
196 git rebase -i F branch2 &&
197 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
198 test $(git rev-parse I) = $(git rev-parse branch2) &&
199 test $(git rev-parse I) = $(git rev-parse HEAD)
202 test_expect_success 'test --onto <branch>' '
203 git checkout -b test-onto branch2 &&
204 git rebase -i --onto branch1 F &&
205 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
206 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
207 test $(git rev-parse I) = $(git rev-parse branch2)
210 test_expect_success 'rebase on top of a non-conflicting commit' '
211 git checkout branch1 &&
212 git tag original-branch1 &&
213 git rebase -i branch2 &&
214 test file6 = $(git diff --name-only original-branch1) &&
215 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
216 test $(git rev-parse I) = $(git rev-parse branch2) &&
217 test $(git rev-parse I) = $(git rev-parse HEAD~2)
220 test_expect_success 'reflog for the branch shows state before rebase' '
221 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
224 test_expect_success 'reflog for the branch shows correct finish message' '
225 printf "rebase -i (finish): refs/heads/branch1 onto %s\n" \
226 "$(git rev-parse branch2)" >expected &&
227 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
228 test_cmp expected actual
231 test_expect_success 'exchange two commits' '
233 set_fake_editor &&
234 FAKE_LINES="2 1" git rebase -i HEAD~2
235 ) &&
236 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
237 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
238 blob1=$(git rev-parse --short HEAD^:file1) &&
239 blob2=$(git rev-parse --short HEAD:file1) &&
240 commit=$(git rev-parse --short HEAD)
243 test_expect_success 'stop on conflicting pick' '
244 cat >expect <<-EOF &&
245 diff --git a/file1 b/file1
246 index $blob1..$blob2 100644
247 --- a/file1
248 +++ b/file1
249 @@ -1 +1 @@
253 cat >expect2 <<-EOF &&
254 <<<<<<< HEAD
256 =======
258 >>>>>>> $commit... G
260 git tag new-branch1 &&
261 test_must_fail git rebase -i master &&
262 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
263 test_cmp expect .git/rebase-merge/patch &&
264 test_cmp expect2 file1 &&
265 test "$(git diff --name-status |
266 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
267 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
268 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
271 test_expect_success 'show conflicted patch' '
272 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
273 grep "show.*REBASE_HEAD" stderr &&
274 # the original stopped-sha1 is abbreviated
275 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
276 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
279 test_expect_success 'abort' '
280 git rebase --abort &&
281 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
282 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
283 test_path_is_missing .git/rebase-merge
286 test_expect_success 'abort with error when new base cannot be checked out' '
287 git rm --cached file1 &&
288 git commit -m "remove file in base" &&
289 test_must_fail git rebase -i master > output 2>&1 &&
290 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
291 output &&
292 test_i18ngrep "file1" output &&
293 test_path_is_missing .git/rebase-merge &&
294 git reset --hard HEAD^
297 test_expect_success 'retain authorship' '
298 echo A > file7 &&
299 git add file7 &&
300 test_tick &&
301 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
302 git tag twerp &&
303 git rebase -i --onto master HEAD^ &&
304 git show HEAD | grep "^Author: Twerp Snog"
307 test_expect_success 'retain authorship w/ conflicts' '
308 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
309 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
311 git reset --hard twerp &&
312 test_commit a conflict a conflict-a &&
313 git reset --hard twerp &&
315 GIT_AUTHOR_NAME=AttributeMe &&
316 export GIT_AUTHOR_NAME &&
317 test_commit b conflict b conflict-b &&
318 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
320 test_must_fail git rebase -i conflict-a &&
321 echo resolved >conflict &&
322 git add conflict &&
323 git rebase --continue &&
324 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
325 git show >out &&
326 grep AttributeMe out
329 test_expect_success 'squash' '
330 git reset --hard twerp &&
331 echo B > file7 &&
332 test_tick &&
333 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
334 echo "******************************" &&
336 set_fake_editor &&
337 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
338 git rebase -i --onto master HEAD~2
339 ) &&
340 test B = $(cat file7) &&
341 test $(git rev-parse HEAD^) = $(git rev-parse master)
344 test_expect_success 'retain authorship when squashing' '
345 git show HEAD | grep "^Author: Twerp Snog"
348 test_expect_success REBASE_P '-p handles "no changes" gracefully' '
349 HEAD=$(git rev-parse HEAD) &&
350 git rebase -i -p HEAD^ &&
351 git update-index --refresh &&
352 git diff-files --quiet &&
353 git diff-index --quiet --cached HEAD -- &&
354 test $HEAD = $(git rev-parse HEAD)
357 test_expect_failure REBASE_P 'exchange two commits with -p' '
358 git checkout H &&
360 set_fake_editor &&
361 FAKE_LINES="2 1" git rebase -i -p HEAD~2
362 ) &&
363 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
364 test G = $(git cat-file commit HEAD | sed -ne \$p)
367 test_expect_success REBASE_P 'preserve merges with -p' '
368 git checkout -b to-be-preserved master^ &&
369 : > unrelated-file &&
370 git add unrelated-file &&
371 test_tick &&
372 git commit -m "unrelated" &&
373 git checkout -b another-branch master &&
374 echo B > file1 &&
375 test_tick &&
376 git commit -m J file1 &&
377 test_tick &&
378 git merge to-be-preserved &&
379 echo C > file1 &&
380 test_tick &&
381 git commit -m K file1 &&
382 echo D > file1 &&
383 test_tick &&
384 git commit -m L1 file1 &&
385 git checkout HEAD^ &&
386 echo 1 > unrelated-file &&
387 test_tick &&
388 git commit -m L2 unrelated-file &&
389 test_tick &&
390 git merge another-branch &&
391 echo E > file1 &&
392 test_tick &&
393 git commit -m M file1 &&
394 git checkout -b to-be-rebased &&
395 test_tick &&
396 git rebase -i -p --onto branch1 master &&
397 git update-index --refresh &&
398 git diff-files --quiet &&
399 git diff-index --quiet --cached HEAD -- &&
400 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
401 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
402 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
403 test $(git show HEAD~5:file1) = B &&
404 test $(git show HEAD~3:file1) = C &&
405 test $(git show HEAD:file1) = E &&
406 test $(git show HEAD:unrelated-file) = 1
409 test_expect_success REBASE_P 'edit ancestor with -p' '
411 set_fake_editor &&
412 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3
413 ) &&
414 echo 2 > unrelated-file &&
415 test_tick &&
416 git commit -m L2-modified --amend unrelated-file &&
417 git rebase --continue &&
418 git update-index --refresh &&
419 git diff-files --quiet &&
420 git diff-index --quiet --cached HEAD -- &&
421 test $(git show HEAD:unrelated-file) = 2
424 test_expect_success '--continue tries to commit' '
425 git reset --hard D &&
426 test_tick &&
428 set_fake_editor &&
429 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
430 echo resolved > file1 &&
431 git add file1 &&
432 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
433 ) &&
434 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
435 git show HEAD | grep chouette
438 test_expect_success 'verbose flag is heeded, even after --continue' '
439 git reset --hard master@{1} &&
440 test_tick &&
441 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
442 echo resolved > file1 &&
443 git add file1 &&
444 git rebase --continue > output &&
445 grep "^ file1 | 2 +-$" output
448 test_expect_success C_LOCALE_OUTPUT 'multi-squash only fires up editor once' '
449 base=$(git rev-parse HEAD~4) &&
451 set_fake_editor &&
452 FAKE_COMMIT_AMEND="ONCE" \
453 FAKE_LINES="1 squash 2 squash 3 squash 4" \
454 EXPECT_HEADER_COUNT=4 \
455 git rebase -i $base
456 ) &&
457 test $base = $(git rev-parse HEAD^) &&
458 test 1 = $(git show | grep ONCE | wc -l)
461 test_expect_success C_LOCALE_OUTPUT 'multi-fixup does not fire up editor' '
462 git checkout -b multi-fixup E &&
463 base=$(git rev-parse HEAD~4) &&
465 set_fake_editor &&
466 FAKE_COMMIT_AMEND="NEVER" \
467 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
468 git rebase -i $base
469 ) &&
470 test $base = $(git rev-parse HEAD^) &&
471 test 0 = $(git show | grep NEVER | wc -l) &&
472 git checkout @{-1} &&
473 git branch -D multi-fixup
476 test_expect_success 'commit message used after conflict' '
477 git checkout -b conflict-fixup conflict-branch &&
478 base=$(git rev-parse HEAD~4) &&
480 set_fake_editor &&
481 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
482 git rebase -i $base &&
483 echo three > conflict &&
484 git add conflict &&
485 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
486 git rebase --continue
487 ) &&
488 test $base = $(git rev-parse HEAD^) &&
489 test 1 = $(git show | grep ONCE | wc -l) &&
490 git checkout @{-1} &&
491 git branch -D conflict-fixup
494 test_expect_success 'commit message retained after conflict' '
495 git checkout -b conflict-squash conflict-branch &&
496 base=$(git rev-parse HEAD~4) &&
498 set_fake_editor &&
499 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
500 git rebase -i $base &&
501 echo three > conflict &&
502 git add conflict &&
503 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
504 git rebase --continue
505 ) &&
506 test $base = $(git rev-parse HEAD^) &&
507 test 2 = $(git show | grep TWICE | wc -l) &&
508 git checkout @{-1} &&
509 git branch -D conflict-squash
512 test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
513 cat >expect-squash-fixup <<-\EOF &&
518 ONCE
520 git checkout -b squash-fixup E &&
521 base=$(git rev-parse HEAD~4) &&
523 set_fake_editor &&
524 FAKE_COMMIT_AMEND="ONCE" \
525 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
526 EXPECT_HEADER_COUNT=4 \
527 git rebase -i $base
528 ) &&
529 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
530 test_cmp expect-squash-fixup actual-squash-fixup &&
531 git cat-file commit HEAD@{2} |
532 grep "^# This is a combination of 3 commits\." &&
533 git cat-file commit HEAD@{3} |
534 grep "^# This is a combination of 2 commits\." &&
535 git checkout @{-1} &&
536 git branch -D squash-fixup
539 test_expect_success C_LOCALE_OUTPUT 'squash ignores comments' '
540 git checkout -b skip-comments E &&
541 base=$(git rev-parse HEAD~4) &&
543 set_fake_editor &&
544 FAKE_COMMIT_AMEND="ONCE" \
545 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
546 EXPECT_HEADER_COUNT=4 \
547 git rebase -i $base
548 ) &&
549 test $base = $(git rev-parse HEAD^) &&
550 test 1 = $(git show | grep ONCE | wc -l) &&
551 git checkout @{-1} &&
552 git branch -D skip-comments
555 test_expect_success C_LOCALE_OUTPUT 'squash ignores blank lines' '
556 git checkout -b skip-blank-lines E &&
557 base=$(git rev-parse HEAD~4) &&
559 set_fake_editor &&
560 FAKE_COMMIT_AMEND="ONCE" \
561 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
562 EXPECT_HEADER_COUNT=4 \
563 git rebase -i $base
564 ) &&
565 test $base = $(git rev-parse HEAD^) &&
566 test 1 = $(git show | grep ONCE | wc -l) &&
567 git checkout @{-1} &&
568 git branch -D skip-blank-lines
571 test_expect_success 'squash works as expected' '
572 git checkout -b squash-works no-conflict-branch &&
573 one=$(git rev-parse HEAD~3) &&
575 set_fake_editor &&
576 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
577 ) &&
578 test $one = $(git rev-parse HEAD~2)
581 test_expect_success 'interrupted squash works as expected' '
582 git checkout -b interrupted-squash conflict-branch &&
583 one=$(git rev-parse HEAD~3) &&
585 set_fake_editor &&
586 test_must_fail env FAKE_LINES="1 squash 3 2" \
587 git rebase -i HEAD~3
588 ) &&
589 test_write_lines one two four > conflict &&
590 git add conflict &&
591 test_must_fail git rebase --continue &&
592 echo resolved > conflict &&
593 git add conflict &&
594 git rebase --continue &&
595 test $one = $(git rev-parse HEAD~2)
598 test_expect_success 'interrupted squash works as expected (case 2)' '
599 git checkout -b interrupted-squash2 conflict-branch &&
600 one=$(git rev-parse HEAD~3) &&
602 set_fake_editor &&
603 test_must_fail env FAKE_LINES="3 squash 1 2" \
604 git rebase -i HEAD~3
605 ) &&
606 test_write_lines one four > conflict &&
607 git add conflict &&
608 test_must_fail git rebase --continue &&
609 test_write_lines one two four > conflict &&
610 git add conflict &&
611 test_must_fail git rebase --continue &&
612 echo resolved > conflict &&
613 git add conflict &&
614 git rebase --continue &&
615 test $one = $(git rev-parse HEAD~2)
618 test_expect_success '--continue tries to commit, even for "edit"' '
619 echo unrelated > file7 &&
620 git add file7 &&
621 test_tick &&
622 git commit -m "unrelated change" &&
623 parent=$(git rev-parse HEAD^) &&
624 test_tick &&
626 set_fake_editor &&
627 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
628 echo edited > file7 &&
629 git add file7 &&
630 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
631 ) &&
632 test edited = $(git show HEAD:file7) &&
633 git show HEAD | grep chouette &&
634 test $parent = $(git rev-parse HEAD^)
637 test_expect_success 'aborted --continue does not squash commits after "edit"' '
638 old=$(git rev-parse HEAD) &&
639 test_tick &&
641 set_fake_editor &&
642 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
643 echo "edited again" > file7 &&
644 git add file7 &&
645 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
646 ) &&
647 test $old = $(git rev-parse HEAD) &&
648 git rebase --abort
651 test_expect_success 'auto-amend only edited commits after "edit"' '
652 test_tick &&
654 set_fake_editor &&
655 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
656 echo "edited again" > file7 &&
657 git add file7 &&
658 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
659 echo "and again" > file7 &&
660 git add file7 &&
661 test_tick &&
662 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
663 git rebase --continue
664 ) &&
665 git rebase --abort
668 test_expect_success 'clean error after failed "exec"' '
669 test_tick &&
670 test_when_finished "git rebase --abort || :" &&
672 set_fake_editor &&
673 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
674 ) &&
675 echo "edited again" > file7 &&
676 git add file7 &&
677 test_must_fail git rebase --continue 2>error &&
678 test_i18ngrep "you have staged changes in your working tree" error
681 test_expect_success 'rebase a detached HEAD' '
682 grandparent=$(git rev-parse HEAD~2) &&
683 git checkout $(git rev-parse HEAD) &&
684 test_tick &&
686 set_fake_editor &&
687 FAKE_LINES="2 1" git rebase -i HEAD~2
688 ) &&
689 test $grandparent = $(git rev-parse HEAD~2)
692 test_expect_success 'rebase a commit violating pre-commit' '
694 mkdir -p .git/hooks &&
695 write_script .git/hooks/pre-commit <<-\EOF &&
696 test -z "$(git diff --cached --check)"
698 echo "monde! " >> file1 &&
699 test_tick &&
700 test_must_fail git commit -m doesnt-verify file1 &&
701 git commit -m doesnt-verify --no-verify file1 &&
702 test_tick &&
704 set_fake_editor &&
705 FAKE_LINES=2 git rebase -i HEAD~2
709 test_expect_success 'rebase with a file named HEAD in worktree' '
711 rm -fr .git/hooks &&
712 git reset --hard &&
713 git checkout -b branch3 A &&
716 GIT_AUTHOR_NAME="Squashed Away" &&
717 export GIT_AUTHOR_NAME &&
718 >HEAD &&
719 git add HEAD &&
720 git commit -m "Add head" &&
721 >BODY &&
722 git add BODY &&
723 git commit -m "Add body"
724 ) &&
727 set_fake_editor &&
728 FAKE_LINES="1 squash 2" git rebase -i @{-1}
729 ) &&
730 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
734 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
736 git checkout -b branch4 HEAD &&
737 GIT_EDITOR=: git commit --amend \
738 --author="Somebody else <somebody@else.com>" &&
739 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
740 git rebase -i branch3 &&
741 test $(git rev-parse branch3) = $(git rev-parse branch4)
745 test_expect_success 'submodule rebase setup' '
746 git checkout A &&
747 mkdir sub &&
749 cd sub && git init && >elif &&
750 git add elif && git commit -m "submodule initial"
751 ) &&
752 echo 1 >file1 &&
753 git add file1 sub &&
754 test_tick &&
755 git commit -m "One" &&
756 echo 2 >file1 &&
757 test_tick &&
758 git commit -a -m "Two" &&
760 cd sub && echo 3 >elif &&
761 git commit -a -m "submodule second"
762 ) &&
763 test_tick &&
764 git commit -a -m "Three changes submodule"
767 test_expect_success 'submodule rebase -i' '
769 set_fake_editor &&
770 FAKE_LINES="1 squash 2 3" git rebase -i A
774 test_expect_success 'submodule conflict setup' '
775 git tag submodule-base &&
776 git checkout HEAD^ &&
778 cd sub && git checkout HEAD^ && echo 4 >elif &&
779 git add elif && git commit -m "submodule conflict"
780 ) &&
781 git add sub &&
782 test_tick &&
783 git commit -m "Conflict in submodule" &&
784 git tag submodule-topic
787 test_expect_success 'rebase -i continue with only submodule staged' '
788 test_must_fail git rebase -i submodule-base &&
789 git add sub &&
790 git rebase --continue &&
791 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
794 test_expect_success 'rebase -i continue with unstaged submodule' '
795 git checkout submodule-topic &&
796 git reset --hard &&
797 test_must_fail git rebase -i submodule-base &&
798 git reset &&
799 git rebase --continue &&
800 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
803 test_expect_success 'avoid unnecessary reset' '
804 git checkout master &&
805 git reset --hard &&
806 test-tool chmtime =123456789 file3 &&
807 git update-index --refresh &&
808 HEAD=$(git rev-parse HEAD) &&
809 git rebase -i HEAD~4 &&
810 test $HEAD = $(git rev-parse HEAD) &&
811 MTIME=$(test-tool chmtime --get file3) &&
812 test 123456789 = $MTIME
815 test_expect_success 'reword' '
816 git checkout -b reword-branch master &&
818 set_fake_editor &&
819 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
820 git rebase -i A &&
821 git show HEAD | grep "E changed" &&
822 test $(git rev-parse master) != $(git rev-parse HEAD) &&
823 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
824 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
825 git rebase -i A &&
826 git show HEAD^ | grep "D changed" &&
827 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
828 git rebase -i A &&
829 git show HEAD~3 | grep "B changed" &&
830 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
831 git rebase -i A
832 ) &&
833 git show HEAD~2 | grep "C changed"
836 test_expect_success 'rebase -i can copy notes' '
837 git config notes.rewrite.rebase true &&
838 git config notes.rewriteRef "refs/notes/*" &&
839 test_commit n1 &&
840 test_commit n2 &&
841 test_commit n3 &&
842 git notes add -m"a note" n3 &&
843 git rebase -i --onto n1 n2 &&
844 test "a note" = "$(git notes show HEAD)"
847 test_expect_success 'rebase -i can copy notes over a fixup' '
848 cat >expect <<-\EOF &&
849 an earlier note
851 a note
853 git reset --hard n3 &&
854 git notes add -m"an earlier note" n2 &&
856 set_fake_editor &&
857 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
858 git rebase -i n1
859 ) &&
860 git notes show > output &&
861 test_cmp expect output
864 test_expect_success 'rebase while detaching HEAD' '
865 git symbolic-ref HEAD &&
866 grandparent=$(git rev-parse HEAD~2) &&
867 test_tick &&
869 set_fake_editor &&
870 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
871 ) &&
872 test $grandparent = $(git rev-parse HEAD~2) &&
873 test_must_fail git symbolic-ref HEAD
876 test_tick # Ensure that the rebased commits get a different timestamp.
877 test_expect_success 'always cherry-pick with --no-ff' '
878 git checkout no-ff-branch &&
879 git tag original-no-ff-branch &&
880 git rebase -i --no-ff A &&
881 for p in 0 1 2
883 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
884 git diff HEAD~$p original-no-ff-branch~$p > out &&
885 test_must_be_empty out
886 done &&
887 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
888 git diff HEAD~3 original-no-ff-branch~3 > out &&
889 test_must_be_empty out
892 test_expect_success 'set up commits with funny messages' '
893 git checkout -b funny A &&
894 echo >>file1 &&
895 test_tick &&
896 git commit -a -m "end with slash\\" &&
897 echo >>file1 &&
898 test_tick &&
899 git commit -a -m "something (\000) that looks like octal" &&
900 echo >>file1 &&
901 test_tick &&
902 git commit -a -m "something (\n) that looks like a newline" &&
903 echo >>file1 &&
904 test_tick &&
905 git commit -a -m "another commit"
908 test_expect_success 'rebase-i history with funny messages' '
909 git rev-list A..funny >expect &&
910 test_tick &&
912 set_fake_editor &&
913 FAKE_LINES="1 2 3 4" git rebase -i A
914 ) &&
915 git rev-list A.. >actual &&
916 test_cmp expect actual
919 test_expect_success 'prepare for rebase -i --exec' '
920 git checkout master &&
921 git checkout -b execute &&
922 test_commit one_exec main.txt one_exec &&
923 test_commit two_exec main.txt two_exec &&
924 test_commit three_exec main.txt three_exec
927 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
929 set_fake_editor &&
930 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
931 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
932 export FAKE_LINES &&
933 git rebase -i HEAD~2 >expect
934 ) &&
935 sed -e "1,9d" expect >expected &&
936 test_cmp expected actual
939 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
940 git reset --hard execute &&
942 set_fake_editor &&
943 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
944 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
945 export FAKE_LINES &&
946 git rebase -i HEAD~2 >expect
947 ) &&
948 sed -e "1,9d" expect >expected &&
949 test_cmp expected actual
952 test_expect_success 'running "git rebase -ix git show HEAD"' '
953 git reset --hard execute &&
955 set_fake_editor &&
956 git rebase -ix "git show HEAD" HEAD~2 >actual &&
957 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
958 export FAKE_LINES &&
959 git rebase -i HEAD~2 >expect
960 ) &&
961 sed -e "1,9d" expect >expected &&
962 test_cmp expected actual
966 test_expect_success 'rebase -ix with several <CMD>' '
967 git reset --hard execute &&
969 set_fake_editor &&
970 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
971 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
972 export FAKE_LINES &&
973 git rebase -i HEAD~2 >expect
974 ) &&
975 sed -e "1,9d" expect >expected &&
976 test_cmp expected actual
979 test_expect_success 'rebase -ix with several instances of --exec' '
980 git reset --hard execute &&
982 set_fake_editor &&
983 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
984 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
985 exec_git_show_HEAD exec_pwd" &&
986 export FAKE_LINES &&
987 git rebase -i HEAD~2 >expect
988 ) &&
989 sed -e "1,11d" expect >expected &&
990 test_cmp expected actual
993 test_expect_success C_LOCALE_OUTPUT 'rebase -ix with --autosquash' '
994 git reset --hard execute &&
995 git checkout -b autosquash &&
996 echo second >second.txt &&
997 git add second.txt &&
998 git commit -m "fixup! two_exec" &&
999 echo bis >bis.txt &&
1000 git add bis.txt &&
1001 git commit -m "fixup! two_exec" &&
1002 git checkout -b autosquash_actual &&
1003 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
1004 git checkout autosquash &&
1006 set_fake_editor &&
1007 git checkout -b autosquash_expected &&
1008 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
1009 export FAKE_LINES &&
1010 git rebase -i HEAD~4 >expect
1011 ) &&
1012 sed -e "1,13d" expect >expected &&
1013 test_cmp expected actual
1016 test_expect_success 'rebase --exec works without -i ' '
1017 git reset --hard execute &&
1018 rm -rf exec_output &&
1019 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
1020 test_i18ngrep "Successfully rebased and updated" actual &&
1021 test_line_count = 2 exec_output &&
1022 test_path_is_missing invoked_editor
1025 test_expect_success 'rebase -i --exec without <CMD>' '
1026 git reset --hard execute &&
1027 test_must_fail git rebase -i --exec 2>actual &&
1028 test_i18ngrep "requires a value" actual &&
1029 git checkout master
1032 test_expect_success 'rebase -i --root re-order and drop commits' '
1033 git checkout E &&
1035 set_fake_editor &&
1036 FAKE_LINES="3 1 2 5" git rebase -i --root
1037 ) &&
1038 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1039 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1040 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
1041 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
1042 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
1045 test_expect_success 'rebase -i --root retain root commit author and message' '
1046 git checkout A &&
1047 echo B >file7 &&
1048 git add file7 &&
1049 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1051 set_fake_editor &&
1052 FAKE_LINES="2" git rebase -i --root
1053 ) &&
1054 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1055 git cat-file commit HEAD | grep -q "^different author$"
1058 test_expect_success 'rebase -i --root temporary sentinel commit' '
1059 git checkout B &&
1061 set_fake_editor &&
1062 test_must_fail env FAKE_LINES="2" git rebase -i --root
1063 ) &&
1064 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1065 git rebase --abort
1068 test_expect_success 'rebase -i --root fixup root commit' '
1069 git checkout B &&
1071 set_fake_editor &&
1072 FAKE_LINES="1 fixup 2" git rebase -i --root
1073 ) &&
1074 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1075 test B = $(git show HEAD:file1) &&
1076 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1079 test_expect_success 'rebase -i --root reword original root commit' '
1080 test_when_finished "test_might_fail git rebase --abort" &&
1081 git checkout -b reword-original-root-branch master &&
1083 set_fake_editor &&
1084 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1085 git rebase -i --root
1086 ) &&
1087 git show HEAD^ | grep "A changed" &&
1088 test -z "$(git show -s --format=%p HEAD^)"
1091 test_expect_success 'rebase -i --root reword new root commit' '
1092 test_when_finished "test_might_fail git rebase --abort" &&
1093 git checkout -b reword-now-root-branch master &&
1095 set_fake_editor &&
1096 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1097 git rebase -i --root
1098 ) &&
1099 git show HEAD^ | grep "C changed" &&
1100 test -z "$(git show -s --format=%p HEAD^)"
1103 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1104 test_when_finished "reset_rebase" &&
1105 git checkout -b failing-root-pick A &&
1106 echo x >file2 &&
1107 git rm file1 &&
1108 git commit -m "remove file 1 add file 2" &&
1109 echo z >file1 &&
1111 set_fake_editor &&
1112 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1113 ) &&
1114 rm file1 &&
1115 git rebase --continue &&
1116 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1117 test "$(git rev-list --count HEAD)" = 2
1120 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1121 test_when_finished "reset_rebase" &&
1122 echo z>file1 &&
1124 set_fake_editor &&
1125 test_must_fail env FAKE_LINES="reword 1 2" \
1126 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1127 rm file1 &&
1128 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1129 ) &&
1130 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1131 test "$(git rev-list --count HEAD)" = 2
1134 test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
1135 git checkout reword-original-root-branch &&
1136 git reset --hard &&
1137 git checkout conflict-branch &&
1139 set_fake_editor &&
1140 test_must_fail git rebase -f --onto HEAD~2 HEAD~ &&
1141 test_must_fail git rebase --edit-todo
1142 ) &&
1143 git rebase --abort
1146 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1147 git reset --hard &&
1148 git checkout no-conflict-branch^0 &&
1150 set_fake_editor &&
1151 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1152 FAKE_LINES="2 1" git rebase --edit-todo &&
1153 git rebase --continue
1154 ) &&
1155 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1156 test L = $(git cat-file commit HEAD | sed -ne \$p)
1159 test_expect_success 'rebase -i produces readable reflog' '
1160 git reset --hard &&
1161 git branch -f branch-reflog-test H &&
1162 git rebase -i --onto I F branch-reflog-test &&
1163 cat >expect <<-\EOF &&
1164 rebase -i (finish): returning to refs/heads/branch-reflog-test
1165 rebase -i (pick): H
1166 rebase -i (pick): G
1167 rebase -i (start): checkout I
1169 git reflog -n4 HEAD |
1170 sed "s/[^:]*: //" >actual &&
1171 test_cmp expect actual
1174 test_expect_success 'rebase -i respects core.commentchar' '
1175 git reset --hard &&
1176 git checkout E^0 &&
1177 test_config core.commentchar "\\" &&
1178 write_script remove-all-but-first.sh <<-\EOF &&
1179 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1180 mv "$1.tmp" "$1"
1183 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1184 git rebase -i B
1185 ) &&
1186 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1189 test_expect_success 'rebase -i respects core.commentchar=auto' '
1190 test_config core.commentchar auto &&
1191 write_script copy-edit-script.sh <<-\EOF &&
1192 cp "$1" edit-script
1194 test_when_finished "git rebase --abort || :" &&
1196 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1197 git rebase -i HEAD^
1198 ) &&
1199 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1202 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1203 test_when_finished "git branch -D torebase" &&
1204 git checkout -b torebase branch1 &&
1205 upstream=$(git rev-parse ":/J") &&
1206 onto=$(git rev-parse ":/A") &&
1207 git rebase --onto $onto $upstream &&
1208 git reset --hard branch1 &&
1209 git rebase --onto ":/A" ":/J" &&
1210 git checkout branch1
1213 test_expect_success 'rebase -i with --strategy and -X' '
1214 git checkout -b conflict-merge-use-theirs conflict-branch &&
1215 git reset --hard HEAD^ &&
1216 echo five >conflict &&
1217 echo Z >file1 &&
1218 git commit -a -m "one file conflict" &&
1219 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1220 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1221 test $(cat file1) = Z
1224 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1225 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1226 git reset --hard HEAD^ &&
1227 >breakpoint &&
1228 git add breakpoint &&
1229 git commit -m "breakpoint for interactive mode" &&
1230 echo five >conflict &&
1231 echo Z >file1 &&
1232 git commit -a -m "one file conflict" &&
1234 set_fake_editor &&
1235 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1236 -Xours conflict-branch
1237 ) &&
1238 git rebase --continue &&
1239 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1240 test $(cat file1) = Z
1243 test_expect_success 'rebase -i error on commits with \ in message' '
1244 current_head=$(git rev-parse HEAD) &&
1245 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1246 test_commit TO-REMOVE will-conflict old-content &&
1247 test_commit "\temp" will-conflict new-content dummy &&
1248 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1249 test_expect_code 1 grep " emp" error
1252 test_expect_success SHA1 'short SHA-1 setup' '
1253 test_when_finished "git checkout master" &&
1254 git checkout --orphan collide &&
1255 git rm -rf . &&
1257 unset test_tick &&
1258 test_commit collide1 collide &&
1259 test_commit --notick collide2 collide &&
1260 test_commit --notick collide3 collide
1264 test_expect_success SHA1 'short SHA-1 collide' '
1265 test_when_finished "reset_rebase && git checkout master" &&
1266 git checkout collide &&
1267 colliding_sha1=6bcda37 &&
1268 test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
1270 unset test_tick &&
1271 test_tick &&
1272 set_fake_editor &&
1273 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1274 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1275 test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
1276 grep "^pick $colliding_sha1 " \
1277 .git/rebase-merge/git-rebase-todo.tmp &&
1278 grep "^pick [0-9a-f]\{40\}" \
1279 .git/rebase-merge/git-rebase-todo &&
1280 git rebase --continue
1281 ) &&
1282 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1283 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1284 test "$collide2" = "$collide3"
1287 test_expect_success 'respect core.abbrev' '
1288 git config core.abbrev 12 &&
1290 set_cat_todo_editor &&
1291 test_must_fail git rebase -i HEAD~4 >todo-list
1292 ) &&
1293 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1296 test_expect_success 'todo count' '
1297 write_script dump-raw.sh <<-\EOF &&
1298 cat "$1"
1301 test_set_editor "$(pwd)/dump-raw.sh" &&
1302 git rebase -i HEAD~4 >actual
1303 ) &&
1304 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1307 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1308 git checkout --force branch2 &&
1309 git clean -f &&
1311 set_fake_editor &&
1312 FAKE_LINES="edit 1 2" git rebase -i A
1313 ) &&
1314 test_cmp_rev HEAD F &&
1315 test_path_is_missing file6 &&
1316 >file6 &&
1317 test_must_fail git rebase --continue &&
1318 test_cmp_rev HEAD F &&
1319 rm file6 &&
1320 git rebase --continue &&
1321 test_cmp_rev HEAD I
1324 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1325 git checkout --force branch2 &&
1326 git clean -f &&
1327 git tag original-branch2 &&
1329 set_fake_editor &&
1330 FAKE_LINES="edit 1 squash 2" git rebase -i A
1331 ) &&
1332 test_cmp_rev HEAD F &&
1333 test_path_is_missing file6 &&
1334 >file6 &&
1335 test_must_fail git rebase --continue &&
1336 test_cmp_rev HEAD F &&
1337 rm file6 &&
1338 git rebase --continue &&
1339 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1340 git reset --hard original-branch2
1343 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1344 git checkout --force branch2 &&
1345 git clean -f &&
1347 set_fake_editor &&
1348 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1349 ) &&
1350 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1351 test_path_is_missing file6 &&
1352 >file6 &&
1353 test_must_fail git rebase --continue &&
1354 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1355 rm file6 &&
1356 git rebase --continue &&
1357 test $(git cat-file commit HEAD | sed -ne \$p) = I
1360 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1361 git checkout -b commit-to-skip &&
1362 for double in X 3 1
1364 test_seq 5 | sed "s/$double/&&/" >seq &&
1365 git add seq &&
1366 test_tick &&
1367 git commit -m seq-$double
1368 done &&
1369 git tag seq-onto &&
1370 git reset --hard HEAD~2 &&
1371 git cherry-pick seq-onto &&
1373 set_fake_editor &&
1374 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1375 ) &&
1376 test -d .git/rebase-merge &&
1377 git rebase --continue &&
1378 git diff --exit-code seq-onto &&
1379 test ! -d .git/rebase-merge &&
1380 test ! -f .git/CHERRY_PICK_HEAD
1383 rebase_setup_and_clean () {
1384 test_when_finished "
1385 git checkout master &&
1386 test_might_fail git branch -D $1 &&
1387 test_might_fail git rebase --abort
1388 " &&
1389 git checkout -b $1 ${2:-master}
1392 test_expect_success 'drop' '
1393 rebase_setup_and_clean drop-test &&
1395 set_fake_editor &&
1396 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1397 ) &&
1398 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1399 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1400 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1403 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1404 test_config rebase.missingCommitsCheck ignore &&
1405 rebase_setup_and_clean missing-commit &&
1407 set_fake_editor &&
1408 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1409 ) &&
1410 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1411 test_i18ngrep \
1412 "Successfully rebased and updated refs/heads/missing-commit" \
1413 actual
1416 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1417 cat >expect <<-EOF &&
1418 Warning: some commits may have been dropped accidentally.
1419 Dropped commits (newer to older):
1420 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1421 To avoid this message, use "drop" to explicitly remove a commit.
1423 test_config rebase.missingCommitsCheck warn &&
1424 rebase_setup_and_clean missing-commit &&
1426 set_fake_editor &&
1427 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1428 ) &&
1429 head -n4 actual.2 >actual &&
1430 test_i18ncmp expect actual &&
1431 test D = $(git cat-file commit HEAD | sed -ne \$p)
1434 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1435 cat >expect <<-EOF &&
1436 Warning: some commits may have been dropped accidentally.
1437 Dropped commits (newer to older):
1438 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1439 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1440 To avoid this message, use "drop" to explicitly remove a commit.
1442 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1443 The possible behaviours are: ignore, warn, error.
1445 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1446 Or you can abort the rebase with '\''git rebase --abort'\''.
1448 test_config rebase.missingCommitsCheck error &&
1449 rebase_setup_and_clean missing-commit &&
1451 set_fake_editor &&
1452 test_must_fail env FAKE_LINES="1 2 4" \
1453 git rebase -i --root 2>actual &&
1454 test_i18ncmp expect actual &&
1455 cp .git/rebase-merge/git-rebase-todo.backup \
1456 .git/rebase-merge/git-rebase-todo &&
1457 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1458 ) &&
1459 git rebase --continue &&
1460 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1461 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1464 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1465 rebase_setup_and_clean abbrevcmd &&
1466 test_commit "first" file1.txt "first line" first &&
1467 test_commit "second" file1.txt "another line" second &&
1468 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1469 test_commit "squash! second" file1.txt "another line here" second_squash &&
1470 cat >expected <<-EOF &&
1471 p $(git rev-list --abbrev-commit -1 first) first
1472 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1473 x git show HEAD
1474 p $(git rev-list --abbrev-commit -1 second) second
1475 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1476 x git show HEAD
1478 git checkout abbrevcmd &&
1479 test_config rebase.abbreviateCommands true &&
1481 set_cat_todo_editor &&
1482 test_must_fail git rebase -i --exec "git show HEAD" \
1483 --autosquash master >actual
1484 ) &&
1485 test_cmp expected actual
1488 test_expect_success 'static check of bad command' '
1489 rebase_setup_and_clean bad-cmd &&
1491 set_fake_editor &&
1492 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1493 git rebase -i --root 2>actual &&
1494 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" \
1495 actual &&
1496 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1497 actual &&
1498 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1499 ) &&
1500 git rebase --continue &&
1501 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1502 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1505 test_expect_success 'tabs and spaces are accepted in the todolist' '
1506 rebase_setup_and_clean indented-comment &&
1507 write_script add-indent.sh <<-\EOF &&
1509 # Turn single spaces into space/tab mix
1510 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1511 printf "\n\t# comment\n #more\n\t # comment\n"
1512 ) >"$1.new"
1513 mv "$1.new" "$1"
1516 test_set_editor "$(pwd)/add-indent.sh" &&
1517 git rebase -i HEAD^^^
1518 ) &&
1519 test E = $(git cat-file commit HEAD | sed -ne \$p)
1522 test_expect_success 'static check of bad SHA-1' '
1523 rebase_setup_and_clean bad-sha &&
1525 set_fake_editor &&
1526 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1527 git rebase -i --root 2>actual &&
1528 test_i18ngrep "edit XXXXXXX False commit" actual &&
1529 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1530 actual &&
1531 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1532 ) &&
1533 git rebase --continue &&
1534 test E = $(git cat-file commit HEAD | sed -ne \$p)
1537 test_expect_success 'editor saves as CR/LF' '
1538 git checkout -b with-crlf &&
1539 write_script add-crs.sh <<-\EOF &&
1540 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1541 mv -f "$1".new "$1"
1544 test_set_editor "$(pwd)/add-crs.sh" &&
1545 git rebase -i HEAD^
1549 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1550 test_when_finished "test_might_fail git rebase --abort" &&
1552 set_fake_editor &&
1553 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1554 HEAD^ >out 2>err
1555 ) &&
1556 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1559 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1560 test_when_finished "test_might_fail git rebase --abort" &&
1561 test_config commit.gpgsign true &&
1563 set_fake_editor &&
1564 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1565 HEAD^ >out 2>err
1566 ) &&
1567 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1570 test_expect_success 'valid author header after --root swap' '
1571 rebase_setup_and_clean author-header no-conflict-branch &&
1572 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1573 git cat-file commit HEAD | grep ^author >expected &&
1575 set_fake_editor &&
1576 FAKE_LINES="5 1" git rebase -i --root
1577 ) &&
1578 git cat-file commit HEAD^ | grep ^author >actual &&
1579 test_cmp expected actual
1582 test_expect_success 'valid author header when author contains single quote' '
1583 rebase_setup_and_clean author-header no-conflict-branch &&
1584 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1585 git cat-file commit HEAD | grep ^author >expected &&
1587 set_fake_editor &&
1588 FAKE_LINES="2" git rebase -i HEAD~2
1589 ) &&
1590 git cat-file commit HEAD | grep ^author >actual &&
1591 test_cmp expected actual
1594 test_expect_success 'post-commit hook is called' '
1595 test_when_finished "rm -f .git/hooks/post-commit" &&
1596 >actual &&
1597 mkdir -p .git/hooks &&
1598 write_script .git/hooks/post-commit <<-\EOS &&
1599 git rev-parse HEAD >>actual
1602 set_fake_editor &&
1603 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1604 echo x>file3 &&
1605 git add file3 &&
1606 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1607 ) &&
1608 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1609 >expect &&
1610 test_cmp expect actual
1613 # This must be the last test in this file
1614 test_expect_success '$EDITOR and friends are unchanged' '
1615 test_editor_unchanged
1618 test_done