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 with the exec command checks tree cleanness' '
133 git checkout master &&
135 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
136 test_cmp_rev master^ HEAD &&
138 git rebase --continue
141 test_expect_success
'rebase -i with exec of inexistent command' '
142 git checkout master &&
143 test_when_finished "git rebase --abort" &&
145 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
146 git rebase -i HEAD^ >actual 2>&1 &&
147 ! grep "Maybe git-rebase is broken" actual
150 test_expect_success
'no changes are a nop' '
151 git checkout branch2 &&
154 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
155 test $(git rev-parse I) = $(git rev-parse HEAD)
158 test_expect_success
'test the [branch] option' '
159 git checkout -b dead-end &&
161 git commit -m "stop here" &&
163 git rebase -i F branch2 &&
164 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
165 test $(git rev-parse I) = $(git rev-parse branch2) &&
166 test $(git rev-parse I) = $(git rev-parse HEAD)
169 test_expect_success
'test --onto <branch>' '
170 git checkout -b test-onto branch2 &&
172 git rebase -i --onto branch1 F &&
173 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
174 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
175 test $(git rev-parse I) = $(git rev-parse branch2)
178 test_expect_success
'rebase on top of a non-conflicting commit' '
179 git checkout branch1 &&
180 git tag original-branch1 &&
182 git rebase -i branch2 &&
183 test file6 = $(git diff --name-only original-branch1) &&
184 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
185 test $(git rev-parse I) = $(git rev-parse branch2) &&
186 test $(git rev-parse I) = $(git rev-parse HEAD~2)
189 test_expect_success
'reflog for the branch shows state before rebase' '
190 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
193 test_expect_success
'reflog for the branch shows correct finish message' '
194 printf "rebase -i (finish): refs/heads/branch1 onto %s\n" \
195 "$(git rev-parse branch2)" >expected &&
196 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
197 test_cmp expected actual
200 test_expect_success
'exchange two commits' '
202 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
203 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
204 test G = $(git cat-file commit HEAD | sed -ne \$p)
208 diff --git a/file1 b/file1
209 index f70f10e..fd79235 100644
225 test_expect_success
'stop on conflicting pick' '
226 git tag new-branch1 &&
228 test_must_fail git rebase -i master &&
229 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
230 test_cmp expect .git/rebase-merge/patch &&
231 test_cmp expect2 file1 &&
232 test "$(git diff --name-status |
233 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
234 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
235 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
238 test_expect_success
'show conflicted patch' '
239 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
240 grep "show.*REBASE_HEAD" stderr &&
241 # the original stopped-sha1 is abbreviated
242 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
243 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
246 test_expect_success
'abort' '
247 git rebase --abort &&
248 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
249 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
250 test_path_is_missing .git/rebase-merge
253 test_expect_success
'abort with error when new base cannot be checked out' '
254 git rm --cached file1 &&
255 git commit -m "remove file in base" &&
257 test_must_fail git rebase -i master > output 2>&1 &&
258 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
260 test_i18ngrep "file1" output &&
261 test_path_is_missing .git/rebase-merge &&
262 git reset --hard HEAD^
265 test_expect_success
'retain authorship' '
269 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
272 git rebase -i --onto master HEAD^ &&
273 git show HEAD | grep "^Author: Twerp Snog"
276 test_expect_success
'retain authorship w/ conflicts' '
277 git reset --hard twerp &&
278 test_commit a conflict a conflict-a &&
279 git reset --hard twerp &&
280 GIT_AUTHOR_NAME=AttributeMe \
281 test_commit b conflict b conflict-b &&
283 test_must_fail git rebase -i conflict-a &&
284 echo resolved >conflict &&
286 git rebase --continue &&
287 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
292 test_expect_success
'squash' '
293 git reset --hard twerp &&
296 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
297 echo "******************************" &&
299 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
300 git rebase -i --onto master HEAD~2 &&
301 test B = $(cat file7) &&
302 test $(git rev-parse HEAD^) = $(git rev-parse master)
305 test_expect_success
'retain authorship when squashing' '
306 git show HEAD | grep "^Author: Twerp Snog"
309 test_expect_success
'-p handles "no changes" gracefully' '
310 HEAD=$(git rev-parse HEAD) &&
312 git rebase -i -p HEAD^ &&
313 git update-index --refresh &&
314 git diff-files --quiet &&
315 git diff-index --quiet --cached HEAD -- &&
316 test $HEAD = $(git rev-parse HEAD)
319 test_expect_failure
'exchange two commits with -p' '
322 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
323 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
324 test G = $(git cat-file commit HEAD | sed -ne \$p)
327 test_expect_success
'preserve merges with -p' '
328 git checkout -b to-be-preserved master^ &&
329 : > unrelated-file &&
330 git add unrelated-file &&
332 git commit -m "unrelated" &&
333 git checkout -b another-branch master &&
336 git commit -m J file1 &&
338 git merge to-be-preserved &&
341 git commit -m K file1 &&
344 git commit -m L1 file1 &&
345 git checkout HEAD^ &&
346 echo 1 > unrelated-file &&
348 git commit -m L2 unrelated-file &&
350 git merge another-branch &&
353 git commit -m M file1 &&
354 git checkout -b to-be-rebased &&
357 git rebase -i -p --onto branch1 master &&
358 git update-index --refresh &&
359 git diff-files --quiet &&
360 git diff-index --quiet --cached HEAD -- &&
361 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
362 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
363 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
364 test $(git show HEAD~5:file1) = B &&
365 test $(git show HEAD~3:file1) = C &&
366 test $(git show HEAD:file1) = E &&
367 test $(git show HEAD:unrelated-file) = 1
370 test_expect_success
'edit ancestor with -p' '
372 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
373 echo 2 > unrelated-file &&
375 git commit -m L2-modified --amend unrelated-file &&
376 git rebase --continue &&
377 git update-index --refresh &&
378 git diff-files --quiet &&
379 git diff-index --quiet --cached HEAD -- &&
380 test $(git show HEAD:unrelated-file) = 2
383 test_expect_success
'--continue tries to commit' '
386 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
387 echo resolved > file1 &&
389 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
390 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
391 git show HEAD | grep chouette
394 test_expect_success
'verbose flag is heeded, even after --continue' '
395 git reset --hard master@{1} &&
398 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
399 echo resolved > file1 &&
401 git rebase --continue > output &&
402 grep "^ file1 | 2 +-$" output
405 test_expect_success C_LOCALE_OUTPUT
'multi-squash only fires up editor once' '
406 base=$(git rev-parse HEAD~4) &&
408 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
409 EXPECT_HEADER_COUNT=4 \
410 git rebase -i $base &&
411 test $base = $(git rev-parse HEAD^) &&
412 test 1 = $(git show | grep ONCE | wc -l)
415 test_expect_success C_LOCALE_OUTPUT
'multi-fixup does not fire up editor' '
416 git checkout -b multi-fixup E &&
417 base=$(git rev-parse HEAD~4) &&
419 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
420 git rebase -i $base &&
421 test $base = $(git rev-parse HEAD^) &&
422 test 0 = $(git show | grep NEVER | wc -l) &&
423 git checkout to-be-rebased &&
424 git branch -D multi-fixup
427 test_expect_success
'commit message used after conflict' '
428 git checkout -b conflict-fixup conflict-branch &&
429 base=$(git rev-parse HEAD~4) &&
431 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
432 echo three > conflict &&
434 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
435 git rebase --continue &&
436 test $base = $(git rev-parse HEAD^) &&
437 test 1 = $(git show | grep ONCE | wc -l) &&
438 git checkout to-be-rebased &&
439 git branch -D conflict-fixup
442 test_expect_success
'commit message retained after conflict' '
443 git checkout -b conflict-squash conflict-branch &&
444 base=$(git rev-parse HEAD~4) &&
446 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
447 echo three > conflict &&
449 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
450 git rebase --continue &&
451 test $base = $(git rev-parse HEAD^) &&
452 test 2 = $(git show | grep TWICE | wc -l) &&
453 git checkout to-be-rebased &&
454 git branch -D conflict-squash
457 cat > expect-squash-fixup
<< EOF
465 test_expect_success C_LOCALE_OUTPUT
'squash and fixup generate correct log messages' '
466 git checkout -b squash-fixup E &&
467 base=$(git rev-parse HEAD~4) &&
469 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
470 EXPECT_HEADER_COUNT=4 \
471 git rebase -i $base &&
472 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
473 test_cmp expect-squash-fixup actual-squash-fixup &&
474 git cat-file commit HEAD@{2} |
475 grep "^# This is a combination of 3 commits\." &&
476 git cat-file commit HEAD@{3} |
477 grep "^# This is a combination of 2 commits\." &&
478 git checkout to-be-rebased &&
479 git branch -D squash-fixup
482 test_expect_success C_LOCALE_OUTPUT
'squash ignores comments' '
483 git checkout -b skip-comments E &&
484 base=$(git rev-parse HEAD~4) &&
486 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
487 EXPECT_HEADER_COUNT=4 \
488 git rebase -i $base &&
489 test $base = $(git rev-parse HEAD^) &&
490 test 1 = $(git show | grep ONCE | wc -l) &&
491 git checkout to-be-rebased &&
492 git branch -D skip-comments
495 test_expect_success C_LOCALE_OUTPUT
'squash ignores blank lines' '
496 git checkout -b skip-blank-lines E &&
497 base=$(git rev-parse HEAD~4) &&
499 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
500 EXPECT_HEADER_COUNT=4 \
501 git rebase -i $base &&
502 test $base = $(git rev-parse HEAD^) &&
503 test 1 = $(git show | grep ONCE | wc -l) &&
504 git checkout to-be-rebased &&
505 git branch -D skip-blank-lines
508 test_expect_success
'squash works as expected' '
509 git checkout -b squash-works no-conflict-branch &&
510 one=$(git rev-parse HEAD~3) &&
512 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
513 git rebase -i HEAD~3 &&
514 test $one = $(git rev-parse HEAD~2)
517 test_expect_success
'interrupted squash works as expected' '
518 git checkout -b interrupted-squash conflict-branch &&
519 one=$(git rev-parse HEAD~3) &&
521 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
522 (echo one; echo two; echo four) > conflict &&
524 test_must_fail git rebase --continue &&
525 echo resolved > conflict &&
527 git rebase --continue &&
528 test $one = $(git rev-parse HEAD~2)
531 test_expect_success
'interrupted squash works as expected (case 2)' '
532 git checkout -b interrupted-squash2 conflict-branch &&
533 one=$(git rev-parse HEAD~3) &&
535 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
536 (echo one; echo four) > conflict &&
538 test_must_fail git rebase --continue &&
539 (echo one; echo two; echo four) > conflict &&
541 test_must_fail git rebase --continue &&
542 echo resolved > conflict &&
544 git rebase --continue &&
545 test $one = $(git rev-parse HEAD~2)
548 test_expect_success
'--continue tries to commit, even for "edit"' '
549 echo unrelated > file7 &&
552 git commit -m "unrelated change" &&
553 parent=$(git rev-parse HEAD^) &&
556 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
557 echo edited > file7 &&
559 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
560 test edited = $(git show HEAD:file7) &&
561 git show HEAD | grep chouette &&
562 test $parent = $(git rev-parse HEAD^)
565 test_expect_success
'aborted --continue does not squash commits after "edit"' '
566 old=$(git rev-parse HEAD) &&
569 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
570 echo "edited again" > file7 &&
572 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
573 test $old = $(git rev-parse HEAD) &&
577 test_expect_success
'auto-amend only edited commits after "edit"' '
580 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
581 echo "edited again" > file7 &&
583 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
584 echo "and again" > file7 &&
587 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
591 test_expect_success
'clean error after failed "exec"' '
593 test_when_finished "git rebase --abort || :" &&
595 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
596 echo "edited again" > file7 &&
598 test_must_fail git rebase --continue 2>error &&
599 test_i18ngrep "you have staged changes in your working tree" error
602 test_expect_success
'rebase a detached HEAD' '
603 grandparent=$(git rev-parse HEAD~2) &&
604 git checkout $(git rev-parse HEAD) &&
607 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
608 test $grandparent = $(git rev-parse HEAD~2)
611 test_expect_success
'rebase a commit violating pre-commit' '
613 mkdir -p .git/hooks &&
614 write_script .git/hooks/pre-commit <<-\EOF &&
615 test -z "$(git diff --cached --check)"
617 echo "monde! " >> file1 &&
619 test_must_fail git commit -m doesnt-verify file1 &&
620 git commit -m doesnt-verify --no-verify file1 &&
623 FAKE_LINES=2 git rebase -i HEAD~2
627 test_expect_success
'rebase with a file named HEAD in worktree' '
631 git checkout -b branch3 A &&
634 GIT_AUTHOR_NAME="Squashed Away" &&
635 export GIT_AUTHOR_NAME &&
638 git commit -m "Add head" &&
641 git commit -m "Add body"
645 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
646 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
650 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
652 git checkout -b branch4 HEAD &&
653 GIT_EDITOR=: git commit --amend \
654 --author="Somebody else <somebody@else.com>" &&
655 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
657 git rebase -i branch3 &&
658 test $(git rev-parse branch3) = $(git rev-parse branch4)
662 test_expect_success
'submodule rebase setup' '
666 cd sub && git init && >elif &&
667 git add elif && git commit -m "submodule initial"
672 git commit -m "One" &&
675 git commit -a -m "Two" &&
677 cd sub && echo 3 >elif &&
678 git commit -a -m "submodule second"
682 git commit -a -m "Three changes submodule"
685 test_expect_success
'submodule rebase -i' '
687 FAKE_LINES="1 squash 2 3" git rebase -i A
690 test_expect_success
'submodule conflict setup' '
691 git tag submodule-base &&
692 git checkout HEAD^ &&
694 cd sub && git checkout HEAD^ && echo 4 >elif &&
695 git add elif && git commit -m "submodule conflict"
699 git commit -m "Conflict in submodule" &&
700 git tag submodule-topic
703 test_expect_success
'rebase -i continue with only submodule staged' '
705 test_must_fail git rebase -i submodule-base &&
707 git rebase --continue &&
708 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
711 test_expect_success
'rebase -i continue with unstaged submodule' '
712 git checkout submodule-topic &&
715 test_must_fail git rebase -i submodule-base &&
717 git rebase --continue &&
718 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
721 test_expect_success
'avoid unnecessary reset' '
722 git checkout master &&
724 test-tool chmtime =123456789 file3 &&
725 git update-index --refresh &&
726 HEAD=$(git rev-parse HEAD) &&
728 git rebase -i HEAD~4 &&
729 test $HEAD = $(git rev-parse HEAD) &&
730 MTIME=$(test-tool chmtime --get file3) &&
731 test 123456789 = $MTIME
734 test_expect_success
'reword' '
735 git checkout -b reword-branch master &&
737 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
738 git show HEAD | grep "E changed" &&
739 test $(git rev-parse master) != $(git rev-parse HEAD) &&
740 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
741 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
742 git show HEAD^ | grep "D changed" &&
743 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
744 git show HEAD~3 | grep "B changed" &&
745 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
746 git show HEAD~2 | grep "C changed"
749 test_expect_success
'rebase -i can copy notes' '
750 git config notes.rewrite.rebase true &&
751 git config notes.rewriteRef "refs/notes/*" &&
755 git notes add -m"a note" n3 &&
757 git rebase -i --onto n1 n2 &&
758 test "a note" = "$(git notes show HEAD)"
767 test_expect_success
'rebase -i can copy notes over a fixup' '
768 git reset --hard n3 &&
769 git notes add -m"an earlier note" n2 &&
771 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
772 git notes show > output &&
773 test_cmp expect output
776 test_expect_success
'rebase while detaching HEAD' '
777 git symbolic-ref HEAD &&
778 grandparent=$(git rev-parse HEAD~2) &&
781 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
782 test $grandparent = $(git rev-parse HEAD~2) &&
783 test_must_fail git symbolic-ref HEAD
786 test_tick
# Ensure that the rebased commits get a different timestamp.
787 test_expect_success
'always cherry-pick with --no-ff' '
788 git checkout no-ff-branch &&
789 git tag original-no-ff-branch &&
791 git rebase -i --no-ff A &&
795 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
796 git diff HEAD~$p original-no-ff-branch~$p > out &&
799 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
800 git diff HEAD~3 original-no-ff-branch~3 > out &&
804 test_expect_success
'set up commits with funny messages' '
805 git checkout -b funny A &&
808 git commit -a -m "end with slash\\" &&
811 git commit -a -m "something (\000) that looks like octal" &&
814 git commit -a -m "something (\n) that looks like a newline" &&
817 git commit -a -m "another commit"
820 test_expect_success
'rebase-i history with funny messages' '
821 git rev-list A..funny >expect &&
824 FAKE_LINES="1 2 3 4" git rebase -i A &&
825 git rev-list A.. >actual &&
826 test_cmp expect actual
829 test_expect_success
'prepare for rebase -i --exec' '
830 git checkout master &&
831 git checkout -b execute &&
832 test_commit one_exec main.txt one_exec &&
833 test_commit two_exec main.txt two_exec &&
834 test_commit three_exec main.txt three_exec
837 test_expect_success
'running "git rebase -i --exec git show HEAD"' '
839 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
841 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
843 git rebase -i HEAD~2 >expect
845 sed -e "1,9d" expect >expected &&
846 test_cmp expected actual
849 test_expect_success
'running "git rebase --exec git show HEAD -i"' '
850 git reset --hard execute &&
852 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
854 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
856 git rebase -i HEAD~2 >expect
858 sed -e "1,9d" expect >expected &&
859 test_cmp expected actual
862 test_expect_success
'running "git rebase -ix git show HEAD"' '
863 git reset --hard execute &&
865 git rebase -ix "git show HEAD" HEAD~2 >actual &&
867 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
869 git rebase -i HEAD~2 >expect
871 sed -e "1,9d" expect >expected &&
872 test_cmp expected actual
876 test_expect_success
'rebase -ix with several <CMD>' '
877 git reset --hard execute &&
879 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
881 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
883 git rebase -i HEAD~2 >expect
885 sed -e "1,9d" expect >expected &&
886 test_cmp expected actual
889 test_expect_success
'rebase -ix with several instances of --exec' '
890 git reset --hard execute &&
892 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
894 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
895 exec_git_show_HEAD exec_pwd" &&
897 git rebase -i HEAD~2 >expect
899 sed -e "1,11d" expect >expected &&
900 test_cmp expected actual
903 test_expect_success C_LOCALE_OUTPUT
'rebase -ix with --autosquash' '
904 git reset --hard execute &&
905 git checkout -b autosquash &&
906 echo second >second.txt &&
907 git add second.txt &&
908 git commit -m "fixup! two_exec" &&
911 git commit -m "fixup! two_exec" &&
914 git checkout -b autosquash_actual &&
915 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
917 git checkout autosquash &&
919 git checkout -b autosquash_expected &&
920 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
922 git rebase -i HEAD~4 >expect
924 sed -e "1,13d" expect >expected &&
925 test_cmp expected actual
928 test_expect_success
'rebase --exec works without -i ' '
929 git reset --hard execute &&
930 rm -rf exec_output &&
931 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
932 test_i18ngrep "Successfully rebased and updated" actual &&
933 test_line_count = 2 exec_output &&
934 test_path_is_missing invoked_editor
937 test_expect_success
'rebase -i --exec without <CMD>' '
938 git reset --hard execute &&
940 test_must_fail git rebase -i --exec 2>actual &&
941 test_i18ngrep "requires a value" actual &&
945 test_expect_success
'rebase -i --root re-order and drop commits' '
948 FAKE_LINES="3 1 2 5" git rebase -i --root &&
949 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
950 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
951 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
952 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
953 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
956 test_expect_success
'rebase -i --root retain root commit author and message' '
960 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
962 FAKE_LINES="2" git rebase -i --root &&
963 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
964 git cat-file commit HEAD | grep -q "^different author$"
967 test_expect_success
'rebase -i --root temporary sentinel commit' '
970 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
971 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
975 test_expect_success
'rebase -i --root fixup root commit' '
978 FAKE_LINES="1 fixup 2" git rebase -i --root &&
979 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
980 test B = $(git show HEAD:file1) &&
981 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
984 test_expect_success
'rebase -i --root reword root commit' '
985 test_when_finished "test_might_fail git rebase --abort" &&
986 git checkout -b reword-root-branch master &&
988 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
989 git rebase -i --root &&
990 git show HEAD^ | grep "A changed" &&
991 test -z "$(git show -s --format=%p HEAD^)"
994 test_expect_success
'rebase -i --root when root has untracked file confilct' '
995 test_when_finished "reset_rebase" &&
996 git checkout -b failing-root-pick A &&
999 git commit -m "remove file 1 add file 2" &&
1002 test_must_fail env FAKE_LINES="1 2" git rebase -i --root &&
1004 git rebase --continue &&
1005 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1006 test "$(git rev-list --count HEAD)" = 2
1009 test_expect_success
'rebase -i --root reword root when root has untracked file conflict' '
1010 test_when_finished "reset_rebase" &&
1013 test_must_fail env FAKE_LINES="reword 1 2" \
1014 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1016 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue &&
1017 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1018 test "$(git rev-list --count HEAD)" = 2
1021 test_expect_success C_LOCALE_OUTPUT
'rebase --edit-todo does not work on non-interactive rebase' '
1022 git checkout reword-root-branch &&
1024 git checkout conflict-branch &&
1026 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
1027 test_must_fail git rebase --edit-todo &&
1031 test_expect_success
'rebase --edit-todo can be used to modify todo' '
1033 git checkout no-conflict-branch^0 &&
1035 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1036 FAKE_LINES="2 1" git rebase --edit-todo &&
1037 git rebase --continue &&
1038 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1039 test L = $(git cat-file commit HEAD | sed -ne \$p)
1042 test_expect_success
'rebase -i produces readable reflog' '
1044 git branch -f branch-reflog-test H &&
1046 git rebase -i --onto I F branch-reflog-test &&
1047 cat >expect <<-\EOF &&
1048 rebase -i (finish): returning to refs/heads/branch-reflog-test
1051 rebase -i (start): checkout I
1053 git reflog -n4 HEAD |
1054 sed "s/[^:]*: //" >actual &&
1055 test_cmp expect actual
1058 test_expect_success
'rebase -i respects core.commentchar' '
1061 test_config core.commentchar "\\" &&
1062 write_script remove-all-but-first.sh <<-\EOF &&
1063 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1066 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1068 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1071 test_expect_success
'rebase -i respects core.commentchar=auto' '
1072 test_config core.commentchar auto &&
1073 write_script copy-edit-script.sh <<-\EOF &&
1076 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1077 test_when_finished "git rebase --abort || :" &&
1078 git rebase -i HEAD^ &&
1079 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1082 test_expect_success
'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1083 test_when_finished "git branch -D torebase" &&
1084 git checkout -b torebase branch1 &&
1085 upstream=$(git rev-parse ":/J") &&
1086 onto=$(git rev-parse ":/A") &&
1087 git rebase --onto $onto $upstream &&
1088 git reset --hard branch1 &&
1089 git rebase --onto ":/A" ":/J" &&
1090 git checkout branch1
1093 test_expect_success
'rebase -i with --strategy and -X' '
1094 git checkout -b conflict-merge-use-theirs conflict-branch &&
1095 git reset --hard HEAD^ &&
1096 echo five >conflict &&
1098 git commit -a -m "one file conflict" &&
1099 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1100 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1101 test $(cat file1) = Z
1104 test_expect_success
'interrupted rebase -i with --strategy and -X' '
1105 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1106 git reset --hard HEAD^ &&
1108 git add breakpoint &&
1109 git commit -m "breakpoint for interactive mode" &&
1110 echo five >conflict &&
1112 git commit -a -m "one file conflict" &&
1114 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1115 git rebase --continue &&
1116 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1117 test $(cat file1) = Z
1120 test_expect_success
'rebase -i error on commits with \ in message' '
1121 current_head=$(git rev-parse HEAD) &&
1122 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1123 test_commit TO-REMOVE will-conflict old-content &&
1124 test_commit "\temp" will-conflict new-content dummy &&
1125 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1126 test_expect_code 1 grep " emp" error
1129 test_expect_success
'short SHA-1 setup' '
1130 test_when_finished "git checkout master" &&
1131 git checkout --orphan collide &&
1135 test_commit collide1 collide &&
1136 test_commit --notick collide2 collide &&
1137 test_commit --notick collide3 collide
1141 test_expect_success
'short SHA-1 collide' '
1142 test_when_finished "reset_rebase && git checkout master" &&
1143 git checkout collide &&
1148 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1149 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1153 test_expect_success
'respect core.abbrev' '
1154 git config core.abbrev 12 &&
1155 set_cat_todo_editor &&
1156 test_must_fail git rebase -i HEAD~4 >todo-list &&
1157 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1160 test_expect_success
'todo count' '
1161 write_script dump-raw.sh <<-\EOF &&
1164 test_set_editor "$(pwd)/dump-raw.sh" &&
1165 git rebase -i HEAD~4 >actual &&
1166 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1169 test_expect_success
'rebase -i commits that overwrite untracked files (pick)' '
1170 git checkout --force branch2 &&
1173 FAKE_LINES="edit 1 2" git rebase -i A &&
1174 test_cmp_rev HEAD F &&
1175 test_path_is_missing file6 &&
1177 test_must_fail git rebase --continue &&
1178 test_cmp_rev HEAD F &&
1180 git rebase --continue &&
1184 test_expect_success
'rebase -i commits that overwrite untracked files (squash)' '
1185 git checkout --force branch2 &&
1187 git tag original-branch2 &&
1189 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1190 test_cmp_rev HEAD F &&
1191 test_path_is_missing file6 &&
1193 test_must_fail git rebase --continue &&
1194 test_cmp_rev HEAD F &&
1196 git rebase --continue &&
1197 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1198 git reset --hard original-branch2
1201 test_expect_success
'rebase -i commits that overwrite untracked files (no ff)' '
1202 git checkout --force branch2 &&
1205 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1206 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1207 test_path_is_missing file6 &&
1209 test_must_fail git rebase --continue &&
1210 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1212 git rebase --continue &&
1213 test $(git cat-file commit HEAD | sed -ne \$p) = I
1216 test_expect_success
'rebase --continue removes CHERRY_PICK_HEAD' '
1217 git checkout -b commit-to-skip &&
1220 test_seq 5 | sed "s/$double/&&/" >seq &&
1223 git commit -m seq-$double
1226 git reset --hard HEAD~2 &&
1227 git cherry-pick seq-onto &&
1229 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1230 test -d .git/rebase-merge &&
1231 git rebase --continue &&
1232 git diff --exit-code seq-onto &&
1233 test ! -d .git/rebase-merge &&
1234 test ! -f .git/CHERRY_PICK_HEAD
1237 rebase_setup_and_clean
() {
1238 test_when_finished
"
1239 git checkout master &&
1240 test_might_fail git branch -D $1 &&
1241 test_might_fail git rebase --abort
1243 git checkout
-b $1 master
1246 test_expect_success
'drop' '
1247 rebase_setup_and_clean drop-test &&
1249 FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1250 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1251 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1252 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1255 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = ignore' '
1256 test_config rebase.missingCommitsCheck ignore &&
1257 rebase_setup_and_clean missing-commit &&
1259 FAKE_LINES="1 2 3 4" \
1260 git rebase -i --root 2>actual &&
1261 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1263 "Successfully rebased and updated refs/heads/missing-commit" \
1268 Warning: some commits may have been dropped accidentally.
1269 Dropped commits (newer to older):
1270 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1271 To avoid this message, use "drop" to explicitly remove a commit.
1273 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1274 The possible behaviours are: ignore, warn, error.
1280 Successfully rebased and updated refs/heads/missing-commit.
1287 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = warn' '
1288 test_config rebase.missingCommitsCheck warn &&
1289 rebase_setup_and_clean missing-commit &&
1291 FAKE_LINES="1 2 3 4" \
1292 git rebase -i --root 2>actual.2 &&
1293 cr_to_nl <actual.2 >actual &&
1294 test_i18ncmp expect actual &&
1295 test D = $(git cat-file commit HEAD | sed -ne \$p)
1299 Warning: some commits may have been dropped accidentally.
1300 Dropped commits (newer to older):
1301 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1302 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1303 To avoid this message, use "drop" to explicitly remove a commit.
1305 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1306 The possible behaviours are: ignore, warn, error.
1308 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1309 Or you can abort the rebase with 'git rebase --abort'.
1312 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = error' '
1313 test_config rebase.missingCommitsCheck error &&
1314 rebase_setup_and_clean missing-commit &&
1316 test_must_fail env FAKE_LINES="1 2 4" \
1317 git rebase -i --root 2>actual &&
1318 test_i18ncmp expect actual &&
1319 cp .git/rebase-merge/git-rebase-todo.backup \
1320 .git/rebase-merge/git-rebase-todo &&
1321 FAKE_LINES="1 2 drop 3 4 drop 5" \
1322 git rebase --edit-todo &&
1323 git rebase --continue &&
1324 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1325 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1328 test_expect_success
'respects rebase.abbreviateCommands with fixup, squash and exec' '
1329 rebase_setup_and_clean abbrevcmd &&
1330 test_commit "first" file1.txt "first line" first &&
1331 test_commit "second" file1.txt "another line" second &&
1332 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1333 test_commit "squash! second" file1.txt "another line here" second_squash &&
1334 cat >expected <<-EOF &&
1335 p $(git rev-list --abbrev-commit -1 first) first
1336 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1338 p $(git rev-list --abbrev-commit -1 second) second
1339 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1342 git checkout abbrevcmd &&
1343 set_cat_todo_editor &&
1344 test_config rebase.abbreviateCommands true &&
1345 test_must_fail git rebase -i --exec "git show HEAD" \
1346 --autosquash master >actual &&
1347 test_cmp expected actual
1350 test_expect_success
'static check of bad command' '
1351 rebase_setup_and_clean bad-cmd &&
1353 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1354 git rebase -i --root 2>actual &&
1355 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
1356 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1357 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1358 git rebase --continue &&
1359 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1360 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1363 test_expect_success
'tabs and spaces are accepted in the todolist' '
1364 rebase_setup_and_clean indented-comment &&
1365 write_script add-indent.sh <<-\EOF &&
1367 # Turn single spaces into space/tab mix
1368 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1369 printf "\n\t# comment\n #more\n\t # comment\n"
1373 test_set_editor "$(pwd)/add-indent.sh" &&
1374 git rebase -i HEAD^^^ &&
1375 test E = $(git cat-file commit HEAD | sed -ne \$p)
1378 test_expect_success
'static check of bad SHA-1' '
1379 rebase_setup_and_clean bad-sha &&
1381 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1382 git rebase -i --root 2>actual &&
1383 test_i18ngrep "edit XXXXXXX False commit" actual &&
1384 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1385 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1386 git rebase --continue &&
1387 test E = $(git cat-file commit HEAD | sed -ne \$p)
1390 test_expect_success
'editor saves as CR/LF' '
1391 git checkout -b with-crlf &&
1392 write_script add-crs.sh <<-\EOF &&
1393 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1397 test_set_editor "$(pwd)/add-crs.sh" &&
1403 test_expect_success
'rebase -i --gpg-sign=<key-id>' '
1404 test_when_finished "test_might_fail git rebase --abort" &&
1406 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1408 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1411 test_expect_success
'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1412 test_when_finished "test_might_fail git rebase --abort" &&
1413 test_config commit.gpgsign true &&
1415 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1417 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err