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 run 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. Unsetting 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 exec allows git commands in subdirs' '
112 test_when_finished "rm -rf subdir" &&
113 test_when_finished "git rebase --abort ||:" &&
114 git checkout master &&
115 mkdir subdir && (cd subdir &&
117 FAKE_LINES="1 exec_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
122 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
123 git checkout master &&
125 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
126 test_cmp_rev master^ HEAD &&
128 git rebase --continue
131 test_expect_success 'rebase -i with exec of inexistent command' '
132 git checkout master &&
133 test_when_finished "git rebase --abort" &&
135 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
136 git rebase -i HEAD^ >actual 2>&1 &&
137 ! grep "Maybe git-rebase is broken" actual
140 test_expect_success 'no changes are a nop' '
141 git checkout branch2 &&
144 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
145 test $(git rev-parse I) = $(git rev-parse HEAD)
148 test_expect_success 'test the [branch] option' '
149 git checkout -b dead-end &&
151 git commit -m "stop here" &&
153 git rebase -i F branch2 &&
154 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
155 test $(git rev-parse I) = $(git rev-parse branch2) &&
156 test $(git rev-parse I) = $(git rev-parse HEAD)
159 test_expect_success 'test --onto <branch>' '
160 git checkout -b test-onto branch2 &&
162 git rebase -i --onto branch1 F &&
163 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
164 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
165 test $(git rev-parse I) = $(git rev-parse branch2)
168 test_expect_success 'rebase on top of a non-conflicting commit' '
169 git checkout branch1 &&
170 git tag original-branch1 &&
172 git rebase -i branch2 &&
173 test file6 = $(git diff --name-only original-branch1) &&
174 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
175 test $(git rev-parse I) = $(git rev-parse branch2) &&
176 test $(git rev-parse I) = $(git rev-parse HEAD~2)
179 test_expect_success 'reflog for the branch shows state before rebase' '
180 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
183 test_expect_success 'exchange two commits' '
185 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
186 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
187 test G = $(git cat-file commit HEAD | sed -ne \$p)
191 diff --git a/file1 b/file1
192 index f70f10e..fd79235 100644
208 test_expect_success 'stop on conflicting pick' '
209 git tag new-branch1 &&
211 test_must_fail git rebase -i master &&
212 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
213 test_cmp expect .git/rebase-merge/patch &&
214 test_cmp expect2 file1 &&
215 test "$(git diff --name-status |
216 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
217 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
218 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
221 test_expect_success 'abort' '
222 git rebase --abort &&
223 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
224 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
225 test_path_is_missing .git/rebase-merge
228 test_expect_success 'abort with error when new base cannot be checked out' '
229 git rm --cached file1 &&
230 git commit -m "remove file in base" &&
232 test_must_fail git rebase -i master > output 2>&1 &&
233 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
235 test_i18ngrep "file1" output &&
236 test_path_is_missing .git/rebase-merge &&
237 git reset --hard HEAD^
240 test_expect_success 'retain authorship' '
244 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
247 git rebase -i --onto master HEAD^ &&
248 git show HEAD | grep "^Author: Twerp Snog"
251 test_expect_success 'retain authorship w/ conflicts' '
252 git reset --hard twerp &&
253 test_commit a conflict a conflict-a &&
254 git reset --hard twerp &&
255 GIT_AUTHOR_NAME=AttributeMe \
256 test_commit b conflict b conflict-b &&
258 test_must_fail git rebase -i conflict-a &&
259 echo resolved >conflict &&
261 git rebase --continue &&
262 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
267 test_expect_success 'squash' '
268 git reset --hard twerp &&
271 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
272 echo "******************************" &&
274 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
275 git rebase -i --onto master HEAD~2 &&
276 test B = $(cat file7) &&
277 test $(git rev-parse HEAD^) = $(git rev-parse master)
280 test_expect_success 'retain authorship when squashing' '
281 git show HEAD | grep "^Author: Twerp Snog"
284 test_expect_success '-p handles "no changes" gracefully' '
285 HEAD=$(git rev-parse HEAD) &&
287 git rebase -i -p HEAD^ &&
288 git update-index --refresh &&
289 git diff-files --quiet &&
290 git diff-index --quiet --cached HEAD -- &&
291 test $HEAD = $(git rev-parse HEAD)
294 test_expect_failure 'exchange two commits with -p' '
297 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
298 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
299 test G = $(git cat-file commit HEAD | sed -ne \$p)
302 test_expect_success 'preserve merges with -p' '
303 git checkout -b to-be-preserved master^ &&
304 : > unrelated-file &&
305 git add unrelated-file &&
307 git commit -m "unrelated" &&
308 git checkout -b another-branch master &&
311 git commit -m J file1 &&
313 git merge to-be-preserved &&
316 git commit -m K file1 &&
319 git commit -m L1 file1 &&
320 git checkout HEAD^ &&
321 echo 1 > unrelated-file &&
323 git commit -m L2 unrelated-file &&
325 git merge another-branch &&
328 git commit -m M file1 &&
329 git checkout -b to-be-rebased &&
332 git rebase -i -p --onto branch1 master &&
333 git update-index --refresh &&
334 git diff-files --quiet &&
335 git diff-index --quiet --cached HEAD -- &&
336 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
337 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
338 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
339 test $(git show HEAD~5:file1) = B &&
340 test $(git show HEAD~3:file1) = C &&
341 test $(git show HEAD:file1) = E &&
342 test $(git show HEAD:unrelated-file) = 1
345 test_expect_success 'edit ancestor with -p' '
347 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
348 echo 2 > unrelated-file &&
350 git commit -m L2-modified --amend unrelated-file &&
351 git rebase --continue &&
352 git update-index --refresh &&
353 git diff-files --quiet &&
354 git diff-index --quiet --cached HEAD -- &&
355 test $(git show HEAD:unrelated-file) = 2
358 test_expect_success '--continue tries to commit' '
361 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
362 echo resolved > file1 &&
364 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
365 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
366 git show HEAD | grep chouette
369 test_expect_success 'verbose flag is heeded, even after --continue' '
370 git reset --hard master@{1} &&
373 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
374 echo resolved > file1 &&
376 git rebase --continue > output &&
377 grep "^ file1 | 2 +-$" output
380 test_expect_success 'multi-squash only fires up editor once' '
381 base=$(git rev-parse HEAD~4) &&
383 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
384 EXPECT_HEADER_COUNT=4 \
385 git rebase -i $base &&
386 test $base = $(git rev-parse HEAD^) &&
387 test 1 = $(git show | grep ONCE | wc -l)
390 test_expect_success 'multi-fixup does not fire up editor' '
391 git checkout -b multi-fixup E &&
392 base=$(git rev-parse HEAD~4) &&
394 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
395 git rebase -i $base &&
396 test $base = $(git rev-parse HEAD^) &&
397 test 0 = $(git show | grep NEVER | wc -l) &&
398 git checkout to-be-rebased &&
399 git branch -D multi-fixup
402 test_expect_success 'commit message used after conflict' '
403 git checkout -b conflict-fixup conflict-branch &&
404 base=$(git rev-parse HEAD~4) &&
406 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
407 echo three > conflict &&
409 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
410 git rebase --continue &&
411 test $base = $(git rev-parse HEAD^) &&
412 test 1 = $(git show | grep ONCE | wc -l) &&
413 git checkout to-be-rebased &&
414 git branch -D conflict-fixup
417 test_expect_success 'commit message retained after conflict' '
418 git checkout -b conflict-squash conflict-branch &&
419 base=$(git rev-parse HEAD~4) &&
421 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
422 echo three > conflict &&
424 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
425 git rebase --continue &&
426 test $base = $(git rev-parse HEAD^) &&
427 test 2 = $(git show | grep TWICE | wc -l) &&
428 git checkout to-be-rebased &&
429 git branch -D conflict-squash
432 cat > expect-squash-fixup << EOF
440 test_expect_success 'squash and fixup generate correct log messages' '
441 git checkout -b squash-fixup E &&
442 base=$(git rev-parse HEAD~4) &&
444 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
445 EXPECT_HEADER_COUNT=4 \
446 git rebase -i $base &&
447 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
448 test_cmp expect-squash-fixup actual-squash-fixup &&
449 git checkout to-be-rebased &&
450 git branch -D squash-fixup
453 test_expect_success 'squash ignores comments' '
454 git checkout -b skip-comments E &&
455 base=$(git rev-parse HEAD~4) &&
457 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
458 EXPECT_HEADER_COUNT=4 \
459 git rebase -i $base &&
460 test $base = $(git rev-parse HEAD^) &&
461 test 1 = $(git show | grep ONCE | wc -l) &&
462 git checkout to-be-rebased &&
463 git branch -D skip-comments
466 test_expect_success 'squash ignores blank lines' '
467 git checkout -b skip-blank-lines E &&
468 base=$(git rev-parse HEAD~4) &&
470 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
471 EXPECT_HEADER_COUNT=4 \
472 git rebase -i $base &&
473 test $base = $(git rev-parse HEAD^) &&
474 test 1 = $(git show | grep ONCE | wc -l) &&
475 git checkout to-be-rebased &&
476 git branch -D skip-blank-lines
479 test_expect_success 'squash works as expected' '
480 git checkout -b squash-works no-conflict-branch &&
481 one=$(git rev-parse HEAD~3) &&
483 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
484 git rebase -i HEAD~3 &&
485 test $one = $(git rev-parse HEAD~2)
488 test_expect_success 'interrupted squash works as expected' '
489 git checkout -b interrupted-squash conflict-branch &&
490 one=$(git rev-parse HEAD~3) &&
492 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
493 (echo one; echo two; echo four) > conflict &&
495 test_must_fail git rebase --continue &&
496 echo resolved > conflict &&
498 git rebase --continue &&
499 test $one = $(git rev-parse HEAD~2)
502 test_expect_success 'interrupted squash works as expected (case 2)' '
503 git checkout -b interrupted-squash2 conflict-branch &&
504 one=$(git rev-parse HEAD~3) &&
506 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
507 (echo one; echo four) > conflict &&
509 test_must_fail git rebase --continue &&
510 (echo one; echo two; echo four) > conflict &&
512 test_must_fail git rebase --continue &&
513 echo resolved > conflict &&
515 git rebase --continue &&
516 test $one = $(git rev-parse HEAD~2)
519 test_expect_success '--continue tries to commit, even for "edit"' '
520 echo unrelated > file7 &&
523 git commit -m "unrelated change" &&
524 parent=$(git rev-parse HEAD^) &&
527 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
528 echo edited > file7 &&
530 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
531 test edited = $(git show HEAD:file7) &&
532 git show HEAD | grep chouette &&
533 test $parent = $(git rev-parse HEAD^)
536 test_expect_success 'aborted --continue does not squash commits after "edit"' '
537 old=$(git rev-parse HEAD) &&
540 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
541 echo "edited again" > file7 &&
543 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
544 test $old = $(git rev-parse HEAD) &&
548 test_expect_success 'auto-amend only edited commits after "edit"' '
551 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
552 echo "edited again" > file7 &&
554 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
555 echo "and again" > file7 &&
558 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
562 test_expect_success 'clean error after failed "exec"' '
564 test_when_finished "git rebase --abort || :" &&
566 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
567 echo "edited again" > file7 &&
569 test_must_fail git rebase --continue 2>error &&
570 test_i18ngrep "you have staged changes in your working tree" error
573 test_expect_success 'rebase a detached HEAD' '
574 grandparent=$(git rev-parse HEAD~2) &&
575 git checkout $(git rev-parse HEAD) &&
578 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
579 test $grandparent = $(git rev-parse HEAD~2)
582 test_expect_success 'rebase a commit violating pre-commit' '
584 mkdir -p .git/hooks &&
585 write_script .git/hooks/pre-commit <<-\EOF &&
586 test -z "$(git diff --cached --check)"
588 echo "monde! " >> file1 &&
590 test_must_fail git commit -m doesnt-verify file1 &&
591 git commit -m doesnt-verify --no-verify file1 &&
594 FAKE_LINES=2 git rebase -i HEAD~2
598 test_expect_success 'rebase with a file named HEAD in worktree' '
602 git checkout -b branch3 A &&
605 GIT_AUTHOR_NAME="Squashed Away" &&
606 export GIT_AUTHOR_NAME &&
609 git commit -m "Add head" &&
612 git commit -m "Add body"
616 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
617 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
621 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
623 git checkout -b branch4 HEAD &&
624 GIT_EDITOR=: git commit --amend \
625 --author="Somebody else <somebody@else.com>" &&
626 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
628 git rebase -i branch3 &&
629 test $(git rev-parse branch3) = $(git rev-parse branch4)
633 test_expect_success 'submodule rebase setup' '
637 cd sub && git init && >elif &&
638 git add elif && git commit -m "submodule initial"
643 git commit -m "One" &&
646 git commit -a -m "Two" &&
648 cd sub && echo 3 >elif &&
649 git commit -a -m "submodule second"
653 git commit -a -m "Three changes submodule"
656 test_expect_success 'submodule rebase -i' '
658 FAKE_LINES="1 squash 2 3" git rebase -i A
661 test_expect_success 'submodule conflict setup' '
662 git tag submodule-base &&
663 git checkout HEAD^ &&
665 cd sub && git checkout HEAD^ && echo 4 >elif &&
666 git add elif && git commit -m "submodule conflict"
670 git commit -m "Conflict in submodule" &&
671 git tag submodule-topic
674 test_expect_success 'rebase -i continue with only submodule staged' '
676 test_must_fail git rebase -i submodule-base &&
678 git rebase --continue &&
679 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
682 test_expect_success 'rebase -i continue with unstaged submodule' '
683 git checkout submodule-topic &&
686 test_must_fail git rebase -i submodule-base &&
688 git rebase --continue &&
689 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
692 test_expect_success 'avoid unnecessary reset' '
693 git checkout master &&
695 test-chmtime =123456789 file3 &&
696 git update-index --refresh &&
697 HEAD=$(git rev-parse HEAD) &&
699 git rebase -i HEAD~4 &&
700 test $HEAD = $(git rev-parse HEAD) &&
701 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
702 test 123456789 = $MTIME
705 test_expect_success 'reword' '
706 git checkout -b reword-branch master &&
708 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
709 git show HEAD | grep "E changed" &&
710 test $(git rev-parse master) != $(git rev-parse HEAD) &&
711 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
712 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
713 git show HEAD^ | grep "D changed" &&
714 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
715 git show HEAD~3 | grep "B changed" &&
716 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
717 git show HEAD~2 | grep "C changed"
720 test_expect_success 'rebase -i can copy notes' '
721 git config notes.rewrite.rebase true &&
722 git config notes.rewriteRef "refs/notes/*" &&
726 git notes add -m"a note" n3 &&
728 git rebase -i --onto n1 n2 &&
729 test "a note" = "$(git notes show HEAD)"
738 test_expect_success 'rebase -i can copy notes over a fixup' '
739 git reset --hard n3 &&
740 git notes add -m"an earlier note" n2 &&
742 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
743 git notes show > output &&
744 test_cmp expect output
747 test_expect_success 'rebase while detaching HEAD' '
748 git symbolic-ref HEAD &&
749 grandparent=$(git rev-parse HEAD~2) &&
752 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
753 test $grandparent = $(git rev-parse HEAD~2) &&
754 test_must_fail git symbolic-ref HEAD
757 test_tick # Ensure that the rebased commits get a different timestamp.
758 test_expect_success 'always cherry-pick with --no-ff' '
759 git checkout no-ff-branch &&
760 git tag original-no-ff-branch &&
762 git rebase -i --no-ff A &&
766 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
767 git diff HEAD~$p original-no-ff-branch~$p > out &&
770 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
771 git diff HEAD~3 original-no-ff-branch~3 > out &&
775 test_expect_success 'set up commits with funny messages' '
776 git checkout -b funny A &&
779 git commit -a -m "end with slash\\" &&
782 git commit -a -m "something (\000) that looks like octal" &&
785 git commit -a -m "something (\n) that looks like a newline" &&
788 git commit -a -m "another commit"
791 test_expect_success 'rebase-i history with funny messages' '
792 git rev-list A..funny >expect &&
795 FAKE_LINES="1 2 3 4" git rebase -i A &&
796 git rev-list A.. >actual &&
797 test_cmp expect actual
800 test_expect_success 'prepare for rebase -i --exec' '
801 git checkout master &&
802 git checkout -b execute &&
803 test_commit one_exec main.txt one_exec &&
804 test_commit two_exec main.txt two_exec &&
805 test_commit three_exec main.txt three_exec
808 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
810 git rebase -i --exec "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
820 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
821 git reset --hard execute &&
823 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
825 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
827 git rebase -i HEAD~2 >expect
829 sed -e "1,9d" expect >expected &&
830 test_cmp expected actual
833 test_expect_success 'running "git rebase -ix git show HEAD"' '
834 git reset --hard execute &&
836 git rebase -ix "git show HEAD" HEAD~2 >actual &&
838 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
840 git rebase -i HEAD~2 >expect
842 sed -e "1,9d" expect >expected &&
843 test_cmp expected actual
847 test_expect_success 'rebase -ix with several <CMD>' '
848 git reset --hard execute &&
850 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
852 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
854 git rebase -i HEAD~2 >expect
856 sed -e "1,9d" expect >expected &&
857 test_cmp expected actual
860 test_expect_success 'rebase -ix with several instances of --exec' '
861 git reset --hard execute &&
863 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
865 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
866 exec_git_show_HEAD exec_pwd" &&
868 git rebase -i HEAD~2 >expect
870 sed -e "1,11d" expect >expected &&
871 test_cmp expected actual
874 test_expect_success 'rebase -ix with --autosquash' '
875 git reset --hard execute &&
876 git checkout -b autosquash &&
877 echo second >second.txt &&
878 git add second.txt &&
879 git commit -m "fixup! two_exec" &&
882 git commit -m "fixup! two_exec" &&
885 git checkout -b autosquash_actual &&
886 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
888 git checkout autosquash &&
890 git checkout -b autosquash_expected &&
891 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
893 git rebase -i HEAD~4 >expect
895 sed -e "1,13d" expect >expected &&
896 test_cmp expected actual
899 test_expect_success 'rebase --exec works without -i ' '
900 git reset --hard execute &&
901 rm -rf exec_output &&
902 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
903 test_i18ngrep "Successfully rebased and updated" actual &&
904 test_line_count = 2 exec_output &&
905 test_path_is_missing invoked_editor
908 test_expect_success 'rebase -i --exec without <CMD>' '
909 git reset --hard execute &&
911 test_must_fail git rebase -i --exec 2>tmp &&
912 sed -e "1d" tmp >actual &&
913 test_must_fail git rebase -h >expected &&
914 test_cmp expected actual &&
918 test_expect_success 'rebase -i --root re-order and drop commits' '
921 FAKE_LINES="3 1 2 5" git rebase -i --root &&
922 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
923 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
924 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
925 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
926 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
929 test_expect_success 'rebase -i --root retain root commit author and message' '
933 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
935 FAKE_LINES="2" git rebase -i --root &&
936 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
937 git cat-file commit HEAD | grep -q "^different author$"
940 test_expect_success 'rebase -i --root temporary sentinel commit' '
943 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
944 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
948 test_expect_success 'rebase -i --root fixup root commit' '
951 FAKE_LINES="1 fixup 2" git rebase -i --root &&
952 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
953 test B = $(git show HEAD:file1) &&
954 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
957 test_expect_success 'rebase --edit-todo does not works on non-interactive rebase' '
959 git checkout conflict-branch &&
961 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
962 test_must_fail git rebase --edit-todo &&
966 test_expect_success 'rebase --edit-todo can be used to modify todo' '
968 git checkout no-conflict-branch^0 &&
970 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
971 FAKE_LINES="2 1" git rebase --edit-todo &&
972 git rebase --continue &&
973 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
974 test L = $(git cat-file commit HEAD | sed -ne \$p)
977 test_expect_success 'rebase -i produces readable reflog' '
979 git branch -f branch-reflog-test H &&
981 git rebase -i --onto I F branch-reflog-test &&
982 cat >expect <<-\EOF &&
983 rebase -i (finish): returning to refs/heads/branch-reflog-test
986 rebase -i (start): checkout I
988 git reflog -n4 HEAD |
989 sed "s/[^:]*: //" >actual &&
990 test_cmp expect actual
993 test_expect_success 'rebase -i respects core.commentchar' '
996 test_config core.commentchar "\\" &&
997 write_script remove-all-but-first.sh <<-\EOF &&
998 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1001 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1003 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1006 test_expect_success 'rebase -i respects core.commentchar=auto' '
1007 test_config core.commentchar auto &&
1008 write_script copy-edit-script.sh <<-\EOF &&
1011 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1012 test_when_finished "git rebase --abort || :" &&
1013 git rebase -i HEAD^ &&
1014 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1017 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1018 test_when_finished "git branch -D torebase" &&
1019 git checkout -b torebase branch1 &&
1020 upstream=$(git rev-parse ":/J") &&
1021 onto=$(git rev-parse ":/A") &&
1022 git rebase --onto $onto $upstream &&
1023 git reset --hard branch1 &&
1024 git rebase --onto ":/A" ":/J" &&
1025 git checkout branch1
1028 test_expect_success 'rebase -i with --strategy and -X' '
1029 git checkout -b conflict-merge-use-theirs conflict-branch &&
1030 git reset --hard HEAD^ &&
1031 echo five >conflict &&
1033 git commit -a -m "one file conflict" &&
1034 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1035 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1036 test $(cat file1) = Z
1039 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1040 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1041 git reset --hard HEAD^ &&
1043 git add breakpoint &&
1044 git commit -m "breakpoint for interactive mode" &&
1045 echo five >conflict &&
1047 git commit -a -m "one file conflict" &&
1049 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1050 git rebase --continue &&
1051 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1052 test $(cat file1) = Z
1055 test_expect_success 'rebase -i error on commits with \ in message' '
1056 current_head=$(git rev-parse HEAD) &&
1057 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1058 test_commit TO-REMOVE will-conflict old-content &&
1059 test_commit "\temp" will-conflict new-content dummy &&
1060 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1061 test_expect_code 1 grep " emp" error
1064 test_expect_success 'short SHA-1 setup' '
1065 test_when_finished "git checkout master" &&
1066 git checkout --orphan collide &&
1070 test_commit collide1 collide &&
1071 test_commit --notick collide2 collide &&
1072 test_commit --notick collide3 collide
1076 test_expect_success 'short SHA-1 collide' '
1077 test_when_finished "reset_rebase && git checkout master" &&
1078 git checkout collide &&
1083 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1084 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1088 test_expect_success 'respect core.abbrev' '
1089 git config core.abbrev 12 &&
1090 set_cat_todo_editor &&
1091 test_must_fail git rebase -i HEAD~4 >todo-list &&
1092 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1095 test_expect_success 'todo count' '
1096 write_script dump-raw.sh <<-\EOF &&
1099 test_set_editor "$(pwd)/dump-raw.sh" &&
1100 git rebase -i HEAD~4 >actual &&
1101 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1104 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1105 git checkout --force branch2 &&
1108 FAKE_LINES="edit 1 2" git rebase -i A &&
1109 test_cmp_rev HEAD F &&
1110 test_path_is_missing file6 &&
1112 test_must_fail git rebase --continue &&
1113 test_cmp_rev HEAD F &&
1115 git rebase --continue &&
1119 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1120 git checkout --force branch2 &&
1122 git tag original-branch2 &&
1124 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1125 test_cmp_rev HEAD F &&
1126 test_path_is_missing file6 &&
1128 test_must_fail git rebase --continue &&
1129 test_cmp_rev HEAD F &&
1131 git rebase --continue &&
1132 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1133 git reset --hard original-branch2
1136 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1137 git checkout --force branch2 &&
1140 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1141 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1142 test_path_is_missing file6 &&
1144 test_must_fail git rebase --continue &&
1145 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1147 git rebase --continue &&
1148 test $(git cat-file commit HEAD | sed -ne \$p) = I
1151 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1152 git checkout -b commit-to-skip &&
1155 test_seq 5 | sed "s/$double/&&/" >seq &&
1158 git commit -m seq-$double
1161 git reset --hard HEAD~2 &&
1162 git cherry-pick seq-onto &&
1164 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1165 test -d .git/rebase-merge &&
1166 git rebase --continue &&
1167 git diff --exit-code seq-onto &&
1168 test ! -d .git/rebase-merge &&
1169 test ! -f .git/CHERRY_PICK_HEAD
1172 rebase_setup_and_clean () {
1173 test_when_finished "
1174 git checkout master &&
1175 test_might_fail git branch -D $1 &&
1176 test_might_fail git rebase --abort
1178 git checkout -b $1 master
1181 test_expect_success 'drop' '
1182 rebase_setup_and_clean drop-test &&
1184 FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1185 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1186 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1187 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1191 Successfully rebased and updated refs/heads/missing-commit.
1194 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1195 test_config rebase.missingCommitsCheck ignore &&
1196 rebase_setup_and_clean missing-commit &&
1198 FAKE_LINES="1 2 3 4" \
1199 git rebase -i --root 2>actual &&
1200 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1201 test_i18ncmp expect actual
1205 Warning: some commits may have been dropped accidentally.
1206 Dropped commits (newer to older):
1207 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1208 To avoid this message, use "drop" to explicitly remove a commit.
1210 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1211 The possible behaviours are: ignore, warn, error.
1213 Successfully rebased and updated refs/heads/missing-commit.
1216 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1217 test_config rebase.missingCommitsCheck warn &&
1218 rebase_setup_and_clean missing-commit &&
1220 FAKE_LINES="1 2 3 4" \
1221 git rebase -i --root 2>actual &&
1222 test_i18ncmp expect actual &&
1223 test D = $(git cat-file commit HEAD | sed -ne \$p)
1227 Warning: some commits may have been dropped accidentally.
1228 Dropped commits (newer to older):
1229 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1230 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1231 To avoid this message, use "drop" to explicitly remove a commit.
1233 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1234 The possible behaviours are: ignore, warn, error.
1236 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1237 Or you can abort the rebase with 'git rebase --abort'.
1240 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1241 test_config rebase.missingCommitsCheck error &&
1242 rebase_setup_and_clean missing-commit &&
1244 test_must_fail env FAKE_LINES="1 2 4" \
1245 git rebase -i --root 2>actual &&
1246 test_i18ncmp expect actual &&
1247 cp .git/rebase-merge/git-rebase-todo.backup \
1248 .git/rebase-merge/git-rebase-todo &&
1249 FAKE_LINES="1 2 drop 3 4 drop 5" \
1250 git rebase --edit-todo &&
1251 git rebase --continue &&
1252 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1253 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1257 Warning: the command isn't recognized in the following line:
1258 - badcmd $(git rev-list --oneline -1 master~1)
1260 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1261 Or you can abort the rebase with 'git rebase --abort'.
1264 test_expect_success 'static check of bad command' '
1265 rebase_setup_and_clean bad-cmd &&
1267 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1268 git rebase -i --root 2>actual &&
1269 test_i18ncmp expect actual &&
1270 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1271 git rebase --continue &&
1272 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1273 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1276 test_expect_success 'tabs and spaces are accepted in the todolist' '
1277 rebase_setup_and_clean indented-comment &&
1278 write_script add-indent.sh <<-\EOF &&
1280 # Turn single spaces into space/tab mix
1281 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1282 printf "\n\t# comment\n #more\n\t # comment\n"
1286 test_set_editor "$(pwd)/add-indent.sh" &&
1287 git rebase -i HEAD^^^ &&
1288 test E = $(git cat-file commit HEAD | sed -ne \$p)
1292 Warning: the SHA-1 is missing or isn't a commit in the following line:
1293 - edit XXXXXXX False commit
1295 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1296 Or you can abort the rebase with 'git rebase --abort'.
1299 test_expect_success 'static check of bad SHA-1' '
1300 rebase_setup_and_clean bad-sha &&
1302 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1303 git rebase -i --root 2>actual &&
1304 test_i18ncmp expect actual &&
1305 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1306 git rebase --continue &&
1307 test E = $(git cat-file commit HEAD | sed -ne \$p)
1310 test_expect_success 'editor saves as CR/LF' '
1311 git checkout -b with-crlf &&
1312 write_script add-crs.sh <<-\EOF &&
1313 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1317 test_set_editor "$(pwd)/add-crs.sh" &&
1323 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1325 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1327 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err