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 . "$TEST_DIRECTORY"/lib-rebase.sh
17 # Set up the repository like this:
19 # one - two - three - four (conflict-branch)
21 # A - B - C - D - E (master)
23 # | F - G - H (branch1)
27 # | J - K - L - M (no-conflict-branch)
29 # N - O - P (no-ff-branch)
31 # where A, B, D and G all touch file1, and one, two, three, four all
32 # touch file "conflict".
34 # WARNING: Modifications to the initial repository can change the SHA ID used
35 # in the expect2 file for the 'stop on conflicting pick' test.
38 test_expect_success 'setup' '
39 test_commit A file1 &&
40 test_commit B file1 &&
41 test_commit C file2 &&
42 test_commit D file1 &&
43 test_commit E file3 &&
44 git checkout -b branch1 A &&
45 test_commit F file4 &&
46 test_commit G file1 &&
47 test_commit H file5 &&
48 git checkout -b branch2 F &&
50 git checkout -b conflict-branch A &&
51 for n in one two three four
53 test_commit $n conflict
55 git checkout -b no-conflict-branch A &&
60 git checkout -b no-ff-branch A &&
67 # "exec" commands are ran with the user shell by default, but this may
68 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
69 # to create a file. Unseting SHELL avoids such non-portable behavior
70 # in tests. It must be exported for it to take effect where needed.
74 test_expect_success 'rebase -i with the exec command' '
75 git checkout master &&
77 FAKE_LINES="1 exec_>touch-one
78 2 exec_>touch-two exec_false exec_>touch-three
79 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
81 test_must_fail git rebase -i A
83 test_path_is_file touch-one &&
84 test_path_is_file touch-two &&
85 test_path_is_missing touch-three " (should have stopped before)" &&
86 test $(git rev-parse C) = $(git rev-parse HEAD) || {
87 echo "Stopped at wrong revision:"
88 echo "($(git describe --tags HEAD) instead of C)"
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 $(git rev-parse master) = $(git rev-parse HEAD) || {
96 echo "Stopped at wrong revision:"
97 echo "($(git describe --tags HEAD) instead of master)"
103 test_expect_success 'rebase -i with the exec command runs from tree root' '
104 git checkout master &&
105 mkdir subdir && (cd subdir &&
106 FAKE_LINES="1 exec_>touch-subdir" \
109 test_path_is_file touch-subdir &&
113 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
114 git checkout master &&
116 FAKE_LINES="exec_echo_foo_>file1 1" &&
118 test_must_fail git rebase -i HEAD^
120 test $(git rev-parse master^) = $(git rev-parse HEAD) || {
121 echo "Stopped at wrong revision:"
122 echo "($(git describe --tags HEAD) instead of master^)"
126 git rebase --continue
129 test_expect_success 'no changes are a nop' '
130 git checkout branch2 &&
132 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
133 test $(git rev-parse I) = $(git rev-parse HEAD)
136 test_expect_success 'test the [branch] option' '
137 git checkout -b dead-end &&
139 git commit -m "stop here" &&
140 git rebase -i F branch2 &&
141 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
142 test $(git rev-parse I) = $(git rev-parse branch2) &&
143 test $(git rev-parse I) = $(git rev-parse HEAD)
146 test_expect_success 'test --onto <branch>' '
147 git checkout -b test-onto branch2 &&
148 git rebase -i --onto branch1 F &&
149 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
150 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
151 test $(git rev-parse I) = $(git rev-parse branch2)
154 test_expect_success 'rebase on top of a non-conflicting commit' '
155 git checkout branch1 &&
156 git tag original-branch1 &&
157 git rebase -i branch2 &&
158 test file6 = $(git diff --name-only original-branch1) &&
159 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
160 test $(git rev-parse I) = $(git rev-parse branch2) &&
161 test $(git rev-parse I) = $(git rev-parse HEAD~2)
164 test_expect_success 'reflog for the branch shows state before rebase' '
165 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
168 test_expect_success 'exchange two commits' '
169 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
170 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
171 test G = $(git cat-file commit HEAD | sed -ne \$p)
175 diff --git a/file1 b/file1
176 index f70f10e..fd79235 100644
192 test_expect_success 'stop on conflicting pick' '
193 git tag new-branch1 &&
194 test_must_fail git rebase -i master &&
195 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
196 test_cmp expect .git/rebase-merge/patch &&
197 test_cmp expect2 file1 &&
198 test "$(git diff --name-status |
199 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
200 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
201 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
204 test_expect_success 'abort' '
205 git rebase --abort &&
206 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
207 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
208 test_path_is_missing .git/rebase-merge
211 test_expect_success 'abort with error when new base cannot be checked out' '
212 git rm --cached file1 &&
213 git commit -m "remove file in base" &&
214 test_must_fail git rebase -i master > output 2>&1 &&
215 grep "The following untracked working tree files would be overwritten by checkout:" \
217 grep "file1" output &&
218 test_path_is_missing .git/rebase-merge &&
219 git reset --hard HEAD^
222 test_expect_success 'retain authorship' '
226 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
228 git rebase -i --onto master HEAD^ &&
229 git show HEAD | grep "^Author: Twerp Snog"
232 test_expect_success 'squash' '
233 git reset --hard twerp &&
236 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
237 echo "******************************" &&
238 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
239 git rebase -i --onto master HEAD~2 &&
240 test B = $(cat file7) &&
241 test $(git rev-parse HEAD^) = $(git rev-parse master)
244 test_expect_success 'retain authorship when squashing' '
245 git show HEAD | grep "^Author: Twerp Snog"
248 test_expect_success '-p handles "no changes" gracefully' '
249 HEAD=$(git rev-parse HEAD) &&
250 git rebase -i -p HEAD^ &&
251 git update-index --refresh &&
252 git diff-files --quiet &&
253 git diff-index --quiet --cached HEAD -- &&
254 test $HEAD = $(git rev-parse HEAD)
257 test_expect_failure 'exchange two commits with -p' '
258 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
259 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
260 test G = $(git cat-file commit HEAD | sed -ne \$p)
263 test_expect_success 'preserve merges with -p' '
264 git checkout -b to-be-preserved master^ &&
265 : > unrelated-file &&
266 git add unrelated-file &&
268 git commit -m "unrelated" &&
269 git checkout -b another-branch master &&
272 git commit -m J file1 &&
274 git merge to-be-preserved &&
277 git commit -m K file1 &&
280 git commit -m L1 file1 &&
281 git checkout HEAD^ &&
282 echo 1 > unrelated-file &&
284 git commit -m L2 unrelated-file &&
286 git merge another-branch &&
289 git commit -m M file1 &&
290 git checkout -b to-be-rebased &&
292 git rebase -i -p --onto branch1 master &&
293 git update-index --refresh &&
294 git diff-files --quiet &&
295 git diff-index --quiet --cached HEAD -- &&
296 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
297 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
298 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
299 test $(git show HEAD~5:file1) = B &&
300 test $(git show HEAD~3:file1) = C &&
301 test $(git show HEAD:file1) = E &&
302 test $(git show HEAD:unrelated-file) = 1
305 test_expect_success 'edit ancestor with -p' '
306 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
307 echo 2 > unrelated-file &&
309 git commit -m L2-modified --amend unrelated-file &&
310 git rebase --continue &&
311 git update-index --refresh &&
312 git diff-files --quiet &&
313 git diff-index --quiet --cached HEAD -- &&
314 test $(git show HEAD:unrelated-file) = 2
317 test_expect_success '--continue tries to commit' '
319 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
320 echo resolved > file1 &&
322 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
323 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
324 git show HEAD | grep chouette
327 test_expect_success 'verbose flag is heeded, even after --continue' '
328 git reset --hard HEAD@{1} &&
330 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
331 echo resolved > file1 &&
333 git rebase --continue > output &&
334 grep "^ file1 | 2 +-$" output
337 test_expect_success 'multi-squash only fires up editor once' '
338 base=$(git rev-parse HEAD~4) &&
339 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
340 EXPECT_HEADER_COUNT=4 \
341 git rebase -i $base &&
342 test $base = $(git rev-parse HEAD^) &&
343 test 1 = $(git show | grep ONCE | wc -l)
346 test_expect_success 'multi-fixup does not fire up editor' '
347 git checkout -b multi-fixup E &&
348 base=$(git rev-parse HEAD~4) &&
349 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
350 git rebase -i $base &&
351 test $base = $(git rev-parse HEAD^) &&
352 test 0 = $(git show | grep NEVER | wc -l) &&
353 git checkout to-be-rebased &&
354 git branch -D multi-fixup
357 test_expect_success 'commit message used after conflict' '
358 git checkout -b conflict-fixup conflict-branch &&
359 base=$(git rev-parse HEAD~4) &&
361 FAKE_LINES="1 fixup 3 fixup 4" &&
363 test_must_fail git rebase -i $base
365 echo three > conflict &&
367 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
368 git rebase --continue &&
369 test $base = $(git rev-parse HEAD^) &&
370 test 1 = $(git show | grep ONCE | wc -l) &&
371 git checkout to-be-rebased &&
372 git branch -D conflict-fixup
375 test_expect_success 'commit message retained after conflict' '
376 git checkout -b conflict-squash conflict-branch &&
377 base=$(git rev-parse HEAD~4) &&
379 FAKE_LINES="1 fixup 3 squash 4" &&
381 test_must_fail git rebase -i $base
383 echo three > conflict &&
385 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
386 git rebase --continue &&
387 test $base = $(git rev-parse HEAD^) &&
388 test 2 = $(git show | grep TWICE | wc -l) &&
389 git checkout to-be-rebased &&
390 git branch -D conflict-squash
393 cat > expect-squash-fixup << EOF
401 test_expect_success 'squash and fixup generate correct log messages' '
402 git checkout -b squash-fixup E &&
403 base=$(git rev-parse HEAD~4) &&
404 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
405 EXPECT_HEADER_COUNT=4 \
406 git rebase -i $base &&
407 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
408 test_cmp expect-squash-fixup actual-squash-fixup &&
409 git checkout to-be-rebased &&
410 git branch -D squash-fixup
413 test_expect_success 'squash ignores comments' '
414 git checkout -b skip-comments E &&
415 base=$(git rev-parse HEAD~4) &&
416 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
417 EXPECT_HEADER_COUNT=4 \
418 git rebase -i $base &&
419 test $base = $(git rev-parse HEAD^) &&
420 test 1 = $(git show | grep ONCE | wc -l) &&
421 git checkout to-be-rebased &&
422 git branch -D skip-comments
425 test_expect_success 'squash ignores blank lines' '
426 git checkout -b skip-blank-lines E &&
427 base=$(git rev-parse HEAD~4) &&
428 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
429 EXPECT_HEADER_COUNT=4 \
430 git rebase -i $base &&
431 test $base = $(git rev-parse HEAD^) &&
432 test 1 = $(git show | grep ONCE | wc -l) &&
433 git checkout to-be-rebased &&
434 git branch -D skip-blank-lines
437 test_expect_success 'squash works as expected' '
438 git checkout -b squash-works no-conflict-branch &&
439 one=$(git rev-parse HEAD~3) &&
440 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
441 git rebase -i HEAD~3 &&
442 test $one = $(git rev-parse HEAD~2)
445 test_expect_success 'interrupted squash works as expected' '
446 git checkout -b interrupted-squash conflict-branch &&
447 one=$(git rev-parse HEAD~3) &&
449 FAKE_LINES="1 squash 3 2" &&
451 test_must_fail git rebase -i HEAD~3
453 (echo one; echo two; echo four) > conflict &&
455 test_must_fail git rebase --continue &&
456 echo resolved > conflict &&
458 git rebase --continue &&
459 test $one = $(git rev-parse HEAD~2)
462 test_expect_success 'interrupted squash works as expected (case 2)' '
463 git checkout -b interrupted-squash2 conflict-branch &&
464 one=$(git rev-parse HEAD~3) &&
466 FAKE_LINES="3 squash 1 2" &&
468 test_must_fail git rebase -i HEAD~3
470 (echo one; echo four) > conflict &&
472 test_must_fail git rebase --continue &&
473 (echo one; echo two; echo four) > conflict &&
475 test_must_fail git rebase --continue &&
476 echo resolved > conflict &&
478 git rebase --continue &&
479 test $one = $(git rev-parse HEAD~2)
482 test_expect_success 'ignore patch if in upstream' '
483 HEAD=$(git rev-parse HEAD) &&
484 git checkout -b has-cherry-picked HEAD^ &&
485 echo unrelated > file7 &&
488 git commit -m "unrelated change" &&
489 git cherry-pick $HEAD &&
490 EXPECT_COUNT=1 git rebase -i $HEAD &&
491 test $HEAD = $(git rev-parse HEAD^)
494 test_expect_success '--continue tries to commit, even for "edit"' '
495 parent=$(git rev-parse HEAD^) &&
497 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
498 echo edited > file7 &&
500 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
501 test edited = $(git show HEAD:file7) &&
502 git show HEAD | grep chouette &&
503 test $parent = $(git rev-parse HEAD^)
506 test_expect_success 'aborted --continue does not squash commits after "edit"' '
507 old=$(git rev-parse HEAD) &&
509 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
510 echo "edited again" > file7 &&
513 FAKE_COMMIT_MESSAGE=" " &&
514 export FAKE_COMMIT_MESSAGE &&
515 test_must_fail git rebase --continue
517 test $old = $(git rev-parse HEAD) &&
521 test_expect_success 'auto-amend only edited commits after "edit"' '
523 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
524 echo "edited again" > file7 &&
526 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
527 echo "and again" > file7 &&
531 FAKE_COMMIT_MESSAGE="and again" &&
532 export FAKE_COMMIT_MESSAGE &&
533 test_must_fail git rebase --continue
538 test_expect_success 'rebase a detached HEAD' '
539 grandparent=$(git rev-parse HEAD~2) &&
540 git checkout $(git rev-parse HEAD) &&
542 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
543 test $grandparent = $(git rev-parse HEAD~2)
546 test_expect_success 'rebase a commit violating pre-commit' '
548 mkdir -p .git/hooks &&
549 PRE_COMMIT=.git/hooks/pre-commit &&
550 echo "#!/bin/sh" > $PRE_COMMIT &&
551 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
552 chmod a+x $PRE_COMMIT &&
553 echo "monde! " >> file1 &&
555 test_must_fail git commit -m doesnt-verify file1 &&
556 git commit -m doesnt-verify --no-verify file1 &&
558 FAKE_LINES=2 git rebase -i HEAD~2
562 test_expect_success 'rebase with a file named HEAD in worktree' '
566 git checkout -b branch3 A &&
569 GIT_AUTHOR_NAME="Squashed Away" &&
570 export GIT_AUTHOR_NAME &&
573 git commit -m "Add head" &&
576 git commit -m "Add body"
579 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
580 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
584 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
586 git checkout -b branch4 HEAD &&
587 GIT_EDITOR=: git commit --amend \
588 --author="Somebody else <somebody@else.com>"
589 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
590 git rebase -i branch3 &&
591 test $(git rev-parse branch3) = $(git rev-parse branch4)
595 test_expect_success 'submodule rebase setup' '
599 cd sub && git init && >elif &&
600 git add elif && git commit -m "submodule initial"
605 git commit -m "One" &&
608 git commit -a -m "Two" &&
610 cd sub && echo 3 >elif &&
611 git commit -a -m "submodule second"
614 git commit -a -m "Three changes submodule"
617 test_expect_success 'submodule rebase -i' '
618 FAKE_LINES="1 squash 2 3" git rebase -i A
621 test_expect_success 'avoid unnecessary reset' '
622 git checkout master &&
623 test-chmtime =123456789 file3 &&
624 git update-index --refresh &&
625 HEAD=$(git rev-parse HEAD) &&
626 git rebase -i HEAD~4 &&
627 test $HEAD = $(git rev-parse HEAD) &&
628 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
629 test 123456789 = $MTIME
632 test_expect_success 'reword' '
633 git checkout -b reword-branch master &&
634 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
635 git show HEAD | grep "E changed" &&
636 test $(git rev-parse master) != $(git rev-parse HEAD) &&
637 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
638 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
639 git show HEAD^ | grep "D changed" &&
640 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
641 git show HEAD~3 | grep "B changed" &&
642 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
643 git show HEAD~2 | grep "C changed"
646 test_expect_success 'rebase -i can copy notes' '
647 git config notes.rewrite.rebase true &&
648 git config notes.rewriteRef "refs/notes/*" &&
652 git notes add -m"a note" n3 &&
653 git rebase --onto n1 n2 &&
654 test "a note" = "$(git notes show HEAD)"
662 test_expect_success 'rebase -i can copy notes over a fixup' '
663 git reset --hard n3 &&
664 git notes add -m"an earlier note" n2 &&
665 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
666 git notes show > output &&
667 test_cmp expect output
670 test_expect_success 'rebase while detaching HEAD' '
671 git symbolic-ref HEAD &&
672 grandparent=$(git rev-parse HEAD~2) &&
674 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
675 test $grandparent = $(git rev-parse HEAD~2) &&
676 test_must_fail git symbolic-ref HEAD
679 test_tick # Ensure that the rebased commits get a different timestamp.
680 test_expect_success 'always cherry-pick with --no-ff' '
681 git checkout no-ff-branch &&
682 git tag original-no-ff-branch &&
683 git rebase -i --no-ff A &&
687 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
688 git diff HEAD~$p original-no-ff-branch~$p > out &&
691 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
692 git diff HEAD~3 original-no-ff-branch~3 > out &&
696 test_expect_success 'set up commits with funny messages' '
697 git checkout -b funny A &&
700 git commit -a -m "end with slash\\" &&
703 git commit -a -m "something (\000) that looks like octal" &&
706 git commit -a -m "something (\n) that looks like a newline" &&
709 git commit -a -m "another commit"
712 test_expect_success 'rebase-i history with funny messages' '
713 git rev-list A..funny >expect &&
715 FAKE_LINES="1 2 3 4" git rebase -i A &&
716 git rev-list A.. >actual &&
717 test_cmp expect actual