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
82 test_expect_success 'rebase -i with empty HEAD' '
84 test_must_fail env FAKE_LINES="1 exec_true" git rebase -i HEAD^ >actual 2>&1 &&
85 test_i18ncmp expect actual
88 test_expect_success 'rebase -i with the exec command' '
89 git checkout master &&
92 FAKE_LINES="1 exec_>touch-one
93 2 exec_>touch-two exec_false exec_>touch-three
94 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
96 test_must_fail git rebase -i A
98 test_path_is_file touch-one &&
99 test_path_is_file touch-two &&
100 test_path_is_missing touch-three " (should have stopped before)" &&
101 test_cmp_rev C HEAD &&
102 git rebase --continue &&
103 test_path_is_file touch-three &&
104 test_path_is_file "touch-file name with spaces" &&
105 test_path_is_file touch-after-semicolon &&
106 test_cmp_rev master HEAD &&
110 test_expect_success 'rebase -i with the exec command runs from tree root' '
111 git checkout master &&
112 mkdir subdir && (cd subdir &&
114 FAKE_LINES="1 exec_>touch-subdir" \
117 test_path_is_file touch-subdir &&
121 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
122 test_when_finished "rm -rf subdir" &&
123 test_when_finished "git rebase --abort ||:" &&
124 git checkout master &&
125 mkdir subdir && (cd subdir &&
127 FAKE_LINES="1 exec_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
132 test_expect_success 'rebase -i sets work tree properly' '
133 test_when_finished "rm -rf subdir" &&
134 test_when_finished "test_might_fail git rebase --abort" &&
136 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
138 ! grep "/subdir$" actual
141 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
142 git checkout master &&
144 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
145 test_cmp_rev master^ HEAD &&
147 git rebase --continue
150 test_expect_success 'rebase -i with exec of inexistent command' '
151 git checkout master &&
152 test_when_finished "git rebase --abort" &&
154 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
155 git rebase -i HEAD^ >actual 2>&1 &&
156 ! grep "Maybe git-rebase is broken" actual
159 test_expect_success 'no changes are a nop' '
160 git checkout branch2 &&
163 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
164 test $(git rev-parse I) = $(git rev-parse HEAD)
167 test_expect_success 'test the [branch] option' '
168 git checkout -b dead-end &&
170 git commit -m "stop here" &&
172 git rebase -i F branch2 &&
173 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
174 test $(git rev-parse I) = $(git rev-parse branch2) &&
175 test $(git rev-parse I) = $(git rev-parse HEAD)
178 test_expect_success 'test --onto <branch>' '
179 git checkout -b test-onto branch2 &&
181 git rebase -i --onto branch1 F &&
182 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
183 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
184 test $(git rev-parse I) = $(git rev-parse branch2)
187 test_expect_success 'rebase on top of a non-conflicting commit' '
188 git checkout branch1 &&
189 git tag original-branch1 &&
191 git rebase -i branch2 &&
192 test file6 = $(git diff --name-only original-branch1) &&
193 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
194 test $(git rev-parse I) = $(git rev-parse branch2) &&
195 test $(git rev-parse I) = $(git rev-parse HEAD~2)
198 test_expect_success 'reflog for the branch shows state before rebase' '
199 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
202 test_expect_success 'reflog for the branch shows correct finish message' '
203 printf "rebase -i (finish): refs/heads/branch1 onto %s\n" \
204 "$(git rev-parse branch2)" >expected &&
205 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
206 test_cmp expected actual
209 test_expect_success 'exchange two commits' '
211 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
212 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
213 test G = $(git cat-file commit HEAD | sed -ne \$p)
217 diff --git a/file1 b/file1
218 index f70f10e..fd79235 100644
234 test_expect_success 'stop on conflicting pick' '
235 git tag new-branch1 &&
237 test_must_fail git rebase -i master &&
238 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
239 test_cmp expect .git/rebase-merge/patch &&
240 test_cmp expect2 file1 &&
241 test "$(git diff --name-status |
242 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
243 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
244 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
247 test_expect_success 'show conflicted patch' '
248 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
249 grep "show.*REBASE_HEAD" stderr &&
250 # the original stopped-sha1 is abbreviated
251 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
252 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
255 test_expect_success 'abort' '
256 git rebase --abort &&
257 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
258 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
259 test_path_is_missing .git/rebase-merge
262 test_expect_success 'abort with error when new base cannot be checked out' '
263 git rm --cached file1 &&
264 git commit -m "remove file in base" &&
266 test_must_fail git rebase -i master > output 2>&1 &&
267 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
269 test_i18ngrep "file1" output &&
270 test_path_is_missing .git/rebase-merge &&
271 git reset --hard HEAD^
274 test_expect_success 'retain authorship' '
278 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
281 git rebase -i --onto master HEAD^ &&
282 git show HEAD | grep "^Author: Twerp Snog"
285 test_expect_success 'retain authorship w/ conflicts' '
286 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
287 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
289 git reset --hard twerp &&
290 test_commit a conflict a conflict-a &&
291 git reset --hard twerp &&
293 GIT_AUTHOR_NAME=AttributeMe &&
294 export GIT_AUTHOR_NAME &&
295 test_commit b conflict b conflict-b &&
296 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
299 test_must_fail git rebase -i conflict-a &&
300 echo resolved >conflict &&
302 git rebase --continue &&
303 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
308 test_expect_success 'squash' '
309 git reset --hard twerp &&
312 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
313 echo "******************************" &&
315 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
316 git rebase -i --onto master HEAD~2 &&
317 test B = $(cat file7) &&
318 test $(git rev-parse HEAD^) = $(git rev-parse master)
321 test_expect_success 'retain authorship when squashing' '
322 git show HEAD | grep "^Author: Twerp Snog"
325 test_expect_success '-p handles "no changes" gracefully' '
326 HEAD=$(git rev-parse HEAD) &&
328 git rebase -i -p HEAD^ &&
329 git update-index --refresh &&
330 git diff-files --quiet &&
331 git diff-index --quiet --cached HEAD -- &&
332 test $HEAD = $(git rev-parse HEAD)
335 test_expect_failure 'exchange two commits with -p' '
338 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
339 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
340 test G = $(git cat-file commit HEAD | sed -ne \$p)
343 test_expect_success 'preserve merges with -p' '
344 git checkout -b to-be-preserved master^ &&
345 : > unrelated-file &&
346 git add unrelated-file &&
348 git commit -m "unrelated" &&
349 git checkout -b another-branch master &&
352 git commit -m J file1 &&
354 git merge to-be-preserved &&
357 git commit -m K file1 &&
360 git commit -m L1 file1 &&
361 git checkout HEAD^ &&
362 echo 1 > unrelated-file &&
364 git commit -m L2 unrelated-file &&
366 git merge another-branch &&
369 git commit -m M file1 &&
370 git checkout -b to-be-rebased &&
373 git rebase -i -p --onto branch1 master &&
374 git update-index --refresh &&
375 git diff-files --quiet &&
376 git diff-index --quiet --cached HEAD -- &&
377 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
378 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
379 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
380 test $(git show HEAD~5:file1) = B &&
381 test $(git show HEAD~3:file1) = C &&
382 test $(git show HEAD:file1) = E &&
383 test $(git show HEAD:unrelated-file) = 1
386 test_expect_success 'edit ancestor with -p' '
388 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
389 echo 2 > unrelated-file &&
391 git commit -m L2-modified --amend unrelated-file &&
392 git rebase --continue &&
393 git update-index --refresh &&
394 git diff-files --quiet &&
395 git diff-index --quiet --cached HEAD -- &&
396 test $(git show HEAD:unrelated-file) = 2
399 test_expect_success '--continue tries to commit' '
402 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
403 echo resolved > file1 &&
405 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
406 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
407 git show HEAD | grep chouette
410 test_expect_success 'verbose flag is heeded, even after --continue' '
411 git reset --hard master@{1} &&
414 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
415 echo resolved > file1 &&
417 git rebase --continue > output &&
418 grep "^ file1 | 2 +-$" output
421 test_expect_success C_LOCALE_OUTPUT 'multi-squash only fires up editor once' '
422 base=$(git rev-parse HEAD~4) &&
424 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
425 EXPECT_HEADER_COUNT=4 \
426 git rebase -i $base &&
427 test $base = $(git rev-parse HEAD^) &&
428 test 1 = $(git show | grep ONCE | wc -l)
431 test_expect_success C_LOCALE_OUTPUT 'multi-fixup does not fire up editor' '
432 git checkout -b multi-fixup E &&
433 base=$(git rev-parse HEAD~4) &&
435 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
436 git rebase -i $base &&
437 test $base = $(git rev-parse HEAD^) &&
438 test 0 = $(git show | grep NEVER | wc -l) &&
439 git checkout to-be-rebased &&
440 git branch -D multi-fixup
443 test_expect_success 'commit message used after conflict' '
444 git checkout -b conflict-fixup conflict-branch &&
445 base=$(git rev-parse HEAD~4) &&
447 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
448 echo three > conflict &&
450 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
451 git rebase --continue &&
452 test $base = $(git rev-parse HEAD^) &&
453 test 1 = $(git show | grep ONCE | wc -l) &&
454 git checkout to-be-rebased &&
455 git branch -D conflict-fixup
458 test_expect_success 'commit message retained after conflict' '
459 git checkout -b conflict-squash conflict-branch &&
460 base=$(git rev-parse HEAD~4) &&
462 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
463 echo three > conflict &&
465 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
466 git rebase --continue &&
467 test $base = $(git rev-parse HEAD^) &&
468 test 2 = $(git show | grep TWICE | wc -l) &&
469 git checkout to-be-rebased &&
470 git branch -D conflict-squash
473 cat > expect-squash-fixup << EOF
481 test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
482 git checkout -b squash-fixup E &&
483 base=$(git rev-parse HEAD~4) &&
485 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
486 EXPECT_HEADER_COUNT=4 \
487 git rebase -i $base &&
488 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
489 test_cmp expect-squash-fixup actual-squash-fixup &&
490 git cat-file commit HEAD@{2} |
491 grep "^# This is a combination of 3 commits\." &&
492 git cat-file commit HEAD@{3} |
493 grep "^# This is a combination of 2 commits\." &&
494 git checkout to-be-rebased &&
495 git branch -D squash-fixup
498 test_expect_success C_LOCALE_OUTPUT 'squash ignores comments' '
499 git checkout -b skip-comments E &&
500 base=$(git rev-parse HEAD~4) &&
502 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
503 EXPECT_HEADER_COUNT=4 \
504 git rebase -i $base &&
505 test $base = $(git rev-parse HEAD^) &&
506 test 1 = $(git show | grep ONCE | wc -l) &&
507 git checkout to-be-rebased &&
508 git branch -D skip-comments
511 test_expect_success C_LOCALE_OUTPUT 'squash ignores blank lines' '
512 git checkout -b skip-blank-lines E &&
513 base=$(git rev-parse HEAD~4) &&
515 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
516 EXPECT_HEADER_COUNT=4 \
517 git rebase -i $base &&
518 test $base = $(git rev-parse HEAD^) &&
519 test 1 = $(git show | grep ONCE | wc -l) &&
520 git checkout to-be-rebased &&
521 git branch -D skip-blank-lines
524 test_expect_success 'squash works as expected' '
525 git checkout -b squash-works no-conflict-branch &&
526 one=$(git rev-parse HEAD~3) &&
528 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
529 git rebase -i HEAD~3 &&
530 test $one = $(git rev-parse HEAD~2)
533 test_expect_success 'interrupted squash works as expected' '
534 git checkout -b interrupted-squash conflict-branch &&
535 one=$(git rev-parse HEAD~3) &&
537 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
538 test_write_lines one two four > conflict &&
540 test_must_fail git rebase --continue &&
541 echo resolved > conflict &&
543 git rebase --continue &&
544 test $one = $(git rev-parse HEAD~2)
547 test_expect_success 'interrupted squash works as expected (case 2)' '
548 git checkout -b interrupted-squash2 conflict-branch &&
549 one=$(git rev-parse HEAD~3) &&
551 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
552 test_write_lines one four > conflict &&
554 test_must_fail git rebase --continue &&
555 test_write_lines one two four > conflict &&
557 test_must_fail git rebase --continue &&
558 echo resolved > conflict &&
560 git rebase --continue &&
561 test $one = $(git rev-parse HEAD~2)
564 test_expect_success '--continue tries to commit, even for "edit"' '
565 echo unrelated > file7 &&
568 git commit -m "unrelated change" &&
569 parent=$(git rev-parse HEAD^) &&
572 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
573 echo edited > file7 &&
575 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
576 test edited = $(git show HEAD:file7) &&
577 git show HEAD | grep chouette &&
578 test $parent = $(git rev-parse HEAD^)
581 test_expect_success 'aborted --continue does not squash commits after "edit"' '
582 old=$(git rev-parse HEAD) &&
585 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
586 echo "edited again" > file7 &&
588 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
589 test $old = $(git rev-parse HEAD) &&
593 test_expect_success 'auto-amend only edited commits after "edit"' '
596 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
597 echo "edited again" > file7 &&
599 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
600 echo "and again" > file7 &&
603 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
607 test_expect_success 'clean error after failed "exec"' '
609 test_when_finished "git rebase --abort || :" &&
611 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
612 echo "edited again" > file7 &&
614 test_must_fail git rebase --continue 2>error &&
615 test_i18ngrep "you have staged changes in your working tree" error
618 test_expect_success 'rebase a detached HEAD' '
619 grandparent=$(git rev-parse HEAD~2) &&
620 git checkout $(git rev-parse HEAD) &&
623 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
624 test $grandparent = $(git rev-parse HEAD~2)
627 test_expect_success 'rebase a commit violating pre-commit' '
629 mkdir -p .git/hooks &&
630 write_script .git/hooks/pre-commit <<-\EOF &&
631 test -z "$(git diff --cached --check)"
633 echo "monde! " >> file1 &&
635 test_must_fail git commit -m doesnt-verify file1 &&
636 git commit -m doesnt-verify --no-verify file1 &&
639 FAKE_LINES=2 git rebase -i HEAD~2
643 test_expect_success 'rebase with a file named HEAD in worktree' '
647 git checkout -b branch3 A &&
650 GIT_AUTHOR_NAME="Squashed Away" &&
651 export GIT_AUTHOR_NAME &&
654 git commit -m "Add head" &&
657 git commit -m "Add body"
661 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
662 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
666 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
668 git checkout -b branch4 HEAD &&
669 GIT_EDITOR=: git commit --amend \
670 --author="Somebody else <somebody@else.com>" &&
671 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
673 git rebase -i branch3 &&
674 test $(git rev-parse branch3) = $(git rev-parse branch4)
678 test_expect_success 'submodule rebase setup' '
682 cd sub && git init && >elif &&
683 git add elif && git commit -m "submodule initial"
688 git commit -m "One" &&
691 git commit -a -m "Two" &&
693 cd sub && echo 3 >elif &&
694 git commit -a -m "submodule second"
698 git commit -a -m "Three changes submodule"
701 test_expect_success 'submodule rebase -i' '
703 FAKE_LINES="1 squash 2 3" git rebase -i A
706 test_expect_success 'submodule conflict setup' '
707 git tag submodule-base &&
708 git checkout HEAD^ &&
710 cd sub && git checkout HEAD^ && echo 4 >elif &&
711 git add elif && git commit -m "submodule conflict"
715 git commit -m "Conflict in submodule" &&
716 git tag submodule-topic
719 test_expect_success 'rebase -i continue with only submodule staged' '
721 test_must_fail git rebase -i submodule-base &&
723 git rebase --continue &&
724 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
727 test_expect_success 'rebase -i continue with unstaged submodule' '
728 git checkout submodule-topic &&
731 test_must_fail git rebase -i submodule-base &&
733 git rebase --continue &&
734 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
737 test_expect_success 'avoid unnecessary reset' '
738 git checkout master &&
740 test-tool chmtime =123456789 file3 &&
741 git update-index --refresh &&
742 HEAD=$(git rev-parse HEAD) &&
744 git rebase -i HEAD~4 &&
745 test $HEAD = $(git rev-parse HEAD) &&
746 MTIME=$(test-tool chmtime --get file3) &&
747 test 123456789 = $MTIME
750 test_expect_success 'reword' '
751 git checkout -b reword-branch master &&
753 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
754 git show HEAD | grep "E changed" &&
755 test $(git rev-parse master) != $(git rev-parse HEAD) &&
756 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
757 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
758 git show HEAD^ | grep "D changed" &&
759 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
760 git show HEAD~3 | grep "B changed" &&
761 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
762 git show HEAD~2 | grep "C changed"
765 test_expect_success 'rebase -i can copy notes' '
766 git config notes.rewrite.rebase true &&
767 git config notes.rewriteRef "refs/notes/*" &&
771 git notes add -m"a note" n3 &&
773 git rebase -i --onto n1 n2 &&
774 test "a note" = "$(git notes show HEAD)"
783 test_expect_success 'rebase -i can copy notes over a fixup' '
784 git reset --hard n3 &&
785 git notes add -m"an earlier note" n2 &&
787 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
788 git notes show > output &&
789 test_cmp expect output
792 test_expect_success 'rebase while detaching HEAD' '
793 git symbolic-ref HEAD &&
794 grandparent=$(git rev-parse HEAD~2) &&
797 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
798 test $grandparent = $(git rev-parse HEAD~2) &&
799 test_must_fail git symbolic-ref HEAD
802 test_tick # Ensure that the rebased commits get a different timestamp.
803 test_expect_success 'always cherry-pick with --no-ff' '
804 git checkout no-ff-branch &&
805 git tag original-no-ff-branch &&
807 git rebase -i --no-ff A &&
810 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
811 git diff HEAD~$p original-no-ff-branch~$p > out &&
812 test_must_be_empty out
814 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
815 git diff HEAD~3 original-no-ff-branch~3 > out &&
816 test_must_be_empty out
819 test_expect_success 'set up commits with funny messages' '
820 git checkout -b funny A &&
823 git commit -a -m "end with slash\\" &&
826 git commit -a -m "something (\000) that looks like octal" &&
829 git commit -a -m "something (\n) that looks like a newline" &&
832 git commit -a -m "another commit"
835 test_expect_success 'rebase-i history with funny messages' '
836 git rev-list A..funny >expect &&
839 FAKE_LINES="1 2 3 4" git rebase -i A &&
840 git rev-list A.. >actual &&
841 test_cmp expect actual
844 test_expect_success 'prepare for rebase -i --exec' '
845 git checkout master &&
846 git checkout -b execute &&
847 test_commit one_exec main.txt one_exec &&
848 test_commit two_exec main.txt two_exec &&
849 test_commit three_exec main.txt three_exec
852 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
854 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
856 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
858 git rebase -i HEAD~2 >expect
860 sed -e "1,9d" expect >expected &&
861 test_cmp expected actual
864 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
865 git reset --hard execute &&
867 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
869 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
871 git rebase -i HEAD~2 >expect
873 sed -e "1,9d" expect >expected &&
874 test_cmp expected actual
877 test_expect_success 'running "git rebase -ix git show HEAD"' '
878 git reset --hard execute &&
880 git rebase -ix "git show HEAD" HEAD~2 >actual &&
882 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
884 git rebase -i HEAD~2 >expect
886 sed -e "1,9d" expect >expected &&
887 test_cmp expected actual
891 test_expect_success 'rebase -ix with several <CMD>' '
892 git reset --hard execute &&
894 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
896 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
898 git rebase -i HEAD~2 >expect
900 sed -e "1,9d" expect >expected &&
901 test_cmp expected actual
904 test_expect_success 'rebase -ix with several instances of --exec' '
905 git reset --hard execute &&
907 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
909 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
910 exec_git_show_HEAD exec_pwd" &&
912 git rebase -i HEAD~2 >expect
914 sed -e "1,11d" expect >expected &&
915 test_cmp expected actual
918 test_expect_success C_LOCALE_OUTPUT 'rebase -ix with --autosquash' '
919 git reset --hard execute &&
920 git checkout -b autosquash &&
921 echo second >second.txt &&
922 git add second.txt &&
923 git commit -m "fixup! two_exec" &&
926 git commit -m "fixup! two_exec" &&
929 git checkout -b autosquash_actual &&
930 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
932 git checkout autosquash &&
934 git checkout -b autosquash_expected &&
935 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
937 git rebase -i HEAD~4 >expect
939 sed -e "1,13d" expect >expected &&
940 test_cmp expected actual
943 test_expect_success 'rebase --exec works without -i ' '
944 git reset --hard execute &&
945 rm -rf exec_output &&
946 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
947 test_i18ngrep "Successfully rebased and updated" actual &&
948 test_line_count = 2 exec_output &&
949 test_path_is_missing invoked_editor
952 test_expect_success 'rebase -i --exec without <CMD>' '
953 git reset --hard execute &&
955 test_must_fail git rebase -i --exec 2>actual &&
956 test_i18ngrep "requires a value" actual &&
960 test_expect_success 'rebase -i --root re-order and drop commits' '
963 FAKE_LINES="3 1 2 5" git rebase -i --root &&
964 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
965 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
966 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
967 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
968 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
971 test_expect_success 'rebase -i --root retain root commit author and message' '
975 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
977 FAKE_LINES="2" git rebase -i --root &&
978 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
979 git cat-file commit HEAD | grep -q "^different author$"
982 test_expect_success 'rebase -i --root temporary sentinel commit' '
985 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
986 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
990 test_expect_success 'rebase -i --root fixup root commit' '
993 FAKE_LINES="1 fixup 2" git rebase -i --root &&
994 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
995 test B = $(git show HEAD:file1) &&
996 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
999 test_expect_success 'rebase -i --root reword root commit' '
1000 test_when_finished "test_might_fail git rebase --abort" &&
1001 git checkout -b reword-root-branch master &&
1003 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1004 git rebase -i --root &&
1005 git show HEAD^ | grep "A changed" &&
1006 test -z "$(git show -s --format=%p HEAD^)"
1009 test_expect_success 'rebase -i --root when root has untracked file confilct' '
1010 test_when_finished "reset_rebase" &&
1011 git checkout -b failing-root-pick A &&
1014 git commit -m "remove file 1 add file 2" &&
1017 test_must_fail env FAKE_LINES="1 2" git rebase -i --root &&
1019 git rebase --continue &&
1020 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1021 test "$(git rev-list --count HEAD)" = 2
1024 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1025 test_when_finished "reset_rebase" &&
1028 test_must_fail env FAKE_LINES="reword 1 2" \
1029 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1031 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue &&
1032 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1033 test "$(git rev-list --count HEAD)" = 2
1036 test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
1037 git checkout reword-root-branch &&
1039 git checkout conflict-branch &&
1041 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
1042 test_must_fail git rebase --edit-todo &&
1046 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1048 git checkout no-conflict-branch^0 &&
1050 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1051 FAKE_LINES="2 1" git rebase --edit-todo &&
1052 git rebase --continue &&
1053 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1054 test L = $(git cat-file commit HEAD | sed -ne \$p)
1057 test_expect_success 'rebase -i produces readable reflog' '
1059 git branch -f branch-reflog-test H &&
1061 git rebase -i --onto I F branch-reflog-test &&
1062 cat >expect <<-\EOF &&
1063 rebase -i (finish): returning to refs/heads/branch-reflog-test
1066 rebase -i (start): checkout I
1068 git reflog -n4 HEAD |
1069 sed "s/[^:]*: //" >actual &&
1070 test_cmp expect actual
1073 test_expect_success 'rebase -i respects core.commentchar' '
1076 test_config core.commentchar "\\" &&
1077 write_script remove-all-but-first.sh <<-\EOF &&
1078 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1081 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1083 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1086 test_expect_success 'rebase -i respects core.commentchar=auto' '
1087 test_config core.commentchar auto &&
1088 write_script copy-edit-script.sh <<-\EOF &&
1091 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1092 test_when_finished "git rebase --abort || :" &&
1093 git rebase -i HEAD^ &&
1094 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1097 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1098 test_when_finished "git branch -D torebase" &&
1099 git checkout -b torebase branch1 &&
1100 upstream=$(git rev-parse ":/J") &&
1101 onto=$(git rev-parse ":/A") &&
1102 git rebase --onto $onto $upstream &&
1103 git reset --hard branch1 &&
1104 git rebase --onto ":/A" ":/J" &&
1105 git checkout branch1
1108 test_expect_success 'rebase -i with --strategy and -X' '
1109 git checkout -b conflict-merge-use-theirs conflict-branch &&
1110 git reset --hard HEAD^ &&
1111 echo five >conflict &&
1113 git commit -a -m "one file conflict" &&
1114 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1115 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1116 test $(cat file1) = Z
1119 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1120 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1121 git reset --hard HEAD^ &&
1123 git add breakpoint &&
1124 git commit -m "breakpoint for interactive mode" &&
1125 echo five >conflict &&
1127 git commit -a -m "one file conflict" &&
1129 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1130 git rebase --continue &&
1131 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1132 test $(cat file1) = Z
1135 test_expect_success 'rebase -i error on commits with \ in message' '
1136 current_head=$(git rev-parse HEAD) &&
1137 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1138 test_commit TO-REMOVE will-conflict old-content &&
1139 test_commit "\temp" will-conflict new-content dummy &&
1140 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1141 test_expect_code 1 grep " emp" error
1144 test_expect_success 'short SHA-1 setup' '
1145 test_when_finished "git checkout master" &&
1146 git checkout --orphan collide &&
1150 test_commit collide1 collide &&
1151 test_commit --notick collide2 collide &&
1152 test_commit --notick collide3 collide
1156 test_expect_success 'short SHA-1 collide' '
1157 test_when_finished "reset_rebase && git checkout master" &&
1158 git checkout collide &&
1163 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1164 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1168 test_expect_success 'respect core.abbrev' '
1169 git config core.abbrev 12 &&
1170 set_cat_todo_editor &&
1171 test_must_fail git rebase -i HEAD~4 >todo-list &&
1172 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1175 test_expect_success 'todo count' '
1176 write_script dump-raw.sh <<-\EOF &&
1179 test_set_editor "$(pwd)/dump-raw.sh" &&
1180 git rebase -i HEAD~4 >actual &&
1181 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1184 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1185 git checkout --force branch2 &&
1188 FAKE_LINES="edit 1 2" git rebase -i A &&
1189 test_cmp_rev HEAD F &&
1190 test_path_is_missing file6 &&
1192 test_must_fail git rebase --continue &&
1193 test_cmp_rev HEAD F &&
1195 git rebase --continue &&
1199 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1200 git checkout --force branch2 &&
1202 git tag original-branch2 &&
1204 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1205 test_cmp_rev HEAD F &&
1206 test_path_is_missing file6 &&
1208 test_must_fail git rebase --continue &&
1209 test_cmp_rev HEAD F &&
1211 git rebase --continue &&
1212 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1213 git reset --hard original-branch2
1216 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1217 git checkout --force branch2 &&
1220 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1221 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1222 test_path_is_missing file6 &&
1224 test_must_fail git rebase --continue &&
1225 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1227 git rebase --continue &&
1228 test $(git cat-file commit HEAD | sed -ne \$p) = I
1231 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1232 git checkout -b commit-to-skip &&
1235 test_seq 5 | sed "s/$double/&&/" >seq &&
1238 git commit -m seq-$double
1241 git reset --hard HEAD~2 &&
1242 git cherry-pick seq-onto &&
1244 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1245 test -d .git/rebase-merge &&
1246 git rebase --continue &&
1247 git diff --exit-code seq-onto &&
1248 test ! -d .git/rebase-merge &&
1249 test ! -f .git/CHERRY_PICK_HEAD
1252 rebase_setup_and_clean () {
1253 test_when_finished "
1254 git checkout master &&
1255 test_might_fail git branch -D $1 &&
1256 test_might_fail git rebase --abort
1258 git checkout -b $1 ${2:-master}
1261 test_expect_success 'drop' '
1262 rebase_setup_and_clean drop-test &&
1264 FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1265 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1266 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1267 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1270 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1271 test_config rebase.missingCommitsCheck ignore &&
1272 rebase_setup_and_clean missing-commit &&
1274 FAKE_LINES="1 2 3 4" \
1275 git rebase -i --root 2>actual &&
1276 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1278 "Successfully rebased and updated refs/heads/missing-commit" \
1283 Warning: some commits may have been dropped accidentally.
1284 Dropped commits (newer to older):
1285 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1286 To avoid this message, use "drop" to explicitly remove a commit.
1288 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1289 The possible behaviours are: ignore, warn, error.
1295 Successfully rebased and updated refs/heads/missing-commit.
1302 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1303 test_config rebase.missingCommitsCheck warn &&
1304 rebase_setup_and_clean missing-commit &&
1306 FAKE_LINES="1 2 3 4" \
1307 git rebase -i --root 2>actual.2 &&
1308 cr_to_nl <actual.2 >actual &&
1309 test_i18ncmp expect actual &&
1310 test D = $(git cat-file commit HEAD | sed -ne \$p)
1314 Warning: some commits may have been dropped accidentally.
1315 Dropped commits (newer to older):
1316 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1317 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1318 To avoid this message, use "drop" to explicitly remove a commit.
1320 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1321 The possible behaviours are: ignore, warn, error.
1323 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1324 Or you can abort the rebase with 'git rebase --abort'.
1327 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1328 test_config rebase.missingCommitsCheck error &&
1329 rebase_setup_and_clean missing-commit &&
1331 test_must_fail env FAKE_LINES="1 2 4" \
1332 git rebase -i --root 2>actual &&
1333 test_i18ncmp expect actual &&
1334 cp .git/rebase-merge/git-rebase-todo.backup \
1335 .git/rebase-merge/git-rebase-todo &&
1336 FAKE_LINES="1 2 drop 3 4 drop 5" \
1337 git rebase --edit-todo &&
1338 git rebase --continue &&
1339 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1340 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1343 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1344 rebase_setup_and_clean abbrevcmd &&
1345 test_commit "first" file1.txt "first line" first &&
1346 test_commit "second" file1.txt "another line" second &&
1347 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1348 test_commit "squash! second" file1.txt "another line here" second_squash &&
1349 cat >expected <<-EOF &&
1350 p $(git rev-list --abbrev-commit -1 first) first
1351 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1353 p $(git rev-list --abbrev-commit -1 second) second
1354 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1357 git checkout abbrevcmd &&
1358 set_cat_todo_editor &&
1359 test_config rebase.abbreviateCommands true &&
1360 test_must_fail git rebase -i --exec "git show HEAD" \
1361 --autosquash master >actual &&
1362 test_cmp expected actual
1365 test_expect_success 'static check of bad command' '
1366 rebase_setup_and_clean bad-cmd &&
1368 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1369 git rebase -i --root 2>actual &&
1370 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
1371 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1372 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1373 git rebase --continue &&
1374 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1375 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1378 test_expect_success 'tabs and spaces are accepted in the todolist' '
1379 rebase_setup_and_clean indented-comment &&
1380 write_script add-indent.sh <<-\EOF &&
1382 # Turn single spaces into space/tab mix
1383 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1384 printf "\n\t# comment\n #more\n\t # comment\n"
1388 test_set_editor "$(pwd)/add-indent.sh" &&
1389 git rebase -i HEAD^^^ &&
1390 test E = $(git cat-file commit HEAD | sed -ne \$p)
1393 test_expect_success 'static check of bad SHA-1' '
1394 rebase_setup_and_clean bad-sha &&
1396 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1397 git rebase -i --root 2>actual &&
1398 test_i18ngrep "edit XXXXXXX False commit" actual &&
1399 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1400 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1401 git rebase --continue &&
1402 test E = $(git cat-file commit HEAD | sed -ne \$p)
1405 test_expect_success 'editor saves as CR/LF' '
1406 git checkout -b with-crlf &&
1407 write_script add-crs.sh <<-\EOF &&
1408 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1412 test_set_editor "$(pwd)/add-crs.sh" &&
1418 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1419 test_when_finished "test_might_fail git rebase --abort" &&
1421 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1423 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1426 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1427 test_when_finished "test_might_fail git rebase --abort" &&
1428 test_config commit.gpgsign true &&
1430 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1432 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1435 test_expect_success 'valid author header after --root swap' '
1436 rebase_setup_and_clean author-header no-conflict-branch &&
1438 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1439 git cat-file commit HEAD | grep ^author >expected &&
1440 FAKE_LINES="5 1" git rebase -i --root &&
1441 git cat-file commit HEAD^ | grep ^author >actual &&
1442 test_cmp expected actual
1445 test_expect_success 'valid author header when author contains single quote' '
1446 rebase_setup_and_clean author-header no-conflict-branch &&
1448 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1449 git cat-file commit HEAD | grep ^author >expected &&
1450 FAKE_LINES="2" git rebase -i HEAD~2 &&
1451 git cat-file commit HEAD | grep ^author >actual &&
1452 test_cmp expected actual