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 (master)
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".
30 . "$TEST_DIRECTORY"/lib-rebase.sh
32 # WARNING: Modifications to the initial repository can change the SHA ID used
33 # in the expect2 file for the 'stop on conflicting pick' test.
35 test_expect_success 'setup' '
36 test_commit A file1 &&
37 test_commit B file1 &&
38 test_commit C file2 &&
39 test_commit D file1 &&
40 test_commit E file3 &&
41 git checkout -b branch1 A &&
42 test_commit F file4 &&
43 test_commit G file1 &&
44 test_commit H file5 &&
45 git checkout -b branch2 F &&
46 test_commit I file6 &&
47 git checkout -b conflict-branch A &&
48 test_commit one conflict &&
49 test_commit two conflict &&
50 test_commit three conflict &&
51 test_commit four conflict &&
52 git checkout -b no-conflict-branch A &&
53 test_commit J fileJ &&
54 test_commit K fileK &&
55 test_commit L fileL &&
56 test_commit M fileM &&
57 git checkout -b no-ff-branch A &&
58 test_commit N fileN &&
59 test_commit O fileO &&
63 # "exec" commands are ran with the user shell by default, but this may
64 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
65 # to create a file. Unseting SHELL avoids such non-portable behavior
66 # in tests. It must be exported for it to take effect where needed.
70 test_expect_success 'rebase --keep-empty' '
71 git checkout -b emptybranch master &&
72 git commit --allow-empty -m "empty" &&
73 git rebase --keep-empty -i HEAD~2 &&
74 git log --oneline >actual &&
75 test_line_count = 6 actual
78 test_expect_success 'rebase -i with the exec command' '
79 git checkout master &&
82 FAKE_LINES="1 exec_>touch-one
83 2 exec_>touch-two exec_false exec_>touch-three
84 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
86 test_must_fail git rebase -i A
88 test_path_is_file touch-one &&
89 test_path_is_file touch-two &&
90 test_path_is_missing touch-three " (should have stopped before)" &&
91 test_cmp_rev C HEAD &&
92 git rebase --continue &&
93 test_path_is_file touch-three &&
94 test_path_is_file "touch-file name with spaces" &&
95 test_path_is_file touch-after-semicolon &&
96 test_cmp_rev master HEAD &&
100 test_expect_success 'rebase -i with the exec command runs from tree root' '
101 git checkout master &&
102 mkdir subdir && (cd subdir &&
104 FAKE_LINES="1 exec_>touch-subdir" \
107 test_path_is_file touch-subdir &&
111 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
112 git checkout master &&
114 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
115 test_cmp_rev master^ HEAD &&
117 git rebase --continue
120 test_expect_success 'rebase -i with exec of inexistent command' '
121 git checkout master &&
122 test_when_finished "git rebase --abort" &&
124 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
125 git rebase -i HEAD^ >actual 2>&1 &&
126 ! grep "Maybe git-rebase is broken" actual
129 test_expect_success 'no changes are a nop' '
130 git checkout branch2 &&
133 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
134 test $(git rev-parse I) = $(git rev-parse HEAD)
137 test_expect_success 'test the [branch] option' '
138 git checkout -b dead-end &&
140 git commit -m "stop here" &&
142 git rebase -i F branch2 &&
143 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
144 test $(git rev-parse I) = $(git rev-parse branch2) &&
145 test $(git rev-parse I) = $(git rev-parse HEAD)
148 test_expect_success 'test --onto <branch>' '
149 git checkout -b test-onto branch2 &&
151 git rebase -i --onto branch1 F &&
152 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
153 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
154 test $(git rev-parse I) = $(git rev-parse branch2)
157 test_expect_success 'rebase on top of a non-conflicting commit' '
158 git checkout branch1 &&
159 git tag original-branch1 &&
161 git rebase -i branch2 &&
162 test file6 = $(git diff --name-only original-branch1) &&
163 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
164 test $(git rev-parse I) = $(git rev-parse branch2) &&
165 test $(git rev-parse I) = $(git rev-parse HEAD~2)
168 test_expect_success 'reflog for the branch shows state before rebase' '
169 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
172 test_expect_success 'exchange two commits' '
174 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
175 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
176 test G = $(git cat-file commit HEAD | sed -ne \$p)
180 diff --git a/file1 b/file1
181 index f70f10e..fd79235 100644
197 test_expect_success 'stop on conflicting pick' '
198 git tag new-branch1 &&
200 test_must_fail git rebase -i master &&
201 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
202 test_cmp expect .git/rebase-merge/patch &&
203 test_cmp expect2 file1 &&
204 test "$(git diff --name-status |
205 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
206 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
207 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
210 test_expect_success 'abort' '
211 git rebase --abort &&
212 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
213 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
214 test_path_is_missing .git/rebase-merge
217 test_expect_success 'abort with error when new base cannot be checked out' '
218 git rm --cached file1 &&
219 git commit -m "remove file in base" &&
221 test_must_fail git rebase -i master > output 2>&1 &&
222 grep "The following untracked working tree files would be overwritten by checkout:" \
224 grep "file1" output &&
225 test_path_is_missing .git/rebase-merge &&
226 git reset --hard HEAD^
229 test_expect_success 'retain authorship' '
233 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
236 git rebase -i --onto master HEAD^ &&
237 git show HEAD | grep "^Author: Twerp Snog"
240 test_expect_success 'squash' '
241 git reset --hard twerp &&
244 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
245 echo "******************************" &&
247 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
248 git rebase -i --onto master HEAD~2 &&
249 test B = $(cat file7) &&
250 test $(git rev-parse HEAD^) = $(git rev-parse master)
253 test_expect_success 'retain authorship when squashing' '
254 git show HEAD | grep "^Author: Twerp Snog"
257 test_expect_success '-p handles "no changes" gracefully' '
258 HEAD=$(git rev-parse HEAD) &&
260 git rebase -i -p HEAD^ &&
261 git update-index --refresh &&
262 git diff-files --quiet &&
263 git diff-index --quiet --cached HEAD -- &&
264 test $HEAD = $(git rev-parse HEAD)
267 test_expect_failure 'exchange two commits with -p' '
270 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
271 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
272 test G = $(git cat-file commit HEAD | sed -ne \$p)
275 test_expect_success 'preserve merges with -p' '
276 git checkout -b to-be-preserved master^ &&
277 : > unrelated-file &&
278 git add unrelated-file &&
280 git commit -m "unrelated" &&
281 git checkout -b another-branch master &&
284 git commit -m J file1 &&
286 git merge to-be-preserved &&
289 git commit -m K file1 &&
292 git commit -m L1 file1 &&
293 git checkout HEAD^ &&
294 echo 1 > unrelated-file &&
296 git commit -m L2 unrelated-file &&
298 git merge another-branch &&
301 git commit -m M file1 &&
302 git checkout -b to-be-rebased &&
305 git rebase -i -p --onto branch1 master &&
306 git update-index --refresh &&
307 git diff-files --quiet &&
308 git diff-index --quiet --cached HEAD -- &&
309 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
310 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
311 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
312 test $(git show HEAD~5:file1) = B &&
313 test $(git show HEAD~3:file1) = C &&
314 test $(git show HEAD:file1) = E &&
315 test $(git show HEAD:unrelated-file) = 1
318 test_expect_success 'edit ancestor with -p' '
320 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
321 echo 2 > unrelated-file &&
323 git commit -m L2-modified --amend unrelated-file &&
324 git rebase --continue &&
325 git update-index --refresh &&
326 git diff-files --quiet &&
327 git diff-index --quiet --cached HEAD -- &&
328 test $(git show HEAD:unrelated-file) = 2
331 test_expect_success '--continue tries to commit' '
334 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
335 echo resolved > file1 &&
337 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
338 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
339 git show HEAD | grep chouette
342 test_expect_success 'verbose flag is heeded, even after --continue' '
343 git reset --hard master@{1} &&
346 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
347 echo resolved > file1 &&
349 git rebase --continue > output &&
350 grep "^ file1 | 2 +-$" output
353 test_expect_success 'multi-squash only fires up editor once' '
354 base=$(git rev-parse HEAD~4) &&
356 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
357 EXPECT_HEADER_COUNT=4 \
358 git rebase -i $base &&
359 test $base = $(git rev-parse HEAD^) &&
360 test 1 = $(git show | grep ONCE | wc -l)
363 test_expect_success 'multi-fixup does not fire up editor' '
364 git checkout -b multi-fixup E &&
365 base=$(git rev-parse HEAD~4) &&
367 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
368 git rebase -i $base &&
369 test $base = $(git rev-parse HEAD^) &&
370 test 0 = $(git show | grep NEVER | wc -l) &&
371 git checkout to-be-rebased &&
372 git branch -D multi-fixup
375 test_expect_success 'commit message used after conflict' '
376 git checkout -b conflict-fixup conflict-branch &&
377 base=$(git rev-parse HEAD~4) &&
379 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
380 echo three > conflict &&
382 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
383 git rebase --continue &&
384 test $base = $(git rev-parse HEAD^) &&
385 test 1 = $(git show | grep ONCE | wc -l) &&
386 git checkout to-be-rebased &&
387 git branch -D conflict-fixup
390 test_expect_success 'commit message retained after conflict' '
391 git checkout -b conflict-squash conflict-branch &&
392 base=$(git rev-parse HEAD~4) &&
394 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
395 echo three > conflict &&
397 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
398 git rebase --continue &&
399 test $base = $(git rev-parse HEAD^) &&
400 test 2 = $(git show | grep TWICE | wc -l) &&
401 git checkout to-be-rebased &&
402 git branch -D conflict-squash
405 cat > expect-squash-fixup << EOF
413 test_expect_success 'squash and fixup generate correct log messages' '
414 git checkout -b squash-fixup E &&
415 base=$(git rev-parse HEAD~4) &&
417 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
418 EXPECT_HEADER_COUNT=4 \
419 git rebase -i $base &&
420 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
421 test_cmp expect-squash-fixup actual-squash-fixup &&
422 git checkout to-be-rebased &&
423 git branch -D squash-fixup
426 test_expect_success 'squash ignores comments' '
427 git checkout -b skip-comments E &&
428 base=$(git rev-parse HEAD~4) &&
430 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
431 EXPECT_HEADER_COUNT=4 \
432 git rebase -i $base &&
433 test $base = $(git rev-parse HEAD^) &&
434 test 1 = $(git show | grep ONCE | wc -l) &&
435 git checkout to-be-rebased &&
436 git branch -D skip-comments
439 test_expect_success 'squash ignores blank lines' '
440 git checkout -b skip-blank-lines E &&
441 base=$(git rev-parse HEAD~4) &&
443 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
444 EXPECT_HEADER_COUNT=4 \
445 git rebase -i $base &&
446 test $base = $(git rev-parse HEAD^) &&
447 test 1 = $(git show | grep ONCE | wc -l) &&
448 git checkout to-be-rebased &&
449 git branch -D skip-blank-lines
452 test_expect_success 'squash works as expected' '
453 git checkout -b squash-works no-conflict-branch &&
454 one=$(git rev-parse HEAD~3) &&
456 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
457 git rebase -i HEAD~3 &&
458 test $one = $(git rev-parse HEAD~2)
461 test_expect_success 'interrupted squash works as expected' '
462 git checkout -b interrupted-squash conflict-branch &&
463 one=$(git rev-parse HEAD~3) &&
465 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
466 (echo one; echo two; echo four) > conflict &&
468 test_must_fail git rebase --continue &&
469 echo resolved > conflict &&
471 git rebase --continue &&
472 test $one = $(git rev-parse HEAD~2)
475 test_expect_success 'interrupted squash works as expected (case 2)' '
476 git checkout -b interrupted-squash2 conflict-branch &&
477 one=$(git rev-parse HEAD~3) &&
479 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
480 (echo one; echo four) > conflict &&
482 test_must_fail git rebase --continue &&
483 (echo one; echo two; echo four) > conflict &&
485 test_must_fail git rebase --continue &&
486 echo resolved > conflict &&
488 git rebase --continue &&
489 test $one = $(git rev-parse HEAD~2)
492 test_expect_success '--continue tries to commit, even for "edit"' '
493 echo unrelated > file7 &&
496 git commit -m "unrelated change" &&
497 parent=$(git rev-parse HEAD^) &&
500 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
501 echo edited > file7 &&
503 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
504 test edited = $(git show HEAD:file7) &&
505 git show HEAD | grep chouette &&
506 test $parent = $(git rev-parse HEAD^)
509 test_expect_success 'aborted --continue does not squash commits after "edit"' '
510 old=$(git rev-parse HEAD) &&
513 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
514 echo "edited again" > file7 &&
516 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
517 test $old = $(git rev-parse HEAD) &&
521 test_expect_success 'auto-amend only edited commits after "edit"' '
524 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
525 echo "edited again" > file7 &&
527 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
528 echo "and again" > file7 &&
531 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
535 test_expect_success 'clean error after failed "exec"' '
537 test_when_finished "git rebase --abort || :" &&
539 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
540 echo "edited again" > file7 &&
542 test_must_fail git rebase --continue 2>error &&
543 grep "You have staged changes in your working tree." error
546 test_expect_success 'rebase a detached HEAD' '
547 grandparent=$(git rev-parse HEAD~2) &&
548 git checkout $(git rev-parse HEAD) &&
551 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
552 test $grandparent = $(git rev-parse HEAD~2)
555 test_expect_success 'rebase a commit violating pre-commit' '
557 mkdir -p .git/hooks &&
558 PRE_COMMIT=.git/hooks/pre-commit &&
559 echo "#!/bin/sh" > $PRE_COMMIT &&
560 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
561 chmod a+x $PRE_COMMIT &&
562 echo "monde! " >> file1 &&
564 test_must_fail git commit -m doesnt-verify file1 &&
565 git commit -m doesnt-verify --no-verify file1 &&
568 FAKE_LINES=2 git rebase -i HEAD~2
572 test_expect_success 'rebase with a file named HEAD in worktree' '
576 git checkout -b branch3 A &&
579 GIT_AUTHOR_NAME="Squashed Away" &&
580 export GIT_AUTHOR_NAME &&
583 git commit -m "Add head" &&
586 git commit -m "Add body"
590 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
591 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
595 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
597 git checkout -b branch4 HEAD &&
598 GIT_EDITOR=: git commit --amend \
599 --author="Somebody else <somebody@else.com>" &&
600 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
602 git rebase -i branch3 &&
603 test $(git rev-parse branch3) = $(git rev-parse branch4)
607 test_expect_success 'submodule rebase setup' '
611 cd sub && git init && >elif &&
612 git add elif && git commit -m "submodule initial"
617 git commit -m "One" &&
620 git commit -a -m "Two" &&
622 cd sub && echo 3 >elif &&
623 git commit -a -m "submodule second"
627 git commit -a -m "Three changes submodule"
630 test_expect_success 'submodule rebase -i' '
632 FAKE_LINES="1 squash 2 3" git rebase -i A
635 test_expect_success 'submodule conflict setup' '
636 git tag submodule-base &&
637 git checkout HEAD^ &&
639 cd sub && git checkout HEAD^ && echo 4 >elif &&
640 git add elif && git commit -m "submodule conflict"
644 git commit -m "Conflict in submodule" &&
645 git tag submodule-topic
648 test_expect_success 'rebase -i continue with only submodule staged' '
650 test_must_fail git rebase -i submodule-base &&
652 git rebase --continue &&
653 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
656 test_expect_success 'rebase -i continue with unstaged submodule' '
657 git checkout submodule-topic &&
660 test_must_fail git rebase -i submodule-base &&
662 git rebase --continue &&
663 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
666 test_expect_success 'avoid unnecessary reset' '
667 git checkout master &&
669 test-chmtime =123456789 file3 &&
670 git update-index --refresh &&
671 HEAD=$(git rev-parse HEAD) &&
673 git rebase -i HEAD~4 &&
674 test $HEAD = $(git rev-parse HEAD) &&
675 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
676 test 123456789 = $MTIME
679 test_expect_success 'reword' '
680 git checkout -b reword-branch master &&
682 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
683 git show HEAD | grep "E changed" &&
684 test $(git rev-parse master) != $(git rev-parse HEAD) &&
685 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
686 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
687 git show HEAD^ | grep "D changed" &&
688 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
689 git show HEAD~3 | grep "B changed" &&
690 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
691 git show HEAD~2 | grep "C changed"
694 test_expect_success 'rebase -i can copy notes' '
695 git config notes.rewrite.rebase true &&
696 git config notes.rewriteRef "refs/notes/*" &&
700 git notes add -m"a note" n3 &&
702 git rebase -i --onto n1 n2 &&
703 test "a note" = "$(git notes show HEAD)"
712 test_expect_success 'rebase -i can copy notes over a fixup' '
713 git reset --hard n3 &&
714 git notes add -m"an earlier note" n2 &&
716 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
717 git notes show > output &&
718 test_cmp expect output
721 test_expect_success 'rebase while detaching HEAD' '
722 git symbolic-ref HEAD &&
723 grandparent=$(git rev-parse HEAD~2) &&
726 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
727 test $grandparent = $(git rev-parse HEAD~2) &&
728 test_must_fail git symbolic-ref HEAD
731 test_tick # Ensure that the rebased commits get a different timestamp.
732 test_expect_success 'always cherry-pick with --no-ff' '
733 git checkout no-ff-branch &&
734 git tag original-no-ff-branch &&
736 git rebase -i --no-ff A &&
740 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
741 git diff HEAD~$p original-no-ff-branch~$p > out &&
744 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
745 git diff HEAD~3 original-no-ff-branch~3 > out &&
749 test_expect_success 'set up commits with funny messages' '
750 git checkout -b funny A &&
753 git commit -a -m "end with slash\\" &&
756 git commit -a -m "something (\000) that looks like octal" &&
759 git commit -a -m "something (\n) that looks like a newline" &&
762 git commit -a -m "another commit"
765 test_expect_success 'rebase-i history with funny messages' '
766 git rev-list A..funny >expect &&
769 FAKE_LINES="1 2 3 4" git rebase -i A &&
770 git rev-list A.. >actual &&
771 test_cmp expect actual
774 test_expect_success 'prepare for rebase -i --exec' '
775 git checkout master &&
776 git checkout -b execute &&
777 test_commit one_exec main.txt one_exec &&
778 test_commit two_exec main.txt two_exec &&
779 test_commit three_exec main.txt three_exec
782 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
784 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
786 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
788 git rebase -i HEAD~2 >expect
790 sed -e "1,9d" expect >expected &&
791 test_cmp expected actual
794 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
795 git reset --hard execute &&
797 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
799 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
801 git rebase -i HEAD~2 >expect
803 sed -e "1,9d" expect >expected &&
804 test_cmp expected actual
807 test_expect_success 'running "git rebase -ix git show HEAD"' '
808 git reset --hard execute &&
810 git rebase -ix "git show HEAD" HEAD~2 >actual &&
812 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
814 git rebase -i HEAD~2 >expect
816 sed -e "1,9d" expect >expected &&
817 test_cmp expected actual
821 test_expect_success 'rebase -ix with several <CMD>' '
822 git reset --hard execute &&
824 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
826 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
828 git rebase -i HEAD~2 >expect
830 sed -e "1,9d" expect >expected &&
831 test_cmp expected actual
834 test_expect_success 'rebase -ix with several instances of --exec' '
835 git reset --hard execute &&
837 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
839 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
840 exec_git_show_HEAD exec_pwd" &&
842 git rebase -i HEAD~2 >expect
844 sed -e "1,11d" expect >expected &&
845 test_cmp expected actual
848 test_expect_success 'rebase -ix with --autosquash' '
849 git reset --hard execute &&
850 git checkout -b autosquash &&
851 echo second >second.txt &&
852 git add second.txt &&
853 git commit -m "fixup! two_exec" &&
856 git commit -m "fixup! two_exec" &&
859 git checkout -b autosquash_actual &&
860 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
862 git checkout autosquash &&
864 git checkout -b autosquash_expected &&
865 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
867 git rebase -i HEAD~4 >expect
869 sed -e "1,13d" expect >expected &&
870 test_cmp expected actual
873 test_expect_success 'rebase --exec works without -i ' '
874 git reset --hard execute &&
875 rm -rf exec_output &&
876 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
877 test_i18ngrep "Successfully rebased and updated" actual &&
878 test_line_count = 2 exec_output &&
879 test_path_is_missing invoked_editor
882 test_expect_success 'rebase -i --exec without <CMD>' '
883 git reset --hard execute &&
885 test_must_fail git rebase -i --exec 2>tmp &&
886 sed -e "1d" tmp >actual &&
887 test_must_fail git rebase -h >expected &&
888 test_cmp expected actual &&
892 test_expect_success 'rebase -i --root re-order and drop commits' '
895 FAKE_LINES="3 1 2 5" git rebase -i --root &&
896 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
897 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
898 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
899 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
900 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
903 test_expect_success 'rebase -i --root retain root commit author and message' '
907 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
909 FAKE_LINES="2" git rebase -i --root &&
910 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
911 git cat-file commit HEAD | grep -q "^different author$"
914 test_expect_success 'rebase -i --root temporary sentinel commit' '
917 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
918 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
922 test_expect_success 'rebase -i --root fixup root commit' '
925 FAKE_LINES="1 fixup 2" git rebase -i --root &&
926 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
927 test B = $(git show HEAD:file1) &&
928 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
931 test_expect_success 'rebase --edit-todo does not works on non-interactive rebase' '
933 git checkout conflict-branch &&
935 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
936 test_must_fail git rebase --edit-todo &&
940 test_expect_success 'rebase --edit-todo can be used to modify todo' '
942 git checkout no-conflict-branch^0 &&
944 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
945 FAKE_LINES="2 1" git rebase --edit-todo &&
946 git rebase --continue &&
947 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
948 test L = $(git cat-file commit HEAD | sed -ne \$p)
951 test_expect_success 'rebase -i produces readable reflog' '
953 git branch -f branch-reflog-test H &&
955 git rebase -i --onto I F branch-reflog-test &&
956 cat >expect <<-\EOF &&
957 rebase -i (finish): returning to refs/heads/branch-reflog-test
960 rebase -i (start): checkout I
962 git reflog -n4 HEAD |
963 sed "s/[^:]*: //" >actual &&
964 test_cmp expect actual
967 test_expect_success 'rebase -i respects core.commentchar' '
970 test_config core.commentchar "\\" &&
971 write_script remove-all-but-first.sh <<-\EOF &&
972 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
975 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
977 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
980 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
981 test_when_finished "git branch -D torebase" &&
982 git checkout -b torebase branch1 &&
983 upstream=$(git rev-parse ":/J") &&
984 onto=$(git rev-parse ":/A") &&
985 git rebase --onto $onto $upstream &&
986 git reset --hard branch1 &&
987 git rebase --onto ":/A" ":/J" &&
991 test_expect_success 'rebase -i with --strategy and -X' '
992 git checkout -b conflict-merge-use-theirs conflict-branch &&
993 git reset --hard HEAD^ &&
994 echo five >conflict &&
996 git commit -a -m "one file conflict" &&
997 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
998 test $(git show conflict-branch:conflict) = $(cat conflict) &&
999 test $(cat file1) = Z
1002 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1003 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1004 git reset --hard HEAD^ &&
1006 git add breakpoint &&
1007 git commit -m "breakpoint for interactive mode" &&
1008 echo five >conflict &&
1010 git commit -a -m "one file conflict" &&
1012 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1013 git rebase --continue &&
1014 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1015 test $(cat file1) = Z
1018 test_expect_success 'rebase -i error on commits with \ in message' '
1019 current_head=$(git rev-parse HEAD) &&
1020 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1021 test_commit TO-REMOVE will-conflict old-content &&
1022 test_commit "\temp" will-conflict new-content dummy &&
1023 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1024 test_expect_code 1 grep " emp" error
1027 test_expect_success 'short SHA-1 setup' '
1028 test_when_finished "git checkout master" &&
1029 git checkout --orphan collide &&
1033 test_commit collide1 collide &&
1034 test_commit --notick collide2 collide &&
1035 test_commit --notick collide3 collide
1039 test_expect_success 'short SHA-1 collide' '
1040 test_when_finished "reset_rebase && git checkout master" &&
1041 git checkout collide &&
1046 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1047 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1051 test_expect_success 'respect core.abbrev' '
1052 git config core.abbrev 12 &&
1053 set_cat_todo_editor &&
1054 test_must_fail git rebase -i HEAD~4 >todo-list &&
1055 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1058 test_expect_success 'todo count' '
1059 write_script dump-raw.sh <<-\EOF &&
1062 test_set_editor "$(pwd)/dump-raw.sh" &&
1063 git rebase -i HEAD~4 >actual &&
1064 grep "^# Rebase ..* onto ..* ([0-9]" actual
1067 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1068 git checkout --force branch2 &&
1071 FAKE_LINES="edit 1 2" git rebase -i A &&
1072 test_cmp_rev HEAD F &&
1073 test_path_is_missing file6 &&
1075 test_must_fail git rebase --continue &&
1076 test_cmp_rev HEAD F &&
1078 git rebase --continue &&
1082 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1083 git checkout --force branch2 &&
1085 git tag original-branch2 &&
1087 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1088 test_cmp_rev HEAD F &&
1089 test_path_is_missing file6 &&
1091 test_must_fail git rebase --continue &&
1092 test_cmp_rev HEAD F &&
1094 git rebase --continue &&
1095 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1096 git reset --hard original-branch2
1099 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1100 git checkout --force branch2 &&
1103 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1104 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1105 test_path_is_missing file6 &&
1107 test_must_fail git rebase --continue &&
1108 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1110 git rebase --continue &&
1111 test $(git cat-file commit HEAD | sed -ne \$p) = I
1114 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1115 git checkout -b commit-to-skip &&
1118 test_seq 5 | sed "s/$double/&&/" >seq &&
1121 git commit -m seq-$double
1124 git reset --hard HEAD~2 &&
1125 git cherry-pick seq-onto &&
1127 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1128 test -d .git/rebase-merge &&
1129 git rebase --continue &&
1130 git diff --exit-code seq-onto &&
1131 test ! -d .git/rebase-merge &&
1132 test ! -f .git/CHERRY_PICK_HEAD
1135 rebase_setup_and_clean () {
1136 test_when_finished "
1137 git checkout master &&
1138 test_might_fail git branch -D $1 &&
1139 test_might_fail git rebase --abort
1141 git checkout -b $1 master
1144 test_expect_success 'drop' '
1145 rebase_setup_and_clean drop-test &&
1147 FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1148 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1149 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1150 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1154 Successfully rebased and updated refs/heads/missing-commit.
1157 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1158 test_config rebase.missingCommitsCheck ignore &&
1159 rebase_setup_and_clean missing-commit &&
1161 FAKE_LINES="1 2 3 4" \
1162 git rebase -i --root 2>actual &&
1163 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1164 test_cmp expect actual
1168 Warning: some commits may have been dropped accidentally.
1169 Dropped commits (newer to older):
1170 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1171 To avoid this message, use "drop" to explicitly remove a commit.
1173 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1174 The possible behaviours are: ignore, warn, error.
1176 Successfully rebased and updated refs/heads/missing-commit.
1179 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1180 test_config rebase.missingCommitsCheck warn &&
1181 rebase_setup_and_clean missing-commit &&
1183 FAKE_LINES="1 2 3 4" \
1184 git rebase -i --root 2>actual &&
1185 test_cmp expect actual &&
1186 test D = $(git cat-file commit HEAD | sed -ne \$p)
1190 Warning: some commits may have been dropped accidentally.
1191 Dropped commits (newer to older):
1192 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1193 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1194 To avoid this message, use "drop" to explicitly remove a commit.
1196 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1197 The possible behaviours are: ignore, warn, error.
1199 You can fix this with 'git rebase --edit-todo'.
1200 Or you can abort the rebase with 'git rebase --abort'.
1203 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1204 test_config rebase.missingCommitsCheck error &&
1205 rebase_setup_and_clean missing-commit &&
1207 test_must_fail env FAKE_LINES="1 2 4" \
1208 git rebase -i --root 2>actual &&
1209 test_cmp expect actual &&
1210 cp .git/rebase-merge/git-rebase-todo.backup \
1211 .git/rebase-merge/git-rebase-todo &&
1212 FAKE_LINES="1 2 drop 3 4 drop 5" \
1213 git rebase --edit-todo &&
1214 git rebase --continue &&
1215 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1216 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1220 Warning: the command isn't recognized in the following line:
1221 - badcmd $(git rev-list --oneline -1 master~1)
1223 You can fix this with 'git rebase --edit-todo'.
1224 Or you can abort the rebase with 'git rebase --abort'.
1227 test_expect_success 'static check of bad command' '
1228 rebase_setup_and_clean bad-cmd &&
1230 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1231 git rebase -i --root 2>actual &&
1232 test_cmp expect actual &&
1233 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1234 git rebase --continue &&
1235 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1236 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1239 test_expect_success 'tabs and spaces are accepted in the todolist' '
1240 rebase_setup_and_clean indented-comment &&
1241 write_script add-indent.sh <<-\EOF &&
1243 # Turn single spaces into space/tab mix
1244 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1245 printf "\n\t# comment\n #more\n\t # comment\n"
1249 test_set_editor "$(pwd)/add-indent.sh" &&
1250 git rebase -i HEAD^^^ &&
1251 test E = $(git cat-file commit HEAD | sed -ne \$p)
1255 Warning: the SHA-1 is missing or isn't a commit in the following line:
1256 - edit XXXXXXX False commit
1258 You can fix this with 'git rebase --edit-todo'.
1259 Or you can abort the rebase with 'git rebase --abort'.
1262 test_expect_success 'static check of bad SHA-1' '
1263 rebase_setup_and_clean bad-sha &&
1265 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1266 git rebase -i --root 2>actual &&
1267 test_cmp expect actual &&
1268 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1269 git rebase --continue &&
1270 test E = $(git cat-file commit HEAD | sed -ne \$p)
1273 test_expect_success 'editor saves as CR/LF' '
1274 git checkout -b with-crlf &&
1275 write_script add-crs.sh <<-\EOF &&
1276 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1280 test_set_editor "$(pwd)/add-crs.sh" &&