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
33 git rev-parse
--verify "$1" >expect.
rev &&
34 git rev-parse
--verify "$2" >actual.
rev &&
35 test_cmp expect.
rev actual.
rev
40 # WARNING: Modifications to the initial repository can change the SHA ID used
41 # in the expect2 file for the 'stop on conflicting pick' test.
43 test_expect_success
'setup' '
44 test_commit A file1 &&
45 test_commit B file1 &&
46 test_commit C file2 &&
47 test_commit D file1 &&
48 test_commit E file3 &&
49 git checkout -b branch1 A &&
50 test_commit F file4 &&
51 test_commit G file1 &&
52 test_commit H file5 &&
53 git checkout -b branch2 F &&
54 test_commit I file6 &&
55 git checkout -b conflict-branch A &&
56 test_commit one conflict &&
57 test_commit two conflict &&
58 test_commit three conflict &&
59 test_commit four conflict &&
60 git checkout -b no-conflict-branch A &&
61 test_commit J fileJ &&
62 test_commit K fileK &&
63 test_commit L fileL &&
64 test_commit M fileM &&
65 git checkout -b no-ff-branch A &&
66 test_commit N fileN &&
67 test_commit O fileO &&
71 # "exec" commands are ran with the user shell by default, but this may
72 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
73 # to create a file. Unseting SHELL avoids such non-portable behavior
74 # in tests. It must be exported for it to take effect where needed.
78 test_expect_success
'rebase -i with the exec command' '
79 git checkout master &&
81 FAKE_LINES="1 exec_>touch-one
82 2 exec_>touch-two exec_false exec_>touch-three
83 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
85 test_must_fail git rebase -i A
87 test_path_is_file touch-one &&
88 test_path_is_file touch-two &&
89 test_path_is_missing touch-three " (should have stopped before)" &&
90 test_cmp_rev C HEAD &&
91 git rebase --continue &&
92 test_path_is_file touch-three &&
93 test_path_is_file "touch-file name with spaces" &&
94 test_path_is_file touch-after-semicolon &&
95 test_cmp_rev master HEAD &&
99 test_expect_success
'rebase -i with the exec command runs from tree root' '
100 git checkout master &&
101 mkdir subdir && (cd subdir &&
102 FAKE_LINES="1 exec_>touch-subdir" \
105 test_path_is_file touch-subdir &&
109 test_expect_success
'rebase -i with the exec command checks tree cleanness' '
110 git checkout master &&
112 FAKE_LINES="exec_echo_foo_>file1 1" &&
114 test_must_fail git rebase -i HEAD^
116 test_cmp_rev master^ HEAD &&
118 git rebase --continue
121 test_expect_success
'no changes are a nop' '
122 git checkout branch2 &&
124 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
125 test $(git rev-parse I) = $(git rev-parse HEAD)
128 test_expect_success
'test the [branch] option' '
129 git checkout -b dead-end &&
131 git commit -m "stop here" &&
132 git rebase -i F branch2 &&
133 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
134 test $(git rev-parse I) = $(git rev-parse branch2) &&
135 test $(git rev-parse I) = $(git rev-parse HEAD)
138 test_expect_success
'test --onto <branch>' '
139 git checkout -b test-onto branch2 &&
140 git rebase -i --onto branch1 F &&
141 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
142 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
143 test $(git rev-parse I) = $(git rev-parse branch2)
146 test_expect_success
'rebase on top of a non-conflicting commit' '
147 git checkout branch1 &&
148 git tag original-branch1 &&
149 git rebase -i branch2 &&
150 test file6 = $(git diff --name-only original-branch1) &&
151 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
152 test $(git rev-parse I) = $(git rev-parse branch2) &&
153 test $(git rev-parse I) = $(git rev-parse HEAD~2)
156 test_expect_success
'reflog for the branch shows state before rebase' '
157 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
160 test_expect_success
'exchange two commits' '
161 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
162 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
163 test G = $(git cat-file commit HEAD | sed -ne \$p)
167 diff --git a/file1 b/file1
168 index f70f10e..fd79235 100644
184 test_expect_success
'stop on conflicting pick' '
185 git tag new-branch1 &&
186 test_must_fail git rebase -i master &&
187 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
188 test_cmp expect .git/rebase-merge/patch &&
189 test_cmp expect2 file1 &&
190 test "$(git diff --name-status |
191 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
192 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
193 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
196 test_expect_success
'abort' '
197 git rebase --abort &&
198 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
199 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
200 test_path_is_missing .git/rebase-merge
203 test_expect_success
'abort with error when new base cannot be checked out' '
204 git rm --cached file1 &&
205 git commit -m "remove file in base" &&
206 test_must_fail git rebase -i master > output 2>&1 &&
207 grep "The following untracked working tree files would be overwritten by checkout:" \
209 grep "file1" output &&
210 test_path_is_missing .git/rebase-merge &&
211 git reset --hard HEAD^
214 test_expect_success
'retain authorship' '
218 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
220 git rebase -i --onto master HEAD^ &&
221 git show HEAD | grep "^Author: Twerp Snog"
224 test_expect_success
'squash' '
225 git reset --hard twerp &&
228 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
229 echo "******************************" &&
230 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
231 git rebase -i --onto master HEAD~2 &&
232 test B = $(cat file7) &&
233 test $(git rev-parse HEAD^) = $(git rev-parse master)
236 test_expect_success
'retain authorship when squashing' '
237 git show HEAD | grep "^Author: Twerp Snog"
240 test_expect_success
'-p handles "no changes" gracefully' '
241 HEAD=$(git rev-parse HEAD) &&
242 git rebase -i -p HEAD^ &&
243 git update-index --refresh &&
244 git diff-files --quiet &&
245 git diff-index --quiet --cached HEAD -- &&
246 test $HEAD = $(git rev-parse HEAD)
249 test_expect_failure
'exchange two commits with -p' '
251 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
252 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
253 test G = $(git cat-file commit HEAD | sed -ne \$p)
256 test_expect_success
'preserve merges with -p' '
257 git checkout -b to-be-preserved master^ &&
258 : > unrelated-file &&
259 git add unrelated-file &&
261 git commit -m "unrelated" &&
262 git checkout -b another-branch master &&
265 git commit -m J file1 &&
267 git merge to-be-preserved &&
270 git commit -m K file1 &&
273 git commit -m L1 file1 &&
274 git checkout HEAD^ &&
275 echo 1 > unrelated-file &&
277 git commit -m L2 unrelated-file &&
279 git merge another-branch &&
282 git commit -m M file1 &&
283 git checkout -b to-be-rebased &&
285 git rebase -i -p --onto branch1 master &&
286 git update-index --refresh &&
287 git diff-files --quiet &&
288 git diff-index --quiet --cached HEAD -- &&
289 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
290 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
291 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
292 test $(git show HEAD~5:file1) = B &&
293 test $(git show HEAD~3:file1) = C &&
294 test $(git show HEAD:file1) = E &&
295 test $(git show HEAD:unrelated-file) = 1
298 test_expect_success
'edit ancestor with -p' '
299 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
300 echo 2 > unrelated-file &&
302 git commit -m L2-modified --amend unrelated-file &&
303 git rebase --continue &&
304 git update-index --refresh &&
305 git diff-files --quiet &&
306 git diff-index --quiet --cached HEAD -- &&
307 test $(git show HEAD:unrelated-file) = 2
310 test_expect_success
'--continue tries to commit' '
312 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
313 echo resolved > file1 &&
315 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
316 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
317 git show HEAD | grep chouette
320 test_expect_success
'verbose flag is heeded, even after --continue' '
321 git reset --hard master@{1} &&
323 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
324 echo resolved > file1 &&
326 git rebase --continue > output &&
327 grep "^ file1 | 2 +-$" output
330 test_expect_success
'multi-squash only fires up editor once' '
331 base=$(git rev-parse HEAD~4) &&
332 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
333 EXPECT_HEADER_COUNT=4 \
334 git rebase -i $base &&
335 test $base = $(git rev-parse HEAD^) &&
336 test 1 = $(git show | grep ONCE | wc -l)
339 test_expect_success
'multi-fixup does not fire up editor' '
340 git checkout -b multi-fixup E &&
341 base=$(git rev-parse HEAD~4) &&
342 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
343 git rebase -i $base &&
344 test $base = $(git rev-parse HEAD^) &&
345 test 0 = $(git show | grep NEVER | wc -l) &&
346 git checkout to-be-rebased &&
347 git branch -D multi-fixup
350 test_expect_success
'commit message used after conflict' '
351 git checkout -b conflict-fixup conflict-branch &&
352 base=$(git rev-parse HEAD~4) &&
354 FAKE_LINES="1 fixup 3 fixup 4" &&
356 test_must_fail git rebase -i $base
358 echo three > conflict &&
360 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
361 git rebase --continue &&
362 test $base = $(git rev-parse HEAD^) &&
363 test 1 = $(git show | grep ONCE | wc -l) &&
364 git checkout to-be-rebased &&
365 git branch -D conflict-fixup
368 test_expect_success
'commit message retained after conflict' '
369 git checkout -b conflict-squash conflict-branch &&
370 base=$(git rev-parse HEAD~4) &&
372 FAKE_LINES="1 fixup 3 squash 4" &&
374 test_must_fail git rebase -i $base
376 echo three > conflict &&
378 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
379 git rebase --continue &&
380 test $base = $(git rev-parse HEAD^) &&
381 test 2 = $(git show | grep TWICE | wc -l) &&
382 git checkout to-be-rebased &&
383 git branch -D conflict-squash
386 cat > expect-squash-fixup
<< EOF
394 test_expect_success
'squash and fixup generate correct log messages' '
395 git checkout -b squash-fixup E &&
396 base=$(git rev-parse HEAD~4) &&
397 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
398 EXPECT_HEADER_COUNT=4 \
399 git rebase -i $base &&
400 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
401 test_cmp expect-squash-fixup actual-squash-fixup &&
402 git checkout to-be-rebased &&
403 git branch -D squash-fixup
406 test_expect_success
'squash ignores comments' '
407 git checkout -b skip-comments E &&
408 base=$(git rev-parse HEAD~4) &&
409 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
410 EXPECT_HEADER_COUNT=4 \
411 git rebase -i $base &&
412 test $base = $(git rev-parse HEAD^) &&
413 test 1 = $(git show | grep ONCE | wc -l) &&
414 git checkout to-be-rebased &&
415 git branch -D skip-comments
418 test_expect_success
'squash ignores blank lines' '
419 git checkout -b skip-blank-lines E &&
420 base=$(git rev-parse HEAD~4) &&
421 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
422 EXPECT_HEADER_COUNT=4 \
423 git rebase -i $base &&
424 test $base = $(git rev-parse HEAD^) &&
425 test 1 = $(git show | grep ONCE | wc -l) &&
426 git checkout to-be-rebased &&
427 git branch -D skip-blank-lines
430 test_expect_success
'squash works as expected' '
431 git checkout -b squash-works no-conflict-branch &&
432 one=$(git rev-parse HEAD~3) &&
433 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
434 git rebase -i HEAD~3 &&
435 test $one = $(git rev-parse HEAD~2)
438 test_expect_success
'interrupted squash works as expected' '
439 git checkout -b interrupted-squash conflict-branch &&
440 one=$(git rev-parse HEAD~3) &&
442 FAKE_LINES="1 squash 3 2" &&
444 test_must_fail git rebase -i HEAD~3
446 (echo one; echo two; echo four) > conflict &&
448 test_must_fail git rebase --continue &&
449 echo resolved > conflict &&
451 git rebase --continue &&
452 test $one = $(git rev-parse HEAD~2)
455 test_expect_success
'interrupted squash works as expected (case 2)' '
456 git checkout -b interrupted-squash2 conflict-branch &&
457 one=$(git rev-parse HEAD~3) &&
459 FAKE_LINES="3 squash 1 2" &&
461 test_must_fail git rebase -i HEAD~3
463 (echo one; echo four) > conflict &&
465 test_must_fail git rebase --continue &&
466 (echo one; echo two; echo four) > conflict &&
468 test_must_fail git rebase --continue &&
469 echo resolved > conflict &&
471 git rebase --continue &&
472 test $one = $(git rev-parse HEAD~2)
475 test_expect_success
'ignore patch if in upstream' '
476 HEAD=$(git rev-parse HEAD) &&
477 git checkout -b has-cherry-picked HEAD^ &&
478 echo unrelated > file7 &&
481 git commit -m "unrelated change" &&
482 git cherry-pick $HEAD &&
483 EXPECT_COUNT=1 git rebase -i $HEAD &&
484 test $HEAD = $(git rev-parse HEAD^)
487 test_expect_success
'--continue tries to commit, even for "edit"' '
488 parent=$(git rev-parse HEAD^) &&
490 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
491 echo edited > file7 &&
493 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
494 test edited = $(git show HEAD:file7) &&
495 git show HEAD | grep chouette &&
496 test $parent = $(git rev-parse HEAD^)
499 test_expect_success
'aborted --continue does not squash commits after "edit"' '
500 old=$(git rev-parse HEAD) &&
502 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
503 echo "edited again" > file7 &&
506 FAKE_COMMIT_MESSAGE=" " &&
507 export FAKE_COMMIT_MESSAGE &&
508 test_must_fail git rebase --continue
510 test $old = $(git rev-parse HEAD) &&
514 test_expect_success
'auto-amend only edited commits after "edit"' '
516 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
517 echo "edited again" > file7 &&
519 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
520 echo "and again" > file7 &&
524 FAKE_COMMIT_MESSAGE="and again" &&
525 export FAKE_COMMIT_MESSAGE &&
526 test_must_fail git rebase --continue
531 test_expect_success
'clean error after failed "exec"' '
533 test_when_finished "git rebase --abort || :" &&
535 FAKE_LINES="1 exec_false" &&
537 test_must_fail git rebase -i HEAD^
539 echo "edited again" > file7 &&
541 test_must_fail git rebase --continue 2>error &&
542 grep "You have staged changes in your working tree." error
545 test_expect_success
'rebase a detached HEAD' '
546 grandparent=$(git rev-parse HEAD~2) &&
547 git checkout $(git rev-parse HEAD) &&
549 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
550 test $grandparent = $(git rev-parse HEAD~2)
553 test_expect_success
'rebase a commit violating pre-commit' '
555 mkdir -p .git/hooks &&
556 PRE_COMMIT=.git/hooks/pre-commit &&
557 echo "#!/bin/sh" > $PRE_COMMIT &&
558 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
559 chmod a+x $PRE_COMMIT &&
560 echo "monde! " >> file1 &&
562 test_must_fail git commit -m doesnt-verify file1 &&
563 git commit -m doesnt-verify --no-verify file1 &&
565 FAKE_LINES=2 git rebase -i HEAD~2
569 test_expect_success
'rebase with a file named HEAD in worktree' '
573 git checkout -b branch3 A &&
576 GIT_AUTHOR_NAME="Squashed Away" &&
577 export GIT_AUTHOR_NAME &&
580 git commit -m "Add head" &&
583 git commit -m "Add body"
586 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
587 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
591 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
593 git checkout -b branch4 HEAD &&
594 GIT_EDITOR=: git commit --amend \
595 --author="Somebody else <somebody@else.com>" &&
596 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
597 git rebase -i branch3 &&
598 test $(git rev-parse branch3) = $(git rev-parse branch4)
602 test_expect_success
'submodule rebase setup' '
606 cd sub && git init && >elif &&
607 git add elif && git commit -m "submodule initial"
612 git commit -m "One" &&
615 git commit -a -m "Two" &&
617 cd sub && echo 3 >elif &&
618 git commit -a -m "submodule second"
621 git commit -a -m "Three changes submodule"
624 test_expect_success
'submodule rebase -i' '
625 FAKE_LINES="1 squash 2 3" git rebase -i A
628 test_expect_success
'submodule conflict setup' '
629 git tag submodule-base &&
630 git checkout HEAD^ &&
632 cd sub && git checkout HEAD^ && echo 4 >elif &&
633 git add elif && git commit -m "submodule conflict"
637 git commit -m "Conflict in submodule" &&
638 git tag submodule-topic
641 test_expect_success
'rebase -i continue with only submodule staged' '
642 test_must_fail git rebase -i submodule-base &&
644 git rebase --continue &&
645 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
648 test_expect_success
'rebase -i continue with unstaged submodule' '
649 git checkout submodule-topic &&
651 test_must_fail git rebase -i submodule-base &&
653 git rebase --continue &&
654 test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
657 test_expect_success
'avoid unnecessary reset' '
658 git checkout master &&
660 test-chmtime =123456789 file3 &&
661 git update-index --refresh &&
662 HEAD=$(git rev-parse HEAD) &&
663 git rebase -i HEAD~4 &&
664 test $HEAD = $(git rev-parse HEAD) &&
665 MTIME=$(test-chmtime -v +0 file3 | sed 's
/[^
0-9].
*$
//') &&
666 test 123456789 = $MTIME
669 test_expect_success
'reword' '
670 git checkout -b reword-branch master &&
671 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
672 git show HEAD | grep "E changed" &&
673 test $(git rev-parse master) != $(git rev-parse HEAD) &&
674 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
675 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
676 git show HEAD^ | grep "D changed" &&
677 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
678 git show HEAD~3 | grep "B changed" &&
679 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
680 git show HEAD~2 | grep "C changed"
683 test_expect_success
'rebase -i can copy notes' '
684 git config notes.rewrite.rebase true &&
685 git config notes.rewriteRef "refs/notes/*" &&
689 git notes add -m"a note" n3 &&
690 git rebase --onto n1 n2 &&
691 test "a note" = "$(git notes show HEAD)"
700 test_expect_success
'rebase -i can copy notes over a fixup' '
701 git reset --hard n3 &&
702 git notes add -m"an earlier note" n2 &&
703 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
704 git notes show > output &&
705 test_cmp expect output
708 test_expect_success
'rebase while detaching HEAD' '
709 git symbolic-ref HEAD &&
710 grandparent=$(git rev-parse HEAD~2) &&
712 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
713 test $grandparent = $(git rev-parse HEAD~2) &&
714 test_must_fail git symbolic-ref HEAD
717 test_tick
# Ensure that the rebased commits get a different timestamp.
718 test_expect_success
'always cherry-pick with --no-ff' '
719 git checkout no-ff-branch &&
720 git tag original-no-ff-branch &&
721 git rebase -i --no-ff A &&
725 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
726 git diff HEAD~$p original-no-ff-branch~$p > out &&
729 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
730 git diff HEAD~3 original-no-ff-branch~3 > out &&
734 test_expect_success
'set up commits with funny messages' '
735 git checkout -b funny A &&
738 git commit -a -m "end with slash\\" &&
741 git commit -a -m "something (\000) that looks like octal" &&
744 git commit -a -m "something (\n) that looks like a newline" &&
747 git commit -a -m "another commit"
750 test_expect_success
'rebase-i history with funny messages' '
751 git rev-list A..funny >expect &&
753 FAKE_LINES="1 2 3 4" git rebase -i A &&
754 git rev-list A.. >actual &&
755 test_cmp expect actual