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
'abort' '
229 git rebase --abort &&
230 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
231 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
232 test_path_is_missing .git/rebase-merge
235 test_expect_success
'abort with error when new base cannot be checked out' '
236 git rm --cached file1 &&
237 git commit -m "remove file in base" &&
239 test_must_fail git rebase -i master > output 2>&1 &&
240 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
242 test_i18ngrep "file1" output &&
243 test_path_is_missing .git/rebase-merge &&
244 git reset --hard HEAD^
247 test_expect_success
'retain authorship' '
251 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
254 git rebase -i --onto master HEAD^ &&
255 git show HEAD | grep "^Author: Twerp Snog"
258 test_expect_success
'retain authorship w/ conflicts' '
259 git reset --hard twerp &&
260 test_commit a conflict a conflict-a &&
261 git reset --hard twerp &&
262 GIT_AUTHOR_NAME=AttributeMe \
263 test_commit b conflict b conflict-b &&
265 test_must_fail git rebase -i conflict-a &&
266 echo resolved >conflict &&
268 git rebase --continue &&
269 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
274 test_expect_success
'squash' '
275 git reset --hard twerp &&
278 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
279 echo "******************************" &&
281 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
282 git rebase -i --onto master HEAD~2 &&
283 test B = $(cat file7) &&
284 test $(git rev-parse HEAD^) = $(git rev-parse master)
287 test_expect_success
'retain authorship when squashing' '
288 git show HEAD | grep "^Author: Twerp Snog"
291 test_expect_success
'-p handles "no changes" gracefully' '
292 HEAD=$(git rev-parse HEAD) &&
294 git rebase -i -p HEAD^ &&
295 git update-index --refresh &&
296 git diff-files --quiet &&
297 git diff-index --quiet --cached HEAD -- &&
298 test $HEAD = $(git rev-parse HEAD)
301 test_expect_failure
'exchange two commits with -p' '
304 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
305 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
306 test G = $(git cat-file commit HEAD | sed -ne \$p)
309 test_expect_success
'preserve merges with -p' '
310 git checkout -b to-be-preserved master^ &&
311 : > unrelated-file &&
312 git add unrelated-file &&
314 git commit -m "unrelated" &&
315 git checkout -b another-branch master &&
318 git commit -m J file1 &&
320 git merge to-be-preserved &&
323 git commit -m K file1 &&
326 git commit -m L1 file1 &&
327 git checkout HEAD^ &&
328 echo 1 > unrelated-file &&
330 git commit -m L2 unrelated-file &&
332 git merge another-branch &&
335 git commit -m M file1 &&
336 git checkout -b to-be-rebased &&
339 git rebase -i -p --onto branch1 master &&
340 git update-index --refresh &&
341 git diff-files --quiet &&
342 git diff-index --quiet --cached HEAD -- &&
343 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
344 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
345 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
346 test $(git show HEAD~5:file1) = B &&
347 test $(git show HEAD~3:file1) = C &&
348 test $(git show HEAD:file1) = E &&
349 test $(git show HEAD:unrelated-file) = 1
352 test_expect_success
'edit ancestor with -p' '
354 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
355 echo 2 > unrelated-file &&
357 git commit -m L2-modified --amend unrelated-file &&
358 git rebase --continue &&
359 git update-index --refresh &&
360 git diff-files --quiet &&
361 git diff-index --quiet --cached HEAD -- &&
362 test $(git show HEAD:unrelated-file) = 2
365 test_expect_success
'--continue tries to commit' '
368 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
369 echo resolved > file1 &&
371 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
372 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
373 git show HEAD | grep chouette
376 test_expect_success
'verbose flag is heeded, even after --continue' '
377 git reset --hard master@{1} &&
380 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
381 echo resolved > file1 &&
383 git rebase --continue > output &&
384 grep "^ file1 | 2 +-$" output
387 test_expect_success C_LOCALE_OUTPUT
'multi-squash only fires up editor once' '
388 base=$(git rev-parse HEAD~4) &&
390 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
391 EXPECT_HEADER_COUNT=4 \
392 git rebase -i $base &&
393 test $base = $(git rev-parse HEAD^) &&
394 test 1 = $(git show | grep ONCE | wc -l)
397 test_expect_success C_LOCALE_OUTPUT
'multi-fixup does not fire up editor' '
398 git checkout -b multi-fixup E &&
399 base=$(git rev-parse HEAD~4) &&
401 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
402 git rebase -i $base &&
403 test $base = $(git rev-parse HEAD^) &&
404 test 0 = $(git show | grep NEVER | wc -l) &&
405 git checkout to-be-rebased &&
406 git branch -D multi-fixup
409 test_expect_success
'commit message used after conflict' '
410 git checkout -b conflict-fixup conflict-branch &&
411 base=$(git rev-parse HEAD~4) &&
413 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
414 echo three > conflict &&
416 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
417 git rebase --continue &&
418 test $base = $(git rev-parse HEAD^) &&
419 test 1 = $(git show | grep ONCE | wc -l) &&
420 git checkout to-be-rebased &&
421 git branch -D conflict-fixup
424 test_expect_success
'commit message retained after conflict' '
425 git checkout -b conflict-squash conflict-branch &&
426 base=$(git rev-parse HEAD~4) &&
428 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
429 echo three > conflict &&
431 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
432 git rebase --continue &&
433 test $base = $(git rev-parse HEAD^) &&
434 test 2 = $(git show | grep TWICE | wc -l) &&
435 git checkout to-be-rebased &&
436 git branch -D conflict-squash
439 cat > expect-squash-fixup
<< EOF
447 test_expect_success C_LOCALE_OUTPUT
'squash and fixup generate correct log messages' '
448 git checkout -b squash-fixup E &&
449 base=$(git rev-parse HEAD~4) &&
451 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
452 EXPECT_HEADER_COUNT=4 \
453 git rebase -i $base &&
454 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
455 test_cmp expect-squash-fixup actual-squash-fixup &&
456 git checkout to-be-rebased &&
457 git branch -D squash-fixup
460 test_expect_success C_LOCALE_OUTPUT
'squash ignores comments' '
461 git checkout -b skip-comments E &&
462 base=$(git rev-parse HEAD~4) &&
464 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
465 EXPECT_HEADER_COUNT=4 \
466 git rebase -i $base &&
467 test $base = $(git rev-parse HEAD^) &&
468 test 1 = $(git show | grep ONCE | wc -l) &&
469 git checkout to-be-rebased &&
470 git branch -D skip-comments
473 test_expect_success C_LOCALE_OUTPUT
'squash ignores blank lines' '
474 git checkout -b skip-blank-lines E &&
475 base=$(git rev-parse HEAD~4) &&
477 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
478 EXPECT_HEADER_COUNT=4 \
479 git rebase -i $base &&
480 test $base = $(git rev-parse HEAD^) &&
481 test 1 = $(git show | grep ONCE | wc -l) &&
482 git checkout to-be-rebased &&
483 git branch -D skip-blank-lines
486 test_expect_success
'squash works as expected' '
487 git checkout -b squash-works no-conflict-branch &&
488 one=$(git rev-parse HEAD~3) &&
490 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
491 git rebase -i HEAD~3 &&
492 test $one = $(git rev-parse HEAD~2)
495 test_expect_success
'interrupted squash works as expected' '
496 git checkout -b interrupted-squash conflict-branch &&
497 one=$(git rev-parse HEAD~3) &&
499 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
500 (echo one; echo two; echo four) > conflict &&
502 test_must_fail git rebase --continue &&
503 echo resolved > conflict &&
505 git rebase --continue &&
506 test $one = $(git rev-parse HEAD~2)
509 test_expect_success
'interrupted squash works as expected (case 2)' '
510 git checkout -b interrupted-squash2 conflict-branch &&
511 one=$(git rev-parse HEAD~3) &&
513 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
514 (echo one; echo four) > conflict &&
516 test_must_fail git rebase --continue &&
517 (echo one; echo two; echo four) > conflict &&
519 test_must_fail git rebase --continue &&
520 echo resolved > conflict &&
522 git rebase --continue &&
523 test $one = $(git rev-parse HEAD~2)
526 test_expect_success
'--continue tries to commit, even for "edit"' '
527 echo unrelated > file7 &&
530 git commit -m "unrelated change" &&
531 parent=$(git rev-parse HEAD^) &&
534 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
535 echo edited > file7 &&
537 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
538 test edited = $(git show HEAD:file7) &&
539 git show HEAD | grep chouette &&
540 test $parent = $(git rev-parse HEAD^)
543 test_expect_success
'aborted --continue does not squash commits after "edit"' '
544 old=$(git rev-parse HEAD) &&
547 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
548 echo "edited again" > file7 &&
550 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
551 test $old = $(git rev-parse HEAD) &&
555 test_expect_success
'auto-amend only edited commits after "edit"' '
558 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
559 echo "edited again" > file7 &&
561 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
562 echo "and again" > file7 &&
565 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
569 test_expect_success
'clean error after failed "exec"' '
571 test_when_finished "git rebase --abort || :" &&
573 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
574 echo "edited again" > file7 &&
576 test_must_fail git rebase --continue 2>error &&
577 test_i18ngrep "you have staged changes in your working tree" error
580 test_expect_success
'rebase a detached HEAD' '
581 grandparent=$(git rev-parse HEAD~2) &&
582 git checkout $(git rev-parse HEAD) &&
585 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
586 test $grandparent = $(git rev-parse HEAD~2)
589 test_expect_success
'rebase a commit violating pre-commit' '
591 mkdir -p .git/hooks &&
592 write_script .git/hooks/pre-commit <<-\EOF &&
593 test -z "$(git diff --cached --check)"
595 echo "monde! " >> file1 &&
597 test_must_fail git commit -m doesnt-verify file1 &&
598 git commit -m doesnt-verify --no-verify file1 &&
601 FAKE_LINES=2 git rebase -i HEAD~2
605 test_expect_success
'rebase with a file named HEAD in worktree' '
609 git checkout -b branch3 A &&
612 GIT_AUTHOR_NAME="Squashed Away" &&
613 export GIT_AUTHOR_NAME &&
616 git commit -m "Add head" &&
619 git commit -m "Add body"
623 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
624 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
628 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
630 git checkout -b branch4 HEAD &&
631 GIT_EDITOR=: git commit --amend \
632 --author="Somebody else <somebody@else.com>" &&
633 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
635 git rebase -i branch3 &&
636 test $(git rev-parse branch3) = $(git rev-parse branch4)
640 test_expect_success
'submodule rebase setup' '
644 cd sub && git init && >elif &&
645 git add elif && git commit -m "submodule initial"
650 git commit -m "One" &&
653 git commit -a -m "Two" &&
655 cd sub && echo 3 >elif &&
656 git commit -a -m "submodule second"
660 git commit -a -m "Three changes submodule"
663 test_expect_success
'submodule rebase -i' '
665 FAKE_LINES="1 squash 2 3" git rebase -i A
668 test_expect_success
'submodule conflict setup' '
669 git tag submodule-base &&
670 git checkout HEAD^ &&
672 cd sub && git checkout HEAD^ && echo 4 >elif &&
673 git add elif && git commit -m "submodule conflict"
677 git commit -m "Conflict in submodule" &&
678 git tag submodule-topic
681 test_expect_success
'rebase -i continue with only submodule staged' '
683 test_must_fail git rebase -i submodule-base &&
685 git rebase --continue &&
686 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
689 test_expect_success
'rebase -i continue with unstaged submodule' '
690 git checkout submodule-topic &&
693 test_must_fail git rebase -i submodule-base &&
695 git rebase --continue &&
696 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
699 test_expect_success
'avoid unnecessary reset' '
700 git checkout master &&
702 test-chmtime =123456789 file3 &&
703 git update-index --refresh &&
704 HEAD=$(git rev-parse HEAD) &&
706 git rebase -i HEAD~4 &&
707 test $HEAD = $(git rev-parse HEAD) &&
708 MTIME=$(test-chmtime -v +0 file3 | sed 's
/[^
0-9].
*$
//') &&
709 test 123456789 = $MTIME
712 test_expect_success
'reword' '
713 git checkout -b reword-branch master &&
715 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
716 git show HEAD | grep "E changed" &&
717 test $(git rev-parse master) != $(git rev-parse HEAD) &&
718 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
719 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
720 git show HEAD^ | grep "D changed" &&
721 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
722 git show HEAD~3 | grep "B changed" &&
723 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
724 git show HEAD~2 | grep "C changed"
727 test_expect_success
'rebase -i can copy notes' '
728 git config notes.rewrite.rebase true &&
729 git config notes.rewriteRef "refs/notes/*" &&
733 git notes add -m"a note" n3 &&
735 git rebase -i --onto n1 n2 &&
736 test "a note" = "$(git notes show HEAD)"
745 test_expect_success
'rebase -i can copy notes over a fixup' '
746 git reset --hard n3 &&
747 git notes add -m"an earlier note" n2 &&
749 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
750 git notes show > output &&
751 test_cmp expect output
754 test_expect_success
'rebase while detaching HEAD' '
755 git symbolic-ref HEAD &&
756 grandparent=$(git rev-parse HEAD~2) &&
759 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
760 test $grandparent = $(git rev-parse HEAD~2) &&
761 test_must_fail git symbolic-ref HEAD
764 test_tick
# Ensure that the rebased commits get a different timestamp.
765 test_expect_success
'always cherry-pick with --no-ff' '
766 git checkout no-ff-branch &&
767 git tag original-no-ff-branch &&
769 git rebase -i --no-ff A &&
773 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
774 git diff HEAD~$p original-no-ff-branch~$p > out &&
777 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
778 git diff HEAD~3 original-no-ff-branch~3 > out &&
782 test_expect_success
'set up commits with funny messages' '
783 git checkout -b funny A &&
786 git commit -a -m "end with slash\\" &&
789 git commit -a -m "something (\000) that looks like octal" &&
792 git commit -a -m "something (\n) that looks like a newline" &&
795 git commit -a -m "another commit"
798 test_expect_success
'rebase-i history with funny messages' '
799 git rev-list A..funny >expect &&
802 FAKE_LINES="1 2 3 4" git rebase -i A &&
803 git rev-list A.. >actual &&
804 test_cmp expect actual
807 test_expect_success
'prepare for rebase -i --exec' '
808 git checkout master &&
809 git checkout -b execute &&
810 test_commit one_exec main.txt one_exec &&
811 test_commit two_exec main.txt two_exec &&
812 test_commit three_exec main.txt three_exec
815 test_expect_success
'running "git rebase -i --exec git show HEAD"' '
817 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
819 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
821 git rebase -i HEAD~2 >expect
823 sed -e "1,9d" expect >expected &&
824 test_cmp expected actual
827 test_expect_success
'running "git rebase --exec git show HEAD -i"' '
828 git reset --hard execute &&
830 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
832 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
834 git rebase -i HEAD~2 >expect
836 sed -e "1,9d" expect >expected &&
837 test_cmp expected actual
840 test_expect_success
'running "git rebase -ix git show HEAD"' '
841 git reset --hard execute &&
843 git rebase -ix "git show HEAD" HEAD~2 >actual &&
845 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
847 git rebase -i HEAD~2 >expect
849 sed -e "1,9d" expect >expected &&
850 test_cmp expected actual
854 test_expect_success
'rebase -ix with several <CMD>' '
855 git reset --hard execute &&
857 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
859 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
861 git rebase -i HEAD~2 >expect
863 sed -e "1,9d" expect >expected &&
864 test_cmp expected actual
867 test_expect_success
'rebase -ix with several instances of --exec' '
868 git reset --hard execute &&
870 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
872 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
873 exec_git_show_HEAD exec_pwd" &&
875 git rebase -i HEAD~2 >expect
877 sed -e "1,11d" expect >expected &&
878 test_cmp expected actual
881 test_expect_success C_LOCALE_OUTPUT
'rebase -ix with --autosquash' '
882 git reset --hard execute &&
883 git checkout -b autosquash &&
884 echo second >second.txt &&
885 git add second.txt &&
886 git commit -m "fixup! two_exec" &&
889 git commit -m "fixup! two_exec" &&
892 git checkout -b autosquash_actual &&
893 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
895 git checkout autosquash &&
897 git checkout -b autosquash_expected &&
898 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
900 git rebase -i HEAD~4 >expect
902 sed -e "1,13d" expect >expected &&
903 test_cmp expected actual
906 test_expect_success
'rebase --exec works without -i ' '
907 git reset --hard execute &&
908 rm -rf exec_output &&
909 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
910 test_i18ngrep "Successfully rebased and updated" actual &&
911 test_line_count = 2 exec_output &&
912 test_path_is_missing invoked_editor
915 test_expect_success
'rebase -i --exec without <CMD>' '
916 git reset --hard execute &&
918 test_must_fail git rebase -i --exec 2>tmp &&
919 sed -e "1d" tmp >actual &&
920 test_must_fail git rebase -h >expected &&
921 test_cmp expected actual &&
925 test_expect_success
'rebase -i --root re-order and drop commits' '
928 FAKE_LINES="3 1 2 5" git rebase -i --root &&
929 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
930 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
931 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
932 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
933 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
936 test_expect_success
'rebase -i --root retain root commit author and message' '
940 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
942 FAKE_LINES="2" git rebase -i --root &&
943 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
944 git cat-file commit HEAD | grep -q "^different author$"
947 test_expect_success
'rebase -i --root temporary sentinel commit' '
950 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
951 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
955 test_expect_success
'rebase -i --root fixup root commit' '
958 FAKE_LINES="1 fixup 2" git rebase -i --root &&
959 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
960 test B = $(git show HEAD:file1) &&
961 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
964 test_expect_success C_LOCALE_OUTPUT
'rebase --edit-todo does not work on non-interactive rebase' '
966 git checkout conflict-branch &&
968 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
969 test_must_fail git rebase --edit-todo &&
973 test_expect_success
'rebase --edit-todo can be used to modify todo' '
975 git checkout no-conflict-branch^0 &&
977 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
978 FAKE_LINES="2 1" git rebase --edit-todo &&
979 git rebase --continue &&
980 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
981 test L = $(git cat-file commit HEAD | sed -ne \$p)
984 test_expect_success
'rebase -i produces readable reflog' '
986 git branch -f branch-reflog-test H &&
988 git rebase -i --onto I F branch-reflog-test &&
989 cat >expect <<-\EOF &&
990 rebase -i (finish): returning to refs/heads/branch-reflog-test
993 rebase -i (start): checkout I
995 git reflog -n4 HEAD |
996 sed "s/[^:]*: //" >actual &&
997 test_cmp expect actual
1000 test_expect_success
'rebase -i respects core.commentchar' '
1003 test_config core.commentchar "\\" &&
1004 write_script remove-all-but-first.sh <<-\EOF &&
1005 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1008 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1010 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1013 test_expect_success
'rebase -i respects core.commentchar=auto' '
1014 test_config core.commentchar auto &&
1015 write_script copy-edit-script.sh <<-\EOF &&
1018 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1019 test_when_finished "git rebase --abort || :" &&
1020 git rebase -i HEAD^ &&
1021 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1024 test_expect_success
'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1025 test_when_finished "git branch -D torebase" &&
1026 git checkout -b torebase branch1 &&
1027 upstream=$(git rev-parse ":/J") &&
1028 onto=$(git rev-parse ":/A") &&
1029 git rebase --onto $onto $upstream &&
1030 git reset --hard branch1 &&
1031 git rebase --onto ":/A" ":/J" &&
1032 git checkout branch1
1035 test_expect_success
'rebase -i with --strategy and -X' '
1036 git checkout -b conflict-merge-use-theirs conflict-branch &&
1037 git reset --hard HEAD^ &&
1038 echo five >conflict &&
1040 git commit -a -m "one file conflict" &&
1041 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1042 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1043 test $(cat file1) = Z
1046 test_expect_success
'interrupted rebase -i with --strategy and -X' '
1047 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1048 git reset --hard HEAD^ &&
1050 git add breakpoint &&
1051 git commit -m "breakpoint for interactive mode" &&
1052 echo five >conflict &&
1054 git commit -a -m "one file conflict" &&
1056 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1057 git rebase --continue &&
1058 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1059 test $(cat file1) = Z
1062 test_expect_success
'rebase -i error on commits with \ in message' '
1063 current_head=$(git rev-parse HEAD) &&
1064 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1065 test_commit TO-REMOVE will-conflict old-content &&
1066 test_commit "\temp" will-conflict new-content dummy &&
1067 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1068 test_expect_code 1 grep " emp" error
1071 test_expect_success
'short SHA-1 setup' '
1072 test_when_finished "git checkout master" &&
1073 git checkout --orphan collide &&
1077 test_commit collide1 collide &&
1078 test_commit --notick collide2 collide &&
1079 test_commit --notick collide3 collide
1083 test_expect_success
'short SHA-1 collide' '
1084 test_when_finished "reset_rebase && git checkout master" &&
1085 git checkout collide &&
1090 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1091 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1095 test_expect_success
'respect core.abbrev' '
1096 git config core.abbrev 12 &&
1097 set_cat_todo_editor &&
1098 test_must_fail git rebase -i HEAD~4 >todo-list &&
1099 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1102 test_expect_success
'todo count' '
1103 write_script dump-raw.sh <<-\EOF &&
1106 test_set_editor "$(pwd)/dump-raw.sh" &&
1107 git rebase -i HEAD~4 >actual &&
1108 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1111 test_expect_success
'rebase -i commits that overwrite untracked files (pick)' '
1112 git checkout --force branch2 &&
1115 FAKE_LINES="edit 1 2" git rebase -i A &&
1116 test_cmp_rev HEAD F &&
1117 test_path_is_missing file6 &&
1119 test_must_fail git rebase --continue &&
1120 test_cmp_rev HEAD F &&
1122 git rebase --continue &&
1126 test_expect_success
'rebase -i commits that overwrite untracked files (squash)' '
1127 git checkout --force branch2 &&
1129 git tag original-branch2 &&
1131 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1132 test_cmp_rev HEAD F &&
1133 test_path_is_missing file6 &&
1135 test_must_fail git rebase --continue &&
1136 test_cmp_rev HEAD F &&
1138 git rebase --continue &&
1139 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1140 git reset --hard original-branch2
1143 test_expect_success
'rebase -i commits that overwrite untracked files (no ff)' '
1144 git checkout --force branch2 &&
1147 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1148 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1149 test_path_is_missing file6 &&
1151 test_must_fail git rebase --continue &&
1152 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1154 git rebase --continue &&
1155 test $(git cat-file commit HEAD | sed -ne \$p) = I
1158 test_expect_success
'rebase --continue removes CHERRY_PICK_HEAD' '
1159 git checkout -b commit-to-skip &&
1162 test_seq 5 | sed "s/$double/&&/" >seq &&
1165 git commit -m seq-$double
1168 git reset --hard HEAD~2 &&
1169 git cherry-pick seq-onto &&
1171 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1172 test -d .git/rebase-merge &&
1173 git rebase --continue &&
1174 git diff --exit-code seq-onto &&
1175 test ! -d .git/rebase-merge &&
1176 test ! -f .git/CHERRY_PICK_HEAD
1179 rebase_setup_and_clean
() {
1180 test_when_finished
"
1181 git checkout master &&
1182 test_might_fail git branch -D $1 &&
1183 test_might_fail git rebase --abort
1185 git checkout
-b $1 master
1188 test_expect_success
'drop' '
1189 rebase_setup_and_clean drop-test &&
1191 FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1192 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1193 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1194 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1198 Successfully rebased and updated refs/heads/missing-commit.
1201 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = ignore' '
1202 test_config rebase.missingCommitsCheck ignore &&
1203 rebase_setup_and_clean missing-commit &&
1205 FAKE_LINES="1 2 3 4" \
1206 git rebase -i --root 2>actual &&
1207 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1208 test_i18ncmp expect actual
1212 Warning: some commits may have been dropped accidentally.
1213 Dropped commits (newer to older):
1214 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1215 To avoid this message, use "drop" to explicitly remove a commit.
1217 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1218 The possible behaviours are: ignore, warn, error.
1220 Successfully rebased and updated refs/heads/missing-commit.
1223 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = warn' '
1224 test_config rebase.missingCommitsCheck warn &&
1225 rebase_setup_and_clean missing-commit &&
1227 FAKE_LINES="1 2 3 4" \
1228 git rebase -i --root 2>actual &&
1229 test_i18ncmp expect actual &&
1230 test D = $(git cat-file commit HEAD | sed -ne \$p)
1234 Warning: some commits may have been dropped accidentally.
1235 Dropped commits (newer to older):
1236 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1237 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1238 To avoid this message, use "drop" to explicitly remove a commit.
1240 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1241 The possible behaviours are: ignore, warn, error.
1243 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1244 Or you can abort the rebase with 'git rebase --abort'.
1247 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = error' '
1248 test_config rebase.missingCommitsCheck error &&
1249 rebase_setup_and_clean missing-commit &&
1251 test_must_fail env FAKE_LINES="1 2 4" \
1252 git rebase -i --root 2>actual &&
1253 test_i18ncmp expect actual &&
1254 cp .git/rebase-merge/git-rebase-todo.backup \
1255 .git/rebase-merge/git-rebase-todo &&
1256 FAKE_LINES="1 2 drop 3 4 drop 5" \
1257 git rebase --edit-todo &&
1258 git rebase --continue &&
1259 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1260 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1263 test_expect_success
'static check of bad command' '
1264 rebase_setup_and_clean bad-cmd &&
1266 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1267 git rebase -i --root 2>actual &&
1268 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
1269 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1270 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1271 git rebase --continue &&
1272 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1273 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1276 test_expect_success
'tabs and spaces are accepted in the todolist' '
1277 rebase_setup_and_clean indented-comment &&
1278 write_script add-indent.sh <<-\EOF &&
1280 # Turn single spaces into space/tab mix
1281 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1282 printf "\n\t# comment\n #more\n\t # comment\n"
1286 test_set_editor "$(pwd)/add-indent.sh" &&
1287 git rebase -i HEAD^^^ &&
1288 test E = $(git cat-file commit HEAD | sed -ne \$p)
1291 test_expect_success
'static check of bad SHA-1' '
1292 rebase_setup_and_clean bad-sha &&
1294 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1295 git rebase -i --root 2>actual &&
1296 test_i18ngrep "edit XXXXXXX False commit" actual &&
1297 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1298 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1299 git rebase --continue &&
1300 test E = $(git cat-file commit HEAD | sed -ne \$p)
1303 test_expect_success
'editor saves as CR/LF' '
1304 git checkout -b with-crlf &&
1305 write_script add-crs.sh <<-\EOF &&
1306 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1310 test_set_editor "$(pwd)/add-crs.sh" &&
1316 test_expect_success
'rebase -i --gpg-sign=<key-id>' '
1318 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1320 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err