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.
13 one - two - three - four (conflict-branch)
15 A - B - C - D - E (primary)
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 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
29 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
33 . "$TEST_DIRECTORY"/lib-rebase.sh
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 &&
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.
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
79 test_expect_success 'rebase -i with empty todo list' '
80 cat >expect <<-\EOF &&
85 test_must_fail env FAKE_LINES="#" \
86 git rebase -i HEAD^ >output 2>&1
88 tail -n 1 output >actual && # Ignore output about changing todo list
89 test_i18ncmp expect actual
92 test_expect_success 'rebase -i with the exec command' '
93 git checkout primary &&
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" &&
100 test_must_fail git rebase -i A
102 test_path_is_file touch-one &&
103 test_path_is_file touch-two &&
104 test_path_is_missing touch-three " (should have stopped before)" &&
105 test_cmp_rev C HEAD &&
106 git rebase --continue &&
107 test_path_is_file touch-three &&
108 test_path_is_file "touch-file name with spaces" &&
109 test_path_is_file touch-after-semicolon &&
110 test_cmp_rev primary HEAD &&
114 test_expect_success 'rebase -i with the exec command runs from tree root' '
115 git checkout primary &&
116 mkdir subdir && (cd subdir &&
118 FAKE_LINES="1 exec_>touch-subdir" \
121 test_path_is_file touch-subdir &&
125 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
126 test_when_finished "rm -rf subdir" &&
127 test_when_finished "git rebase --abort ||:" &&
128 git checkout primary &&
129 mkdir subdir && (cd subdir &&
131 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
136 test_expect_success 'rebase -i sets work tree properly' '
137 test_when_finished "rm -rf subdir" &&
138 test_when_finished "test_might_fail git rebase --abort" &&
140 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
142 ! grep "/subdir$" actual
145 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
146 git checkout primary &&
149 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
152 test_cmp_rev primary^ HEAD &&
154 git rebase --continue
157 test_expect_success 'rebase -x with empty command fails' '
158 test_when_finished "git rebase --abort ||:" &&
159 test_must_fail env git rebase -x "" @ 2>actual &&
160 test_write_lines "error: empty exec command" >expected &&
161 test_i18ncmp expected actual &&
162 test_must_fail env git rebase -x " " @ 2>actual &&
163 test_i18ncmp expected actual
166 test_expect_success 'rebase -x with newline in command fails' '
167 test_when_finished "git rebase --abort ||:" &&
168 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
169 test_write_lines "error: exec commands cannot contain newlines" \
171 test_i18ncmp expected actual
174 test_expect_success 'rebase -i with exec of inexistent command' '
175 git checkout primary &&
176 test_when_finished "git rebase --abort" &&
179 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
180 git rebase -i HEAD^ >actual 2>&1
182 ! grep "Maybe git-rebase is broken" actual
185 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
186 test_when_finished "git rebase --abort ||:" &&
187 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
190 test_expect_success 'no changes are a nop' '
191 git checkout branch2 &&
193 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
197 test_expect_success 'test the [branch] option' '
198 git checkout -b dead-end &&
200 git commit -m "stop here" &&
201 git rebase -i F branch2 &&
202 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
203 test_cmp_rev I branch2 &&
207 test_expect_success 'test --onto <branch>' '
208 git checkout -b test-onto branch2 &&
209 git rebase -i --onto branch1 F &&
210 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
211 test_cmp_rev HEAD^ branch1 &&
212 test_cmp_rev I branch2
215 test_expect_success 'rebase on top of a non-conflicting commit' '
216 git checkout branch1 &&
217 git tag original-branch1 &&
218 git rebase -i branch2 &&
219 test file6 = $(git diff --name-only original-branch1) &&
220 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
221 test_cmp_rev I branch2 &&
222 test_cmp_rev I HEAD~2
225 test_expect_success 'reflog for the branch shows state before rebase' '
226 test_cmp_rev branch1@{1} original-branch1
229 test_expect_success 'reflog for the branch shows correct finish message' '
230 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
231 "$(git rev-parse branch2)" >expected &&
232 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
233 test_cmp expected actual
236 test_expect_success 'exchange two commits' '
239 FAKE_LINES="2 1" git rebase -i HEAD~2
241 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
242 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
243 blob1=$(git rev-parse --short HEAD^:file1) &&
244 blob2=$(git rev-parse --short HEAD:file1) &&
245 commit=$(git rev-parse --short HEAD)
248 test_expect_success 'stop on conflicting pick' '
249 cat >expect <<-EOF &&
250 diff --git a/file1 b/file1
251 index $blob1..$blob2 100644
258 cat >expect2 <<-EOF &&
265 git tag new-branch1 &&
266 test_must_fail git rebase -i primary &&
267 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
268 test_cmp expect .git/rebase-merge/patch &&
269 test_cmp expect2 file1 &&
270 test "$(git diff --name-status |
271 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
272 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
273 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
276 test_expect_success 'show conflicted patch' '
277 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
278 grep "show.*REBASE_HEAD" stderr &&
279 # the original stopped-sha1 is abbreviated
280 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
281 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
284 test_expect_success 'abort' '
285 git rebase --abort &&
286 test_cmp_rev new-branch1 HEAD &&
287 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
288 test_path_is_missing .git/rebase-merge
291 test_expect_success 'abort with error when new base cannot be checked out' '
292 git rm --cached file1 &&
293 git commit -m "remove file in base" &&
294 test_must_fail git rebase -i primary > output 2>&1 &&
295 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
297 test_i18ngrep "file1" output &&
298 test_path_is_missing .git/rebase-merge &&
299 git reset --hard HEAD^
302 test_expect_success 'retain authorship' '
306 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
308 git rebase -i --onto primary HEAD^ &&
309 git show HEAD | grep "^Author: Twerp Snog"
312 test_expect_success 'retain authorship w/ conflicts' '
313 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
314 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
316 git reset --hard twerp &&
317 test_commit a conflict a conflict-a &&
318 git reset --hard twerp &&
320 GIT_AUTHOR_NAME=AttributeMe &&
321 export GIT_AUTHOR_NAME &&
322 test_commit b conflict b conflict-b &&
323 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
325 test_must_fail git rebase -i conflict-a &&
326 echo resolved >conflict &&
328 git rebase --continue &&
329 test_cmp_rev conflict-a^0 HEAD^ &&
334 test_expect_success 'squash' '
335 git reset --hard twerp &&
338 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
339 echo "******************************" &&
342 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
343 git rebase -i --onto primary HEAD~2
345 test B = $(cat file7) &&
346 test_cmp_rev HEAD^ primary
349 test_expect_success 'retain authorship when squashing' '
350 git show HEAD | grep "^Author: Twerp Snog"
353 test_expect_success REBASE_P '-p handles "no changes" gracefully' '
354 HEAD=$(git rev-parse HEAD) &&
355 git rebase -i -p HEAD^ &&
356 git update-index --refresh &&
357 git diff-files --quiet &&
358 git diff-index --quiet --cached HEAD -- &&
359 test $HEAD = $(git rev-parse HEAD)
362 test_expect_failure REBASE_P 'exchange two commits with -p' '
366 FAKE_LINES="2 1" git rebase -i -p HEAD~2
368 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
369 test G = $(git cat-file commit HEAD | sed -ne \$p)
372 test_expect_success REBASE_P 'preserve merges with -p' '
373 git checkout -b to-be-preserved primary^ &&
374 : > unrelated-file &&
375 git add unrelated-file &&
377 git commit -m "unrelated" &&
378 git checkout -b another-branch primary &&
381 git commit -m J file1 &&
383 git merge to-be-preserved &&
386 git commit -m K file1 &&
389 git commit -m L1 file1 &&
390 git checkout HEAD^ &&
391 echo 1 > unrelated-file &&
393 git commit -m L2 unrelated-file &&
395 git merge another-branch &&
398 git commit -m M file1 &&
399 git checkout -b to-be-rebased &&
401 git rebase -i -p --onto branch1 primary &&
402 git update-index --refresh &&
403 git diff-files --quiet &&
404 git diff-index --quiet --cached HEAD -- &&
405 test_cmp_rev HEAD~6 branch1 &&
406 test_cmp_rev HEAD~4^2 to-be-preserved &&
407 test_cmp_rev HEAD^^2^ HEAD^^^ &&
408 test $(git show HEAD~5:file1) = B &&
409 test $(git show HEAD~3:file1) = C &&
410 test $(git show HEAD:file1) = E &&
411 test $(git show HEAD:unrelated-file) = 1
414 test_expect_success REBASE_P 'edit ancestor with -p' '
417 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3
419 echo 2 > unrelated-file &&
421 git commit -m L2-modified --amend unrelated-file &&
422 git rebase --continue &&
423 git update-index --refresh &&
424 git diff-files --quiet &&
425 git diff-index --quiet --cached HEAD -- &&
426 test $(git show HEAD:unrelated-file) = 2
429 test_expect_success '--continue tries to commit' '
430 git reset --hard D &&
434 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
435 echo resolved > file1 &&
437 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
439 test_cmp_rev HEAD^ new-branch1 &&
440 git show HEAD | grep chouette
443 test_expect_success 'verbose flag is heeded, even after --continue' '
444 git reset --hard primary@{1} &&
446 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
447 echo resolved > file1 &&
449 git rebase --continue > output &&
450 grep "^ file1 | 2 +-$" output
453 test_expect_success C_LOCALE_OUTPUT 'multi-squash only fires up editor once' '
454 base=$(git rev-parse HEAD~4) &&
457 FAKE_COMMIT_AMEND="ONCE" \
458 FAKE_LINES="1 squash 2 squash 3 squash 4" \
459 EXPECT_HEADER_COUNT=4 \
462 test $base = $(git rev-parse HEAD^) &&
463 test 1 = $(git show | grep ONCE | wc -l)
466 test_expect_success C_LOCALE_OUTPUT 'multi-fixup does not fire up editor' '
467 git checkout -b multi-fixup E &&
468 base=$(git rev-parse HEAD~4) &&
471 FAKE_COMMIT_AMEND="NEVER" \
472 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
475 test $base = $(git rev-parse HEAD^) &&
476 test 0 = $(git show | grep NEVER | wc -l) &&
477 git checkout @{-1} &&
478 git branch -D multi-fixup
481 test_expect_success 'commit message used after conflict' '
482 git checkout -b conflict-fixup conflict-branch &&
483 base=$(git rev-parse HEAD~4) &&
486 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
487 git rebase -i $base &&
488 echo three > conflict &&
490 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
491 git rebase --continue
493 test $base = $(git rev-parse HEAD^) &&
494 test 1 = $(git show | grep ONCE | wc -l) &&
495 git checkout @{-1} &&
496 git branch -D conflict-fixup
499 test_expect_success 'commit message retained after conflict' '
500 git checkout -b conflict-squash conflict-branch &&
501 base=$(git rev-parse HEAD~4) &&
504 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
505 git rebase -i $base &&
506 echo three > conflict &&
508 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
509 git rebase --continue
511 test $base = $(git rev-parse HEAD^) &&
512 test 2 = $(git show | grep TWICE | wc -l) &&
513 git checkout @{-1} &&
514 git branch -D conflict-squash
517 test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
518 cat >expect-squash-fixup <<-\EOF &&
525 git checkout -b squash-fixup E &&
526 base=$(git rev-parse HEAD~4) &&
529 FAKE_COMMIT_AMEND="ONCE" \
530 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
531 EXPECT_HEADER_COUNT=4 \
534 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
535 test_cmp expect-squash-fixup actual-squash-fixup &&
536 git cat-file commit HEAD@{2} |
537 grep "^# This is a combination of 3 commits\." &&
538 git cat-file commit HEAD@{3} |
539 grep "^# This is a combination of 2 commits\." &&
540 git checkout @{-1} &&
541 git branch -D squash-fixup
544 test_expect_success C_LOCALE_OUTPUT 'squash ignores comments' '
545 git checkout -b skip-comments E &&
546 base=$(git rev-parse HEAD~4) &&
549 FAKE_COMMIT_AMEND="ONCE" \
550 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
551 EXPECT_HEADER_COUNT=4 \
554 test $base = $(git rev-parse HEAD^) &&
555 test 1 = $(git show | grep ONCE | wc -l) &&
556 git checkout @{-1} &&
557 git branch -D skip-comments
560 test_expect_success C_LOCALE_OUTPUT 'squash ignores blank lines' '
561 git checkout -b skip-blank-lines E &&
562 base=$(git rev-parse HEAD~4) &&
565 FAKE_COMMIT_AMEND="ONCE" \
566 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
567 EXPECT_HEADER_COUNT=4 \
570 test $base = $(git rev-parse HEAD^) &&
571 test 1 = $(git show | grep ONCE | wc -l) &&
572 git checkout @{-1} &&
573 git branch -D skip-blank-lines
576 test_expect_success 'squash works as expected' '
577 git checkout -b squash-works no-conflict-branch &&
578 one=$(git rev-parse HEAD~3) &&
581 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
583 test $one = $(git rev-parse HEAD~2)
586 test_expect_success 'interrupted squash works as expected' '
587 git checkout -b interrupted-squash conflict-branch &&
588 one=$(git rev-parse HEAD~3) &&
591 test_must_fail env FAKE_LINES="1 squash 3 2" \
594 test_write_lines one two four > conflict &&
596 test_must_fail git rebase --continue &&
597 echo resolved > conflict &&
599 git rebase --continue &&
600 test $one = $(git rev-parse HEAD~2)
603 test_expect_success 'interrupted squash works as expected (case 2)' '
604 git checkout -b interrupted-squash2 conflict-branch &&
605 one=$(git rev-parse HEAD~3) &&
608 test_must_fail env FAKE_LINES="3 squash 1 2" \
611 test_write_lines one four > conflict &&
613 test_must_fail git rebase --continue &&
614 test_write_lines one two four > conflict &&
616 test_must_fail git rebase --continue &&
617 echo resolved > conflict &&
619 git rebase --continue &&
620 test $one = $(git rev-parse HEAD~2)
623 test_expect_success '--continue tries to commit, even for "edit"' '
624 echo unrelated > file7 &&
627 git commit -m "unrelated change" &&
628 parent=$(git rev-parse HEAD^) &&
632 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
633 echo edited > file7 &&
635 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
637 test edited = $(git show HEAD:file7) &&
638 git show HEAD | grep chouette &&
639 test $parent = $(git rev-parse HEAD^)
642 test_expect_success 'aborted --continue does not squash commits after "edit"' '
643 old=$(git rev-parse HEAD) &&
647 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
648 echo "edited again" > file7 &&
650 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
652 test $old = $(git rev-parse HEAD) &&
656 test_expect_success 'auto-amend only edited commits after "edit"' '
660 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
661 echo "edited again" > file7 &&
663 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
664 echo "and again" > file7 &&
667 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
668 git rebase --continue
673 test_expect_success 'clean error after failed "exec"' '
675 test_when_finished "git rebase --abort || :" &&
678 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
680 echo "edited again" > file7 &&
682 test_must_fail git rebase --continue 2>error &&
683 test_i18ngrep "you have staged changes in your working tree" error
686 test_expect_success 'rebase a detached HEAD' '
687 grandparent=$(git rev-parse HEAD~2) &&
688 git checkout $(git rev-parse HEAD) &&
692 FAKE_LINES="2 1" git rebase -i HEAD~2
694 test $grandparent = $(git rev-parse HEAD~2)
697 test_expect_success 'rebase a commit violating pre-commit' '
699 mkdir -p .git/hooks &&
700 write_script .git/hooks/pre-commit <<-\EOF &&
701 test -z "$(git diff --cached --check)"
703 echo "monde! " >> file1 &&
705 test_must_fail git commit -m doesnt-verify file1 &&
706 git commit -m doesnt-verify --no-verify file1 &&
710 FAKE_LINES=2 git rebase -i HEAD~2
714 test_expect_success 'rebase with a file named HEAD in worktree' '
718 git checkout -b branch3 A &&
721 GIT_AUTHOR_NAME="Squashed Away" &&
722 export GIT_AUTHOR_NAME &&
725 git commit -m "Add head" &&
728 git commit -m "Add body"
733 FAKE_LINES="1 squash 2" git rebase -i @{-1}
735 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
739 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
741 git checkout -b branch4 HEAD &&
742 GIT_EDITOR=: git commit --amend \
743 --author="Somebody else <somebody@else.com>" &&
744 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
745 git rebase -i branch3 &&
746 test_cmp_rev branch3 branch4
750 test_expect_success 'submodule rebase setup' '
754 cd sub && git init && >elif &&
755 git add elif && git commit -m "submodule initial"
760 git commit -m "One" &&
763 git commit -a -m "Two" &&
765 cd sub && echo 3 >elif &&
766 git commit -a -m "submodule second"
769 git commit -a -m "Three changes submodule"
772 test_expect_success 'submodule rebase -i' '
775 FAKE_LINES="1 squash 2 3" git rebase -i A
779 test_expect_success 'submodule conflict setup' '
780 git tag submodule-base &&
781 git checkout HEAD^ &&
783 cd sub && git checkout HEAD^ && echo 4 >elif &&
784 git add elif && git commit -m "submodule conflict"
788 git commit -m "Conflict in submodule" &&
789 git tag submodule-topic
792 test_expect_success 'rebase -i continue with only submodule staged' '
793 test_must_fail git rebase -i submodule-base &&
795 git rebase --continue &&
796 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
799 test_expect_success 'rebase -i continue with unstaged submodule' '
800 git checkout submodule-topic &&
802 test_must_fail git rebase -i submodule-base &&
804 git rebase --continue &&
805 test_cmp_rev submodule-base HEAD
808 test_expect_success 'avoid unnecessary reset' '
809 git checkout primary &&
811 test-tool chmtime =123456789 file3 &&
812 git update-index --refresh &&
813 HEAD=$(git rev-parse HEAD) &&
814 git rebase -i HEAD~4 &&
815 test $HEAD = $(git rev-parse HEAD) &&
816 MTIME=$(test-tool chmtime --get file3) &&
817 test 123456789 = $MTIME
820 test_expect_success 'reword' '
821 git checkout -b reword-branch primary &&
824 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
826 git show HEAD | grep "E changed" &&
827 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
828 test_cmp_rev primary^ HEAD^ &&
829 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
831 git show HEAD^ | grep "D changed" &&
832 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
834 git show HEAD~3 | grep "B changed" &&
835 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
838 git show HEAD~2 | grep "C changed"
841 test_expect_success 'rebase -i can copy notes' '
842 git config notes.rewrite.rebase true &&
843 git config notes.rewriteRef "refs/notes/*" &&
847 git notes add -m"a note" n3 &&
848 git rebase -i --onto n1 n2 &&
849 test "a note" = "$(git notes show HEAD)"
852 test_expect_success 'rebase -i can copy notes over a fixup' '
853 cat >expect <<-\EOF &&
858 git reset --hard n3 &&
859 git notes add -m"an earlier note" n2 &&
862 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
865 git notes show > output &&
866 test_cmp expect output
869 test_expect_success 'rebase while detaching HEAD' '
870 git symbolic-ref HEAD &&
871 grandparent=$(git rev-parse HEAD~2) &&
875 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
877 test $grandparent = $(git rev-parse HEAD~2) &&
878 test_must_fail git symbolic-ref HEAD
881 test_tick # Ensure that the rebased commits get a different timestamp.
882 test_expect_success 'always cherry-pick with --no-ff' '
883 git checkout no-ff-branch &&
884 git tag original-no-ff-branch &&
885 git rebase -i --no-ff A &&
888 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
889 git diff HEAD~$p original-no-ff-branch~$p > out &&
890 test_must_be_empty out
892 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
893 git diff HEAD~3 original-no-ff-branch~3 > out &&
894 test_must_be_empty out
897 test_expect_success 'set up commits with funny messages' '
898 git checkout -b funny A &&
901 git commit -a -m "end with slash\\" &&
904 git commit -a -m "something (\000) that looks like octal" &&
907 git commit -a -m "something (\n) that looks like a newline" &&
910 git commit -a -m "another commit"
913 test_expect_success 'rebase-i history with funny messages' '
914 git rev-list A..funny >expect &&
918 FAKE_LINES="1 2 3 4" git rebase -i A
920 git rev-list A.. >actual &&
921 test_cmp expect actual
924 test_expect_success 'prepare for rebase -i --exec' '
925 git checkout primary &&
926 git checkout -b execute &&
927 test_commit one_exec main.txt one_exec &&
928 test_commit two_exec main.txt two_exec &&
929 test_commit three_exec main.txt three_exec
932 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
935 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
936 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
938 git rebase -i HEAD~2 >expect
940 sed -e "1,9d" expect >expected &&
941 test_cmp expected actual
944 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
945 git reset --hard execute &&
948 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
949 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
951 git rebase -i HEAD~2 >expect
953 sed -e "1,9d" expect >expected &&
954 test_cmp expected actual
957 test_expect_success 'running "git rebase -ix git show HEAD"' '
958 git reset --hard execute &&
961 git rebase -ix "git show HEAD" HEAD~2 >actual &&
962 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
964 git rebase -i HEAD~2 >expect
966 sed -e "1,9d" expect >expected &&
967 test_cmp expected actual
971 test_expect_success 'rebase -ix with several <CMD>' '
972 git reset --hard execute &&
975 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
976 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
978 git rebase -i HEAD~2 >expect
980 sed -e "1,9d" expect >expected &&
981 test_cmp expected actual
984 test_expect_success 'rebase -ix with several instances of --exec' '
985 git reset --hard execute &&
988 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
989 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
990 exec_git_show_HEAD exec_pwd" &&
992 git rebase -i HEAD~2 >expect
994 sed -e "1,11d" expect >expected &&
995 test_cmp expected actual
998 test_expect_success C_LOCALE_OUTPUT 'rebase -ix with --autosquash' '
999 git reset --hard execute &&
1000 git checkout -b autosquash &&
1001 echo second >second.txt &&
1002 git add second.txt &&
1003 git commit -m "fixup! two_exec" &&
1004 echo bis >bis.txt &&
1006 git commit -m "fixup! two_exec" &&
1007 git checkout -b autosquash_actual &&
1008 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
1009 git checkout autosquash &&
1012 git checkout -b autosquash_expected &&
1013 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
1014 export FAKE_LINES &&
1015 git rebase -i HEAD~4 >expect
1017 sed -e "1,13d" expect >expected &&
1018 test_cmp expected actual
1021 test_expect_success 'rebase --exec works without -i ' '
1022 git reset --hard execute &&
1023 rm -rf exec_output &&
1024 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
1025 test_i18ngrep "Successfully rebased and updated" actual &&
1026 test_line_count = 2 exec_output &&
1027 test_path_is_missing invoked_editor
1030 test_expect_success 'rebase -i --exec without <CMD>' '
1031 git reset --hard execute &&
1032 test_must_fail git rebase -i --exec 2>actual &&
1033 test_i18ngrep "requires a value" actual &&
1034 git checkout primary
1037 test_expect_success 'rebase -i --root re-order and drop commits' '
1041 FAKE_LINES="3 1 2 5" git rebase -i --root
1043 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1044 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1045 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
1046 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
1047 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
1050 test_expect_success 'rebase -i --root retain root commit author and message' '
1054 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1057 FAKE_LINES="2" git rebase -i --root
1059 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1060 git cat-file commit HEAD | grep -q "^different author$"
1063 test_expect_success 'rebase -i --root temporary sentinel commit' '
1067 test_must_fail env FAKE_LINES="2" git rebase -i --root
1069 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1073 test_expect_success 'rebase -i --root fixup root commit' '
1077 FAKE_LINES="1 fixup 2" git rebase -i --root
1079 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1080 test B = $(git show HEAD:file1) &&
1081 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1084 test_expect_success 'rebase -i --root reword original root commit' '
1085 test_when_finished "test_might_fail git rebase --abort" &&
1086 git checkout -b reword-original-root-branch primary &&
1089 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1090 git rebase -i --root
1092 git show HEAD^ | grep "A changed" &&
1093 test -z "$(git show -s --format=%p HEAD^)"
1096 test_expect_success 'rebase -i --root reword new root commit' '
1097 test_when_finished "test_might_fail git rebase --abort" &&
1098 git checkout -b reword-now-root-branch primary &&
1101 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1102 git rebase -i --root
1104 git show HEAD^ | grep "C changed" &&
1105 test -z "$(git show -s --format=%p HEAD^)"
1108 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1109 test_when_finished "reset_rebase" &&
1110 git checkout -b failing-root-pick A &&
1113 git commit -m "remove file 1 add file 2" &&
1117 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1120 git rebase --continue &&
1121 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1122 test "$(git rev-list --count HEAD)" = 2
1125 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1126 test_when_finished "reset_rebase" &&
1130 test_must_fail env FAKE_LINES="reword 1 2" \
1131 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1133 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1135 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1136 test "$(git rev-list --count HEAD)" = 2
1139 test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
1140 git checkout reword-original-root-branch &&
1142 git checkout conflict-branch &&
1145 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1146 test_must_fail git rebase --edit-todo
1151 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1153 git checkout no-conflict-branch^0 &&
1156 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1157 FAKE_LINES="2 1" git rebase --edit-todo &&
1158 git rebase --continue
1160 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1161 test L = $(git cat-file commit HEAD | sed -ne \$p)
1164 test_expect_success 'rebase -i produces readable reflog' '
1166 git branch -f branch-reflog-test H &&
1167 git rebase -i --onto I F branch-reflog-test &&
1168 cat >expect <<-\EOF &&
1169 rebase (finish): returning to refs/heads/branch-reflog-test
1172 rebase (start): checkout I
1174 git reflog -n4 HEAD |
1175 sed "s/[^:]*: //" >actual &&
1176 test_cmp expect actual
1179 test_expect_success 'rebase -i respects core.commentchar' '
1182 test_config core.commentchar "\\" &&
1183 write_script remove-all-but-first.sh <<-\EOF &&
1184 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1188 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1191 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1194 test_expect_success 'rebase -i respects core.commentchar=auto' '
1195 test_config core.commentchar auto &&
1196 write_script copy-edit-script.sh <<-\EOF &&
1199 test_when_finished "git rebase --abort || :" &&
1201 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1204 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1207 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1208 test_when_finished "git branch -D torebase" &&
1209 git checkout -b torebase branch1 &&
1210 upstream=$(git rev-parse ":/J") &&
1211 onto=$(git rev-parse ":/A") &&
1212 git rebase --onto $onto $upstream &&
1213 git reset --hard branch1 &&
1214 git rebase --onto ":/A" ":/J" &&
1215 git checkout branch1
1218 test_expect_success 'rebase -i with --strategy and -X' '
1219 git checkout -b conflict-merge-use-theirs conflict-branch &&
1220 git reset --hard HEAD^ &&
1221 echo five >conflict &&
1223 git commit -a -m "one file conflict" &&
1224 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1225 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1226 test $(cat file1) = Z
1229 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1230 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1231 git reset --hard HEAD^ &&
1233 git add breakpoint &&
1234 git commit -m "breakpoint for interactive mode" &&
1235 echo five >conflict &&
1237 git commit -a -m "one file conflict" &&
1240 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1241 -Xours conflict-branch
1243 git rebase --continue &&
1244 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1245 test $(cat file1) = Z
1248 test_expect_success 'rebase -i error on commits with \ in message' '
1249 current_head=$(git rev-parse HEAD) &&
1250 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1251 test_commit TO-REMOVE will-conflict old-content &&
1252 test_commit "\temp" will-conflict new-content dummy &&
1253 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1254 test_expect_code 1 grep " emp" error
1257 test_expect_success 'short commit ID setup' '
1258 test_when_finished "git checkout primary" &&
1259 git checkout --orphan collide &&
1263 test_commit collide1 collide &&
1264 test_commit --notick collide2 collide &&
1265 test_commit --notick collide3 collide
1269 if test -n "$GIT_TEST_FIND_COLLIDER"
1271 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1272 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1273 blob="$(git rev-parse collide2:collide)"
1274 from="$(git rev-parse collide1^0)"
1275 repl="commit refs/heads/collider-&\\n"
1276 repl="${repl}author $author\\ncommitter $committer\\n"
1277 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1278 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1279 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1280 git fast-import <script &&
1282 git for-each-ref >refs &&
1283 grep "^$(test_oid t3404_collision)" <refs >matches &&
1285 test_line_count -gt 2 matches || {
1286 echo "Could not find a collider" >&2
1291 test_expect_success 'short commit ID collide' '
1292 test_oid_cache <<-EOF &&
1293 # collision-related constants
1294 t3404_collision sha1:6bcd
1295 t3404_collision sha256:0161
1296 t3404_collider sha1:ac4f2ee
1297 t3404_collider sha256:16697
1299 test_when_finished "reset_rebase && git checkout primary" &&
1300 git checkout collide &&
1301 colliding_id=$(test_oid t3404_collision) &&
1302 hexsz=$(test_oid hexsz) &&
1303 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1304 test_config core.abbrev 4 &&
1309 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1310 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1311 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1312 grep "^pick $colliding_id " \
1313 .git/rebase-merge/git-rebase-todo.tmp &&
1314 grep "^pick [0-9a-f]\{$hexsz\}" \
1315 .git/rebase-merge/git-rebase-todo &&
1316 grep "^pick [0-9a-f]\{$hexsz\}" \
1317 .git/rebase-merge/git-rebase-todo.backup &&
1318 git rebase --continue
1320 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1321 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1322 test "$collide2" = "$collide3"
1325 test_expect_success 'respect core.abbrev' '
1326 git config core.abbrev 12 &&
1328 set_cat_todo_editor &&
1329 test_must_fail git rebase -i HEAD~4 >todo-list
1331 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1334 test_expect_success 'todo count' '
1335 write_script dump-raw.sh <<-\EOF &&
1339 test_set_editor "$(pwd)/dump-raw.sh" &&
1340 git rebase -i HEAD~4 >actual
1342 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1345 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1346 git checkout --force branch2 &&
1350 FAKE_LINES="edit 1 2" git rebase -i A
1352 test_cmp_rev HEAD F &&
1353 test_path_is_missing file6 &&
1355 test_must_fail git rebase --continue &&
1356 test_cmp_rev HEAD F &&
1358 git rebase --continue &&
1362 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1363 git checkout --force branch2 &&
1365 git tag original-branch2 &&
1368 FAKE_LINES="edit 1 squash 2" git rebase -i A
1370 test_cmp_rev HEAD F &&
1371 test_path_is_missing file6 &&
1373 test_must_fail git rebase --continue &&
1374 test_cmp_rev HEAD F &&
1376 git rebase --continue &&
1377 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1378 git reset --hard original-branch2
1381 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1382 git checkout --force branch2 &&
1386 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1388 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1389 test_path_is_missing file6 &&
1391 test_must_fail git rebase --continue &&
1392 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1394 git rebase --continue &&
1395 test $(git cat-file commit HEAD | sed -ne \$p) = I
1398 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1399 git checkout -b commit-to-skip &&
1402 test_seq 5 | sed "s/$double/&&/" >seq &&
1405 git commit -m seq-$double
1408 git reset --hard HEAD~2 &&
1409 git cherry-pick seq-onto &&
1412 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1414 test -d .git/rebase-merge &&
1415 git rebase --continue &&
1416 git diff --exit-code seq-onto &&
1417 test ! -d .git/rebase-merge &&
1418 test ! -f .git/CHERRY_PICK_HEAD
1421 rebase_setup_and_clean () {
1422 test_when_finished "
1423 git checkout primary &&
1424 test_might_fail git branch -D $1 &&
1425 test_might_fail git rebase --abort
1427 git checkout -b $1 ${2:-primary}
1430 test_expect_success 'drop' '
1431 rebase_setup_and_clean drop-test &&
1434 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1436 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1437 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1438 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1441 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1442 test_config rebase.missingCommitsCheck ignore &&
1443 rebase_setup_and_clean missing-commit &&
1446 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1448 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1450 "Successfully rebased and updated refs/heads/missing-commit" \
1454 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1455 cat >expect <<-EOF &&
1456 Warning: some commits may have been dropped accidentally.
1457 Dropped commits (newer to older):
1458 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1459 To avoid this message, use "drop" to explicitly remove a commit.
1461 test_config rebase.missingCommitsCheck warn &&
1462 rebase_setup_and_clean missing-commit &&
1465 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1467 head -n4 actual.2 >actual &&
1468 test_i18ncmp expect actual &&
1469 test D = $(git cat-file commit HEAD | sed -ne \$p)
1472 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1473 cat >expect <<-EOF &&
1474 Warning: some commits may have been dropped accidentally.
1475 Dropped commits (newer to older):
1476 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1477 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1478 To avoid this message, use "drop" to explicitly remove a commit.
1480 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1481 The possible behaviours are: ignore, warn, error.
1483 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1484 Or you can abort the rebase with '\''git rebase --abort'\''.
1486 test_config rebase.missingCommitsCheck error &&
1487 rebase_setup_and_clean missing-commit &&
1490 test_must_fail env FAKE_LINES="1 2 4" \
1491 git rebase -i --root 2>actual &&
1492 test_i18ncmp expect actual &&
1493 cp .git/rebase-merge/git-rebase-todo.backup \
1494 .git/rebase-merge/git-rebase-todo &&
1495 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1497 git rebase --continue &&
1498 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1499 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1502 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1503 test_config rebase.missingCommitsCheck ignore &&
1504 rebase_setup_and_clean missing-commit &&
1507 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1508 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1509 git rebase --continue 2>actual
1511 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1513 "Successfully rebased and updated refs/heads/missing-commit" \
1517 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1518 cat >expect <<-EOF &&
1519 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1520 Warning: some commits may have been dropped accidentally.
1521 Dropped commits (newer to older):
1522 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1523 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1524 To avoid this message, use "drop" to explicitly remove a commit.
1526 head -n4 expect >expect.2 &&
1527 tail -n1 expect >>expect.2 &&
1528 tail -n4 expect.2 >expect.3 &&
1529 test_config rebase.missingCommitsCheck warn &&
1530 rebase_setup_and_clean missing-commit &&
1533 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1534 git rebase -i --root &&
1535 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1536 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1537 head -n6 actual.2 >actual &&
1538 test_i18ncmp expect actual &&
1539 cp orig .git/rebase-merge/git-rebase-todo &&
1540 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1541 head -n4 actual.2 >actual &&
1542 test_i18ncmp expect.3 actual &&
1543 git rebase --continue 2>actual
1545 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1547 "Successfully rebased and updated refs/heads/missing-commit" \
1551 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1552 cat >expect <<-EOF &&
1553 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1554 Warning: some commits may have been dropped accidentally.
1555 Dropped commits (newer to older):
1556 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1557 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1558 To avoid this message, use "drop" to explicitly remove a commit.
1560 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1561 The possible behaviours are: ignore, warn, error.
1563 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1564 Or you can abort the rebase with '\''git rebase --abort'\''.
1566 tail -n11 expect >expect.2 &&
1567 head -n3 expect.2 >expect.3 &&
1568 tail -n7 expect.2 >>expect.3 &&
1569 test_config rebase.missingCommitsCheck error &&
1570 rebase_setup_and_clean missing-commit &&
1573 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1574 git rebase -i --root &&
1575 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1576 test_must_fail env FAKE_LINES="2 3 4" \
1577 git rebase --edit-todo 2>actual &&
1578 test_i18ncmp expect actual &&
1579 test_must_fail git rebase --continue 2>actual &&
1580 test_i18ncmp expect.2 actual &&
1581 test_must_fail git rebase --edit-todo &&
1582 cp orig .git/rebase-merge/git-rebase-todo &&
1583 test_must_fail env FAKE_LINES="1 2 3 4" \
1584 git rebase --edit-todo 2>actual &&
1585 test_i18ncmp expect.3 actual &&
1586 test_must_fail git rebase --continue 2>actual &&
1587 test_i18ncmp expect.3 actual &&
1588 cp orig .git/rebase-merge/git-rebase-todo &&
1589 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1590 git rebase --continue 2>actual
1592 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1594 "Successfully rebased and updated refs/heads/missing-commit" \
1598 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1599 test_config rebase.missingCommitsCheck error &&
1602 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1604 git rebase --edit-todo &&
1605 test_must_fail git rebase --continue &&
1608 git rebase --continue
1611 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1612 test_config rebase.missingCommitsCheck error &&
1615 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1616 cp .git/rebase-merge/git-rebase-todo todo &&
1617 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1618 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1619 git rebase --continue
1623 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1624 rebase_setup_and_clean abbrevcmd &&
1625 test_commit "first" file1.txt "first line" first &&
1626 test_commit "second" file1.txt "another line" second &&
1627 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1628 test_commit "squash! second" file1.txt "another line here" second_squash &&
1629 cat >expected <<-EOF &&
1630 p $(git rev-list --abbrev-commit -1 first) first
1631 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1633 p $(git rev-list --abbrev-commit -1 second) second
1634 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1637 git checkout abbrevcmd &&
1638 test_config rebase.abbreviateCommands true &&
1640 set_cat_todo_editor &&
1641 test_must_fail git rebase -i --exec "git show HEAD" \
1642 --autosquash primary >actual
1644 test_cmp expected actual
1647 test_expect_success 'static check of bad command' '
1648 rebase_setup_and_clean bad-cmd &&
1651 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1652 git rebase -i --root 2>actual &&
1653 test_i18ngrep "badcmd $(git rev-list --oneline -1 primary~1)" \
1655 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1657 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1659 git rebase --continue &&
1660 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1661 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1664 test_expect_success 'tabs and spaces are accepted in the todolist' '
1665 rebase_setup_and_clean indented-comment &&
1666 write_script add-indent.sh <<-\EOF &&
1668 # Turn single spaces into space/tab mix
1669 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1670 printf "\n\t# comment\n #more\n\t # comment\n"
1675 test_set_editor "$(pwd)/add-indent.sh" &&
1676 git rebase -i HEAD^^^
1678 test E = $(git cat-file commit HEAD | sed -ne \$p)
1681 test_expect_success 'static check of bad SHA-1' '
1682 rebase_setup_and_clean bad-sha &&
1685 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1686 git rebase -i --root 2>actual &&
1687 test_i18ngrep "edit XXXXXXX False commit" actual &&
1688 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1690 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1692 git rebase --continue &&
1693 test E = $(git cat-file commit HEAD | sed -ne \$p)
1696 test_expect_success 'editor saves as CR/LF' '
1697 git checkout -b with-crlf &&
1698 write_script add-crs.sh <<-\EOF &&
1699 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1703 test_set_editor "$(pwd)/add-crs.sh" &&
1708 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1709 test_when_finished "test_might_fail git rebase --abort" &&
1712 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1715 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1718 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1719 test_when_finished "test_might_fail git rebase --abort" &&
1720 test_config commit.gpgsign true &&
1723 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1726 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1729 test_expect_success 'valid author header after --root swap' '
1730 rebase_setup_and_clean author-header no-conflict-branch &&
1731 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1732 git cat-file commit HEAD | grep ^author >expected &&
1735 FAKE_LINES="5 1" git rebase -i --root
1737 git cat-file commit HEAD^ | grep ^author >actual &&
1738 test_cmp expected actual
1741 test_expect_success 'valid author header when author contains single quote' '
1742 rebase_setup_and_clean author-header no-conflict-branch &&
1743 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1744 git cat-file commit HEAD | grep ^author >expected &&
1747 FAKE_LINES="2" git rebase -i HEAD~2
1749 git cat-file commit HEAD | grep ^author >actual &&
1750 test_cmp expected actual
1753 test_expect_success 'post-commit hook is called' '
1754 test_when_finished "rm -f .git/hooks/post-commit" &&
1756 mkdir -p .git/hooks &&
1757 write_script .git/hooks/post-commit <<-\EOS &&
1758 git rev-parse HEAD >>actual
1762 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1765 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1767 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1769 test_cmp expect actual
1772 test_expect_success 'correct error message for partial commit after empty pick' '
1773 test_when_finished "git rebase --abort" &&
1776 FAKE_LINES="2 1 1" &&
1777 export FAKE_LINES &&
1778 test_must_fail git rebase -i A D
1781 test_must_fail git commit file1 2>err &&
1782 test_i18ngrep "cannot do a partial commit during a rebase." err
1785 test_expect_success 'correct error message for commit --amend after empty pick' '
1786 test_when_finished "git rebase --abort" &&
1790 export FAKE_LINES &&
1791 test_must_fail git rebase -i A D
1794 test_must_fail git commit -a --amend 2>err &&
1795 test_i18ngrep "middle of a rebase -- cannot amend." err
1798 test_expect_success 'todo has correct onto hash' '
1799 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1800 onto=$(git rev-parse --short HEAD~4) &&
1801 test_i18ngrep "^# Rebase ..* onto $onto" actual
1804 test_expect_success 'ORIG_HEAD is updated correctly' '
1805 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1806 git checkout -b test-orig-head A &&
1807 git commit --allow-empty -m A1 &&
1808 git commit --allow-empty -m A2 &&
1809 git commit --allow-empty -m A3 &&
1810 git commit --allow-empty -m A4 &&
1811 git rebase primary &&
1812 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1815 # This must be the last test in this file
1816 test_expect_success '$EDITOR and friends are unchanged' '
1817 test_editor_unchanged