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 sets work tree properly' '
123 test_when_finished "rm -rf subdir" &&
124 test_when_finished "test_might_fail git rebase --abort" &&
126 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
128 ! grep "/subdir$" actual
131 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
132 git checkout master &&
134 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
135 test_cmp_rev master^ HEAD &&
137 git rebase --continue
140 test_expect_success 'rebase -i with exec of inexistent command' '
141 git checkout master &&
142 test_when_finished "git rebase --abort" &&
144 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
145 git rebase -i HEAD^ >actual 2>&1 &&
146 ! grep "Maybe git-rebase is broken" actual
149 test_expect_success 'no changes are a nop' '
150 git checkout branch2 &&
153 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
154 test $(git rev-parse I) = $(git rev-parse HEAD)
157 test_expect_success 'test the [branch] option' '
158 git checkout -b dead-end &&
160 git commit -m "stop here" &&
162 git rebase -i F branch2 &&
163 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
164 test $(git rev-parse I) = $(git rev-parse branch2) &&
165 test $(git rev-parse I) = $(git rev-parse HEAD)
168 test_expect_success 'test --onto <branch>' '
169 git checkout -b test-onto branch2 &&
171 git rebase -i --onto branch1 F &&
172 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
173 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
174 test $(git rev-parse I) = $(git rev-parse branch2)
177 test_expect_success 'rebase on top of a non-conflicting commit' '
178 git checkout branch1 &&
179 git tag original-branch1 &&
181 git rebase -i branch2 &&
182 test file6 = $(git diff --name-only original-branch1) &&
183 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
184 test $(git rev-parse I) = $(git rev-parse branch2) &&
185 test $(git rev-parse I) = $(git rev-parse HEAD~2)
188 test_expect_success 'reflog for the branch shows state before rebase' '
189 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
192 test_expect_success 'reflog for the branch shows correct finish message' '
193 printf "rebase -i (finish): refs/heads/branch1 onto %s\n" \
194 "$(git rev-parse branch2)" >expected &&
195 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
196 test_cmp expected actual
199 test_expect_success 'exchange two commits' '
201 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
202 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
203 test G = $(git cat-file commit HEAD | sed -ne \$p)
207 diff --git a/file1 b/file1
208 index f70f10e..fd79235 100644
224 test_expect_success 'stop on conflicting pick' '
225 git tag new-branch1 &&
227 test_must_fail git rebase -i master &&
228 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
229 test_cmp expect .git/rebase-merge/patch &&
230 test_cmp expect2 file1 &&
231 test "$(git diff --name-status |
232 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
233 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
234 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
237 test_expect_success 'show conflicted patch' '
238 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
239 grep "show.*REBASE_HEAD" stderr &&
240 # the original stopped-sha1 is abbreviated
241 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
242 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
245 test_expect_success 'abort' '
246 git rebase --abort &&
247 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
248 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
249 test_path_is_missing .git/rebase-merge
252 test_expect_success 'abort with error when new base cannot be checked out' '
253 git rm --cached file1 &&
254 git commit -m "remove file in base" &&
256 test_must_fail git rebase -i master > output 2>&1 &&
257 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
259 test_i18ngrep "file1" output &&
260 test_path_is_missing .git/rebase-merge &&
261 git reset --hard HEAD^
264 test_expect_success 'retain authorship' '
268 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
271 git rebase -i --onto master HEAD^ &&
272 git show HEAD | grep "^Author: Twerp Snog"
275 test_expect_success 'retain authorship w/ conflicts' '
276 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
277 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
279 git reset --hard twerp &&
280 test_commit a conflict a conflict-a &&
281 git reset --hard twerp &&
283 GIT_AUTHOR_NAME=AttributeMe &&
284 export GIT_AUTHOR_NAME &&
285 test_commit b conflict b conflict-b &&
286 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
289 test_must_fail git rebase -i conflict-a &&
290 echo resolved >conflict &&
292 git rebase --continue &&
293 test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
298 test_expect_success 'squash' '
299 git reset --hard twerp &&
302 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
303 echo "******************************" &&
305 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
306 git rebase -i --onto master HEAD~2 &&
307 test B = $(cat file7) &&
308 test $(git rev-parse HEAD^) = $(git rev-parse master)
311 test_expect_success 'retain authorship when squashing' '
312 git show HEAD | grep "^Author: Twerp Snog"
315 test_expect_success '-p handles "no changes" gracefully' '
316 HEAD=$(git rev-parse HEAD) &&
318 git rebase -i -p HEAD^ &&
319 git update-index --refresh &&
320 git diff-files --quiet &&
321 git diff-index --quiet --cached HEAD -- &&
322 test $HEAD = $(git rev-parse HEAD)
325 test_expect_failure 'exchange two commits with -p' '
328 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
329 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
330 test G = $(git cat-file commit HEAD | sed -ne \$p)
333 test_expect_success 'preserve merges with -p' '
334 git checkout -b to-be-preserved master^ &&
335 : > unrelated-file &&
336 git add unrelated-file &&
338 git commit -m "unrelated" &&
339 git checkout -b another-branch master &&
342 git commit -m J file1 &&
344 git merge to-be-preserved &&
347 git commit -m K file1 &&
350 git commit -m L1 file1 &&
351 git checkout HEAD^ &&
352 echo 1 > unrelated-file &&
354 git commit -m L2 unrelated-file &&
356 git merge another-branch &&
359 git commit -m M file1 &&
360 git checkout -b to-be-rebased &&
363 git rebase -i -p --onto branch1 master &&
364 git update-index --refresh &&
365 git diff-files --quiet &&
366 git diff-index --quiet --cached HEAD -- &&
367 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
368 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
369 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
370 test $(git show HEAD~5:file1) = B &&
371 test $(git show HEAD~3:file1) = C &&
372 test $(git show HEAD:file1) = E &&
373 test $(git show HEAD:unrelated-file) = 1
376 test_expect_success 'edit ancestor with -p' '
378 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
379 echo 2 > unrelated-file &&
381 git commit -m L2-modified --amend unrelated-file &&
382 git rebase --continue &&
383 git update-index --refresh &&
384 git diff-files --quiet &&
385 git diff-index --quiet --cached HEAD -- &&
386 test $(git show HEAD:unrelated-file) = 2
389 test_expect_success '--continue tries to commit' '
392 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
393 echo resolved > file1 &&
395 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
396 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
397 git show HEAD | grep chouette
400 test_expect_success 'verbose flag is heeded, even after --continue' '
401 git reset --hard master@{1} &&
404 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
405 echo resolved > file1 &&
407 git rebase --continue > output &&
408 grep "^ file1 | 2 +-$" output
411 test_expect_success C_LOCALE_OUTPUT 'multi-squash only fires up editor once' '
412 base=$(git rev-parse HEAD~4) &&
414 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
415 EXPECT_HEADER_COUNT=4 \
416 git rebase -i $base &&
417 test $base = $(git rev-parse HEAD^) &&
418 test 1 = $(git show | grep ONCE | wc -l)
421 test_expect_success C_LOCALE_OUTPUT 'multi-fixup does not fire up editor' '
422 git checkout -b multi-fixup E &&
423 base=$(git rev-parse HEAD~4) &&
425 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
426 git rebase -i $base &&
427 test $base = $(git rev-parse HEAD^) &&
428 test 0 = $(git show | grep NEVER | wc -l) &&
429 git checkout to-be-rebased &&
430 git branch -D multi-fixup
433 test_expect_success 'commit message used after conflict' '
434 git checkout -b conflict-fixup conflict-branch &&
435 base=$(git rev-parse HEAD~4) &&
437 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
438 echo three > conflict &&
440 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
441 git rebase --continue &&
442 test $base = $(git rev-parse HEAD^) &&
443 test 1 = $(git show | grep ONCE | wc -l) &&
444 git checkout to-be-rebased &&
445 git branch -D conflict-fixup
448 test_expect_success 'commit message retained after conflict' '
449 git checkout -b conflict-squash conflict-branch &&
450 base=$(git rev-parse HEAD~4) &&
452 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
453 echo three > conflict &&
455 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
456 git rebase --continue &&
457 test $base = $(git rev-parse HEAD^) &&
458 test 2 = $(git show | grep TWICE | wc -l) &&
459 git checkout to-be-rebased &&
460 git branch -D conflict-squash
463 cat > expect-squash-fixup << EOF
471 test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
472 git checkout -b squash-fixup E &&
473 base=$(git rev-parse HEAD~4) &&
475 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
476 EXPECT_HEADER_COUNT=4 \
477 git rebase -i $base &&
478 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
479 test_cmp expect-squash-fixup actual-squash-fixup &&
480 git cat-file commit HEAD@{2} |
481 grep "^# This is a combination of 3 commits\." &&
482 git cat-file commit HEAD@{3} |
483 grep "^# This is a combination of 2 commits\." &&
484 git checkout to-be-rebased &&
485 git branch -D squash-fixup
488 test_expect_success C_LOCALE_OUTPUT 'squash ignores comments' '
489 git checkout -b skip-comments E &&
490 base=$(git rev-parse HEAD~4) &&
492 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
493 EXPECT_HEADER_COUNT=4 \
494 git rebase -i $base &&
495 test $base = $(git rev-parse HEAD^) &&
496 test 1 = $(git show | grep ONCE | wc -l) &&
497 git checkout to-be-rebased &&
498 git branch -D skip-comments
501 test_expect_success C_LOCALE_OUTPUT 'squash ignores blank lines' '
502 git checkout -b skip-blank-lines E &&
503 base=$(git rev-parse HEAD~4) &&
505 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
506 EXPECT_HEADER_COUNT=4 \
507 git rebase -i $base &&
508 test $base = $(git rev-parse HEAD^) &&
509 test 1 = $(git show | grep ONCE | wc -l) &&
510 git checkout to-be-rebased &&
511 git branch -D skip-blank-lines
514 test_expect_success 'squash works as expected' '
515 git checkout -b squash-works no-conflict-branch &&
516 one=$(git rev-parse HEAD~3) &&
518 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
519 git rebase -i HEAD~3 &&
520 test $one = $(git rev-parse HEAD~2)
523 test_expect_success 'interrupted squash works as expected' '
524 git checkout -b interrupted-squash conflict-branch &&
525 one=$(git rev-parse HEAD~3) &&
527 test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
528 test_write_lines one two four > conflict &&
530 test_must_fail git rebase --continue &&
531 echo resolved > conflict &&
533 git rebase --continue &&
534 test $one = $(git rev-parse HEAD~2)
537 test_expect_success 'interrupted squash works as expected (case 2)' '
538 git checkout -b interrupted-squash2 conflict-branch &&
539 one=$(git rev-parse HEAD~3) &&
541 test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
542 test_write_lines one four > conflict &&
544 test_must_fail git rebase --continue &&
545 test_write_lines one two four > conflict &&
547 test_must_fail git rebase --continue &&
548 echo resolved > conflict &&
550 git rebase --continue &&
551 test $one = $(git rev-parse HEAD~2)
554 test_expect_success '--continue tries to commit, even for "edit"' '
555 echo unrelated > file7 &&
558 git commit -m "unrelated change" &&
559 parent=$(git rev-parse HEAD^) &&
562 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
563 echo edited > file7 &&
565 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
566 test edited = $(git show HEAD:file7) &&
567 git show HEAD | grep chouette &&
568 test $parent = $(git rev-parse HEAD^)
571 test_expect_success 'aborted --continue does not squash commits after "edit"' '
572 test_when_finished "git rebase --abort" &&
573 old=$(git rev-parse HEAD) &&
576 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
577 echo "edited again" > file7 &&
579 echo all the things >>conflict &&
580 test_must_fail git rebase --continue &&
581 test $old = $(git rev-parse HEAD)
584 test_expect_success 'auto-amend only edited commits after "edit"' '
587 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
588 echo "edited again" > file7 &&
590 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
591 echo "and again" > file7 &&
594 test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
598 test_expect_success 'clean error after failed "exec"' '
600 test_when_finished "git rebase --abort || :" &&
602 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
603 echo "edited again" > file7 &&
605 test_must_fail git rebase --continue 2>error &&
606 test_i18ngrep "you have staged changes in your working tree" error
609 test_expect_success 'rebase a detached HEAD' '
610 grandparent=$(git rev-parse HEAD~2) &&
611 git checkout $(git rev-parse HEAD) &&
614 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
615 test $grandparent = $(git rev-parse HEAD~2)
618 test_expect_success 'rebase a commit violating pre-commit' '
620 mkdir -p .git/hooks &&
621 write_script .git/hooks/pre-commit <<-\EOF &&
622 test -z "$(git diff --cached --check)"
624 echo "monde! " >> file1 &&
626 test_must_fail git commit -m doesnt-verify file1 &&
627 git commit -m doesnt-verify --no-verify file1 &&
630 FAKE_LINES=2 git rebase -i HEAD~2
634 test_expect_success 'rebase with a file named HEAD in worktree' '
638 git checkout -b branch3 A &&
641 GIT_AUTHOR_NAME="Squashed Away" &&
642 export GIT_AUTHOR_NAME &&
645 git commit -m "Add head" &&
648 git commit -m "Add body"
652 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
653 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
657 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
659 git checkout -b branch4 HEAD &&
660 GIT_EDITOR=: git commit --amend \
661 --author="Somebody else <somebody@else.com>" &&
662 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
664 git rebase -i branch3 &&
665 test $(git rev-parse branch3) = $(git rev-parse branch4)
669 test_expect_success 'submodule rebase setup' '
673 cd sub && git init && >elif &&
674 git add elif && git commit -m "submodule initial"
679 git commit -m "One" &&
682 git commit -a -m "Two" &&
684 cd sub && echo 3 >elif &&
685 git commit -a -m "submodule second"
689 git commit -a -m "Three changes submodule"
692 test_expect_success 'submodule rebase -i' '
694 FAKE_LINES="1 squash 2 3" git rebase -i A
697 test_expect_success 'submodule conflict setup' '
698 git tag submodule-base &&
699 git checkout HEAD^ &&
701 cd sub && git checkout HEAD^ && echo 4 >elif &&
702 git add elif && git commit -m "submodule conflict"
706 git commit -m "Conflict in submodule" &&
707 git tag submodule-topic
710 test_expect_success 'rebase -i continue with only submodule staged' '
712 test_must_fail git rebase -i submodule-base &&
714 git rebase --continue &&
715 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
718 test_expect_success 'rebase -i continue with unstaged submodule' '
719 git checkout submodule-topic &&
722 test_must_fail git rebase -i submodule-base &&
724 git rebase --continue &&
725 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
728 test_expect_success 'avoid unnecessary reset' '
729 git checkout master &&
731 test-tool chmtime =123456789 file3 &&
732 git update-index --refresh &&
733 HEAD=$(git rev-parse HEAD) &&
735 git rebase -i HEAD~4 &&
736 test $HEAD = $(git rev-parse HEAD) &&
737 MTIME=$(test-tool chmtime --get file3) &&
738 test 123456789 = $MTIME
741 test_expect_success 'reword' '
742 git checkout -b reword-branch master &&
744 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
745 git show HEAD | grep "E changed" &&
746 test $(git rev-parse master) != $(git rev-parse HEAD) &&
747 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
748 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
749 git show HEAD^ | grep "D changed" &&
750 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
751 git show HEAD~3 | grep "B changed" &&
752 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
753 git show HEAD~2 | grep "C changed"
756 test_expect_success 'rebase -i can copy notes' '
757 git config notes.rewrite.rebase true &&
758 git config notes.rewriteRef "refs/notes/*" &&
762 git notes add -m"a note" n3 &&
764 git rebase -i --onto n1 n2 &&
765 test "a note" = "$(git notes show HEAD)"
774 test_expect_success 'rebase -i can copy notes over a fixup' '
775 git reset --hard n3 &&
776 git notes add -m"an earlier note" n2 &&
778 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
779 git notes show > output &&
780 test_cmp expect output
783 test_expect_success 'rebase while detaching HEAD' '
784 git symbolic-ref HEAD &&
785 grandparent=$(git rev-parse HEAD~2) &&
788 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
789 test $grandparent = $(git rev-parse HEAD~2) &&
790 test_must_fail git symbolic-ref HEAD
793 test_tick # Ensure that the rebased commits get a different timestamp.
794 test_expect_success 'always cherry-pick with --no-ff' '
795 git checkout no-ff-branch &&
796 git tag original-no-ff-branch &&
798 git rebase -i --no-ff A &&
801 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
802 git diff HEAD~$p original-no-ff-branch~$p > out &&
803 test_must_be_empty out
805 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
806 git diff HEAD~3 original-no-ff-branch~3 > out &&
807 test_must_be_empty out
810 test_expect_success 'set up commits with funny messages' '
811 git checkout -b funny A &&
814 git commit -a -m "end with slash\\" &&
817 git commit -a -m "something (\000) that looks like octal" &&
820 git commit -a -m "something (\n) that looks like a newline" &&
823 git commit -a -m "another commit"
826 test_expect_success 'rebase-i history with funny messages' '
827 git rev-list A..funny >expect &&
830 FAKE_LINES="1 2 3 4" git rebase -i A &&
831 git rev-list A.. >actual &&
832 test_cmp expect actual
835 test_expect_success 'prepare for rebase -i --exec' '
836 git checkout master &&
837 git checkout -b execute &&
838 test_commit one_exec main.txt one_exec &&
839 test_commit two_exec main.txt two_exec &&
840 test_commit three_exec main.txt three_exec
843 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
845 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
847 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
849 git rebase -i HEAD~2 >expect
851 sed -e "1,9d" expect >expected &&
852 test_cmp expected actual
855 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
856 git reset --hard execute &&
858 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
860 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
862 git rebase -i HEAD~2 >expect
864 sed -e "1,9d" expect >expected &&
865 test_cmp expected actual
868 test_expect_success 'running "git rebase -ix git show HEAD"' '
869 git reset --hard execute &&
871 git rebase -ix "git show HEAD" HEAD~2 >actual &&
873 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
875 git rebase -i HEAD~2 >expect
877 sed -e "1,9d" expect >expected &&
878 test_cmp expected actual
882 test_expect_success 'rebase -ix with several <CMD>' '
883 git reset --hard execute &&
885 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
887 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
889 git rebase -i HEAD~2 >expect
891 sed -e "1,9d" expect >expected &&
892 test_cmp expected actual
895 test_expect_success 'rebase -ix with several instances of --exec' '
896 git reset --hard execute &&
898 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
900 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
901 exec_git_show_HEAD exec_pwd" &&
903 git rebase -i HEAD~2 >expect
905 sed -e "1,11d" expect >expected &&
906 test_cmp expected actual
909 test_expect_success C_LOCALE_OUTPUT 'rebase -ix with --autosquash' '
910 git reset --hard execute &&
911 git checkout -b autosquash &&
912 echo second >second.txt &&
913 git add second.txt &&
914 git commit -m "fixup! two_exec" &&
917 git commit -m "fixup! two_exec" &&
920 git checkout -b autosquash_actual &&
921 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
923 git checkout autosquash &&
925 git checkout -b autosquash_expected &&
926 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
928 git rebase -i HEAD~4 >expect
930 sed -e "1,13d" expect >expected &&
931 test_cmp expected actual
934 test_expect_success 'rebase --exec works without -i ' '
935 git reset --hard execute &&
936 rm -rf exec_output &&
937 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
938 test_i18ngrep "Successfully rebased and updated" actual &&
939 test_line_count = 2 exec_output &&
940 test_path_is_missing invoked_editor
943 test_expect_success 'rebase -i --exec without <CMD>' '
944 git reset --hard execute &&
946 test_must_fail git rebase -i --exec 2>actual &&
947 test_i18ngrep "requires a value" actual &&
951 test_expect_success 'rebase -i --root re-order and drop commits' '
954 FAKE_LINES="3 1 2 5" git rebase -i --root &&
955 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
956 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
957 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
958 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
959 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
962 test_expect_success 'rebase -i --root retain root commit author and message' '
966 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
968 FAKE_LINES="2" git rebase -i --root &&
969 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
970 git cat-file commit HEAD | grep -q "^different author$"
973 test_expect_success 'rebase -i --root temporary sentinel commit' '
976 test_must_fail env FAKE_LINES="2" git rebase -i --root &&
977 git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
981 test_expect_success 'rebase -i --root fixup root commit' '
984 FAKE_LINES="1 fixup 2" git rebase -i --root &&
985 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
986 test B = $(git show HEAD:file1) &&
987 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
990 test_expect_success 'rebase -i --root reword root commit' '
991 test_when_finished "test_might_fail git rebase --abort" &&
992 git checkout -b reword-root-branch master &&
994 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
995 git rebase -i --root &&
996 git show HEAD^ | grep "A changed" &&
997 test -z "$(git show -s --format=%p HEAD^)"
1000 test_expect_success 'rebase -i --root when root has untracked file confilct' '
1001 test_when_finished "reset_rebase" &&
1002 git checkout -b failing-root-pick A &&
1005 git commit -m "remove file 1 add file 2" &&
1008 test_must_fail env FAKE_LINES="1 2" git rebase -i --root &&
1010 git rebase --continue &&
1011 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1012 test "$(git rev-list --count HEAD)" = 2
1015 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1016 test_when_finished "reset_rebase" &&
1019 test_must_fail env FAKE_LINES="reword 1 2" \
1020 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1022 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue &&
1023 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1024 test "$(git rev-list --count HEAD)" = 2
1027 test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
1028 git checkout reword-root-branch &&
1030 git checkout conflict-branch &&
1032 test_must_fail git rebase --onto HEAD~2 HEAD~ &&
1033 test_must_fail git rebase --edit-todo &&
1037 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1039 git checkout no-conflict-branch^0 &&
1041 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1042 FAKE_LINES="2 1" git rebase --edit-todo &&
1043 git rebase --continue &&
1044 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1045 test L = $(git cat-file commit HEAD | sed -ne \$p)
1048 test_expect_success 'rebase -i produces readable reflog' '
1050 git branch -f branch-reflog-test H &&
1052 git rebase -i --onto I F branch-reflog-test &&
1053 cat >expect <<-\EOF &&
1054 rebase -i (finish): returning to refs/heads/branch-reflog-test
1057 rebase -i (start): checkout I
1059 git reflog -n4 HEAD |
1060 sed "s/[^:]*: //" >actual &&
1061 test_cmp expect actual
1064 test_expect_success 'rebase -i respects core.commentchar' '
1067 test_config core.commentchar "\\" &&
1068 write_script remove-all-but-first.sh <<-\EOF &&
1069 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1072 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1074 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1077 test_expect_success 'rebase -i respects core.commentchar=auto' '
1078 test_config core.commentchar auto &&
1079 write_script copy-edit-script.sh <<-\EOF &&
1082 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1083 test_when_finished "git rebase --abort || :" &&
1084 git rebase -i HEAD^ &&
1085 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1088 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1089 test_when_finished "git branch -D torebase" &&
1090 git checkout -b torebase branch1 &&
1091 upstream=$(git rev-parse ":/J") &&
1092 onto=$(git rev-parse ":/A") &&
1093 git rebase --onto $onto $upstream &&
1094 git reset --hard branch1 &&
1095 git rebase --onto ":/A" ":/J" &&
1096 git checkout branch1
1099 test_expect_success 'rebase -i with --strategy and -X' '
1100 git checkout -b conflict-merge-use-theirs conflict-branch &&
1101 git reset --hard HEAD^ &&
1102 echo five >conflict &&
1104 git commit -a -m "one file conflict" &&
1105 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1106 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1107 test $(cat file1) = Z
1110 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1111 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1112 git reset --hard HEAD^ &&
1114 git add breakpoint &&
1115 git commit -m "breakpoint for interactive mode" &&
1116 echo five >conflict &&
1118 git commit -a -m "one file conflict" &&
1120 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1121 git rebase --continue &&
1122 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1123 test $(cat file1) = Z
1126 test_expect_success 'rebase -i error on commits with \ in message' '
1127 current_head=$(git rev-parse HEAD) &&
1128 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1129 test_commit TO-REMOVE will-conflict old-content &&
1130 test_commit "\temp" will-conflict new-content dummy &&
1131 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1132 test_expect_code 1 grep " emp" error
1135 test_expect_success 'short SHA-1 setup' '
1136 test_when_finished "git checkout master" &&
1137 git checkout --orphan collide &&
1141 test_commit collide1 collide &&
1142 test_commit --notick collide2 collide &&
1143 test_commit --notick collide3 collide
1147 test_expect_success 'short SHA-1 collide' '
1148 test_when_finished "reset_rebase && git checkout master" &&
1149 git checkout collide &&
1154 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1155 FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1159 test_expect_success 'respect core.abbrev' '
1160 git config core.abbrev 12 &&
1161 set_cat_todo_editor &&
1162 test_must_fail git rebase -i HEAD~4 >todo-list &&
1163 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1166 test_expect_success 'todo count' '
1167 write_script dump-raw.sh <<-\EOF &&
1170 test_set_editor "$(pwd)/dump-raw.sh" &&
1171 git rebase -i HEAD~4 >actual &&
1172 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1175 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1176 git checkout --force branch2 &&
1179 FAKE_LINES="edit 1 2" git rebase -i A &&
1180 test_cmp_rev HEAD F &&
1181 test_path_is_missing file6 &&
1183 test_must_fail git rebase --continue &&
1184 test_cmp_rev HEAD F &&
1186 git rebase --continue &&
1190 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1191 git checkout --force branch2 &&
1193 git tag original-branch2 &&
1195 FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1196 test_cmp_rev HEAD F &&
1197 test_path_is_missing file6 &&
1199 test_must_fail git rebase --continue &&
1200 test_cmp_rev HEAD F &&
1202 git rebase --continue &&
1203 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1204 git reset --hard original-branch2
1207 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1208 git checkout --force branch2 &&
1211 FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1212 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1213 test_path_is_missing file6 &&
1215 test_must_fail git rebase --continue &&
1216 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1218 git rebase --continue &&
1219 test $(git cat-file commit HEAD | sed -ne \$p) = I
1222 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1223 git checkout -b commit-to-skip &&
1226 test_seq 5 | sed "s/$double/&&/" >seq &&
1229 git commit -m seq-$double
1232 git reset --hard HEAD~2 &&
1233 git cherry-pick seq-onto &&
1235 test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1236 test -d .git/rebase-merge &&
1237 git rebase --continue &&
1238 git diff --exit-code seq-onto &&
1239 test ! -d .git/rebase-merge &&
1240 test ! -f .git/CHERRY_PICK_HEAD
1243 rebase_setup_and_clean () {
1244 test_when_finished "
1245 git checkout master &&
1246 test_might_fail git branch -D $1 &&
1247 test_might_fail git rebase --abort
1249 git checkout -b $1 ${2:-master}
1252 test_expect_success 'drop' '
1253 rebase_setup_and_clean drop-test &&
1255 FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1256 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1257 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1258 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1261 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1262 test_config rebase.missingCommitsCheck ignore &&
1263 rebase_setup_and_clean missing-commit &&
1265 FAKE_LINES="1 2 3 4" \
1266 git rebase -i --root 2>actual &&
1267 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1269 "Successfully rebased and updated refs/heads/missing-commit" \
1274 Warning: some commits may have been dropped accidentally.
1275 Dropped commits (newer to older):
1276 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1277 To avoid this message, use "drop" to explicitly remove a commit.
1279 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1280 The possible behaviours are: ignore, warn, error.
1286 Successfully rebased and updated refs/heads/missing-commit.
1293 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1294 test_config rebase.missingCommitsCheck warn &&
1295 rebase_setup_and_clean missing-commit &&
1297 FAKE_LINES="1 2 3 4" \
1298 git rebase -i --root 2>actual.2 &&
1299 cr_to_nl <actual.2 >actual &&
1300 test_i18ncmp expect actual &&
1301 test D = $(git cat-file commit HEAD | sed -ne \$p)
1305 Warning: some commits may have been dropped accidentally.
1306 Dropped commits (newer to older):
1307 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1308 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1309 To avoid this message, use "drop" to explicitly remove a commit.
1311 Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1312 The possible behaviours are: ignore, warn, error.
1314 You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1315 Or you can abort the rebase with 'git rebase --abort'.
1318 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1319 test_config rebase.missingCommitsCheck error &&
1320 rebase_setup_and_clean missing-commit &&
1322 test_must_fail env FAKE_LINES="1 2 4" \
1323 git rebase -i --root 2>actual &&
1324 test_i18ncmp expect actual &&
1325 cp .git/rebase-merge/git-rebase-todo.backup \
1326 .git/rebase-merge/git-rebase-todo &&
1327 FAKE_LINES="1 2 drop 3 4 drop 5" \
1328 git rebase --edit-todo &&
1329 git rebase --continue &&
1330 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1331 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1334 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1335 rebase_setup_and_clean abbrevcmd &&
1336 test_commit "first" file1.txt "first line" first &&
1337 test_commit "second" file1.txt "another line" second &&
1338 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1339 test_commit "squash! second" file1.txt "another line here" second_squash &&
1340 cat >expected <<-EOF &&
1341 p $(git rev-list --abbrev-commit -1 first) first
1342 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1344 p $(git rev-list --abbrev-commit -1 second) second
1345 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1348 git checkout abbrevcmd &&
1349 set_cat_todo_editor &&
1350 test_config rebase.abbreviateCommands true &&
1351 test_must_fail git rebase -i --exec "git show HEAD" \
1352 --autosquash master >actual &&
1353 test_cmp expected actual
1356 test_expect_success 'static check of bad command' '
1357 rebase_setup_and_clean bad-cmd &&
1359 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1360 git rebase -i --root 2>actual &&
1361 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
1362 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1363 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1364 git rebase --continue &&
1365 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1366 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1369 test_expect_success 'tabs and spaces are accepted in the todolist' '
1370 rebase_setup_and_clean indented-comment &&
1371 write_script add-indent.sh <<-\EOF &&
1373 # Turn single spaces into space/tab mix
1374 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1375 printf "\n\t# comment\n #more\n\t # comment\n"
1379 test_set_editor "$(pwd)/add-indent.sh" &&
1380 git rebase -i HEAD^^^ &&
1381 test E = $(git cat-file commit HEAD | sed -ne \$p)
1384 test_expect_success 'static check of bad SHA-1' '
1385 rebase_setup_and_clean bad-sha &&
1387 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1388 git rebase -i --root 2>actual &&
1389 test_i18ngrep "edit XXXXXXX False commit" actual &&
1390 test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1391 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1392 git rebase --continue &&
1393 test E = $(git cat-file commit HEAD | sed -ne \$p)
1396 test_expect_success 'editor saves as CR/LF' '
1397 git checkout -b with-crlf &&
1398 write_script add-crs.sh <<-\EOF &&
1399 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1403 test_set_editor "$(pwd)/add-crs.sh" &&
1409 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1410 test_when_finished "test_might_fail git rebase --abort" &&
1412 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1414 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1417 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1418 test_when_finished "test_might_fail git rebase --abort" &&
1419 test_config commit.gpgsign true &&
1421 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1423 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1426 test_expect_success 'valid author header after --root swap' '
1427 rebase_setup_and_clean author-header no-conflict-branch &&
1429 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1430 git cat-file commit HEAD | grep ^author >expected &&
1431 FAKE_LINES="5 1" git rebase -i --root &&
1432 git cat-file commit HEAD^ | grep ^author >actual &&
1433 test_cmp expected actual
1436 test_expect_success 'valid author header when author contains single quote' '
1437 rebase_setup_and_clean author-header no-conflict-branch &&
1439 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1440 git cat-file commit HEAD | grep ^author >expected &&
1441 FAKE_LINES="2" git rebase -i HEAD~2 &&
1442 git cat-file commit HEAD | grep ^author >actual &&
1443 test_cmp expected actual