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 'reflog for the branch shows correct finish message' '
184 printf "rebase -i (finish): refs/heads/branch1 onto %s\n" \
185 "$(git rev-parse branch2)" >expected &&
186 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
187 test_cmp expected actual
190 test_expect_success 'exchange two commits' '
192 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
193 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
194 test G = $(git cat-file commit HEAD | sed -ne \$p)
198 diff --git a/file1 b/file1
199 index f70f10e..fd79235 100644
215 test_expect_success 'stop on conflicting pick' '
216 git tag new-branch1 &&
218 test_must_fail git rebase -i master &&
219 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
220 test_cmp expect .git/rebase-merge/patch &&
221 test_cmp expect2 file1 &&
222 test "$(git diff --name-status |
223 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
224 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
225 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
228 test_expect_success 'show conflicted patch' '
229 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
230 grep "show.*REBASE_HEAD" stderr &&
231 # the original stopped-sha1 is abbreviated
232 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
233 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
236 test_expect_success 'abort' '
237 git rebase --abort &&
238 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
239 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
240 test_path_is_missing .git/rebase-merge
243 test_expect_success 'abort with error when new base cannot be checked out' '
244 git rm --cached file1 &&
245 git commit -m "remove file in base" &&
247 test_must_fail git rebase -i master > output 2>&1 &&
248 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
250 test_i18ngrep "file1" output &&
251 test_path_is_missing .git/rebase-merge &&
252 git reset --hard HEAD^
255 test_expect_success 'retain authorship' '
259 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
262 git rebase -i --onto master HEAD^ &&
263 git show HEAD | grep "^Author: Twerp Snog"
266 test_expect_success 'retain authorship w/ conflicts' '
267 git reset --hard twerp &&
268 test_commit a conflict a conflict-a &&
269 git reset --hard twerp &&
270 GIT_AUTHOR_NAME=AttributeMe \
271 test_commit b conflict b conflict-b &&
273 test_must_fail git rebase -i conflict-a &&
274 echo resolved >conflict &&
276 git rebase --continue &&
277 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
282 test_expect_success 'squash' '
283 git reset --hard twerp &&
286 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
287 echo "******************************" &&
289 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
290 git rebase -i --onto master HEAD~2 &&
291 test B = $(cat file7) &&
292 test $(git rev-parse HEAD^) = $(git rev-parse master)
295 test_expect_success 'retain authorship when squashing' '
296 git show HEAD | grep "^Author: Twerp Snog"
299 test_expect_success '-p handles "no changes" gracefully' '
300 HEAD=$(git rev-parse HEAD) &&
302 git rebase -i -p HEAD^ &&
303 git update-index --refresh &&
304 git diff-files --quiet &&
305 git diff-index --quiet --cached HEAD -- &&
306 test $HEAD = $(git rev-parse HEAD)
309 test_expect_failure 'exchange two commits with -p' '
312 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
313 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
314 test G = $(git cat-file commit HEAD | sed -ne \$p)
317 test_expect_success 'preserve merges with -p' '
318 git checkout -b to-be-preserved master^ &&
319 : > unrelated-file &&
320 git add unrelated-file &&
322 git commit -m "unrelated" &&
323 git checkout -b another-branch master &&
326 git commit -m J file1 &&
328 git merge to-be-preserved &&
331 git commit -m K file1 &&
334 git commit -m L1 file1 &&
335 git checkout HEAD^ &&
336 echo 1 > unrelated-file &&
338 git commit -m L2 unrelated-file &&
340 git merge another-branch &&
343 git commit -m M file1 &&
344 git checkout -b to-be-rebased &&
347 git rebase -i -p --onto branch1 master &&
348 git update-index --refresh &&
349 git diff-files --quiet &&
350 git diff-index --quiet --cached HEAD -- &&
351 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
352 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
353 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
354 test $(git show HEAD~5:file1) = B &&
355 test $(git show HEAD~3:file1) = C &&
356 test $(git show HEAD:file1) = E &&
357 test $(git show HEAD:unrelated-file) = 1
360 test_expect_success 'edit ancestor with -p' '
362 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
363 echo 2 > unrelated-file &&
365 git commit -m L2-modified --amend unrelated-file &&
366 git rebase --continue &&
367 git update-index --refresh &&
368 git diff-files --quiet &&
369 git diff-index --quiet --cached HEAD -- &&
370 test $(git show HEAD:unrelated-file) = 2
373 test_expect_success '--continue tries to commit' '
376 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
377 echo resolved > file1 &&
379 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
380 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
381 git show HEAD | grep chouette
384 test_expect_success 'verbose flag is heeded, even after --continue' '
385 git reset --hard master@{1} &&
388 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
389 echo resolved > file1 &&
391 git rebase --continue > output &&
392 grep "^ file1 | 2 +-$" output
395 test_expect_success C_LOCALE_OUTPUT 'multi-squash only fires up editor once' '
396 base=$(git rev-parse HEAD~4) &&
398 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
399 EXPECT_HEADER_COUNT=4 \
400 git rebase -i $base &&
401 test $base = $(git rev-parse HEAD^) &&
402 test 1 = $(git show | grep ONCE | wc -l)
405 test_expect_success C_LOCALE_OUTPUT 'multi-fixup does not fire up editor' '
406 git checkout -b multi-fixup E &&
407 base=$(git rev-parse HEAD~4) &&
409 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
410 git rebase -i $base &&
411 test $base = $(git rev-parse HEAD^) &&
412 test 0 = $(git show | grep NEVER | wc -l) &&
413 git checkout to-be-rebased &&
414 git branch -D multi-fixup
417 test_expect_success 'commit message used after conflict' '
418 git checkout -b conflict-fixup conflict-branch &&
419 base=$(git rev-parse HEAD~4) &&
421 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
422 echo three > conflict &&
424 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
425 git rebase --continue &&
426 test $base = $(git rev-parse HEAD^) &&
427 test 1 = $(git show | grep ONCE | wc -l) &&
428 git checkout to-be-rebased &&
429 git branch -D conflict-fixup
432 test_expect_success 'commit message retained after conflict' '
433 git checkout -b conflict-squash conflict-branch &&
434 base=$(git rev-parse HEAD~4) &&
436 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
437 echo three > conflict &&
439 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
440 git rebase --continue &&
441 test $base = $(git rev-parse HEAD^) &&
442 test 2 = $(git show | grep TWICE | wc -l) &&
443 git checkout to-be-rebased &&
444 git branch -D conflict-squash
447 cat > expect-squash-fixup << EOF
455 test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
456 git checkout -b squash-fixup E &&
457 base=$(git rev-parse HEAD~4) &&
459 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
460 EXPECT_HEADER_COUNT=4 \
461 git rebase -i $base &&
462 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
463 test_cmp expect-squash-fixup actual-squash-fixup &&
464 git checkout to-be-rebased &&
465 git branch -D squash-fixup
468 test_expect_success C_LOCALE_OUTPUT 'squash ignores comments' '
469 git checkout -b skip-comments E &&
470 base=$(git rev-parse HEAD~4) &&
472 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
473 EXPECT_HEADER_COUNT=4 \
474 git rebase -i $base &&
475 test $base = $(git rev-parse HEAD^) &&
476 test 1 = $(git show | grep ONCE | wc -l) &&
477 git checkout to-be-rebased &&
478 git branch -D skip-comments
481 test_expect_success C_LOCALE_OUTPUT 'squash ignores blank lines' '
482 git checkout -b skip-blank-lines E &&
483 base=$(git rev-parse HEAD~4) &&
485 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
486 EXPECT_HEADER_COUNT=4 \
487 git rebase -i $base &&
488 test $base = $(git rev-parse HEAD^) &&
489 test 1 = $(git show | grep ONCE | wc -l) &&
490 git checkout to-be-rebased &&
491 git branch -D skip-blank-lines
494 test_expect_success 'squash works as expected' '
495 git checkout -b squash-works no-conflict-branch &&
496 one=$(git rev-parse HEAD~3) &&
498 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
499 git rebase -i HEAD~3 &&
500 test $one = $(git rev-parse HEAD~2)
503 test_expect_success 'interrupted squash works as expected' '
504 git checkout -b interrupted-squash conflict-branch &&
505 one=$(git rev-parse HEAD~3) &&
507 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
508 (echo one; echo two; echo four) > conflict &&
510 test_must_fail git rebase --continue &&
511 echo resolved > conflict &&
513 git rebase --continue &&
514 test $one = $(git rev-parse HEAD~2)
517 test_expect_success 'interrupted squash works as expected (case 2)' '
518 git checkout -b interrupted-squash2 conflict-branch &&
519 one=$(git rev-parse HEAD~3) &&
521 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
522 (echo one; echo four) > conflict &&
524 test_must_fail git rebase --continue &&
525 (echo one; echo two; echo four) > conflict &&
527 test_must_fail git rebase --continue &&
528 echo resolved > conflict &&
530 git rebase --continue &&
531 test $one = $(git rev-parse HEAD~2)
534 test_expect_success '--continue tries to commit, even for "edit"' '
535 echo unrelated > file7 &&
538 git commit -m "unrelated change" &&
539 parent=$(git rev-parse HEAD^) &&
542 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
543 echo edited > file7 &&
545 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
546 test edited = $(git show HEAD:file7) &&
547 git show HEAD | grep chouette &&
548 test $parent = $(git rev-parse HEAD^)
551 test_expect_success 'aborted --continue does not squash commits after "edit"' '
552 old=$(git rev-parse HEAD) &&
555 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
556 echo "edited again" > file7 &&
558 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
559 test $old = $(git rev-parse HEAD) &&
563 test_expect_success 'auto-amend only edited commits after "edit"' '
566 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
567 echo "edited again" > file7 &&
569 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
570 echo "and again" > file7 &&
573 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
577 test_expect_success 'clean error after failed "exec"' '
579 test_when_finished "git rebase --abort || :" &&
581 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
582 echo "edited again" > file7 &&
584 test_must_fail git rebase --continue 2>error &&
585 test_i18ngrep "you have staged changes in your working tree" error
588 test_expect_success 'rebase a detached HEAD' '
589 grandparent=$(git rev-parse HEAD~2) &&
590 git checkout $(git rev-parse HEAD) &&
593 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
594 test $grandparent = $(git rev-parse HEAD~2)
597 test_expect_success 'rebase a commit violating pre-commit' '
599 mkdir -p .git/hooks &&
600 write_script .git/hooks/pre-commit <<-\EOF &&
601 test -z "$(git diff --cached --check)"
603 echo "monde! " >> file1 &&
605 test_must_fail git commit -m doesnt-verify file1 &&
606 git commit -m doesnt-verify --no-verify file1 &&
609 FAKE_LINES=2 git rebase -i HEAD~2
613 test_expect_success 'rebase with a file named HEAD in worktree' '
617 git checkout -b branch3 A &&
620 GIT_AUTHOR_NAME="Squashed Away" &&
621 export GIT_AUTHOR_NAME &&
624 git commit -m "Add head" &&
627 git commit -m "Add body"
631 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
632 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
636 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
638 git checkout -b branch4 HEAD &&
639 GIT_EDITOR=: git commit --amend \
640 --author="Somebody else <somebody@else.com>" &&
641 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
643 git rebase -i branch3 &&
644 test $(git rev-parse branch3) = $(git rev-parse branch4)
648 test_expect_success 'submodule rebase setup' '
652 cd sub && git init && >elif &&
653 git add elif && git commit -m "submodule initial"
658 git commit -m "One" &&
661 git commit -a -m "Two" &&
663 cd sub && echo 3 >elif &&
664 git commit -a -m "submodule second"
668 git commit -a -m "Three changes submodule"
671 test_expect_success 'submodule rebase -i' '
673 FAKE_LINES="1 squash 2 3" git rebase -i A
676 test_expect_success 'submodule conflict setup' '
677 git tag submodule-base &&
678 git checkout HEAD^ &&
680 cd sub && git checkout HEAD^ && echo 4 >elif &&
681 git add elif && git commit -m "submodule conflict"
685 git commit -m "Conflict in submodule" &&
686 git tag submodule-topic
689 test_expect_success 'rebase -i continue with only submodule staged' '
691 test_must_fail git rebase -i submodule-base &&
693 git rebase --continue &&
694 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
697 test_expect_success 'rebase -i continue with unstaged submodule' '
698 git checkout submodule-topic &&
701 test_must_fail git rebase -i submodule-base &&
703 git rebase --continue &&
704 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
707 test_expect_success 'avoid unnecessary reset' '
708 git checkout master &&
710 test-chmtime =123456789 file3 &&
711 git update-index --refresh &&
712 HEAD=$(git rev-parse HEAD) &&
714 git rebase -i HEAD~4 &&
715 test $HEAD = $(git rev-parse HEAD) &&
716 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
717 test 123456789 = $MTIME
720 test_expect_success 'reword' '
721 git checkout -b reword-branch master &&
723 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
724 git show HEAD | grep "E changed" &&
725 test $(git rev-parse master) != $(git rev-parse HEAD) &&
726 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
727 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
728 git show HEAD^ | grep "D changed" &&
729 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
730 git show HEAD~3 | grep "B changed" &&
731 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
732 git show HEAD~2 | grep "C changed"
735 test_expect_success 'rebase -i can copy notes' '
736 git config notes.rewrite.rebase true &&
737 git config notes.rewriteRef "refs/notes/*" &&
741 git notes add -m"a note" n3 &&
743 git rebase -i --onto n1 n2 &&
744 test "a note" = "$(git notes show HEAD)"
753 test_expect_success 'rebase -i can copy notes over a fixup' '
754 git reset --hard n3 &&
755 git notes add -m"an earlier note" n2 &&
757 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
758 git notes show > output &&
759 test_cmp expect output
762 test_expect_success 'rebase while detaching HEAD' '
763 git symbolic-ref HEAD &&
764 grandparent=$(git rev-parse HEAD~2) &&
767 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
768 test $grandparent = $(git rev-parse HEAD~2) &&
769 test_must_fail git symbolic-ref HEAD
772 test_tick # Ensure that the rebased commits get a different timestamp.
773 test_expect_success 'always cherry-pick with --no-ff' '
774 git checkout no-ff-branch &&
775 git tag original-no-ff-branch &&
777 git rebase -i --no-ff A &&
781 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
782 git diff HEAD~$p original-no-ff-branch~$p > out &&
785 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
786 git diff HEAD~3 original-no-ff-branch~3 > out &&
790 test_expect_success 'set up commits with funny messages' '
791 git checkout -b funny A &&
794 git commit -a -m "end with slash\\" &&
797 git commit -a -m "something (\000) that looks like octal" &&
800 git commit -a -m "something (\n) that looks like a newline" &&
803 git commit -a -m "another commit"
806 test_expect_success 'rebase-i history with funny messages' '
807 git rev-list A..funny >expect &&
810 FAKE_LINES="1 2 3 4" git rebase -i A &&
811 git rev-list A.. >actual &&
812 test_cmp expect actual
815 test_expect_success 'prepare for rebase -i --exec' '
816 git checkout master &&
817 git checkout -b execute &&
818 test_commit one_exec main.txt one_exec &&
819 test_commit two_exec main.txt two_exec &&
820 test_commit three_exec main.txt three_exec
823 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
825 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
827 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
829 git rebase -i HEAD~2 >expect
831 sed -e "1,9d" expect >expected &&
832 test_cmp expected actual
835 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
836 git reset --hard execute &&
838 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
840 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
842 git rebase -i HEAD~2 >expect
844 sed -e "1,9d" expect >expected &&
845 test_cmp expected actual
848 test_expect_success 'running "git rebase -ix git show HEAD"' '
849 git reset --hard execute &&
851 git rebase -ix "git show HEAD" HEAD~2 >actual &&
853 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
855 git rebase -i HEAD~2 >expect
857 sed -e "1,9d" expect >expected &&
858 test_cmp expected actual
862 test_expect_success 'rebase -ix with several <CMD>' '
863 git reset --hard execute &&
865 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
867 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
869 git rebase -i HEAD~2 >expect
871 sed -e "1,9d" expect >expected &&
872 test_cmp expected actual
875 test_expect_success 'rebase -ix with several instances of --exec' '
876 git reset --hard execute &&
878 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
880 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
881 exec_git_show_HEAD exec_pwd" &&
883 git rebase -i HEAD~2 >expect
885 sed -e "1,11d" expect >expected &&
886 test_cmp expected actual
889 test_expect_success C_LOCALE_OUTPUT 'rebase -ix with --autosquash' '
890 git reset --hard execute &&
891 git checkout -b autosquash &&
892 echo second >second.txt &&
893 git add second.txt &&
894 git commit -m "fixup! two_exec" &&
897 git commit -m "fixup! two_exec" &&
900 git checkout -b autosquash_actual &&
901 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
903 git checkout autosquash &&
905 git checkout -b autosquash_expected &&
906 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
908 git rebase -i HEAD~4 >expect
910 sed -e "1,13d" expect >expected &&
911 test_cmp expected actual
914 test_expect_success 'rebase --exec works without -i ' '
915 git reset --hard execute &&
916 rm -rf exec_output &&
917 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
918 test_i18ngrep "Successfully rebased and updated" actual &&
919 test_line_count = 2 exec_output &&
920 test_path_is_missing invoked_editor
923 test_expect_success 'rebase -i --exec without <CMD>' '
924 git reset --hard execute &&
926 test_must_fail git rebase -i --exec 2>tmp &&
927 sed -e "1d" tmp >actual &&
928 test_must_fail git rebase -h >expected &&
929 test_cmp expected actual &&
933 test_expect_success 'rebase -i --root re-order and drop commits' '
936 FAKE_LINES="3 1 2 5" git rebase -i --root &&
937 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
938 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
939 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
940 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
941 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
944 test_expect_success 'rebase -i --root retain root commit author and message' '
948 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
950 FAKE_LINES="2" git rebase -i --root &&
951 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
952 git cat-file commit HEAD | grep -q "^different author$"
955 test_expect_success 'rebase -i --root temporary sentinel commit' '
958 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
959 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
963 test_expect_success 'rebase -i --root fixup root commit' '
966 FAKE_LINES="1 fixup 2" git rebase -i --root &&
967 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
968 test B = $(git show HEAD:file1) &&
969 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
972 test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
974 git checkout conflict-branch &&
976 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
977 test_must_fail git rebase --edit-todo &&
981 test_expect_success 'rebase --edit-todo can be used to modify todo' '
983 git checkout no-conflict-branch^0 &&
985 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
986 FAKE_LINES="2 1" git rebase --edit-todo &&
987 git rebase --continue &&
988 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
989 test L = $(git cat-file commit HEAD | sed -ne \$p)
992 test_expect_success 'rebase -i produces readable reflog' '
994 git branch -f branch-reflog-test H &&
996 git rebase -i --onto I F branch-reflog-test &&
997 cat >expect <<-\EOF &&
998 rebase -i (finish): returning to refs/heads/branch-reflog-test
1001 rebase -i (start): checkout I
1003 git reflog -n4 HEAD |
1004 sed "s/[^:]*: //" >actual &&
1005 test_cmp expect actual
1008 test_expect_success 'rebase -i respects core.commentchar' '
1011 test_config core.commentchar "\\" &&
1012 write_script remove-all-but-first.sh <<-\EOF &&
1013 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1016 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1018 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1021 test_expect_success 'rebase -i respects core.commentchar=auto' '
1022 test_config core.commentchar auto &&
1023 write_script copy-edit-script.sh <<-\EOF &&
1026 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1027 test_when_finished "git rebase --abort || :" &&
1028 git rebase -i HEAD^ &&
1029 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1032 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1033 test_when_finished "git branch -D torebase" &&
1034 git checkout -b torebase branch1 &&
1035 upstream=$(git rev-parse ":/J") &&
1036 onto=$(git rev-parse ":/A") &&
1037 git rebase --onto $onto $upstream &&
1038 git reset --hard branch1 &&
1039 git rebase --onto ":/A" ":/J" &&
1040 git checkout branch1
1043 test_expect_success 'rebase -i with --strategy and -X' '
1044 git checkout -b conflict-merge-use-theirs conflict-branch &&
1045 git reset --hard HEAD^ &&
1046 echo five >conflict &&
1048 git commit -a -m "one file conflict" &&
1049 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1050 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1051 test $(cat file1) = Z
1054 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1055 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1056 git reset --hard HEAD^ &&
1058 git add breakpoint &&
1059 git commit -m "breakpoint for interactive mode" &&
1060 echo five >conflict &&
1062 git commit -a -m "one file conflict" &&
1064 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1065 git rebase --continue &&
1066 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1067 test $(cat file1) = Z
1070 test_expect_success 'rebase -i error on commits with \ in message' '
1071 current_head=$(git rev-parse HEAD) &&
1072 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1073 test_commit TO-REMOVE will-conflict old-content &&
1074 test_commit "\temp" will-conflict new-content dummy &&
1075 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1076 test_expect_code 1 grep " emp" error
1079 test_expect_success 'short SHA-1 setup' '
1080 test_when_finished "git checkout master" &&
1081 git checkout --orphan collide &&
1085 test_commit collide1 collide &&
1086 test_commit --notick collide2 collide &&
1087 test_commit --notick collide3 collide
1091 test_expect_success 'short SHA-1 collide' '
1092 test_when_finished "reset_rebase && git checkout master" &&
1093 git checkout collide &&
1098 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1099 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1103 test_expect_success 'respect core.abbrev' '
1104 git config core.abbrev 12 &&
1105 set_cat_todo_editor &&
1106 test_must_fail git rebase -i HEAD~4 >todo-list &&
1107 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1110 test_expect_success 'todo count' '
1111 write_script dump-raw.sh <<-\EOF &&
1114 test_set_editor "$(pwd)/dump-raw.sh" &&
1115 git rebase -i HEAD~4 >actual &&
1116 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1119 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1120 git checkout --force branch2 &&
1123 FAKE_LINES="edit 1 2" git rebase -i A &&
1124 test_cmp_rev HEAD F &&
1125 test_path_is_missing file6 &&
1127 test_must_fail git rebase --continue &&
1128 test_cmp_rev HEAD F &&
1130 git rebase --continue &&
1134 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1135 git checkout --force branch2 &&
1137 git tag original-branch2 &&
1139 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1140 test_cmp_rev HEAD F &&
1141 test_path_is_missing file6 &&
1143 test_must_fail git rebase --continue &&
1144 test_cmp_rev HEAD F &&
1146 git rebase --continue &&
1147 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1148 git reset --hard original-branch2
1151 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1152 git checkout --force branch2 &&
1155 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1156 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1157 test_path_is_missing file6 &&
1159 test_must_fail git rebase --continue &&
1160 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1162 git rebase --continue &&
1163 test $(git cat-file commit HEAD | sed -ne \$p) = I
1166 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1167 git checkout -b commit-to-skip &&
1170 test_seq 5 | sed "s/$double/&&/" >seq &&
1173 git commit -m seq-$double
1176 git reset --hard HEAD~2 &&
1177 git cherry-pick seq-onto &&
1179 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1180 test -d .git/rebase-merge &&
1181 git rebase --continue &&
1182 git diff --exit-code seq-onto &&
1183 test ! -d .git/rebase-merge &&
1184 test ! -f .git/CHERRY_PICK_HEAD
1187 rebase_setup_and_clean () {
1188 test_when_finished "
1189 git checkout master &&
1190 test_might_fail git branch -D $1 &&
1191 test_might_fail git rebase --abort
1193 git checkout -b $1 master
1196 test_expect_success 'drop' '
1197 rebase_setup_and_clean drop-test &&
1199 FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1200 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1201 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1202 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1206 Successfully rebased and updated refs/heads/missing-commit.
1209 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1210 test_config rebase.missingCommitsCheck ignore &&
1211 rebase_setup_and_clean missing-commit &&
1213 FAKE_LINES="1 2 3 4" \
1214 git rebase -i --root 2>actual &&
1215 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1216 test_i18ncmp expect actual
1220 Warning: some commits may have been dropped accidentally.
1221 Dropped commits (newer to older):
1222 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1223 To avoid this message, use "drop" to explicitly remove a commit.
1225 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1226 The possible behaviours are: ignore, warn, error.
1228 Successfully rebased and updated refs/heads/missing-commit.
1231 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1232 test_config rebase.missingCommitsCheck warn &&
1233 rebase_setup_and_clean missing-commit &&
1235 FAKE_LINES="1 2 3 4" \
1236 git rebase -i --root 2>actual &&
1237 test_i18ncmp expect actual &&
1238 test D = $(git cat-file commit HEAD | sed -ne \$p)
1242 Warning: some commits may have been dropped accidentally.
1243 Dropped commits (newer to older):
1244 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1245 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1246 To avoid this message, use "drop" to explicitly remove a commit.
1248 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1249 The possible behaviours are: ignore, warn, error.
1251 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1252 Or you can abort the rebase with 'git rebase --abort'.
1255 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1256 test_config rebase.missingCommitsCheck error &&
1257 rebase_setup_and_clean missing-commit &&
1259 test_must_fail env FAKE_LINES="1 2 4" \
1260 git rebase -i --root 2>actual &&
1261 test_i18ncmp expect actual &&
1262 cp .git/rebase-merge/git-rebase-todo.backup \
1263 .git/rebase-merge/git-rebase-todo &&
1264 FAKE_LINES="1 2 drop 3 4 drop 5" \
1265 git rebase --edit-todo &&
1266 git rebase --continue &&
1267 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1268 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1271 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1272 rebase_setup_and_clean abbrevcmd &&
1273 test_commit "first" file1.txt "first line" first &&
1274 test_commit "second" file1.txt "another line" second &&
1275 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1276 test_commit "squash! second" file1.txt "another line here" second_squash &&
1277 cat >expected <<-EOF &&
1278 p $(git rev-list --abbrev-commit -1 first) first
1279 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1281 p $(git rev-list --abbrev-commit -1 second) second
1282 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1285 git checkout abbrevcmd &&
1286 set_cat_todo_editor &&
1287 test_config rebase.abbreviateCommands true &&
1288 test_must_fail git rebase -i --exec "git show HEAD" \
1289 --autosquash master >actual &&
1290 test_cmp expected actual
1293 test_expect_success 'static check of bad command' '
1294 rebase_setup_and_clean bad-cmd &&
1296 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1297 git rebase -i --root 2>actual &&
1298 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
1299 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1300 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1301 git rebase --continue &&
1302 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1303 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1306 test_expect_success 'tabs and spaces are accepted in the todolist' '
1307 rebase_setup_and_clean indented-comment &&
1308 write_script add-indent.sh <<-\EOF &&
1310 # Turn single spaces into space/tab mix
1311 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1312 printf "\n\t# comment\n #more\n\t # comment\n"
1316 test_set_editor "$(pwd)/add-indent.sh" &&
1317 git rebase -i HEAD^^^ &&
1318 test E = $(git cat-file commit HEAD | sed -ne \$p)
1321 test_expect_success 'static check of bad SHA-1' '
1322 rebase_setup_and_clean bad-sha &&
1324 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1325 git rebase -i --root 2>actual &&
1326 test_i18ngrep "edit XXXXXXX False commit" actual &&
1327 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1328 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1329 git rebase --continue &&
1330 test E = $(git cat-file commit HEAD | sed -ne \$p)
1333 test_expect_success 'editor saves as CR/LF' '
1334 git checkout -b with-crlf &&
1335 write_script add-crs.sh <<-\EOF &&
1336 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1340 test_set_editor "$(pwd)/add-crs.sh" &&
1346 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1348 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1350 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err