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
77 test_expect_success 'rebase -i with the exec command' '
78 git checkout master &&
80 FAKE_LINES="1 exec_>touch-one
81 2 exec_>touch-two exec_false exec_>touch-three
82 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
84 test_must_fail git rebase -i A
86 test_path_is_file touch-one &&
87 test_path_is_file touch-two &&
88 test_path_is_missing touch-three " (should have stopped before)" &&
89 test_cmp_rev C HEAD &&
90 git rebase --continue &&
91 test_path_is_file touch-three &&
92 test_path_is_file "touch-file name with spaces" &&
93 test_path_is_file touch-after-semicolon &&
94 test_cmp_rev master HEAD &&
98 test_expect_success 'rebase -i with the exec command runs from tree root' '
99 git checkout master &&
100 mkdir subdir && (cd subdir &&
101 FAKE_LINES="1 exec_>touch-subdir" \
104 test_path_is_file touch-subdir &&
108 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
109 git checkout master &&
111 FAKE_LINES="exec_echo_foo_>file1 1" &&
113 test_must_fail git rebase -i HEAD^
115 test_cmp_rev master^ HEAD &&
117 git rebase --continue
120 test_expect_success 'no changes are a nop' '
121 git checkout branch2 &&
123 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
124 test $(git rev-parse I) = $(git rev-parse HEAD)
127 test_expect_success 'test the [branch] option' '
128 git checkout -b dead-end &&
130 git commit -m "stop here" &&
131 git rebase -i F branch2 &&
132 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
133 test $(git rev-parse I) = $(git rev-parse branch2) &&
134 test $(git rev-parse I) = $(git rev-parse HEAD)
137 test_expect_success 'test --onto <branch>' '
138 git checkout -b test-onto branch2 &&
139 git rebase -i --onto branch1 F &&
140 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
141 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
142 test $(git rev-parse I) = $(git rev-parse branch2)
145 test_expect_success 'rebase on top of a non-conflicting commit' '
146 git checkout branch1 &&
147 git tag original-branch1 &&
148 git rebase -i branch2 &&
149 test file6 = $(git diff --name-only original-branch1) &&
150 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
151 test $(git rev-parse I) = $(git rev-parse branch2) &&
152 test $(git rev-parse I) = $(git rev-parse HEAD~2)
155 test_expect_success 'reflog for the branch shows state before rebase' '
156 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
159 test_expect_success 'exchange two commits' '
160 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
161 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
162 test G = $(git cat-file commit HEAD | sed -ne \$p)
166 diff --git a/file1 b/file1
167 index f70f10e..fd79235 100644
183 test_expect_success 'stop on conflicting pick' '
184 git tag new-branch1 &&
185 test_must_fail git rebase -i master &&
186 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
187 test_cmp expect .git/rebase-merge/patch &&
188 test_cmp expect2 file1 &&
189 test "$(git diff --name-status |
190 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
191 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
192 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
195 test_expect_success 'abort' '
196 git rebase --abort &&
197 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
198 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
199 test_path_is_missing .git/rebase-merge
202 test_expect_success 'abort with error when new base cannot be checked out' '
203 git rm --cached file1 &&
204 git commit -m "remove file in base" &&
205 test_must_fail git rebase -i master > output 2>&1 &&
206 grep "The following untracked working tree files would be overwritten by checkout:" \
208 grep "file1" output &&
209 test_path_is_missing .git/rebase-merge &&
210 git reset --hard HEAD^
213 test_expect_success 'retain authorship' '
217 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
219 git rebase -i --onto master HEAD^ &&
220 git show HEAD | grep "^Author: Twerp Snog"
223 test_expect_success 'squash' '
224 git reset --hard twerp &&
227 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
228 echo "******************************" &&
229 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
230 git rebase -i --onto master HEAD~2 &&
231 test B = $(cat file7) &&
232 test $(git rev-parse HEAD^) = $(git rev-parse master)
235 test_expect_success 'retain authorship when squashing' '
236 git show HEAD | grep "^Author: Twerp Snog"
239 test_expect_success '-p handles "no changes" gracefully' '
240 HEAD=$(git rev-parse HEAD) &&
241 git rebase -i -p HEAD^ &&
242 git update-index --refresh &&
243 git diff-files --quiet &&
244 git diff-index --quiet --cached HEAD -- &&
245 test $HEAD = $(git rev-parse HEAD)
248 test_expect_failure 'exchange two commits with -p' '
249 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
250 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
251 test G = $(git cat-file commit HEAD | sed -ne \$p)
254 test_expect_success 'preserve merges with -p' '
255 git checkout -b to-be-preserved master^ &&
256 : > unrelated-file &&
257 git add unrelated-file &&
259 git commit -m "unrelated" &&
260 git checkout -b another-branch master &&
263 git commit -m J file1 &&
265 git merge to-be-preserved &&
268 git commit -m K file1 &&
271 git commit -m L1 file1 &&
272 git checkout HEAD^ &&
273 echo 1 > unrelated-file &&
275 git commit -m L2 unrelated-file &&
277 git merge another-branch &&
280 git commit -m M file1 &&
281 git checkout -b to-be-rebased &&
283 git rebase -i -p --onto branch1 master &&
284 git update-index --refresh &&
285 git diff-files --quiet &&
286 git diff-index --quiet --cached HEAD -- &&
287 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
288 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
289 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
290 test $(git show HEAD~5:file1) = B &&
291 test $(git show HEAD~3:file1) = C &&
292 test $(git show HEAD:file1) = E &&
293 test $(git show HEAD:unrelated-file) = 1
296 test_expect_success 'edit ancestor with -p' '
297 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
298 echo 2 > unrelated-file &&
300 git commit -m L2-modified --amend unrelated-file &&
301 git rebase --continue &&
302 git update-index --refresh &&
303 git diff-files --quiet &&
304 git diff-index --quiet --cached HEAD -- &&
305 test $(git show HEAD:unrelated-file) = 2
308 test_expect_success '--continue tries to commit' '
310 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
311 echo resolved > file1 &&
313 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
314 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
315 git show HEAD | grep chouette
318 test_expect_success 'verbose flag is heeded, even after --continue' '
319 git reset --hard HEAD@{1} &&
321 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
322 echo resolved > file1 &&
324 git rebase --continue > output &&
325 grep "^ file1 | 2 +-$" output
328 test_expect_success 'multi-squash only fires up editor once' '
329 base=$(git rev-parse HEAD~4) &&
330 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
331 EXPECT_HEADER_COUNT=4 \
332 git rebase -i $base &&
333 test $base = $(git rev-parse HEAD^) &&
334 test 1 = $(git show | grep ONCE | wc -l)
337 test_expect_success 'multi-fixup does not fire up editor' '
338 git checkout -b multi-fixup E &&
339 base=$(git rev-parse HEAD~4) &&
340 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
341 git rebase -i $base &&
342 test $base = $(git rev-parse HEAD^) &&
343 test 0 = $(git show | grep NEVER | wc -l) &&
344 git checkout to-be-rebased &&
345 git branch -D multi-fixup
348 test_expect_success 'commit message used after conflict' '
349 git checkout -b conflict-fixup conflict-branch &&
350 base=$(git rev-parse HEAD~4) &&
352 FAKE_LINES="1 fixup 3 fixup 4" &&
354 test_must_fail git rebase -i $base
356 echo three > conflict &&
358 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
359 git rebase --continue &&
360 test $base = $(git rev-parse HEAD^) &&
361 test 1 = $(git show | grep ONCE | wc -l) &&
362 git checkout to-be-rebased &&
363 git branch -D conflict-fixup
366 test_expect_success 'commit message retained after conflict' '
367 git checkout -b conflict-squash conflict-branch &&
368 base=$(git rev-parse HEAD~4) &&
370 FAKE_LINES="1 fixup 3 squash 4" &&
372 test_must_fail git rebase -i $base
374 echo three > conflict &&
376 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
377 git rebase --continue &&
378 test $base = $(git rev-parse HEAD^) &&
379 test 2 = $(git show | grep TWICE | wc -l) &&
380 git checkout to-be-rebased &&
381 git branch -D conflict-squash
384 cat > expect-squash-fixup << EOF
392 test_expect_success 'squash and fixup generate correct log messages' '
393 git checkout -b squash-fixup E &&
394 base=$(git rev-parse HEAD~4) &&
395 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
396 EXPECT_HEADER_COUNT=4 \
397 git rebase -i $base &&
398 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
399 test_cmp expect-squash-fixup actual-squash-fixup &&
400 git checkout to-be-rebased &&
401 git branch -D squash-fixup
404 test_expect_success 'squash ignores comments' '
405 git checkout -b skip-comments E &&
406 base=$(git rev-parse HEAD~4) &&
407 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
408 EXPECT_HEADER_COUNT=4 \
409 git rebase -i $base &&
410 test $base = $(git rev-parse HEAD^) &&
411 test 1 = $(git show | grep ONCE | wc -l) &&
412 git checkout to-be-rebased &&
413 git branch -D skip-comments
416 test_expect_success 'squash ignores blank lines' '
417 git checkout -b skip-blank-lines E &&
418 base=$(git rev-parse HEAD~4) &&
419 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
420 EXPECT_HEADER_COUNT=4 \
421 git rebase -i $base &&
422 test $base = $(git rev-parse HEAD^) &&
423 test 1 = $(git show | grep ONCE | wc -l) &&
424 git checkout to-be-rebased &&
425 git branch -D skip-blank-lines
428 test_expect_success 'squash works as expected' '
429 git checkout -b squash-works no-conflict-branch &&
430 one=$(git rev-parse HEAD~3) &&
431 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
432 git rebase -i HEAD~3 &&
433 test $one = $(git rev-parse HEAD~2)
436 test_expect_success 'interrupted squash works as expected' '
437 git checkout -b interrupted-squash conflict-branch &&
438 one=$(git rev-parse HEAD~3) &&
440 FAKE_LINES="1 squash 3 2" &&
442 test_must_fail git rebase -i HEAD~3
444 (echo one; echo two; echo four) > conflict &&
446 test_must_fail git rebase --continue &&
447 echo resolved > conflict &&
449 git rebase --continue &&
450 test $one = $(git rev-parse HEAD~2)
453 test_expect_success 'interrupted squash works as expected (case 2)' '
454 git checkout -b interrupted-squash2 conflict-branch &&
455 one=$(git rev-parse HEAD~3) &&
457 FAKE_LINES="3 squash 1 2" &&
459 test_must_fail git rebase -i HEAD~3
461 (echo one; echo four) > conflict &&
463 test_must_fail git rebase --continue &&
464 (echo one; echo two; echo four) > conflict &&
466 test_must_fail git rebase --continue &&
467 echo resolved > conflict &&
469 git rebase --continue &&
470 test $one = $(git rev-parse HEAD~2)
473 test_expect_success 'ignore patch if in upstream' '
474 HEAD=$(git rev-parse HEAD) &&
475 git checkout -b has-cherry-picked HEAD^ &&
476 echo unrelated > file7 &&
479 git commit -m "unrelated change" &&
480 git cherry-pick $HEAD &&
481 EXPECT_COUNT=1 git rebase -i $HEAD &&
482 test $HEAD = $(git rev-parse HEAD^)
485 test_expect_success '--continue tries to commit, even for "edit"' '
486 parent=$(git rev-parse HEAD^) &&
488 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
489 echo edited > file7 &&
491 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
492 test edited = $(git show HEAD:file7) &&
493 git show HEAD | grep chouette &&
494 test $parent = $(git rev-parse HEAD^)
497 test_expect_success 'aborted --continue does not squash commits after "edit"' '
498 old=$(git rev-parse HEAD) &&
500 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
501 echo "edited again" > file7 &&
504 FAKE_COMMIT_MESSAGE=" " &&
505 export FAKE_COMMIT_MESSAGE &&
506 test_must_fail git rebase --continue
508 test $old = $(git rev-parse HEAD) &&
512 test_expect_success 'auto-amend only edited commits after "edit"' '
514 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
515 echo "edited again" > file7 &&
517 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
518 echo "and again" > file7 &&
522 FAKE_COMMIT_MESSAGE="and again" &&
523 export FAKE_COMMIT_MESSAGE &&
524 test_must_fail git rebase --continue
529 test_expect_success 'rebase a detached HEAD' '
530 grandparent=$(git rev-parse HEAD~2) &&
531 git checkout $(git rev-parse HEAD) &&
533 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
534 test $grandparent = $(git rev-parse HEAD~2)
537 test_expect_success 'rebase a commit violating pre-commit' '
539 mkdir -p .git/hooks &&
540 PRE_COMMIT=.git/hooks/pre-commit &&
541 echo "#!/bin/sh" > $PRE_COMMIT &&
542 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
543 chmod a+x $PRE_COMMIT &&
544 echo "monde! " >> file1 &&
546 test_must_fail git commit -m doesnt-verify file1 &&
547 git commit -m doesnt-verify --no-verify file1 &&
549 FAKE_LINES=2 git rebase -i HEAD~2
553 test_expect_success 'rebase with a file named HEAD in worktree' '
557 git checkout -b branch3 A &&
560 GIT_AUTHOR_NAME="Squashed Away" &&
561 export GIT_AUTHOR_NAME &&
564 git commit -m "Add head" &&
567 git commit -m "Add body"
570 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
571 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
575 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
577 git checkout -b branch4 HEAD &&
578 GIT_EDITOR=: git commit --amend \
579 --author="Somebody else <somebody@else.com>" &&
580 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
581 git rebase -i branch3 &&
582 test $(git rev-parse branch3) = $(git rev-parse branch4)
586 test_expect_success 'submodule rebase setup' '
590 cd sub && git init && >elif &&
591 git add elif && git commit -m "submodule initial"
596 git commit -m "One" &&
599 git commit -a -m "Two" &&
601 cd sub && echo 3 >elif &&
602 git commit -a -m "submodule second"
605 git commit -a -m "Three changes submodule"
608 test_expect_success 'submodule rebase -i' '
609 FAKE_LINES="1 squash 2 3" git rebase -i A
612 test_expect_success 'avoid unnecessary reset' '
613 git checkout master &&
614 test-chmtime =123456789 file3 &&
615 git update-index --refresh &&
616 HEAD=$(git rev-parse HEAD) &&
617 git rebase -i HEAD~4 &&
618 test $HEAD = $(git rev-parse HEAD) &&
619 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
620 test 123456789 = $MTIME
623 test_expect_success 'reword' '
624 git checkout -b reword-branch master &&
625 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
626 git show HEAD | grep "E changed" &&
627 test $(git rev-parse master) != $(git rev-parse HEAD) &&
628 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
629 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
630 git show HEAD^ | grep "D changed" &&
631 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
632 git show HEAD~3 | grep "B changed" &&
633 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
634 git show HEAD~2 | grep "C changed"
637 test_expect_success 'rebase -i can copy notes' '
638 git config notes.rewrite.rebase true &&
639 git config notes.rewriteRef "refs/notes/*" &&
643 git notes add -m"a note" n3 &&
644 git rebase --onto n1 n2 &&
645 test "a note" = "$(git notes show HEAD)"
653 test_expect_success 'rebase -i can copy notes over a fixup' '
654 git reset --hard n3 &&
655 git notes add -m"an earlier note" n2 &&
656 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
657 git notes show > output &&
658 test_cmp expect output
661 test_expect_success 'rebase while detaching HEAD' '
662 git symbolic-ref HEAD &&
663 grandparent=$(git rev-parse HEAD~2) &&
665 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
666 test $grandparent = $(git rev-parse HEAD~2) &&
667 test_must_fail git symbolic-ref HEAD
670 test_tick # Ensure that the rebased commits get a different timestamp.
671 test_expect_success 'always cherry-pick with --no-ff' '
672 git checkout no-ff-branch &&
673 git tag original-no-ff-branch &&
674 git rebase -i --no-ff A &&
678 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
679 git diff HEAD~$p original-no-ff-branch~$p > out &&
682 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
683 git diff HEAD~3 original-no-ff-branch~3 > out &&
687 test_expect_success 'set up commits with funny messages' '
688 git checkout -b funny A &&
691 git commit -a -m "end with slash\\" &&
694 git commit -a -m "something (\000) that looks like octal" &&
697 git commit -a -m "something (\n) that looks like a newline" &&
700 git commit -a -m "another commit"
703 test_expect_success 'rebase-i history with funny messages' '
704 git rev-list A..funny >expect &&
706 FAKE_LINES="1 2 3 4" git rebase -i A &&
707 git rev-list A.. >actual &&
708 test_cmp expect actual