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 &&
49 test_commit I file6 &&
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
73 test_expect_success 'rebase -i with the exec command' '
74 git checkout master &&
76 FAKE_LINES="1 exec_>touch-one
77 2 exec_>touch-two exec_false exec_>touch-three
78 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
80 test_must_fail git rebase -i A
82 test_path_is_file touch-one &&
83 test_path_is_file touch-two &&
84 test_path_is_missing touch-three " (should have stopped before)" &&
85 test $(git rev-parse C) = $(git rev-parse HEAD) || {
86 echo "Stopped at wrong revision:"
87 echo "($(git describe --tags HEAD) instead of C)"
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 $(git rev-parse master) = $(git rev-parse HEAD) || {
95 echo "Stopped at wrong revision:"
96 echo "($(git describe --tags HEAD) instead of master)"
102 test_expect_success 'rebase -i with the exec command runs from tree root' '
103 git checkout master &&
104 mkdir subdir && (cd subdir &&
105 FAKE_LINES="1 exec_>touch-subdir" \
108 test_path_is_file touch-subdir &&
112 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
113 git checkout master &&
115 FAKE_LINES="exec_echo_foo_>file1 1" &&
117 test_must_fail git rebase -i HEAD^
119 test $(git rev-parse master^) = $(git rev-parse HEAD) || {
120 echo "Stopped at wrong revision:"
121 echo "($(git describe --tags HEAD) instead of master^)"
125 git rebase --continue
128 test_expect_success 'no changes are a nop' '
129 git checkout branch2 &&
131 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
132 test $(git rev-parse I) = $(git rev-parse HEAD)
135 test_expect_success 'test the [branch] option' '
136 git checkout -b dead-end &&
138 git commit -m "stop here" &&
139 git rebase -i F branch2 &&
140 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
141 test $(git rev-parse I) = $(git rev-parse branch2) &&
142 test $(git rev-parse I) = $(git rev-parse HEAD)
145 test_expect_success 'test --onto <branch>' '
146 git checkout -b test-onto branch2 &&
147 git rebase -i --onto branch1 F &&
148 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
149 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
150 test $(git rev-parse I) = $(git rev-parse branch2)
153 test_expect_success 'rebase on top of a non-conflicting commit' '
154 git checkout branch1 &&
155 git tag original-branch1 &&
156 git rebase -i branch2 &&
157 test file6 = $(git diff --name-only original-branch1) &&
158 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
159 test $(git rev-parse I) = $(git rev-parse branch2) &&
160 test $(git rev-parse I) = $(git rev-parse HEAD~2)
163 test_expect_success 'reflog for the branch shows state before rebase' '
164 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
167 test_expect_success 'exchange two commits' '
168 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
169 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
170 test G = $(git cat-file commit HEAD | sed -ne \$p)
174 diff --git a/file1 b/file1
175 index f70f10e..fd79235 100644
191 test_expect_success 'stop on conflicting pick' '
192 git tag new-branch1 &&
193 test_must_fail git rebase -i master &&
194 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
195 test_cmp expect .git/rebase-merge/patch &&
196 test_cmp expect2 file1 &&
197 test "$(git diff --name-status |
198 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
199 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
200 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
203 test_expect_success 'abort' '
204 git rebase --abort &&
205 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
206 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
207 test_path_is_missing .git/rebase-merge
210 test_expect_success 'abort with error when new base cannot be checked out' '
211 git rm --cached file1 &&
212 git commit -m "remove file in base" &&
213 test_must_fail git rebase -i master > output 2>&1 &&
214 grep "The following untracked working tree files would be overwritten by checkout:" \
216 grep "file1" output &&
217 test_path_is_missing .git/rebase-merge &&
218 git reset --hard HEAD^
221 test_expect_success 'retain authorship' '
225 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
227 git rebase -i --onto master HEAD^ &&
228 git show HEAD | grep "^Author: Twerp Snog"
231 test_expect_success 'squash' '
232 git reset --hard twerp &&
235 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
236 echo "******************************" &&
237 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
238 git rebase -i --onto master HEAD~2 &&
239 test B = $(cat file7) &&
240 test $(git rev-parse HEAD^) = $(git rev-parse master)
243 test_expect_success 'retain authorship when squashing' '
244 git show HEAD | grep "^Author: Twerp Snog"
247 test_expect_success '-p handles "no changes" gracefully' '
248 HEAD=$(git rev-parse HEAD) &&
249 git rebase -i -p HEAD^ &&
250 git update-index --refresh &&
251 git diff-files --quiet &&
252 git diff-index --quiet --cached HEAD -- &&
253 test $HEAD = $(git rev-parse HEAD)
256 test_expect_failure 'exchange two commits with -p' '
257 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
258 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
259 test G = $(git cat-file commit HEAD | sed -ne \$p)
262 test_expect_success 'preserve merges with -p' '
263 git checkout -b to-be-preserved master^ &&
264 : > unrelated-file &&
265 git add unrelated-file &&
267 git commit -m "unrelated" &&
268 git checkout -b another-branch master &&
271 git commit -m J file1 &&
273 git merge to-be-preserved &&
276 git commit -m K file1 &&
279 git commit -m L1 file1 &&
280 git checkout HEAD^ &&
281 echo 1 > unrelated-file &&
283 git commit -m L2 unrelated-file &&
285 git merge another-branch &&
288 git commit -m M file1 &&
289 git checkout -b to-be-rebased &&
291 git rebase -i -p --onto branch1 master &&
292 git update-index --refresh &&
293 git diff-files --quiet &&
294 git diff-index --quiet --cached HEAD -- &&
295 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
296 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
297 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
298 test $(git show HEAD~5:file1) = B &&
299 test $(git show HEAD~3:file1) = C &&
300 test $(git show HEAD:file1) = E &&
301 test $(git show HEAD:unrelated-file) = 1
304 test_expect_success 'edit ancestor with -p' '
305 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
306 echo 2 > unrelated-file &&
308 git commit -m L2-modified --amend unrelated-file &&
309 git rebase --continue &&
310 git update-index --refresh &&
311 git diff-files --quiet &&
312 git diff-index --quiet --cached HEAD -- &&
313 test $(git show HEAD:unrelated-file) = 2
316 test_expect_success '--continue tries to commit' '
318 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
319 echo resolved > file1 &&
321 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
322 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
323 git show HEAD | grep chouette
326 test_expect_success 'verbose flag is heeded, even after --continue' '
327 git reset --hard HEAD@{1} &&
329 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
330 echo resolved > file1 &&
332 git rebase --continue > output &&
333 grep "^ file1 | 2 +-$" output
336 test_expect_success 'multi-squash only fires up editor once' '
337 base=$(git rev-parse HEAD~4) &&
338 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
339 EXPECT_HEADER_COUNT=4 \
340 git rebase -i $base &&
341 test $base = $(git rev-parse HEAD^) &&
342 test 1 = $(git show | grep ONCE | wc -l)
345 test_expect_success 'multi-fixup does not fire up editor' '
346 git checkout -b multi-fixup E &&
347 base=$(git rev-parse HEAD~4) &&
348 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
349 git rebase -i $base &&
350 test $base = $(git rev-parse HEAD^) &&
351 test 0 = $(git show | grep NEVER | wc -l) &&
352 git checkout to-be-rebased &&
353 git branch -D multi-fixup
356 test_expect_success 'commit message used after conflict' '
357 git checkout -b conflict-fixup conflict-branch &&
358 base=$(git rev-parse HEAD~4) &&
360 FAKE_LINES="1 fixup 3 fixup 4" &&
362 test_must_fail git rebase -i $base
364 echo three > conflict &&
366 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
367 git rebase --continue &&
368 test $base = $(git rev-parse HEAD^) &&
369 test 1 = $(git show | grep ONCE | wc -l) &&
370 git checkout to-be-rebased &&
371 git branch -D conflict-fixup
374 test_expect_success 'commit message retained after conflict' '
375 git checkout -b conflict-squash conflict-branch &&
376 base=$(git rev-parse HEAD~4) &&
378 FAKE_LINES="1 fixup 3 squash 4" &&
380 test_must_fail git rebase -i $base
382 echo three > conflict &&
384 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
385 git rebase --continue &&
386 test $base = $(git rev-parse HEAD^) &&
387 test 2 = $(git show | grep TWICE | wc -l) &&
388 git checkout to-be-rebased &&
389 git branch -D conflict-squash
392 cat > expect-squash-fixup << EOF
400 test_expect_success 'squash and fixup generate correct log messages' '
401 git checkout -b squash-fixup E &&
402 base=$(git rev-parse HEAD~4) &&
403 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
404 EXPECT_HEADER_COUNT=4 \
405 git rebase -i $base &&
406 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
407 test_cmp expect-squash-fixup actual-squash-fixup &&
408 git checkout to-be-rebased &&
409 git branch -D squash-fixup
412 test_expect_success 'squash ignores comments' '
413 git checkout -b skip-comments E &&
414 base=$(git rev-parse HEAD~4) &&
415 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
416 EXPECT_HEADER_COUNT=4 \
417 git rebase -i $base &&
418 test $base = $(git rev-parse HEAD^) &&
419 test 1 = $(git show | grep ONCE | wc -l) &&
420 git checkout to-be-rebased &&
421 git branch -D skip-comments
424 test_expect_success 'squash ignores blank lines' '
425 git checkout -b skip-blank-lines E &&
426 base=$(git rev-parse HEAD~4) &&
427 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
428 EXPECT_HEADER_COUNT=4 \
429 git rebase -i $base &&
430 test $base = $(git rev-parse HEAD^) &&
431 test 1 = $(git show | grep ONCE | wc -l) &&
432 git checkout to-be-rebased &&
433 git branch -D skip-blank-lines
436 test_expect_success 'squash works as expected' '
437 git checkout -b squash-works no-conflict-branch &&
438 one=$(git rev-parse HEAD~3) &&
439 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
440 git rebase -i HEAD~3 &&
441 test $one = $(git rev-parse HEAD~2)
444 test_expect_success 'interrupted squash works as expected' '
445 git checkout -b interrupted-squash conflict-branch &&
446 one=$(git rev-parse HEAD~3) &&
448 FAKE_LINES="1 squash 3 2" &&
450 test_must_fail git rebase -i HEAD~3
452 (echo one; echo two; echo four) > conflict &&
454 test_must_fail git rebase --continue &&
455 echo resolved > conflict &&
457 git rebase --continue &&
458 test $one = $(git rev-parse HEAD~2)
461 test_expect_success 'interrupted squash works as expected (case 2)' '
462 git checkout -b interrupted-squash2 conflict-branch &&
463 one=$(git rev-parse HEAD~3) &&
465 FAKE_LINES="3 squash 1 2" &&
467 test_must_fail git rebase -i HEAD~3
469 (echo one; echo four) > conflict &&
471 test_must_fail git rebase --continue &&
472 (echo one; echo two; echo four) > conflict &&
474 test_must_fail git rebase --continue &&
475 echo resolved > conflict &&
477 git rebase --continue &&
478 test $one = $(git rev-parse HEAD~2)
481 test_expect_success 'ignore patch if in upstream' '
482 HEAD=$(git rev-parse HEAD) &&
483 git checkout -b has-cherry-picked HEAD^ &&
484 echo unrelated > file7 &&
487 git commit -m "unrelated change" &&
488 git cherry-pick $HEAD &&
489 EXPECT_COUNT=1 git rebase -i $HEAD &&
490 test $HEAD = $(git rev-parse HEAD^)
493 test_expect_success '--continue tries to commit, even for "edit"' '
494 parent=$(git rev-parse HEAD^) &&
496 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
497 echo edited > file7 &&
499 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
500 test edited = $(git show HEAD:file7) &&
501 git show HEAD | grep chouette &&
502 test $parent = $(git rev-parse HEAD^)
505 test_expect_success 'aborted --continue does not squash commits after "edit"' '
506 old=$(git rev-parse HEAD) &&
508 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
509 echo "edited again" > file7 &&
512 FAKE_COMMIT_MESSAGE=" " &&
513 export FAKE_COMMIT_MESSAGE &&
514 test_must_fail git rebase --continue
516 test $old = $(git rev-parse HEAD) &&
520 test_expect_success 'auto-amend only edited commits after "edit"' '
522 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
523 echo "edited again" > file7 &&
525 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
526 echo "and again" > file7 &&
530 FAKE_COMMIT_MESSAGE="and again" &&
531 export FAKE_COMMIT_MESSAGE &&
532 test_must_fail git rebase --continue
537 test_expect_success 'rebase a detached HEAD' '
538 grandparent=$(git rev-parse HEAD~2) &&
539 git checkout $(git rev-parse HEAD) &&
541 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
542 test $grandparent = $(git rev-parse HEAD~2)
545 test_expect_success 'rebase a commit violating pre-commit' '
547 mkdir -p .git/hooks &&
548 PRE_COMMIT=.git/hooks/pre-commit &&
549 echo "#!/bin/sh" > $PRE_COMMIT &&
550 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
551 chmod a+x $PRE_COMMIT &&
552 echo "monde! " >> file1 &&
554 test_must_fail git commit -m doesnt-verify file1 &&
555 git commit -m doesnt-verify --no-verify file1 &&
557 FAKE_LINES=2 git rebase -i HEAD~2
561 test_expect_success 'rebase with a file named HEAD in worktree' '
565 git checkout -b branch3 A &&
568 GIT_AUTHOR_NAME="Squashed Away" &&
569 export GIT_AUTHOR_NAME &&
572 git commit -m "Add head" &&
575 git commit -m "Add body"
578 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
579 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
583 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
585 git checkout -b branch4 HEAD &&
586 GIT_EDITOR=: git commit --amend \
587 --author="Somebody else <somebody@else.com>" &&
588 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
589 git rebase -i branch3 &&
590 test $(git rev-parse branch3) = $(git rev-parse branch4)
594 test_expect_success 'submodule rebase setup' '
598 cd sub && git init && >elif &&
599 git add elif && git commit -m "submodule initial"
604 git commit -m "One" &&
607 git commit -a -m "Two" &&
609 cd sub && echo 3 >elif &&
610 git commit -a -m "submodule second"
613 git commit -a -m "Three changes submodule"
616 test_expect_success 'submodule rebase -i' '
617 FAKE_LINES="1 squash 2 3" git rebase -i A
620 test_expect_success 'avoid unnecessary reset' '
621 git checkout master &&
622 test-chmtime =123456789 file3 &&
623 git update-index --refresh &&
624 HEAD=$(git rev-parse HEAD) &&
625 git rebase -i HEAD~4 &&
626 test $HEAD = $(git rev-parse HEAD) &&
627 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
628 test 123456789 = $MTIME
631 test_expect_success 'reword' '
632 git checkout -b reword-branch master &&
633 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
634 git show HEAD | grep "E changed" &&
635 test $(git rev-parse master) != $(git rev-parse HEAD) &&
636 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
637 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
638 git show HEAD^ | grep "D changed" &&
639 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
640 git show HEAD~3 | grep "B changed" &&
641 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
642 git show HEAD~2 | grep "C changed"
645 test_expect_success 'rebase -i can copy notes' '
646 git config notes.rewrite.rebase true &&
647 git config notes.rewriteRef "refs/notes/*" &&
651 git notes add -m"a note" n3 &&
652 git rebase --onto n1 n2 &&
653 test "a note" = "$(git notes show HEAD)"
661 test_expect_success 'rebase -i can copy notes over a fixup' '
662 git reset --hard n3 &&
663 git notes add -m"an earlier note" n2 &&
664 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
665 git notes show > output &&
666 test_cmp expect output
669 test_expect_success 'rebase while detaching HEAD' '
670 git symbolic-ref HEAD &&
671 grandparent=$(git rev-parse HEAD~2) &&
673 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
674 test $grandparent = $(git rev-parse HEAD~2) &&
675 test_must_fail git symbolic-ref HEAD
678 test_tick # Ensure that the rebased commits get a different timestamp.
679 test_expect_success 'always cherry-pick with --no-ff' '
680 git checkout no-ff-branch &&
681 git tag original-no-ff-branch &&
682 git rebase -i --no-ff A &&
686 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
687 git diff HEAD~$p original-no-ff-branch~$p > out &&
690 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
691 git diff HEAD~3 original-no-ff-branch~3 > out &&
695 test_expect_success 'set up commits with funny messages' '
696 git checkout -b funny A &&
699 git commit -a -m "end with slash\\" &&
702 git commit -a -m "something (\000) that looks like octal" &&
705 git commit -a -m "something (\n) that looks like a newline" &&
708 git commit -a -m "another commit"
711 test_expect_success 'rebase-i history with funny messages' '
712 git rev-list A..funny >expect &&
714 FAKE_LINES="1 2 3 4" git rebase -i A &&
715 git rev-list A.. >actual &&
716 test_cmp expect actual