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_cmp 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 # Missing because we should have stopped by now.
105 test_path_is_missing touch-three &&
106 test_cmp_rev C HEAD &&
107 git rebase --continue &&
108 test_path_is_file touch-three &&
109 test_path_is_file "touch-file name with spaces" &&
110 test_path_is_file touch-after-semicolon &&
111 test_cmp_rev primary HEAD &&
115 test_expect_success
'rebase -i with the exec command runs from tree root' '
116 git checkout primary &&
117 mkdir subdir && (cd subdir &&
119 FAKE_LINES="1 exec_>touch-subdir" \
122 test_path_is_file touch-subdir &&
126 test_expect_success
'rebase -i with exec allows git commands in subdirs' '
127 test_when_finished "rm -rf subdir" &&
128 test_when_finished "git rebase --abort ||:" &&
129 git checkout primary &&
130 mkdir subdir && (cd subdir &&
132 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
137 test_expect_success
'rebase -i sets work tree properly' '
138 test_when_finished "rm -rf subdir" &&
139 test_when_finished "test_might_fail git rebase --abort" &&
141 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
143 ! grep "/subdir$" actual
146 test_expect_success
'rebase -i with the exec command checks tree cleanness' '
147 git checkout primary &&
150 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
153 test_cmp_rev primary^ HEAD &&
155 git rebase --continue
158 test_expect_success
'rebase -x with empty command fails' '
159 test_when_finished "git rebase --abort ||:" &&
160 test_must_fail env git rebase -x "" @ 2>actual &&
161 test_write_lines "error: empty exec command" >expected &&
162 test_cmp expected actual &&
163 test_must_fail env git rebase -x " " @ 2>actual &&
164 test_cmp expected actual
167 test_expect_success
'rebase -x with newline in command fails' '
168 test_when_finished "git rebase --abort ||:" &&
169 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
170 test_write_lines "error: exec commands cannot contain newlines" \
172 test_cmp expected actual
175 test_expect_success
'rebase -i with exec of inexistent command' '
176 git checkout primary &&
177 test_when_finished "git rebase --abort" &&
180 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
181 git rebase -i HEAD^ >actual 2>&1
183 ! grep "Maybe git-rebase is broken" actual
186 test_expect_success
'implicit interactive rebase does not invoke sequence editor' '
187 test_when_finished "git rebase --abort ||:" &&
188 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
191 test_expect_success
'no changes are a nop' '
192 git checkout branch2 &&
194 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
198 test_expect_success
'test the [branch] option' '
199 git checkout -b dead-end &&
201 git commit -m "stop here" &&
202 git rebase -i F branch2 &&
203 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
204 test_cmp_rev I branch2 &&
208 test_expect_success
'test --onto <branch>' '
209 git checkout -b test-onto branch2 &&
210 git rebase -i --onto branch1 F &&
211 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
212 test_cmp_rev HEAD^ branch1 &&
213 test_cmp_rev I branch2
216 test_expect_success
'rebase on top of a non-conflicting commit' '
217 git checkout branch1 &&
218 git tag original-branch1 &&
219 git rebase -i branch2 &&
220 test file6 = $(git diff --name-only original-branch1) &&
221 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
222 test_cmp_rev I branch2 &&
223 test_cmp_rev I HEAD~2
226 test_expect_success
'reflog for the branch shows state before rebase' '
227 test_cmp_rev branch1@{1} original-branch1
230 test_expect_success
'reflog for the branch shows correct finish message' '
231 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
232 "$(git rev-parse branch2)" >expected &&
233 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
234 test_cmp expected actual
237 test_expect_success
'exchange two commits' '
240 FAKE_LINES="2 1" git rebase -i HEAD~2
242 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
243 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
244 blob1=$(git rev-parse --short HEAD^:file1) &&
245 blob2=$(git rev-parse --short HEAD:file1) &&
246 commit=$(git rev-parse --short HEAD)
249 test_expect_success
'stop on conflicting pick' '
250 cat >expect <<-EOF &&
251 diff --git a/file1 b/file1
252 index $blob1..$blob2 100644
259 cat >expect2 <<-EOF &&
266 git tag new-branch1 &&
267 test_must_fail git rebase -i primary &&
268 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
269 test_cmp expect .git/rebase-merge/patch &&
270 test_cmp expect2 file1 &&
271 test "$(git diff --name-status |
272 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
273 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
274 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
277 test_expect_success
'show conflicted patch' '
278 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
279 grep "show.*REBASE_HEAD" stderr &&
280 # the original stopped-sha1 is abbreviated
281 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
282 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
285 test_expect_success
'abort' '
286 git rebase --abort &&
287 test_cmp_rev new-branch1 HEAD &&
288 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
289 test_path_is_missing .git/rebase-merge
292 test_expect_success
'abort with error when new base cannot be checked out' '
293 git rm --cached file1 &&
294 git commit -m "remove file in base" &&
295 test_must_fail git rebase -i primary > output 2>&1 &&
296 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
298 test_i18ngrep "file1" output &&
299 test_path_is_missing .git/rebase-merge &&
301 git reset --hard HEAD^
304 test_expect_success
'retain authorship' '
308 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
310 git rebase -i --onto primary HEAD^ &&
311 git show HEAD | grep "^Author: Twerp Snog"
314 test_expect_success
'retain authorship w/ conflicts' '
315 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
316 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
318 git reset --hard twerp &&
319 test_commit a conflict a conflict-a &&
320 git reset --hard twerp &&
322 GIT_AUTHOR_NAME=AttributeMe &&
323 export GIT_AUTHOR_NAME &&
324 test_commit b conflict b conflict-b &&
325 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
327 test_must_fail git rebase -i conflict-a &&
328 echo resolved >conflict &&
330 git rebase --continue &&
331 test_cmp_rev conflict-a^0 HEAD^ &&
336 test_expect_success
'squash' '
337 git reset --hard twerp &&
340 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
341 echo "******************************" &&
344 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
345 git rebase -i --onto primary HEAD~2
347 test B = $(cat file7) &&
348 test_cmp_rev HEAD^ primary
351 test_expect_success
'retain authorship when squashing' '
352 git show HEAD | grep "^Author: Twerp Snog"
355 test_expect_success REBASE_P
'-p handles "no changes" gracefully' '
356 HEAD=$(git rev-parse HEAD) &&
357 git rebase -i -p HEAD^ &&
358 git update-index --refresh &&
359 git diff-files --quiet &&
360 git diff-index --quiet --cached HEAD -- &&
361 test $HEAD = $(git rev-parse HEAD)
364 test_expect_failure REBASE_P
'exchange two commits with -p' '
368 FAKE_LINES="2 1" git rebase -i -p HEAD~2
370 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
371 test G = $(git cat-file commit HEAD | sed -ne \$p)
374 test_expect_success REBASE_P
'preserve merges with -p' '
375 git checkout -b to-be-preserved primary^ &&
376 : > unrelated-file &&
377 git add unrelated-file &&
379 git commit -m "unrelated" &&
380 git checkout -b another-branch primary &&
383 git commit -m J file1 &&
385 git merge to-be-preserved &&
388 git commit -m K file1 &&
391 git commit -m L1 file1 &&
392 git checkout HEAD^ &&
393 echo 1 > unrelated-file &&
395 git commit -m L2 unrelated-file &&
397 git merge another-branch &&
400 git commit -m M file1 &&
401 git checkout -b to-be-rebased &&
403 git rebase -i -p --onto branch1 primary &&
404 git update-index --refresh &&
405 git diff-files --quiet &&
406 git diff-index --quiet --cached HEAD -- &&
407 test_cmp_rev HEAD~6 branch1 &&
408 test_cmp_rev HEAD~4^2 to-be-preserved &&
409 test_cmp_rev HEAD^^2^ HEAD^^^ &&
410 test $(git show HEAD~5:file1) = B &&
411 test $(git show HEAD~3:file1) = C &&
412 test $(git show HEAD:file1) = E &&
413 test $(git show HEAD:unrelated-file) = 1
416 test_expect_success REBASE_P
'edit ancestor with -p' '
419 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3
421 echo 2 > unrelated-file &&
423 git commit -m L2-modified --amend unrelated-file &&
424 git rebase --continue &&
425 git update-index --refresh &&
426 git diff-files --quiet &&
427 git diff-index --quiet --cached HEAD -- &&
428 test $(git show HEAD:unrelated-file) = 2
431 test_expect_success
'--continue tries to commit' '
432 git reset --hard D &&
436 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
437 echo resolved > file1 &&
439 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
441 test_cmp_rev HEAD^ new-branch1 &&
442 git show HEAD | grep chouette
445 test_expect_success
'verbose flag is heeded, even after --continue' '
446 git reset --hard primary@{1} &&
448 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
449 echo resolved > file1 &&
451 git rebase --continue > output &&
452 grep "^ file1 | 2 +-$" output
455 test_expect_success
'multi-squash only fires up editor once' '
456 base=$(git rev-parse HEAD~4) &&
459 FAKE_COMMIT_AMEND="ONCE" \
460 FAKE_LINES="1 squash 2 squash 3 squash 4" \
461 EXPECT_HEADER_COUNT=4 \
464 test $base = $(git rev-parse HEAD^) &&
465 test 1 = $(git show | grep ONCE | wc -l)
468 test_expect_success
'multi-fixup does not fire up editor' '
469 git checkout -b multi-fixup E &&
470 base=$(git rev-parse HEAD~4) &&
473 FAKE_COMMIT_AMEND="NEVER" \
474 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
477 test $base = $(git rev-parse HEAD^) &&
478 test 0 = $(git show | grep NEVER | wc -l) &&
479 git checkout @{-1} &&
480 git branch -D multi-fixup
483 test_expect_success
'commit message used after conflict' '
484 git checkout -b conflict-fixup conflict-branch &&
485 base=$(git rev-parse HEAD~4) &&
488 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
489 git rebase -i $base &&
490 echo three > conflict &&
492 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
493 git rebase --continue
495 test $base = $(git rev-parse HEAD^) &&
496 test 1 = $(git show | grep ONCE | wc -l) &&
497 git checkout @{-1} &&
498 git branch -D conflict-fixup
501 test_expect_success
'commit message retained after conflict' '
502 git checkout -b conflict-squash conflict-branch &&
503 base=$(git rev-parse HEAD~4) &&
506 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
507 git rebase -i $base &&
508 echo three > conflict &&
510 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
511 git rebase --continue
513 test $base = $(git rev-parse HEAD^) &&
514 test 2 = $(git show | grep TWICE | wc -l) &&
515 git checkout @{-1} &&
516 git branch -D conflict-squash
519 test_expect_success
'squash and fixup generate correct log messages' '
520 cat >expect-squash-fixup <<-\EOF &&
527 git checkout -b squash-fixup E &&
528 base=$(git rev-parse HEAD~4) &&
531 FAKE_COMMIT_AMEND="ONCE" \
532 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
533 EXPECT_HEADER_COUNT=4 \
536 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
537 test_cmp expect-squash-fixup actual-squash-fixup &&
538 git cat-file commit HEAD@{2} |
539 grep "^# This is a combination of 3 commits\." &&
540 git cat-file commit HEAD@{3} |
541 grep "^# This is a combination of 2 commits\." &&
542 git checkout @{-1} &&
543 git branch -D squash-fixup
546 test_expect_success
'squash ignores comments' '
547 git checkout -b skip-comments E &&
548 base=$(git rev-parse HEAD~4) &&
551 FAKE_COMMIT_AMEND="ONCE" \
552 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
553 EXPECT_HEADER_COUNT=4 \
556 test $base = $(git rev-parse HEAD^) &&
557 test 1 = $(git show | grep ONCE | wc -l) &&
558 git checkout @{-1} &&
559 git branch -D skip-comments
562 test_expect_success
'squash ignores blank lines' '
563 git checkout -b skip-blank-lines E &&
564 base=$(git rev-parse HEAD~4) &&
567 FAKE_COMMIT_AMEND="ONCE" \
568 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
569 EXPECT_HEADER_COUNT=4 \
572 test $base = $(git rev-parse HEAD^) &&
573 test 1 = $(git show | grep ONCE | wc -l) &&
574 git checkout @{-1} &&
575 git branch -D skip-blank-lines
578 test_expect_success
'squash works as expected' '
579 git checkout -b squash-works no-conflict-branch &&
580 one=$(git rev-parse HEAD~3) &&
583 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
585 test $one = $(git rev-parse HEAD~2)
588 test_expect_success
'interrupted squash works as expected' '
589 git checkout -b interrupted-squash conflict-branch &&
590 one=$(git rev-parse HEAD~3) &&
593 test_must_fail env FAKE_LINES="1 squash 3 2" \
596 test_write_lines one two four > conflict &&
598 test_must_fail git rebase --continue &&
599 echo resolved > conflict &&
601 git rebase --continue &&
602 test $one = $(git rev-parse HEAD~2)
605 test_expect_success
'interrupted squash works as expected (case 2)' '
606 git checkout -b interrupted-squash2 conflict-branch &&
607 one=$(git rev-parse HEAD~3) &&
610 test_must_fail env FAKE_LINES="3 squash 1 2" \
613 test_write_lines one four > conflict &&
615 test_must_fail git rebase --continue &&
616 test_write_lines one two four > conflict &&
618 test_must_fail git rebase --continue &&
619 echo resolved > conflict &&
621 git rebase --continue &&
622 test $one = $(git rev-parse HEAD~2)
625 test_expect_success
'--continue tries to commit, even for "edit"' '
626 echo unrelated > file7 &&
629 git commit -m "unrelated change" &&
630 parent=$(git rev-parse HEAD^) &&
634 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
635 echo edited > file7 &&
637 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
639 test edited = $(git show HEAD:file7) &&
640 git show HEAD | grep chouette &&
641 test $parent = $(git rev-parse HEAD^)
644 test_expect_success
'aborted --continue does not squash commits after "edit"' '
645 old=$(git rev-parse HEAD) &&
649 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
650 echo "edited again" > file7 &&
652 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
654 test $old = $(git rev-parse HEAD) &&
658 test_expect_success
'auto-amend only edited commits after "edit"' '
662 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
663 echo "edited again" > file7 &&
665 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
666 echo "and again" > file7 &&
669 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
670 git rebase --continue
675 test_expect_success
'clean error after failed "exec"' '
677 test_when_finished "git rebase --abort || :" &&
680 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
682 echo "edited again" > file7 &&
684 test_must_fail git rebase --continue 2>error &&
685 test_i18ngrep "you have staged changes in your working tree" error
688 test_expect_success
'rebase a detached HEAD' '
689 grandparent=$(git rev-parse HEAD~2) &&
690 git checkout $(git rev-parse HEAD) &&
694 FAKE_LINES="2 1" git rebase -i HEAD~2
696 test $grandparent = $(git rev-parse HEAD~2)
699 test_expect_success
'rebase a commit violating pre-commit' '
701 mkdir -p .git/hooks &&
702 write_script .git/hooks/pre-commit <<-\EOF &&
703 test -z "$(git diff --cached --check)"
705 echo "monde! " >> file1 &&
707 test_must_fail git commit -m doesnt-verify file1 &&
708 git commit -m doesnt-verify --no-verify file1 &&
712 FAKE_LINES=2 git rebase -i HEAD~2
716 test_expect_success
'rebase with a file named HEAD in worktree' '
720 git checkout -b branch3 A &&
723 GIT_AUTHOR_NAME="Squashed Away" &&
724 export GIT_AUTHOR_NAME &&
727 git commit -m "Add head" &&
730 git commit -m "Add body"
735 FAKE_LINES="1 squash 2" git rebase -i @{-1}
737 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
741 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
743 git checkout -b branch4 HEAD &&
744 GIT_EDITOR=: git commit --amend \
745 --author="Somebody else <somebody@else.com>" &&
746 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
747 git rebase -i branch3 &&
748 test_cmp_rev branch3 branch4
752 test_expect_success
'submodule rebase setup' '
756 cd sub && git init && >elif &&
757 git add elif && git commit -m "submodule initial"
762 git commit -m "One" &&
765 git commit -a -m "Two" &&
767 cd sub && echo 3 >elif &&
768 git commit -a -m "submodule second"
771 git commit -a -m "Three changes submodule"
774 test_expect_success
'submodule rebase -i' '
777 FAKE_LINES="1 squash 2 3" git rebase -i A
781 test_expect_success
'submodule conflict setup' '
782 git tag submodule-base &&
783 git checkout HEAD^ &&
785 cd sub && git checkout HEAD^ && echo 4 >elif &&
786 git add elif && git commit -m "submodule conflict"
790 git commit -m "Conflict in submodule" &&
791 git tag submodule-topic
794 test_expect_success
'rebase -i continue with only submodule staged' '
795 test_must_fail git rebase -i submodule-base &&
797 git rebase --continue &&
798 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
801 test_expect_success
'rebase -i continue with unstaged submodule' '
802 git checkout submodule-topic &&
804 test_must_fail git rebase -i submodule-base &&
806 git rebase --continue &&
807 test_cmp_rev submodule-base HEAD
810 test_expect_success
'avoid unnecessary reset' '
811 git checkout primary &&
813 test-tool chmtime =123456789 file3 &&
814 git update-index --refresh &&
815 HEAD=$(git rev-parse HEAD) &&
816 git rebase -i HEAD~4 &&
817 test $HEAD = $(git rev-parse HEAD) &&
818 MTIME=$(test-tool chmtime --get file3) &&
819 test 123456789 = $MTIME
822 test_expect_success
'reword' '
823 git checkout -b reword-branch primary &&
826 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
828 git show HEAD | grep "E changed" &&
829 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
830 test_cmp_rev primary^ HEAD^ &&
831 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
833 git show HEAD^ | grep "D changed" &&
834 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
836 git show HEAD~3 | grep "B changed" &&
837 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
840 git show HEAD~2 | grep "C changed"
843 test_expect_success
'no uncommited changes when rewording the todo list is reloaded' '
845 test_when_finished "git checkout @{-1}" &&
848 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
849 export GIT_SEQUENCE_EDITOR &&
851 FAKE_LINES="reword 1 reword 2" git rebase -i C
853 check_reworded_commits D E
856 test_expect_success
'rebase -i can copy notes' '
857 git config notes.rewrite.rebase true &&
858 git config notes.rewriteRef "refs/notes/*" &&
862 git notes add -m"a note" n3 &&
863 git rebase -i --onto n1 n2 &&
864 test "a note" = "$(git notes show HEAD)"
867 test_expect_success
'rebase -i can copy notes over a fixup' '
868 cat >expect <<-\EOF &&
873 git reset --hard n3 &&
874 git notes add -m"an earlier note" n2 &&
877 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
880 git notes show > output &&
881 test_cmp expect output
884 test_expect_success
'rebase while detaching HEAD' '
885 git symbolic-ref HEAD &&
886 grandparent=$(git rev-parse HEAD~2) &&
890 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
892 test $grandparent = $(git rev-parse HEAD~2) &&
893 test_must_fail git symbolic-ref HEAD
896 test_tick
# Ensure that the rebased commits get a different timestamp.
897 test_expect_success
'always cherry-pick with --no-ff' '
898 git checkout no-ff-branch &&
899 git tag original-no-ff-branch &&
900 git rebase -i --no-ff A &&
903 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
904 git diff HEAD~$p original-no-ff-branch~$p > out &&
905 test_must_be_empty out
907 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
908 git diff HEAD~3 original-no-ff-branch~3 > out &&
909 test_must_be_empty out
912 test_expect_success
'set up commits with funny messages' '
913 git checkout -b funny A &&
916 git commit -a -m "end with slash\\" &&
919 git commit -a -m "something (\000) that looks like octal" &&
922 git commit -a -m "something (\n) that looks like a newline" &&
925 git commit -a -m "another commit"
928 test_expect_success
'rebase-i history with funny messages' '
929 git rev-list A..funny >expect &&
933 FAKE_LINES="1 2 3 4" git rebase -i A
935 git rev-list A.. >actual &&
936 test_cmp expect actual
939 test_expect_success
'prepare for rebase -i --exec' '
940 git checkout primary &&
941 git checkout -b execute &&
942 test_commit one_exec main.txt one_exec &&
943 test_commit two_exec main.txt two_exec &&
944 test_commit three_exec main.txt three_exec
947 test_expect_success
'running "git rebase -i --exec git show HEAD"' '
950 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
951 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
953 git rebase -i HEAD~2 >expect
955 sed -e "1,9d" expect >expected &&
956 test_cmp expected actual
959 test_expect_success
'running "git rebase --exec git show HEAD -i"' '
960 git reset --hard execute &&
963 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
964 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
966 git rebase -i HEAD~2 >expect
968 sed -e "1,9d" expect >expected &&
969 test_cmp expected actual
972 test_expect_success
'running "git rebase -ix git show HEAD"' '
973 git reset --hard execute &&
976 git rebase -ix "git show HEAD" HEAD~2 >actual &&
977 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
979 git rebase -i HEAD~2 >expect
981 sed -e "1,9d" expect >expected &&
982 test_cmp expected actual
986 test_expect_success
'rebase -ix with several <CMD>' '
987 git reset --hard execute &&
990 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
991 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
993 git rebase -i HEAD~2 >expect
995 sed -e "1,9d" expect >expected &&
996 test_cmp expected actual
999 test_expect_success
'rebase -ix with several instances of --exec' '
1000 git reset --hard execute &&
1003 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
1004 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
1005 exec_git_show_HEAD exec_pwd" &&
1006 export FAKE_LINES &&
1007 git rebase -i HEAD~2 >expect
1009 sed -e "1,11d" expect >expected &&
1010 test_cmp expected actual
1013 test_expect_success
'rebase -ix with --autosquash' '
1014 git reset --hard execute &&
1015 git checkout -b autosquash &&
1016 echo second >second.txt &&
1017 git add second.txt &&
1018 git commit -m "fixup! two_exec" &&
1019 echo bis >bis.txt &&
1021 git commit -m "fixup! two_exec" &&
1022 git checkout -b autosquash_actual &&
1023 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
1024 git checkout autosquash &&
1027 git checkout -b autosquash_expected &&
1028 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
1029 export FAKE_LINES &&
1030 git rebase -i HEAD~4 >expect
1032 sed -e "1,13d" expect >expected &&
1033 test_cmp expected actual
1036 test_expect_success
'rebase --exec works without -i ' '
1037 git reset --hard execute &&
1038 rm -rf exec_output &&
1039 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
1040 test_i18ngrep "Successfully rebased and updated" actual &&
1041 test_line_count = 2 exec_output &&
1042 test_path_is_missing invoked_editor
1045 test_expect_success
'rebase -i --exec without <CMD>' '
1046 git reset --hard execute &&
1047 test_must_fail git rebase -i --exec 2>actual &&
1048 test_i18ngrep "requires a value" actual &&
1049 git checkout primary
1052 test_expect_success
'rebase -i --root re-order and drop commits' '
1056 FAKE_LINES="3 1 2 5" git rebase -i --root
1058 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1059 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1060 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
1061 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
1062 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
1065 test_expect_success
'rebase -i --root retain root commit author and message' '
1069 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1072 FAKE_LINES="2" git rebase -i --root
1074 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1075 git cat-file commit HEAD | grep -q "^different author$"
1078 test_expect_success
'rebase -i --root temporary sentinel commit' '
1082 test_must_fail env FAKE_LINES="2" git rebase -i --root
1084 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1088 test_expect_success
'rebase -i --root fixup root commit' '
1092 FAKE_LINES="1 fixup 2" git rebase -i --root
1094 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1095 test B = $(git show HEAD:file1) &&
1096 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1099 test_expect_success
'rebase -i --root reword original root commit' '
1100 test_when_finished "test_might_fail git rebase --abort" &&
1101 git checkout -b reword-original-root-branch primary &&
1104 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1105 git rebase -i --root
1107 git show HEAD^ | grep "A changed" &&
1108 test -z "$(git show -s --format=%p HEAD^)"
1111 test_expect_success
'rebase -i --root reword new root commit' '
1112 test_when_finished "test_might_fail git rebase --abort" &&
1113 git checkout -b reword-now-root-branch primary &&
1116 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1117 git rebase -i --root
1119 git show HEAD^ | grep "C changed" &&
1120 test -z "$(git show -s --format=%p HEAD^)"
1123 test_expect_success
'rebase -i --root when root has untracked file conflict' '
1124 test_when_finished "reset_rebase" &&
1125 git checkout -b failing-root-pick A &&
1128 git commit -m "remove file 1 add file 2" &&
1132 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1135 git rebase --continue &&
1136 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1137 test "$(git rev-list --count HEAD)" = 2
1140 test_expect_success
'rebase -i --root reword root when root has untracked file conflict' '
1141 test_when_finished "reset_rebase" &&
1145 test_must_fail env FAKE_LINES="reword 1 2" \
1146 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1148 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1150 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1151 test "$(git rev-list --count HEAD)" = 2
1154 test_expect_success
'rebase --edit-todo does not work on non-interactive rebase' '
1155 git checkout reword-original-root-branch &&
1157 git checkout conflict-branch &&
1160 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1161 test_must_fail git rebase --edit-todo
1166 test_expect_success
'rebase --edit-todo can be used to modify todo' '
1168 git checkout no-conflict-branch^0 &&
1171 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1172 FAKE_LINES="2 1" git rebase --edit-todo &&
1173 git rebase --continue
1175 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1176 test L = $(git cat-file commit HEAD | sed -ne \$p)
1179 test_expect_success
'rebase -i produces readable reflog' '
1181 git branch -f branch-reflog-test H &&
1182 git rebase -i --onto I F branch-reflog-test &&
1183 cat >expect <<-\EOF &&
1184 rebase (finish): returning to refs/heads/branch-reflog-test
1187 rebase (start): checkout I
1189 git reflog -n4 HEAD |
1190 sed "s/[^:]*: //" >actual &&
1191 test_cmp expect actual
1194 test_expect_success
'rebase -i respects core.commentchar' '
1197 test_config core.commentchar "\\" &&
1198 write_script remove-all-but-first.sh <<-\EOF &&
1199 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1203 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1206 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1209 test_expect_success
'rebase -i respects core.commentchar=auto' '
1210 test_config core.commentchar auto &&
1211 write_script copy-edit-script.sh <<-\EOF &&
1214 test_when_finished "git rebase --abort || :" &&
1216 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1219 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1222 test_expect_success
'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1223 test_when_finished "git branch -D torebase" &&
1224 git checkout -b torebase branch1 &&
1225 upstream=$(git rev-parse ":/J") &&
1226 onto=$(git rev-parse ":/A") &&
1227 git rebase --onto $onto $upstream &&
1228 git reset --hard branch1 &&
1229 git rebase --onto ":/A" ":/J" &&
1230 git checkout branch1
1233 test_expect_success
'rebase -i with --strategy and -X' '
1234 git checkout -b conflict-merge-use-theirs conflict-branch &&
1235 git reset --hard HEAD^ &&
1236 echo five >conflict &&
1238 git commit -a -m "one file conflict" &&
1239 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1240 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1241 test $(cat file1) = Z
1244 test_expect_success
'interrupted rebase -i with --strategy and -X' '
1245 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1246 git reset --hard HEAD^ &&
1248 git add breakpoint &&
1249 git commit -m "breakpoint for interactive mode" &&
1250 echo five >conflict &&
1252 git commit -a -m "one file conflict" &&
1255 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1256 -Xours conflict-branch
1258 git rebase --continue &&
1259 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1260 test $(cat file1) = Z
1263 test_expect_success
'rebase -i error on commits with \ in message' '
1264 current_head=$(git rev-parse HEAD) &&
1265 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1266 test_commit TO-REMOVE will-conflict old-content &&
1267 test_commit "\temp" will-conflict new-content dummy &&
1268 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1269 test_expect_code 1 grep " emp" error
1272 test_expect_success
'short commit ID setup' '
1273 test_when_finished "git checkout primary" &&
1274 git checkout --orphan collide &&
1278 test_commit collide1 collide &&
1279 test_commit --notick collide2 collide &&
1280 test_commit --notick collide3 collide
1284 if test -n "$GIT_TEST_FIND_COLLIDER"
1286 author
="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1287 committer
="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1288 blob
="$(git rev-parse collide2:collide)"
1289 from
="$(git rev-parse collide1^0)"
1290 repl
="commit refs/heads/collider-&\\n"
1291 repl
="${repl}author $author\\ncommitter $committer\\n"
1292 repl
="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1293 repl
="${repl}from $from\\nM 100644 $blob collide\\n"
1294 test_seq
1 32768 |
sed "s|.*|$repl|" >script &&
1295 git fast-import
<script &&
1297 git for-each-ref
>refs
&&
1298 grep "^$(test_oid t3404_collision)" <refs
>matches
&&
1300 test_line_count
-gt 2 matches ||
{
1301 echo "Could not find a collider" >&2
1306 test_expect_success
'short commit ID collide' '
1307 test_oid_cache <<-EOF &&
1308 # collision-related constants
1309 t3404_collision sha1:6bcd
1310 t3404_collision sha256:0161
1311 t3404_collider sha1:ac4f2ee
1312 t3404_collider sha256:16697
1314 test_when_finished "reset_rebase && git checkout primary" &&
1315 git checkout collide &&
1316 colliding_id=$(test_oid t3404_collision) &&
1317 hexsz=$(test_oid hexsz) &&
1318 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1319 test_config core.abbrev 4 &&
1324 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1325 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1326 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1327 grep "^pick $colliding_id " \
1328 .git/rebase-merge/git-rebase-todo.tmp &&
1329 grep "^pick [0-9a-f]\{$hexsz\}" \
1330 .git/rebase-merge/git-rebase-todo &&
1331 grep "^pick [0-9a-f]\{$hexsz\}" \
1332 .git/rebase-merge/git-rebase-todo.backup &&
1333 git rebase --continue
1335 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1336 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1337 test "$collide2" = "$collide3"
1340 test_expect_success
'respect core.abbrev' '
1341 git config core.abbrev 12 &&
1343 set_cat_todo_editor &&
1344 test_must_fail git rebase -i HEAD~4 >todo-list
1346 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1349 test_expect_success
'todo count' '
1350 write_script dump-raw.sh <<-\EOF &&
1354 test_set_editor "$(pwd)/dump-raw.sh" &&
1355 git rebase -i HEAD~4 >actual
1357 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1360 test_expect_success
'rebase -i commits that overwrite untracked files (pick)' '
1361 git checkout --force branch2 &&
1365 FAKE_LINES="edit 1 2" git rebase -i A
1367 test_cmp_rev HEAD F &&
1368 test_path_is_missing file6 &&
1370 test_must_fail git rebase --continue &&
1371 test_cmp_rev HEAD F &&
1373 git rebase --continue &&
1377 test_expect_success
'rebase -i commits that overwrite untracked files (squash)' '
1378 git checkout --force branch2 &&
1380 git tag original-branch2 &&
1383 FAKE_LINES="edit 1 squash 2" git rebase -i A
1385 test_cmp_rev HEAD F &&
1386 test_path_is_missing file6 &&
1388 test_must_fail git rebase --continue &&
1389 test_cmp_rev HEAD F &&
1391 git rebase --continue &&
1392 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1393 git reset --hard original-branch2
1396 test_expect_success
'rebase -i commits that overwrite untracked files (no ff)' '
1397 git checkout --force branch2 &&
1401 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1403 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1404 test_path_is_missing file6 &&
1406 test_must_fail git rebase --continue &&
1407 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1409 git rebase --continue &&
1410 test $(git cat-file commit HEAD | sed -ne \$p) = I
1413 test_expect_success
'rebase --continue removes CHERRY_PICK_HEAD' '
1414 git checkout -b commit-to-skip &&
1417 test_seq 5 | sed "s/$double/&&/" >seq &&
1420 git commit -m seq-$double
1423 git reset --hard HEAD~2 &&
1424 git cherry-pick seq-onto &&
1427 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1429 test -d .git/rebase-merge &&
1430 git rebase --continue &&
1431 git diff --exit-code seq-onto &&
1432 test ! -d .git/rebase-merge &&
1433 test ! -f .git/CHERRY_PICK_HEAD
1436 rebase_setup_and_clean
() {
1437 test_when_finished
"
1438 git checkout primary &&
1439 test_might_fail git branch -D $1 &&
1440 test_might_fail git rebase --abort
1442 git checkout
-b $1 ${2:-primary}
1445 test_expect_success
'drop' '
1446 rebase_setup_and_clean drop-test &&
1449 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1451 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1452 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1453 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1456 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = ignore' '
1457 test_config rebase.missingCommitsCheck ignore &&
1458 rebase_setup_and_clean missing-commit &&
1461 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1463 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1465 "Successfully rebased and updated refs/heads/missing-commit" \
1469 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = warn' '
1470 cat >expect <<-EOF &&
1471 Warning: some commits may have been dropped accidentally.
1472 Dropped commits (newer to older):
1473 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1474 To avoid this message, use "drop" to explicitly remove a commit.
1476 test_config rebase.missingCommitsCheck warn &&
1477 rebase_setup_and_clean missing-commit &&
1480 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1482 head -n4 actual.2 >actual &&
1483 test_cmp expect actual &&
1484 test D = $(git cat-file commit HEAD | sed -ne \$p)
1487 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = error' '
1488 cat >expect <<-EOF &&
1489 Warning: some commits may have been dropped accidentally.
1490 Dropped commits (newer to older):
1491 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1492 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1493 To avoid this message, use "drop" to explicitly remove a commit.
1495 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1496 The possible behaviours are: ignore, warn, error.
1498 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1499 Or you can abort the rebase with '\''git rebase --abort'\''.
1501 test_config rebase.missingCommitsCheck error &&
1502 rebase_setup_and_clean missing-commit &&
1505 test_must_fail env FAKE_LINES="1 2 4" \
1506 git rebase -i --root 2>actual &&
1507 test_cmp expect actual &&
1508 cp .git/rebase-merge/git-rebase-todo.backup \
1509 .git/rebase-merge/git-rebase-todo &&
1510 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1512 git rebase --continue &&
1513 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1514 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1517 test_expect_success
'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1518 test_config rebase.missingCommitsCheck ignore &&
1519 rebase_setup_and_clean missing-commit &&
1522 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1523 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1524 git rebase --continue 2>actual
1526 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1528 "Successfully rebased and updated refs/heads/missing-commit" \
1532 test_expect_success
'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1533 cat >expect <<-EOF &&
1534 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1535 Warning: some commits may have been dropped accidentally.
1536 Dropped commits (newer to older):
1537 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1538 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1539 To avoid this message, use "drop" to explicitly remove a commit.
1541 head -n4 expect >expect.2 &&
1542 tail -n1 expect >>expect.2 &&
1543 tail -n4 expect.2 >expect.3 &&
1544 test_config rebase.missingCommitsCheck warn &&
1545 rebase_setup_and_clean missing-commit &&
1548 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1549 git rebase -i --root &&
1550 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1551 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1552 head -n6 actual.2 >actual &&
1553 test_cmp expect actual &&
1554 cp orig .git/rebase-merge/git-rebase-todo &&
1555 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1556 head -n4 actual.2 >actual &&
1557 test_cmp expect.3 actual &&
1558 git rebase --continue 2>actual
1560 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1562 "Successfully rebased and updated refs/heads/missing-commit" \
1566 test_expect_success
'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1567 cat >expect <<-EOF &&
1568 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1569 Warning: some commits may have been dropped accidentally.
1570 Dropped commits (newer to older):
1571 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1572 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1573 To avoid this message, use "drop" to explicitly remove a commit.
1575 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1576 The possible behaviours are: ignore, warn, error.
1578 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1579 Or you can abort the rebase with '\''git rebase --abort'\''.
1581 tail -n11 expect >expect.2 &&
1582 head -n3 expect.2 >expect.3 &&
1583 tail -n7 expect.2 >>expect.3 &&
1584 test_config rebase.missingCommitsCheck error &&
1585 rebase_setup_and_clean missing-commit &&
1588 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1589 git rebase -i --root &&
1590 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1591 test_must_fail env FAKE_LINES="2 3 4" \
1592 git rebase --edit-todo 2>actual &&
1593 test_cmp expect actual &&
1594 test_must_fail git rebase --continue 2>actual &&
1595 test_cmp expect.2 actual &&
1596 test_must_fail git rebase --edit-todo &&
1597 cp orig .git/rebase-merge/git-rebase-todo &&
1598 test_must_fail env FAKE_LINES="1 2 3 4" \
1599 git rebase --edit-todo 2>actual &&
1600 test_cmp expect.3 actual &&
1601 test_must_fail git rebase --continue 2>actual &&
1602 test_cmp expect.3 actual &&
1603 cp orig .git/rebase-merge/git-rebase-todo &&
1604 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1605 git rebase --continue 2>actual
1607 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1609 "Successfully rebased and updated refs/heads/missing-commit" \
1613 test_expect_success
'rebase.missingCommitsCheck = error after resolving conflicts' '
1614 test_config rebase.missingCommitsCheck error &&
1617 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1619 git rebase --edit-todo &&
1620 test_must_fail git rebase --continue &&
1623 git rebase --continue
1626 test_expect_success
'rebase.missingCommitsCheck = error when editing for a second time' '
1627 test_config rebase.missingCommitsCheck error &&
1630 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1631 cp .git/rebase-merge/git-rebase-todo todo &&
1632 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1633 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1634 git rebase --continue
1638 test_expect_success
'respects rebase.abbreviateCommands with fixup, squash and exec' '
1639 rebase_setup_and_clean abbrevcmd &&
1640 test_commit "first" file1.txt "first line" first &&
1641 test_commit "second" file1.txt "another line" second &&
1642 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1643 test_commit "squash! second" file1.txt "another line here" second_squash &&
1644 cat >expected <<-EOF &&
1645 p $(git rev-list --abbrev-commit -1 first) first
1646 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1648 p $(git rev-list --abbrev-commit -1 second) second
1649 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1652 git checkout abbrevcmd &&
1653 test_config rebase.abbreviateCommands true &&
1655 set_cat_todo_editor &&
1656 test_must_fail git rebase -i --exec "git show HEAD" \
1657 --autosquash primary >actual
1659 test_cmp expected actual
1662 test_expect_success
'static check of bad command' '
1663 rebase_setup_and_clean bad-cmd &&
1666 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1667 git rebase -i --root 2>actual &&
1668 test_i18ngrep "badcmd $(git rev-list --oneline -1 primary~1)" \
1670 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1672 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1674 git rebase --continue &&
1675 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1676 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1679 test_expect_success
'tabs and spaces are accepted in the todolist' '
1680 rebase_setup_and_clean indented-comment &&
1681 write_script add-indent.sh <<-\EOF &&
1683 # Turn single spaces into space/tab mix
1684 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1685 printf "\n\t# comment\n #more\n\t # comment\n"
1690 test_set_editor "$(pwd)/add-indent.sh" &&
1691 git rebase -i HEAD^^^
1693 test E = $(git cat-file commit HEAD | sed -ne \$p)
1696 test_expect_success
'static check of bad SHA-1' '
1697 rebase_setup_and_clean bad-sha &&
1700 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1701 git rebase -i --root 2>actual &&
1702 test_i18ngrep "edit XXXXXXX False commit" actual &&
1703 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1705 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1707 git rebase --continue &&
1708 test E = $(git cat-file commit HEAD | sed -ne \$p)
1711 test_expect_success
'editor saves as CR/LF' '
1712 git checkout -b with-crlf &&
1713 write_script add-crs.sh <<-\EOF &&
1714 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1718 test_set_editor "$(pwd)/add-crs.sh" &&
1723 test_expect_success
'rebase -i --gpg-sign=<key-id>' '
1724 test_when_finished "test_might_fail git rebase --abort" &&
1727 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1730 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1733 test_expect_success
'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1734 test_when_finished "test_might_fail git rebase --abort" &&
1735 test_config commit.gpgsign true &&
1738 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1741 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1744 test_expect_success
'valid author header after --root swap' '
1745 rebase_setup_and_clean author-header no-conflict-branch &&
1746 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1747 git cat-file commit HEAD | grep ^author >expected &&
1750 FAKE_LINES="5 1" git rebase -i --root
1752 git cat-file commit HEAD^ | grep ^author >actual &&
1753 test_cmp expected actual
1756 test_expect_success
'valid author header when author contains single quote' '
1757 rebase_setup_and_clean author-header no-conflict-branch &&
1758 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1759 git cat-file commit HEAD | grep ^author >expected &&
1762 FAKE_LINES="2" git rebase -i HEAD~2
1764 git cat-file commit HEAD | grep ^author >actual &&
1765 test_cmp expected actual
1768 test_expect_success
'post-commit hook is called' '
1769 test_when_finished "rm -f .git/hooks/post-commit" &&
1771 mkdir -p .git/hooks &&
1772 write_script .git/hooks/post-commit <<-\EOS &&
1773 git rev-parse HEAD >>actual
1777 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1780 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1782 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1784 test_cmp expect actual
1787 test_expect_success
'correct error message for partial commit after empty pick' '
1788 test_when_finished "git rebase --abort" &&
1791 FAKE_LINES="2 1 1" &&
1792 export FAKE_LINES &&
1793 test_must_fail git rebase -i A D
1796 test_must_fail git commit file1 2>err &&
1797 test_i18ngrep "cannot do a partial commit during a rebase." err
1800 test_expect_success
'correct error message for commit --amend after empty pick' '
1801 test_when_finished "git rebase --abort" &&
1805 export FAKE_LINES &&
1806 test_must_fail git rebase -i A D
1809 test_must_fail git commit -a --amend 2>err &&
1810 test_i18ngrep "middle of a rebase -- cannot amend." err
1813 test_expect_success
'todo has correct onto hash' '
1814 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1815 onto=$(git rev-parse --short HEAD~4) &&
1816 test_i18ngrep "^# Rebase ..* onto $onto" actual
1819 test_expect_success
'ORIG_HEAD is updated correctly' '
1820 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1821 git checkout -b test-orig-head A &&
1822 git commit --allow-empty -m A1 &&
1823 git commit --allow-empty -m A2 &&
1824 git commit --allow-empty -m A3 &&
1825 git commit --allow-empty -m A4 &&
1826 git rebase primary &&
1827 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1830 # This must be the last test in this file
1831 test_expect_success
'$EDITOR and friends are unchanged' '
1832 test_editor_unchanged