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 (primary)
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".
31 .
"$TEST_DIRECTORY"/lib-rebase.sh
33 test_expect_success
'setup' '
34 git switch -C primary &&
35 test_commit A file1 &&
36 test_commit B file1 &&
37 test_commit C file2 &&
38 test_commit D file1 &&
39 test_commit E file3 &&
40 git checkout -b branch1 A &&
41 test_commit F file4 &&
42 test_commit G file1 &&
43 test_commit H file5 &&
44 git checkout -b branch2 F &&
45 test_commit I file6 &&
46 git checkout -b conflict-branch A &&
47 test_commit one conflict &&
48 test_commit two conflict &&
49 test_commit three conflict &&
50 test_commit four conflict &&
51 git checkout -b no-conflict-branch A &&
52 test_commit J fileJ &&
53 test_commit K fileK &&
54 test_commit L fileL &&
55 test_commit M fileM &&
56 git checkout -b no-ff-branch A &&
57 test_commit N fileN &&
58 test_commit O fileO &&
62 # "exec" commands are run with the user shell by default, but this may
63 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
64 # to create a file. Unsetting SHELL avoids such non-portable behavior
65 # in tests. It must be exported for it to take effect where needed.
69 test_expect_success
'rebase --keep-empty' '
70 git checkout -b emptybranch primary &&
71 git commit --allow-empty -m "empty" &&
72 git rebase --keep-empty -i HEAD~2 &&
73 git log --oneline >actual &&
74 test_line_count = 6 actual
77 test_expect_success
'rebase -i with empty todo list' '
78 cat >expect <<-\EOF &&
83 test_must_fail env FAKE_LINES="#" \
84 git rebase -i HEAD^ >output 2>&1
86 tail -n 1 output >actual && # Ignore output about changing todo list
87 test_cmp expect actual
90 test_expect_success
'rebase -i with the exec command' '
91 git checkout primary &&
94 FAKE_LINES="1 exec_>touch-one
95 2 exec_>touch-two exec_false exec_>touch-three
96 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
98 test_must_fail git rebase -i A
100 test_path_is_file touch-one &&
101 test_path_is_file touch-two &&
102 # Missing because we should have stopped by now.
103 test_path_is_missing touch-three &&
104 test_cmp_rev C HEAD &&
105 git rebase --continue &&
106 test_path_is_file touch-three &&
107 test_path_is_file "touch-file name with spaces" &&
108 test_path_is_file touch-after-semicolon &&
109 test_cmp_rev primary HEAD &&
113 test_expect_success
'rebase -i with the exec command runs from tree root' '
114 git checkout primary &&
115 mkdir subdir && (cd subdir &&
117 FAKE_LINES="1 exec_>touch-subdir" \
120 test_path_is_file touch-subdir &&
124 test_expect_success
'rebase -i with exec allows git commands in subdirs' '
125 test_when_finished "rm -rf subdir" &&
126 test_when_finished "git rebase --abort ||:" &&
127 git checkout primary &&
128 mkdir subdir && (cd subdir &&
130 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
135 test_expect_success
'rebase -i sets work tree properly' '
136 test_when_finished "rm -rf subdir" &&
137 test_when_finished "test_might_fail git rebase --abort" &&
139 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
141 ! grep "/subdir$" actual
144 test_expect_success
'rebase -i with the exec command checks tree cleanness' '
145 git checkout primary &&
148 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
151 test_cmp_rev primary^ HEAD &&
153 git rebase --continue
156 test_expect_success
'cherry-pick works with rebase --exec' '
157 test_when_finished "git cherry-pick --abort; \
158 git rebase --abort; \
159 git checkout primary" &&
160 echo "exec git cherry-pick G" >todo &&
162 set_replace_editor todo &&
163 test_must_fail git rebase -i D D
165 test_cmp_rev G CHERRY_PICK_HEAD
168 test_expect_success
'rebase -x with empty command fails' '
169 test_when_finished "git rebase --abort ||:" &&
170 test_must_fail env git rebase -x "" @ 2>actual &&
171 test_write_lines "error: empty exec command" >expected &&
172 test_cmp expected actual &&
173 test_must_fail env git rebase -x " " @ 2>actual &&
174 test_cmp expected actual
177 test_expect_success
'rebase -x with newline in command fails' '
178 test_when_finished "git rebase --abort ||:" &&
179 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
180 test_write_lines "error: exec commands cannot contain newlines" \
182 test_cmp expected actual
185 test_expect_success
'rebase -i with exec of inexistent command' '
186 git checkout primary &&
187 test_when_finished "git rebase --abort" &&
190 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
191 git rebase -i HEAD^ >actual 2>&1
193 ! grep "Maybe git-rebase is broken" actual
196 test_expect_success
'implicit interactive rebase does not invoke sequence editor' '
197 test_when_finished "git rebase --abort ||:" &&
198 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
201 test_expect_success
'no changes are a nop' '
202 git checkout branch2 &&
204 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
208 test_expect_success
'test the [branch] option' '
209 git checkout -b dead-end &&
211 git commit -m "stop here" &&
212 git rebase -i F branch2 &&
213 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
214 test_cmp_rev I branch2 &&
218 test_expect_success
'test --onto <branch>' '
219 git checkout -b test-onto branch2 &&
220 git rebase -i --onto branch1 F &&
221 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
222 test_cmp_rev HEAD^ branch1 &&
223 test_cmp_rev I branch2
226 test_expect_success
'rebase on top of a non-conflicting commit' '
227 git checkout branch1 &&
228 git tag original-branch1 &&
229 git rebase -i branch2 &&
230 test file6 = $(git diff --name-only original-branch1) &&
231 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
232 test_cmp_rev I branch2 &&
233 test_cmp_rev I HEAD~2
236 test_expect_success
'reflog for the branch shows state before rebase' '
237 test_cmp_rev branch1@{1} original-branch1
240 test_expect_success
'reflog for the branch shows correct finish message' '
241 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
242 "$(git rev-parse branch2)" >expected &&
243 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
244 test_cmp expected actual
247 test_expect_success
'exchange two commits' '
250 FAKE_LINES="2 1" git rebase -i HEAD~2
252 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
253 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
254 blob1=$(git rev-parse --short HEAD^:file1) &&
255 blob2=$(git rev-parse --short HEAD:file1) &&
256 commit=$(git rev-parse --short HEAD)
259 test_expect_success
'stop on conflicting pick' '
260 cat >expect <<-EOF &&
261 diff --git a/file1 b/file1
262 index $blob1..$blob2 100644
269 cat >expect2 <<-EOF &&
276 git tag new-branch1 &&
277 test_must_fail git rebase -i primary &&
278 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
279 test_cmp expect .git/rebase-merge/patch &&
280 test_cmp expect2 file1 &&
281 test "$(git diff --name-status |
282 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
283 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
284 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
287 test_expect_success
'show conflicted patch' '
288 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
289 grep "show.*REBASE_HEAD" stderr &&
290 # the original stopped-sha1 is abbreviated
291 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
292 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
295 test_expect_success
'abort' '
296 git rebase --abort &&
297 test_cmp_rev new-branch1 HEAD &&
298 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
299 test_path_is_missing .git/rebase-merge
302 test_expect_success
'abort with error when new base cannot be checked out' '
303 git rm --cached file1 &&
304 git commit -m "remove file in base" &&
305 test_must_fail git rebase -i primary > output 2>&1 &&
306 test_grep "The following untracked working tree files would be overwritten by checkout:" \
308 test_grep "file1" output &&
309 test_path_is_missing .git/rebase-merge &&
311 git reset --hard HEAD^
314 test_expect_success
'retain authorship' '
318 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
320 git rebase -i --onto primary HEAD^ &&
321 git show HEAD | grep "^Author: Twerp Snog"
324 test_expect_success
'retain authorship w/ conflicts' '
325 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
326 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
328 git reset --hard twerp &&
329 test_commit a conflict a conflict-a &&
330 git reset --hard twerp &&
332 GIT_AUTHOR_NAME=AttributeMe &&
333 export GIT_AUTHOR_NAME &&
334 test_commit b conflict b conflict-b &&
335 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
337 test_must_fail git rebase -i conflict-a &&
338 echo resolved >conflict &&
340 git rebase --continue &&
341 test_cmp_rev conflict-a^0 HEAD^ &&
346 test_expect_success
'squash' '
347 git reset --hard twerp &&
350 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
351 echo "******************************" &&
354 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
355 git rebase -i --onto primary HEAD~2
357 test B = $(cat file7) &&
358 test_cmp_rev HEAD^ primary
361 test_expect_success
'retain authorship when squashing' '
362 git show HEAD | grep "^Author: Twerp Snog"
365 test_expect_success
'--continue tries to commit' '
366 git reset --hard D &&
370 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
371 echo resolved > file1 &&
373 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
375 test_cmp_rev HEAD^ new-branch1 &&
376 git show HEAD | grep chouette
379 test_expect_success
'verbose flag is heeded, even after --continue' '
380 git reset --hard primary@{1} &&
382 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
383 echo resolved > file1 &&
385 git rebase --continue > output &&
386 grep "^ file1 | 2 +-$" output
389 test_expect_success
'multi-squash only fires up editor once' '
390 base=$(git rev-parse HEAD~4) &&
393 FAKE_COMMIT_AMEND="ONCE" \
394 FAKE_LINES="1 squash 2 squash 3 squash 4" \
395 EXPECT_HEADER_COUNT=4 \
398 test $base = $(git rev-parse HEAD^) &&
399 test 1 = $(git show | grep ONCE | wc -l)
402 test_expect_success
'multi-fixup does not fire up editor' '
403 git checkout -b multi-fixup E &&
404 base=$(git rev-parse HEAD~4) &&
407 FAKE_COMMIT_AMEND="NEVER" \
408 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
411 test $base = $(git rev-parse HEAD^) &&
412 test 0 = $(git show | grep NEVER | wc -l) &&
413 git checkout @{-1} &&
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) &&
422 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
423 git rebase -i $base &&
424 echo three > conflict &&
426 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
427 git rebase --continue
429 test $base = $(git rev-parse HEAD^) &&
430 test 1 = $(git show | grep ONCE | wc -l) &&
431 git checkout @{-1} &&
432 git branch -D conflict-fixup
435 test_expect_success
'commit message retained after conflict' '
436 git checkout -b conflict-squash conflict-branch &&
437 base=$(git rev-parse HEAD~4) &&
440 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
441 git rebase -i $base &&
442 echo three > conflict &&
444 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
445 git rebase --continue
447 test $base = $(git rev-parse HEAD^) &&
448 test 2 = $(git show | grep TWICE | wc -l) &&
449 git checkout @{-1} &&
450 git branch -D conflict-squash
453 test_expect_success
'squash and fixup generate correct log messages' '
454 cat >expect-squash-fixup <<-\EOF &&
461 git checkout -b squash-fixup E &&
462 base=$(git rev-parse HEAD~4) &&
465 FAKE_COMMIT_AMEND="ONCE" \
466 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
467 EXPECT_HEADER_COUNT=4 \
470 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
471 test_cmp expect-squash-fixup actual-squash-fixup &&
472 git cat-file commit HEAD@{2} |
473 grep "^# This is a combination of 3 commits\." &&
474 git cat-file commit HEAD@{3} |
475 grep "^# This is a combination of 2 commits\." &&
476 git checkout @{-1} &&
477 git branch -D squash-fixup
480 test_expect_success
'squash ignores comments' '
481 git checkout -b skip-comments E &&
482 base=$(git rev-parse HEAD~4) &&
485 FAKE_COMMIT_AMEND="ONCE" \
486 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
487 EXPECT_HEADER_COUNT=4 \
490 test $base = $(git rev-parse HEAD^) &&
491 test 1 = $(git show | grep ONCE | wc -l) &&
492 git checkout @{-1} &&
493 git branch -D skip-comments
496 test_expect_success
'squash ignores blank lines' '
497 git checkout -b skip-blank-lines E &&
498 base=$(git rev-parse HEAD~4) &&
501 FAKE_COMMIT_AMEND="ONCE" \
502 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
503 EXPECT_HEADER_COUNT=4 \
506 test $base = $(git rev-parse HEAD^) &&
507 test 1 = $(git show | grep ONCE | wc -l) &&
508 git checkout @{-1} &&
509 git branch -D skip-blank-lines
512 test_expect_success
'squash works as expected' '
513 git checkout -b squash-works no-conflict-branch &&
514 one=$(git rev-parse HEAD~3) &&
517 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
519 test $one = $(git rev-parse HEAD~2)
522 test_expect_success
'interrupted squash works as expected' '
523 git checkout -b interrupted-squash conflict-branch &&
524 one=$(git rev-parse HEAD~3) &&
527 test_must_fail env FAKE_LINES="1 squash 3 2" \
530 test_write_lines one two four > conflict &&
532 test_must_fail git rebase --continue &&
533 echo resolved > conflict &&
535 git rebase --continue &&
536 test $one = $(git rev-parse HEAD~2)
539 test_expect_success
'interrupted squash works as expected (case 2)' '
540 git checkout -b interrupted-squash2 conflict-branch &&
541 one=$(git rev-parse HEAD~3) &&
544 test_must_fail env FAKE_LINES="3 squash 1 2" \
547 test_write_lines one four > conflict &&
549 test_must_fail git rebase --continue &&
550 test_write_lines one two four > conflict &&
552 test_must_fail git rebase --continue &&
553 echo resolved > conflict &&
555 git rebase --continue &&
556 test $one = $(git rev-parse HEAD~2)
559 test_expect_success
'--continue tries to commit, even for "edit"' '
560 echo unrelated > file7 &&
563 git commit -m "unrelated change" &&
564 parent=$(git rev-parse HEAD^) &&
568 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
569 echo edited > file7 &&
571 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
573 test edited = $(git show HEAD:file7) &&
574 git show HEAD | grep chouette &&
575 test $parent = $(git rev-parse HEAD^)
578 test_expect_success
'aborted --continue does not squash commits after "edit"' '
579 old=$(git rev-parse HEAD) &&
583 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
584 echo "edited again" > file7 &&
586 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
588 test $old = $(git rev-parse HEAD) &&
592 test_expect_success
'auto-amend only edited commits after "edit"' '
596 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
597 echo "edited again" > file7 &&
599 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
600 echo "and again" > file7 &&
603 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
604 git rebase --continue
609 test_expect_success
'clean error after failed "exec"' '
611 test_when_finished "git rebase --abort || :" &&
614 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
616 echo "edited again" > file7 &&
618 test_must_fail git rebase --continue 2>error &&
619 test_grep "you have staged changes in your working tree" error &&
620 test_grep ! "could not open.*for reading" error
623 test_expect_success
'rebase a detached HEAD' '
624 grandparent=$(git rev-parse HEAD~2) &&
625 git checkout $(git rev-parse HEAD) &&
629 FAKE_LINES="2 1" git rebase -i HEAD~2
631 test $grandparent = $(git rev-parse HEAD~2)
634 test_expect_success
'rebase a commit violating pre-commit' '
635 test_hook pre-commit <<-\EOF &&
636 test -z "$(git diff --cached --check)"
638 echo "monde! " >> file1 &&
640 test_must_fail git commit -m doesnt-verify file1 &&
641 git commit -m doesnt-verify --no-verify file1 &&
645 FAKE_LINES=2 git rebase -i HEAD~2
649 test_expect_success
'rebase with a file named HEAD in worktree' '
651 git checkout -b branch3 A &&
654 GIT_AUTHOR_NAME="Squashed Away" &&
655 export GIT_AUTHOR_NAME &&
658 git commit -m "Add head" &&
661 git commit -m "Add body"
666 FAKE_LINES="1 squash 2" git rebase -i @{-1}
668 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
672 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
674 git checkout -b branch4 HEAD &&
675 GIT_EDITOR=: git commit --amend \
676 --author="Somebody else <somebody@else.com>" &&
677 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
678 git rebase -i branch3 &&
679 test_cmp_rev branch3 branch4
683 test_expect_success
'submodule rebase setup' '
687 cd sub && git init && >elif &&
688 git add elif && git commit -m "submodule initial"
693 git commit -m "One" &&
696 git commit -a -m "Two" &&
698 cd sub && echo 3 >elif &&
699 git commit -a -m "submodule second"
702 git commit -a -m "Three changes submodule"
705 test_expect_success
'submodule rebase -i' '
708 FAKE_LINES="1 squash 2 3" git rebase -i A
712 test_expect_success
'submodule conflict setup' '
713 git tag submodule-base &&
714 git checkout HEAD^ &&
716 cd sub && git checkout HEAD^ && echo 4 >elif &&
717 git add elif && git commit -m "submodule conflict"
721 git commit -m "Conflict in submodule" &&
722 git tag submodule-topic
725 test_expect_success
'rebase -i continue with only submodule staged' '
726 test_must_fail git rebase -i submodule-base &&
728 git rebase --continue &&
729 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
732 test_expect_success
'rebase -i continue with unstaged submodule' '
733 git checkout submodule-topic &&
735 test_must_fail git rebase -i submodule-base &&
737 git rebase --continue &&
738 test_cmp_rev submodule-base HEAD
741 test_expect_success
'avoid unnecessary reset' '
742 git checkout primary &&
744 test-tool chmtime =123456789 file3 &&
745 git update-index --refresh &&
746 HEAD=$(git rev-parse HEAD) &&
747 git rebase -i HEAD~4 &&
748 test $HEAD = $(git rev-parse HEAD) &&
749 MTIME=$(test-tool chmtime --get file3) &&
750 test 123456789 = $MTIME
753 test_expect_success
'reword' '
754 git checkout -b reword-branch primary &&
757 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
759 git show HEAD | grep "E changed" &&
760 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
761 test_cmp_rev primary^ HEAD^ &&
762 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
764 git show HEAD^ | grep "D changed" &&
765 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
767 git show HEAD~3 | grep "B changed" &&
768 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
771 git show HEAD~2 | grep "C changed"
774 test_expect_success
'no uncommitted changes when rewording and the todo list is reloaded' '
776 test_when_finished "git checkout @{-1}" &&
779 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
780 export GIT_SEQUENCE_EDITOR &&
782 FAKE_LINES="reword 1 reword 2" git rebase -i C
784 check_reworded_commits D E
787 test_expect_success
'rebase -i can copy notes' '
788 git config notes.rewrite.rebase true &&
789 git config notes.rewriteRef "refs/notes/*" &&
793 git notes add -m"a note" n3 &&
794 git rebase -i --onto n1 n2 &&
795 test "a note" = "$(git notes show HEAD)"
798 test_expect_success
'rebase -i can copy notes over a fixup' '
799 cat >expect <<-\EOF &&
804 git reset --hard n3 &&
805 git notes add -m"an earlier note" n2 &&
808 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
811 git notes show > output &&
812 test_cmp expect output
815 test_expect_success
'rebase while detaching HEAD' '
816 git symbolic-ref HEAD &&
817 grandparent=$(git rev-parse HEAD~2) &&
821 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
823 test $grandparent = $(git rev-parse HEAD~2) &&
824 test_must_fail git symbolic-ref HEAD
827 test_tick
# Ensure that the rebased commits get a different timestamp.
828 test_expect_success
'always cherry-pick with --no-ff' '
829 git checkout no-ff-branch &&
830 git tag original-no-ff-branch &&
831 git rebase -i --no-ff A &&
834 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
835 git diff HEAD~$p original-no-ff-branch~$p > out &&
836 test_must_be_empty out || return 1
838 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
839 git diff HEAD~3 original-no-ff-branch~3 > out &&
840 test_must_be_empty out
843 test_expect_success
'set up commits with funny messages' '
844 git checkout -b funny A &&
847 git commit -a -m "end with slash\\" &&
850 git commit -a -m "something (\000) that looks like octal" &&
853 git commit -a -m "something (\n) that looks like a newline" &&
856 git commit -a -m "another commit"
859 test_expect_success
'rebase-i history with funny messages' '
860 git rev-list A..funny >expect &&
864 FAKE_LINES="1 2 3 4" git rebase -i A
866 git rev-list A.. >actual &&
867 test_cmp expect actual
870 test_expect_success
'prepare for rebase -i --exec' '
871 git checkout primary &&
872 git checkout -b execute &&
873 test_commit one_exec main.txt one_exec &&
874 test_commit two_exec main.txt two_exec &&
875 test_commit three_exec main.txt three_exec
878 test_expect_success
'running "git rebase -i --exec git show HEAD"' '
881 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
882 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
884 git rebase -i HEAD~2 >expect
886 sed -e "1,9d" expect >expected &&
887 test_cmp expected actual
890 test_expect_success
'running "git rebase --exec git show HEAD -i"' '
891 git reset --hard execute &&
894 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
895 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
897 git rebase -i HEAD~2 >expect
899 sed -e "1,9d" expect >expected &&
900 test_cmp expected actual
903 test_expect_success
'running "git rebase -ix git show HEAD"' '
904 git reset --hard execute &&
907 git rebase -ix "git show HEAD" HEAD~2 >actual &&
908 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
910 git rebase -i HEAD~2 >expect
912 sed -e "1,9d" expect >expected &&
913 test_cmp expected actual
917 test_expect_success
'rebase -ix with several <CMD>' '
918 git reset --hard execute &&
921 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
922 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
924 git rebase -i HEAD~2 >expect
926 sed -e "1,9d" expect >expected &&
927 test_cmp expected actual
930 test_expect_success
'rebase -ix with several instances of --exec' '
931 git reset --hard execute &&
934 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
935 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
936 exec_git_show_HEAD exec_pwd" &&
938 git rebase -i HEAD~2 >expect
940 sed -e "1,11d" expect >expected &&
941 test_cmp expected actual
944 test_expect_success
'rebase -ix with --autosquash' '
945 git reset --hard execute &&
946 git checkout -b autosquash &&
947 echo second >second.txt &&
948 git add second.txt &&
949 git commit -m "fixup! two_exec" &&
952 git commit -m "fixup! two_exec" &&
953 git checkout -b autosquash_actual &&
954 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
955 git checkout autosquash &&
958 git checkout -b autosquash_expected &&
959 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
961 git rebase -i HEAD~4 >expect
963 sed -e "1,13d" expect >expected &&
964 test_cmp expected actual
967 test_expect_success
'rebase --exec works without -i ' '
968 git reset --hard execute &&
969 rm -rf exec_output &&
970 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
971 test_grep "Successfully rebased and updated" actual &&
972 test_line_count = 2 exec_output &&
973 test_path_is_missing invoked_editor
976 test_expect_success
'rebase -i --exec without <CMD>' '
977 git reset --hard execute &&
978 test_must_fail git rebase -i --exec 2>actual &&
979 test_grep "requires a value" actual &&
983 test_expect_success
'rebase -i --root re-order and drop commits' '
987 FAKE_LINES="3 1 2 5" git rebase -i --root
989 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
990 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
991 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
992 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
993 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
996 test_expect_success
'rebase -i --root retain root commit author and message' '
1000 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1003 FAKE_LINES="2" git rebase -i --root
1005 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1006 git cat-file commit HEAD | grep -q "^different author$"
1009 test_expect_success
'rebase -i --root temporary sentinel commit' '
1013 test_must_fail env FAKE_LINES="2" git rebase -i --root
1015 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1019 test_expect_success
'rebase -i --root fixup root commit' '
1023 FAKE_LINES="1 fixup 2" git rebase -i --root
1025 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1026 test B = $(git show HEAD:file1) &&
1027 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1030 test_expect_success
'rebase -i --root reword original root commit' '
1031 test_when_finished "test_might_fail git rebase --abort" &&
1032 git checkout -b reword-original-root-branch primary &&
1035 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1036 git rebase -i --root
1038 git show HEAD^ | grep "A changed" &&
1039 test -z "$(git show -s --format=%p HEAD^)"
1042 test_expect_success
'rebase -i --root reword new root commit' '
1043 test_when_finished "test_might_fail git rebase --abort" &&
1044 git checkout -b reword-now-root-branch primary &&
1047 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1048 git rebase -i --root
1050 git show HEAD^ | grep "C changed" &&
1051 test -z "$(git show -s --format=%p HEAD^)"
1054 test_expect_success
'rebase -i --root when root has untracked file conflict' '
1055 test_when_finished "reset_rebase" &&
1056 git checkout -b failing-root-pick A &&
1059 git commit -m "remove file 1 add file 2" &&
1063 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1066 git rebase --continue &&
1067 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1068 test "$(git rev-list --count HEAD)" = 2
1071 test_expect_success
'rebase -i --root reword root when root has untracked file conflict' '
1072 test_when_finished "reset_rebase" &&
1076 test_must_fail env FAKE_LINES="reword 1 2" \
1077 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1079 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1081 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1082 test "$(git rev-list --count HEAD)" = 2
1085 test_expect_success
'rebase --edit-todo does not work on non-interactive rebase' '
1086 git checkout reword-original-root-branch &&
1088 git checkout conflict-branch &&
1091 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1092 test_must_fail git rebase --edit-todo
1097 test_expect_success
'rebase --edit-todo can be used to modify todo' '
1099 git checkout no-conflict-branch^0 &&
1102 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1103 FAKE_LINES="2 1" git rebase --edit-todo &&
1104 git rebase --continue
1106 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1107 test L = $(git cat-file commit HEAD | sed -ne \$p)
1110 test_expect_success
'rebase -i produces readable reflog' '
1112 git branch -f branch-reflog-test H &&
1113 git rebase -i --onto I F branch-reflog-test &&
1114 cat >expect <<-\EOF &&
1115 rebase (finish): returning to refs/heads/branch-reflog-test
1118 rebase (start): checkout I
1120 git reflog -n4 HEAD |
1121 sed "s/[^:]*: //" >actual &&
1122 test_cmp expect actual
1125 test_expect_success
'rebase -i respects core.commentchar' '
1128 test_config core.commentchar "\\" &&
1129 write_script remove-all-but-first.sh <<-\EOF &&
1130 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1134 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1137 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1140 test_expect_success
'rebase -i respects core.commentchar=auto' '
1141 test_config core.commentchar auto &&
1142 write_script copy-edit-script.sh <<-\EOF &&
1145 test_when_finished "git rebase --abort || :" &&
1147 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1150 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1153 test_expect_success
'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1154 test_when_finished "git branch -D torebase" &&
1155 git checkout -b torebase branch1 &&
1156 upstream=$(git rev-parse ":/J") &&
1157 onto=$(git rev-parse ":/A") &&
1158 git rebase --onto $onto $upstream &&
1159 git reset --hard branch1 &&
1160 git rebase --onto ":/A" ":/J" &&
1161 git checkout branch1
1164 test_expect_success
'rebase -i with --strategy and -X' '
1165 git checkout -b conflict-merge-use-theirs conflict-branch &&
1166 git reset --hard HEAD^ &&
1167 echo five >conflict &&
1169 git commit -a -m "one file conflict" &&
1170 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1171 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1172 test $(cat file1) = Z
1175 test_expect_success
'interrupted rebase -i with --strategy and -X' '
1176 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1177 git reset --hard HEAD^ &&
1179 git add breakpoint &&
1180 git commit -m "breakpoint for interactive mode" &&
1181 echo five >conflict &&
1183 git commit -a -m "one file conflict" &&
1186 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1187 -Xours conflict-branch
1189 git rebase --continue &&
1190 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1191 test $(cat file1) = Z
1194 test_expect_success
'rebase -i error on commits with \ in message' '
1195 current_head=$(git rev-parse HEAD) &&
1196 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1197 test_commit TO-REMOVE will-conflict old-content &&
1198 test_commit "\temp" will-conflict new-content dummy &&
1199 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1200 test_expect_code 1 grep " emp" error
1203 test_expect_success
'short commit ID setup' '
1204 test_when_finished "git checkout primary" &&
1205 git checkout --orphan collide &&
1209 test_commit collide1 collide &&
1210 test_commit --notick collide2 collide &&
1211 test_commit --notick collide3 collide
1215 if test -n "$GIT_TEST_FIND_COLLIDER"
1217 author
="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1218 committer
="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1219 blob
="$(git rev-parse collide2:collide)"
1220 from
="$(git rev-parse collide1^0)"
1221 repl
="commit refs/heads/collider-&\\n"
1222 repl
="${repl}author $author\\ncommitter $committer\\n"
1223 repl
="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1224 repl
="${repl}from $from\\nM 100644 $blob collide\\n"
1225 test_seq
1 32768 |
sed "s|.*|$repl|" >script &&
1226 git fast-import
<script &&
1228 git for-each-ref
>refs
&&
1229 grep "^$(test_oid t3404_collision)" <refs
>matches
&&
1231 test_line_count
-gt 2 matches ||
{
1232 echo "Could not find a collider" >&2
1237 test_expect_success
'short commit ID collide' '
1238 test_oid_cache <<-EOF &&
1239 # collision-related constants
1240 t3404_collision sha1:6bcd
1241 t3404_collision sha256:0161
1242 t3404_collider sha1:ac4f2ee
1243 t3404_collider sha256:16697
1245 test_when_finished "reset_rebase && git checkout primary" &&
1246 git checkout collide &&
1247 colliding_id=$(test_oid t3404_collision) &&
1248 hexsz=$(test_oid hexsz) &&
1249 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1250 test_config core.abbrev 4 &&
1255 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1256 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1257 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1258 grep "^pick $colliding_id " \
1259 .git/rebase-merge/git-rebase-todo.tmp &&
1260 grep -E "^pick [0-9a-f]{$hexsz}" \
1261 .git/rebase-merge/git-rebase-todo &&
1262 grep -E "^pick [0-9a-f]{$hexsz}" \
1263 .git/rebase-merge/git-rebase-todo.backup &&
1264 git rebase --continue
1266 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1267 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1268 test "$collide2" = "$collide3"
1271 test_expect_success
'respect core.abbrev' '
1272 git config core.abbrev 12 &&
1274 set_cat_todo_editor &&
1275 test_must_fail git rebase -i HEAD~4 >todo-list
1277 test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list)
1280 test_expect_success
'todo count' '
1281 write_script dump-raw.sh <<-\EOF &&
1285 test_set_editor "$(pwd)/dump-raw.sh" &&
1286 git rebase -i HEAD~4 >actual
1288 test_grep "^# Rebase ..* onto ..* ([0-9]" actual
1291 test_expect_success
'rebase -i commits that overwrite untracked files (pick)' '
1292 git checkout --force A &&
1296 pick $(git rev-parse B) B
1297 pick $(git rev-parse C) C
1298 pick $(git rev-parse D) D
1299 exec cat .git/rebase-merge/done >actual
1302 set_replace_editor todo &&
1303 test_must_fail git rebase -i A
1305 test_cmp_rev HEAD B &&
1306 test_cmp_rev REBASE_HEAD C &&
1307 head -n3 todo >expect &&
1308 test_cmp expect .git/rebase-merge/done &&
1310 test_path_is_missing .git/rebase-merge/patch &&
1311 echo changed >file1 &&
1313 test_must_fail git rebase --continue 2>err &&
1314 grep "error: you have staged changes in your working tree" err &&
1315 git reset --hard HEAD &&
1316 git rebase --continue &&
1317 test_cmp_rev HEAD D &&
1318 tail -n3 todo >>expect &&
1319 test_cmp expect actual
1322 test_expect_success
'rebase -i commits that overwrite untracked files (squash)' '
1323 git checkout --force branch2 &&
1325 git tag original-branch2 &&
1328 FAKE_LINES="edit 1 squash 2" git rebase -i A
1330 test_cmp_rev HEAD F &&
1331 test_path_is_missing file6 &&
1333 test_must_fail git rebase --continue &&
1334 test_cmp_rev HEAD F &&
1335 test_cmp_rev REBASE_HEAD I &&
1337 test_path_is_missing .git/rebase-merge/patch &&
1338 echo changed >file1 &&
1340 test_must_fail git rebase --continue 2>err &&
1341 grep "error: you have staged changes in your working tree" err &&
1342 git reset --hard HEAD &&
1343 git rebase --continue &&
1344 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1345 git reset --hard original-branch2
1348 test_expect_success
'rebase -i commits that overwrite untracked files (no ff)' '
1349 git checkout --force branch2 &&
1353 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1355 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1356 test_path_is_missing file6 &&
1358 test_must_fail git rebase --continue &&
1359 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1360 test_cmp_rev REBASE_HEAD I &&
1362 test_path_is_missing .git/rebase-merge/patch &&
1363 echo changed >file1 &&
1365 test_must_fail git rebase --continue 2>err &&
1366 grep "error: you have staged changes in your working tree" err &&
1367 git reset --hard HEAD &&
1368 git rebase --continue &&
1369 test $(git cat-file commit HEAD | sed -ne \$p) = I
1372 test_expect_success
'rebase --continue removes CHERRY_PICK_HEAD' '
1373 git checkout -b commit-to-skip &&
1376 test_seq 5 | sed "s/$double/&&/" >seq &&
1379 git commit -m seq-$double || return 1
1382 git reset --hard HEAD~2 &&
1383 git cherry-pick seq-onto &&
1386 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1388 test -d .git/rebase-merge &&
1389 git rebase --continue &&
1390 git diff --exit-code seq-onto &&
1391 test ! -d .git/rebase-merge &&
1392 test ! -f .git/CHERRY_PICK_HEAD
1395 rebase_setup_and_clean
() {
1396 test_when_finished
"
1397 git checkout primary &&
1398 test_might_fail git branch -D $1 &&
1399 test_might_fail git rebase --abort
1401 git checkout
-b $1 ${2:-primary}
1404 test_expect_success
'drop' '
1405 rebase_setup_and_clean drop-test &&
1408 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1410 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1411 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1412 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1415 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = ignore' '
1416 test_config rebase.missingCommitsCheck ignore &&
1417 rebase_setup_and_clean missing-commit &&
1420 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1422 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1424 "Successfully rebased and updated refs/heads/missing-commit" \
1428 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = warn' '
1429 cat >expect <<-EOF &&
1430 Warning: some commits may have been dropped accidentally.
1431 Dropped commits (newer to older):
1432 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1433 To avoid this message, use "drop" to explicitly remove a commit.
1435 test_config rebase.missingCommitsCheck warn &&
1436 rebase_setup_and_clean missing-commit &&
1439 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1441 head -n4 actual.2 >actual &&
1442 test_cmp expect actual &&
1443 test D = $(git cat-file commit HEAD | sed -ne \$p)
1446 test_expect_success
'rebase -i respects rebase.missingCommitsCheck = error' '
1447 cat >expect <<-EOF &&
1448 Warning: some commits may have been dropped accidentally.
1449 Dropped commits (newer to older):
1450 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1451 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1452 To avoid this message, use "drop" to explicitly remove a commit.
1454 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1455 The possible behaviours are: ignore, warn, error.
1457 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1458 Or you can abort the rebase with '\''git rebase --abort'\''.
1460 test_config rebase.missingCommitsCheck error &&
1461 rebase_setup_and_clean missing-commit &&
1464 test_must_fail env FAKE_LINES="1 2 4" \
1465 git rebase -i --root 2>actual &&
1466 test_cmp expect actual &&
1467 cp .git/rebase-merge/git-rebase-todo.backup \
1468 .git/rebase-merge/git-rebase-todo &&
1469 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1471 git rebase --continue &&
1472 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1473 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1476 test_expect_success
'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1477 test_config rebase.missingCommitsCheck ignore &&
1478 rebase_setup_and_clean missing-commit &&
1481 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1482 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1483 git rebase --continue 2>actual
1485 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1487 "Successfully rebased and updated refs/heads/missing-commit" \
1491 test_expect_success
'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1492 cat >expect <<-EOF &&
1493 error: invalid command '\''pickled'\''
1494 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1495 Warning: some commits may have been dropped accidentally.
1496 Dropped commits (newer to older):
1497 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1498 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1499 To avoid this message, use "drop" to explicitly remove a commit.
1501 head -n5 expect >expect.2 &&
1502 tail -n1 expect >>expect.2 &&
1503 tail -n4 expect.2 >expect.3 &&
1504 test_config rebase.missingCommitsCheck warn &&
1505 rebase_setup_and_clean missing-commit &&
1508 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1509 git rebase -i --root &&
1510 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1511 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1512 head -n7 actual.2 >actual &&
1513 test_cmp expect actual &&
1514 cp orig .git/rebase-merge/git-rebase-todo &&
1515 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1516 head -n4 actual.2 >actual &&
1517 test_cmp expect.3 actual &&
1518 git rebase --continue 2>actual
1520 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1522 "Successfully rebased and updated refs/heads/missing-commit" \
1526 test_expect_success
'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1527 cat >expect <<-EOF &&
1528 error: invalid command '\''pickled'\''
1529 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1530 Warning: some commits may have been dropped accidentally.
1531 Dropped commits (newer to older):
1532 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1533 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1534 To avoid this message, use "drop" to explicitly remove a commit.
1536 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1537 The possible behaviours are: ignore, warn, error.
1539 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1540 Or you can abort the rebase with '\''git rebase --abort'\''.
1542 tail -n11 expect >expect.2 &&
1543 head -n3 expect.2 >expect.3 &&
1544 tail -n7 expect.2 >>expect.3 &&
1545 test_config rebase.missingCommitsCheck error &&
1546 rebase_setup_and_clean missing-commit &&
1549 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1550 git rebase -i --root &&
1551 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1552 test_must_fail env FAKE_LINES="2 3 4" \
1553 git rebase --edit-todo 2>actual &&
1554 test_cmp expect actual &&
1555 test_must_fail git rebase --continue 2>actual &&
1556 test_cmp expect.2 actual &&
1557 test_must_fail git rebase --edit-todo &&
1558 cp orig .git/rebase-merge/git-rebase-todo &&
1559 test_must_fail env FAKE_LINES="1 2 3 4" \
1560 git rebase --edit-todo 2>actual &&
1561 test_cmp expect.3 actual &&
1562 test_must_fail git rebase --continue 2>actual &&
1563 test_cmp expect.3 actual &&
1564 cp orig .git/rebase-merge/git-rebase-todo &&
1565 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1566 git rebase --continue 2>actual
1568 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1570 "Successfully rebased and updated refs/heads/missing-commit" \
1574 test_expect_success
'rebase.missingCommitsCheck = error after resolving conflicts' '
1575 test_config rebase.missingCommitsCheck error &&
1578 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1580 git rebase --edit-todo &&
1581 test_must_fail git rebase --continue &&
1584 git rebase --continue
1587 test_expect_success
'rebase.missingCommitsCheck = error when editing for a second time' '
1588 test_config rebase.missingCommitsCheck error &&
1591 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1592 cp .git/rebase-merge/git-rebase-todo todo &&
1593 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1594 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1595 git rebase --continue
1599 test_expect_success
'respects rebase.abbreviateCommands with fixup, squash and exec' '
1600 rebase_setup_and_clean abbrevcmd &&
1601 test_commit "first" file1.txt "first line" first &&
1602 test_commit "second" file1.txt "another line" second &&
1603 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1604 test_commit "squash! second" file1.txt "another line here" second_squash &&
1605 cat >expected <<-EOF &&
1606 p $(git rev-list --abbrev-commit -1 first) first
1607 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1609 p $(git rev-list --abbrev-commit -1 second) second
1610 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1613 git checkout abbrevcmd &&
1614 test_config rebase.abbreviateCommands true &&
1616 set_cat_todo_editor &&
1617 test_must_fail git rebase -i --exec "git show HEAD" \
1618 --autosquash primary >actual
1620 test_cmp expected actual
1623 test_expect_success
'static check of bad command' '
1624 rebase_setup_and_clean bad-cmd &&
1627 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1628 git rebase -i --root 2>actual &&
1629 test_grep "pickled $(git rev-list --oneline -1 primary~1)" \
1631 test_grep "You can fix this with .git rebase --edit-todo.." \
1633 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1635 git rebase --continue &&
1636 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1637 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1640 test_expect_success
'the first command cannot be a fixup' '
1641 rebase_setup_and_clean fixup-first &&
1644 fixup $(git log -1 --format="%h %s" B)
1645 pick $(git log -1 --format="%h %s" C)
1649 set_replace_editor orig &&
1650 test_must_fail git rebase -i A 2>actual
1652 grep "cannot .fixup. without a previous commit" actual &&
1653 grep "You can fix this with .git rebase --edit-todo.." actual &&
1654 # verify that the todo list has not been truncated
1655 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1656 test_cmp orig actual &&
1658 test_must_fail git rebase --edit-todo 2>actual &&
1659 grep "cannot .fixup. without a previous commit" actual &&
1660 grep "You can fix this with .git rebase --edit-todo.." actual &&
1661 # verify that the todo list has not been truncated
1662 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1663 test_cmp orig actual
1666 test_expect_success
'tabs and spaces are accepted in the todolist' '
1667 rebase_setup_and_clean indented-comment &&
1668 write_script add-indent.sh <<-\EOF &&
1670 # Turn single spaces into space/tab mix
1671 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1672 printf "\n\t# comment\n #more\n\t # comment\n"
1677 test_set_editor "$(pwd)/add-indent.sh" &&
1678 git rebase -i HEAD^^^
1680 test E = $(git cat-file commit HEAD | sed -ne \$p)
1683 test_expect_success
'static check of bad SHA-1' '
1684 rebase_setup_and_clean bad-sha &&
1687 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1688 git rebase -i --root 2>actual &&
1689 test_grep "edit XXXXXXX False commit" actual &&
1690 test_grep "You can fix this with .git rebase --edit-todo.." \
1692 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1694 git rebase --continue &&
1695 test E = $(git cat-file commit HEAD | sed -ne \$p)
1698 test_expect_success
'editor saves as CR/LF' '
1699 git checkout -b with-crlf &&
1700 write_script add-crs.sh <<-\EOF &&
1701 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1705 test_set_editor "$(pwd)/add-crs.sh" &&
1710 test_expect_success
'rebase -i --gpg-sign=<key-id>' '
1711 test_when_finished "test_might_fail git rebase --abort" &&
1714 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1717 test_grep "$SQ-S\"S I Gner\"$SQ" err
1720 test_expect_success
'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1721 test_when_finished "test_might_fail git rebase --abort" &&
1722 test_config commit.gpgsign true &&
1725 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1728 test_grep "$SQ-S\"S I Gner\"$SQ" err
1731 test_expect_success
'valid author header after --root swap' '
1732 rebase_setup_and_clean author-header no-conflict-branch &&
1733 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1734 git cat-file commit HEAD | grep ^author >expected &&
1737 FAKE_LINES="5 1" git rebase -i --root
1739 git cat-file commit HEAD^ | grep ^author >actual &&
1740 test_cmp expected actual
1743 test_expect_success
'valid author header when author contains single quote' '
1744 rebase_setup_and_clean author-header no-conflict-branch &&
1745 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1746 git cat-file commit HEAD | grep ^author >expected &&
1749 FAKE_LINES="2" git rebase -i HEAD~2
1751 git cat-file commit HEAD | grep ^author >actual &&
1752 test_cmp expected actual
1755 test_expect_success
'post-commit hook is called' '
1757 test_hook post-commit <<-\EOS &&
1758 git rev-parse HEAD >>actual
1762 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1765 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1767 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1769 test_cmp expect actual
1772 test_expect_success
'correct error message for partial commit after empty pick' '
1773 test_when_finished "git rebase --abort" &&
1776 FAKE_LINES="2 1 1" &&
1777 export FAKE_LINES &&
1778 test_must_fail git rebase -i A D
1781 test_must_fail git commit file1 2>err &&
1782 test_grep "cannot do a partial commit during a rebase." err
1785 test_expect_success
'correct error message for commit --amend after empty pick' '
1786 test_when_finished "git rebase --abort" &&
1790 export FAKE_LINES &&
1791 test_must_fail git rebase -i A D
1794 test_must_fail git commit -a --amend 2>err &&
1795 test_grep "middle of a rebase -- cannot amend." err
1798 test_expect_success
'todo has correct onto hash' '
1799 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1800 onto=$(git rev-parse --short HEAD~4) &&
1801 test_grep "^# Rebase ..* onto $onto" actual
1804 test_expect_success
'ORIG_HEAD is updated correctly' '
1805 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1806 git checkout -b test-orig-head A &&
1807 git commit --allow-empty -m A1 &&
1808 git commit --allow-empty -m A2 &&
1809 git commit --allow-empty -m A3 &&
1810 git commit --allow-empty -m A4 &&
1811 git rebase primary &&
1812 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1815 test_expect_success
'--update-refs adds label and update-ref commands' '
1816 git checkout -b update-refs no-conflict-branch &&
1817 git branch -f base HEAD~4 &&
1818 git branch -f first HEAD~3 &&
1819 git branch -f second HEAD~3 &&
1820 git branch -f third HEAD~1 &&
1821 git commit --allow-empty --fixup=third &&
1822 git branch -f is-not-reordered &&
1823 git commit --allow-empty --fixup=HEAD~4 &&
1824 git branch -f shared-tip &&
1826 set_cat_todo_editor &&
1828 cat >expect <<-EOF &&
1829 pick $(git log -1 --format=%h J) J
1830 fixup $(git log -1 --format=%h update-refs) fixup! J # empty
1831 update-ref refs/heads/second
1832 update-ref refs/heads/first
1833 pick $(git log -1 --format=%h K) K
1834 pick $(git log -1 --format=%h L) L
1835 fixup $(git log -1 --format=%h is-not-reordered) fixup! L # empty
1836 update-ref refs/heads/third
1837 pick $(git log -1 --format=%h M) M
1838 update-ref refs/heads/no-conflict-branch
1839 update-ref refs/heads/is-not-reordered
1840 update-ref refs/heads/shared-tip
1843 test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
1844 test_cmp expect todo &&
1846 test_must_fail git -c rebase.autosquash=true \
1847 -c rebase.updaterefs=true \
1848 rebase -i primary >todo &&
1850 test_cmp expect todo
1854 test_expect_success
'--update-refs adds commands with --rebase-merges' '
1855 git checkout -b update-refs-with-merge no-conflict-branch &&
1856 git branch -f base HEAD~4 &&
1857 git branch -f first HEAD~3 &&
1858 git branch -f second HEAD~3 &&
1859 git branch -f third HEAD~1 &&
1860 git merge -m merge branch2 &&
1861 git branch -f merge-branch &&
1862 git commit --fixup=third --allow-empty &&
1864 set_cat_todo_editor &&
1866 cat >expect <<-EOF &&
1869 pick $(git log -1 --format=%h branch2~1) F
1870 pick $(git log -1 --format=%h branch2) I
1871 update-ref refs/heads/branch2
1874 pick $(git log -1 --format=%h refs/heads/second) J
1875 update-ref refs/heads/second
1876 update-ref refs/heads/first
1877 pick $(git log -1 --format=%h refs/heads/third~1) K
1878 pick $(git log -1 --format=%h refs/heads/third) L
1879 fixup $(git log -1 --format=%h update-refs-with-merge) fixup! L # empty
1880 update-ref refs/heads/third
1881 pick $(git log -1 --format=%h HEAD~2) M
1882 update-ref refs/heads/no-conflict-branch
1883 merge -C $(git log -1 --format=%h HEAD~1) merge # merge
1884 update-ref refs/heads/merge-branch
1887 test_must_fail git rebase -i --autosquash \
1888 --rebase-merges=rebase-cousins \
1889 --update-refs primary >todo &&
1891 test_cmp expect todo &&
1893 test_must_fail git -c rebase.autosquash=true \
1894 -c rebase.updaterefs=true \
1896 --rebase-merges=rebase-cousins \
1899 test_cmp expect todo
1903 test_expect_success
'--update-refs updates refs correctly' '
1904 git checkout -B update-refs no-conflict-branch &&
1905 git branch -f base HEAD~4 &&
1906 git branch -f first HEAD~3 &&
1907 git branch -f second HEAD~3 &&
1908 git branch -f third HEAD~1 &&
1909 test_commit extra2 fileX &&
1910 git commit --amend --fixup=L &&
1912 git rebase -i --autosquash --update-refs primary 2>err &&
1914 test_cmp_rev HEAD~3 refs/heads/first &&
1915 test_cmp_rev HEAD~3 refs/heads/second &&
1916 test_cmp_rev HEAD~1 refs/heads/third &&
1917 test_cmp_rev HEAD refs/heads/no-conflict-branch &&
1919 cat >expect <<-\EOF &&
1920 Successfully rebased and updated refs/heads/update-refs.
1921 Updated the following refs with --update-refs:
1923 refs/heads/no-conflict-branch
1928 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
1929 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
1930 <err >err.trimmed &&
1931 test_cmp expect err.trimmed
1934 test_expect_success
'respect user edits to update-ref steps' '
1935 git checkout -B update-refs-break no-conflict-branch &&
1936 git branch -f base HEAD~4 &&
1937 git branch -f first HEAD~3 &&
1938 git branch -f second HEAD~3 &&
1939 git branch -f third HEAD~1 &&
1940 git branch -f unseen base &&
1942 # First, we will add breaks to the expected todo file
1943 cat >fake-todo-1 <<-EOF &&
1944 pick $(git rev-parse HEAD~3)
1946 update-ref refs/heads/second
1947 update-ref refs/heads/first
1949 pick $(git rev-parse HEAD~2)
1950 pick $(git rev-parse HEAD~1)
1951 update-ref refs/heads/third
1953 pick $(git rev-parse HEAD)
1954 update-ref refs/heads/no-conflict-branch
1957 # Second, we will drop some update-refs commands (and move one)
1958 cat >fake-todo-2 <<-EOF &&
1959 update-ref refs/heads/second
1961 pick $(git rev-parse HEAD~2)
1962 update-ref refs/heads/third
1963 pick $(git rev-parse HEAD~1)
1966 pick $(git rev-parse HEAD)
1970 # * insert a new one (new-branch),
1971 # * re-add an old one (first), and
1972 # * add a second instance of a previously-stored one (second)
1973 cat >fake-todo-3 <<-EOF &&
1974 update-ref refs/heads/unseen
1975 update-ref refs/heads/new-branch
1976 pick $(git rev-parse HEAD)
1977 update-ref refs/heads/first
1978 update-ref refs/heads/second
1982 set_replace_editor fake-todo-1 &&
1983 git rebase -i --update-refs primary &&
1985 # These branches are currently locked.
1986 for b in first second third no-conflict-branch
1988 test_must_fail git branch -f $b base || return 1
1991 set_replace_editor fake-todo-2 &&
1992 git rebase --edit-todo &&
1994 # These branches are currently locked.
1995 for b in second third
1997 test_must_fail git branch -f $b base || return 1
2000 # These branches are currently unlocked for checkout.
2001 for b in first no-conflict-branch
2003 git worktree add wt-$b $b &&
2004 git worktree remove wt-$b || return 1
2007 git rebase --continue &&
2009 set_replace_editor fake-todo-3 &&
2010 git rebase --edit-todo &&
2012 # These branches are currently locked.
2013 for b in second third first unseen
2015 test_must_fail git branch -f $b base || return 1
2018 # These branches are currently unlocked for checkout.
2019 for b in no-conflict-branch
2021 git worktree add wt-$b $b &&
2022 git worktree remove wt-$b || return 1
2025 git rebase --continue
2028 test_cmp_rev HEAD~2 refs/heads/third &&
2029 test_cmp_rev HEAD~1 refs/heads/unseen &&
2030 test_cmp_rev HEAD~1 refs/heads/new-branch &&
2031 test_cmp_rev HEAD refs/heads/first &&
2032 test_cmp_rev HEAD refs/heads/second &&
2033 test_cmp_rev HEAD refs/heads/no-conflict-branch
2036 test_expect_success
'--update-refs: all update-ref lines removed' '
2037 git checkout -b test-refs-not-removed no-conflict-branch &&
2038 git branch -f base HEAD~4 &&
2039 git branch -f first HEAD~3 &&
2040 git branch -f second HEAD~3 &&
2041 git branch -f third HEAD~1 &&
2042 git branch -f tip &&
2044 test_commit test-refs-not-removed &&
2045 git commit --amend --fixup first &&
2047 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2050 set_cat_todo_editor &&
2051 test_must_fail git rebase -i --update-refs base >todo.raw &&
2052 sed -e "/^update-ref/d" <todo.raw >todo
2055 set_replace_editor todo &&
2056 git rebase -i --update-refs base
2059 # Ensure refs are not deleted and their OIDs have not changed
2060 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2061 test_cmp expect-oids actual-oids
2064 test_expect_success
'--update-refs: all update-ref lines removed, then some re-added' '
2065 git checkout -b test-refs-not-removed2 no-conflict-branch &&
2066 git branch -f base HEAD~4 &&
2067 git branch -f first HEAD~3 &&
2068 git branch -f second HEAD~3 &&
2069 git branch -f third HEAD~1 &&
2070 git branch -f tip &&
2072 test_commit test-refs-not-removed2 &&
2073 git commit --amend --fixup first &&
2075 git rev-parse first second third >expect-oids &&
2078 set_cat_todo_editor &&
2079 test_must_fail git rebase -i \
2080 --autosquash --update-refs \
2082 sed -e "/^update-ref/d" <todo.raw >todo
2085 # Add a break to the end of the todo so we can edit later
2086 echo "break" >>todo &&
2089 set_replace_editor todo &&
2090 git rebase -i --autosquash --update-refs base &&
2091 echo "update-ref refs/heads/tip" >todo &&
2092 git rebase --edit-todo &&
2093 git rebase --continue
2096 # Ensure first/second/third are unchanged, but tip is updated
2097 git rev-parse first second third >actual-oids &&
2098 test_cmp expect-oids actual-oids &&
2099 test_cmp_rev HEAD tip
2102 test_expect_success
'--update-refs: --edit-todo with no update-ref lines' '
2103 git checkout -b test-refs-not-removed3 no-conflict-branch &&
2104 git branch -f base HEAD~4 &&
2105 git branch -f first HEAD~3 &&
2106 git branch -f second HEAD~3 &&
2107 git branch -f third HEAD~1 &&
2108 git branch -f tip &&
2110 test_commit test-refs-not-removed3 &&
2111 git commit --amend --fixup first &&
2113 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2116 set_cat_todo_editor &&
2117 test_must_fail git rebase -i \
2118 --autosquash --update-refs \
2120 sed -e "/^update-ref/d" <todo.raw >todo
2123 # Add a break to the beginning of the todo so we can resume with no
2125 echo "break" >todo.new &&
2126 cat todo >>todo.new &&
2129 set_replace_editor todo.new &&
2130 git rebase -i --autosquash --update-refs base &&
2132 # Make no changes when editing so update-refs is still empty
2133 cat todo >todo.new &&
2134 git rebase --edit-todo &&
2135 git rebase --continue
2138 # Ensure refs are not deleted and their OIDs have not changed
2139 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2140 test_cmp expect-oids actual-oids
2143 test_expect_success
'--update-refs: check failed ref update' '
2144 test_when_finished "test_might_fail git rebase --abort" &&
2145 git checkout -B update-refs-error no-conflict-branch &&
2146 git branch -f base HEAD~4 &&
2147 git branch -f first HEAD~3 &&
2148 git branch -f second HEAD~2 &&
2149 git branch -f third HEAD~1 &&
2151 cat >fake-todo <<-EOF &&
2152 pick $(git rev-parse HEAD~3)
2154 update-ref refs/heads/first
2156 pick $(git rev-parse HEAD~2)
2157 update-ref refs/heads/second
2159 pick $(git rev-parse HEAD~1)
2160 update-ref refs/heads/third
2162 pick $(git rev-parse HEAD)
2163 update-ref refs/heads/no-conflict-branch
2167 set_replace_editor fake-todo &&
2168 git rebase -i --update-refs base
2171 # At this point, the values of first, second, and third are
2172 # recorded in the update-refs file. We will force-update the
2173 # "second" ref, but "git branch -f" will not work because of
2174 # the lock in the update-refs file.
2175 git update-ref refs/heads/second third &&
2177 test_must_fail git rebase --continue 2>err &&
2178 grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
2180 cat >expect <<-\EOF &&
2181 Updated the following refs with --update-refs:
2183 refs/heads/no-conflict-branch
2185 Failed to update the following refs with --update-refs:
2189 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
2190 tail -n 6 err >err.last &&
2191 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
2192 <err.last >err.trimmed &&
2193 test_cmp expect err.trimmed
2196 test_expect_success
'bad labels and refs rejected when parsing todo list' '
2197 test_when_finished "test_might_fail git rebase --abort" &&
2198 cat >todo <<-\EOF &&
2207 set_replace_editor todo &&
2208 test_must_fail git rebase -i HEAD 2>err
2210 grep "'\''#'\'' is not a valid label" err &&
2211 grep "'\'':invalid'\'' is not a valid label" err &&
2212 grep "'\'':bad'\'' is not a valid refname" err &&
2213 grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \
2215 test_path_is_missing execed
2218 # This must be the last test in this file
2219 test_expect_success
'$EDITOR and friends are unchanged' '
2220 test_editor_unchanged