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
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.
37 test_expect_success
'setup' '
38 test_commit A file1 &&
39 test_commit B file1 &&
40 test_commit C file2 &&
41 test_commit D file1 &&
42 test_commit E file3 &&
43 git checkout -b branch1 A &&
44 test_commit F file4 &&
45 test_commit G file1 &&
46 test_commit H file5 &&
47 git checkout -b branch2 F &&
48 test_commit I file6 &&
49 git checkout -b conflict-branch A &&
50 test_commit one conflict &&
51 test_commit two conflict &&
52 test_commit three conflict &&
53 test_commit four conflict &&
54 git checkout -b no-conflict-branch A &&
55 test_commit J fileJ &&
56 test_commit K fileK &&
57 test_commit L fileL &&
58 test_commit M fileM &&
59 git checkout -b no-ff-branch A &&
60 test_commit N fileN &&
61 test_commit O fileO &&
65 # "exec" commands are ran with the user shell by default, but this may
66 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
67 # to create a file. Unseting SHELL avoids such non-portable behavior
71 test_expect_success
'rebase -i with the exec command' '
72 git checkout master &&
74 FAKE_LINES="1 exec_>touch-one
75 2 exec_>touch-two exec_false exec_>touch-three
76 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
78 test_must_fail git rebase -i A
80 test_path_is_file touch-one &&
81 test_path_is_file touch-two &&
82 test_path_is_missing touch-three " (should have stopped before)" &&
83 test $(git rev-parse C) = $(git rev-parse HEAD) || {
84 echo "Stopped at wrong revision:"
85 echo "($(git describe --tags HEAD) instead of C)"
88 git rebase --continue &&
89 test_path_is_file touch-three &&
90 test_path_is_file "touch-file name with spaces" &&
91 test_path_is_file touch-after-semicolon &&
92 test $(git rev-parse master) = $(git rev-parse HEAD) || {
93 echo "Stopped at wrong revision:"
94 echo "($(git describe --tags HEAD) instead of master)"
100 test_expect_success
'rebase -i with the exec command runs from tree root' '
101 git checkout master &&
102 mkdir subdir && (cd subdir &&
103 FAKE_LINES="1 exec_>touch-subdir" \
106 test_path_is_file touch-subdir &&
110 test_expect_success
'rebase -i with the exec command checks tree cleanness' '
111 git checkout master &&
113 FAKE_LINES="exec_echo_foo_>file1 1" &&
115 test_must_fail git rebase -i HEAD^
117 test $(git rev-parse master^) = $(git rev-parse HEAD) || {
118 echo "Stopped at wrong revision:"
119 echo "($(git describe --tags HEAD) instead of master^)"
123 git rebase --continue
126 test_expect_success
'no changes are a nop' '
127 git checkout branch2 &&
129 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
130 test $(git rev-parse I) = $(git rev-parse HEAD)
133 test_expect_success
'test the [branch] option' '
134 git checkout -b dead-end &&
136 git commit -m "stop here" &&
137 git rebase -i F branch2 &&
138 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
139 test $(git rev-parse I) = $(git rev-parse branch2) &&
140 test $(git rev-parse I) = $(git rev-parse HEAD)
143 test_expect_success
'test --onto <branch>' '
144 git checkout -b test-onto branch2 &&
145 git rebase -i --onto branch1 F &&
146 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
147 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
148 test $(git rev-parse I) = $(git rev-parse branch2)
151 test_expect_success
'rebase on top of a non-conflicting commit' '
152 git checkout branch1 &&
153 git tag original-branch1 &&
154 git rebase -i branch2 &&
155 test file6 = $(git diff --name-only original-branch1) &&
156 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
157 test $(git rev-parse I) = $(git rev-parse branch2) &&
158 test $(git rev-parse I) = $(git rev-parse HEAD~2)
161 test_expect_success
'reflog for the branch shows state before rebase' '
162 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
165 test_expect_success
'exchange two commits' '
166 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
167 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
168 test G = $(git cat-file commit HEAD | sed -ne \$p)
172 diff --git a/file1 b/file1
173 index f70f10e..fd79235 100644
189 test_expect_success
'stop on conflicting pick' '
190 git tag new-branch1 &&
191 test_must_fail git rebase -i master &&
192 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
193 test_cmp expect .git/rebase-merge/patch &&
194 test_cmp expect2 file1 &&
195 test "$(git diff --name-status |
196 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
197 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
198 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
201 test_expect_success
'abort' '
202 git rebase --abort &&
203 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
204 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
205 test_path_is_missing .git/rebase-merge
208 test_expect_success
'abort with error when new base cannot be checked out' '
209 git rm --cached file1 &&
210 git commit -m "remove file in base" &&
211 test_must_fail git rebase -i master > output 2>&1 &&
212 grep "The following untracked working tree files would be overwritten by checkout:" \
214 grep "file1" output &&
215 test_path_is_missing .git/rebase-merge &&
216 git reset --hard HEAD^
219 test_expect_success
'retain authorship' '
223 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
225 git rebase -i --onto master HEAD^ &&
226 git show HEAD | grep "^Author: Twerp Snog"
229 test_expect_success
'squash' '
230 git reset --hard twerp &&
233 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
234 echo "******************************" &&
235 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
236 git rebase -i --onto master HEAD~2 &&
237 test B = $(cat file7) &&
238 test $(git rev-parse HEAD^) = $(git rev-parse master)
241 test_expect_success
'retain authorship when squashing' '
242 git show HEAD | grep "^Author: Twerp Snog"
245 test_expect_success
'-p handles "no changes" gracefully' '
246 HEAD=$(git rev-parse HEAD) &&
247 git rebase -i -p HEAD^ &&
248 git update-index --refresh &&
249 git diff-files --quiet &&
250 git diff-index --quiet --cached HEAD -- &&
251 test $HEAD = $(git rev-parse HEAD)
254 test_expect_failure
'exchange two commits with -p' '
255 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
256 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
257 test G = $(git cat-file commit HEAD | sed -ne \$p)
260 test_expect_success
'preserve merges with -p' '
261 git checkout -b to-be-preserved master^ &&
262 : > unrelated-file &&
263 git add unrelated-file &&
265 git commit -m "unrelated" &&
266 git checkout -b another-branch master &&
269 git commit -m J file1 &&
271 git merge to-be-preserved &&
274 git commit -m K file1 &&
277 git commit -m L1 file1 &&
278 git checkout HEAD^ &&
279 echo 1 > unrelated-file &&
281 git commit -m L2 unrelated-file &&
283 git merge another-branch &&
286 git commit -m M file1 &&
287 git checkout -b to-be-rebased &&
289 git rebase -i -p --onto branch1 master &&
290 git update-index --refresh &&
291 git diff-files --quiet &&
292 git diff-index --quiet --cached HEAD -- &&
293 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
294 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
295 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
296 test $(git show HEAD~5:file1) = B &&
297 test $(git show HEAD~3:file1) = C &&
298 test $(git show HEAD:file1) = E &&
299 test $(git show HEAD:unrelated-file) = 1
302 test_expect_success
'edit ancestor with -p' '
303 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
304 echo 2 > unrelated-file &&
306 git commit -m L2-modified --amend unrelated-file &&
307 git rebase --continue &&
308 git update-index --refresh &&
309 git diff-files --quiet &&
310 git diff-index --quiet --cached HEAD -- &&
311 test $(git show HEAD:unrelated-file) = 2
314 test_expect_success
'--continue tries to commit' '
316 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
317 echo resolved > file1 &&
319 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
320 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
321 git show HEAD | grep chouette
324 test_expect_success
'verbose flag is heeded, even after --continue' '
325 git reset --hard HEAD@{1} &&
327 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
328 echo resolved > file1 &&
330 git rebase --continue > output &&
331 grep "^ file1 | 2 +-$" output
334 test_expect_success
'multi-squash only fires up editor once' '
335 base=$(git rev-parse HEAD~4) &&
336 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
337 EXPECT_HEADER_COUNT=4 \
338 git rebase -i $base &&
339 test $base = $(git rev-parse HEAD^) &&
340 test 1 = $(git show | grep ONCE | wc -l)
343 test_expect_success
'multi-fixup does not fire up editor' '
344 git checkout -b multi-fixup E &&
345 base=$(git rev-parse HEAD~4) &&
346 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
347 git rebase -i $base &&
348 test $base = $(git rev-parse HEAD^) &&
349 test 0 = $(git show | grep NEVER | wc -l) &&
350 git checkout to-be-rebased &&
351 git branch -D multi-fixup
354 test_expect_success
'commit message used after conflict' '
355 git checkout -b conflict-fixup conflict-branch &&
356 base=$(git rev-parse HEAD~4) &&
358 FAKE_LINES="1 fixup 3 fixup 4" &&
360 test_must_fail git rebase -i $base
362 echo three > conflict &&
364 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
365 git rebase --continue &&
366 test $base = $(git rev-parse HEAD^) &&
367 test 1 = $(git show | grep ONCE | wc -l) &&
368 git checkout to-be-rebased &&
369 git branch -D conflict-fixup
372 test_expect_success
'commit message retained after conflict' '
373 git checkout -b conflict-squash conflict-branch &&
374 base=$(git rev-parse HEAD~4) &&
376 FAKE_LINES="1 fixup 3 squash 4" &&
378 test_must_fail git rebase -i $base
380 echo three > conflict &&
382 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
383 git rebase --continue &&
384 test $base = $(git rev-parse HEAD^) &&
385 test 2 = $(git show | grep TWICE | wc -l) &&
386 git checkout to-be-rebased &&
387 git branch -D conflict-squash
390 cat > expect-squash-fixup
<< EOF
398 test_expect_success
'squash and fixup generate correct log messages' '
399 git checkout -b squash-fixup E &&
400 base=$(git rev-parse HEAD~4) &&
401 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
402 EXPECT_HEADER_COUNT=4 \
403 git rebase -i $base &&
404 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
405 test_cmp expect-squash-fixup actual-squash-fixup &&
406 git checkout to-be-rebased &&
407 git branch -D squash-fixup
410 test_expect_success
'squash ignores comments' '
411 git checkout -b skip-comments E &&
412 base=$(git rev-parse HEAD~4) &&
413 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
414 EXPECT_HEADER_COUNT=4 \
415 git rebase -i $base &&
416 test $base = $(git rev-parse HEAD^) &&
417 test 1 = $(git show | grep ONCE | wc -l) &&
418 git checkout to-be-rebased &&
419 git branch -D skip-comments
422 test_expect_success
'squash ignores blank lines' '
423 git checkout -b skip-blank-lines E &&
424 base=$(git rev-parse HEAD~4) &&
425 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
426 EXPECT_HEADER_COUNT=4 \
427 git rebase -i $base &&
428 test $base = $(git rev-parse HEAD^) &&
429 test 1 = $(git show | grep ONCE | wc -l) &&
430 git checkout to-be-rebased &&
431 git branch -D skip-blank-lines
434 test_expect_success
'squash works as expected' '
435 git checkout -b squash-works no-conflict-branch &&
436 one=$(git rev-parse HEAD~3) &&
437 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
438 git rebase -i HEAD~3 &&
439 test $one = $(git rev-parse HEAD~2)
442 test_expect_success
'interrupted squash works as expected' '
443 git checkout -b interrupted-squash conflict-branch &&
444 one=$(git rev-parse HEAD~3) &&
446 FAKE_LINES="1 squash 3 2" &&
448 test_must_fail git rebase -i HEAD~3
450 (echo one; echo two; echo four) > conflict &&
452 test_must_fail git rebase --continue &&
453 echo resolved > conflict &&
455 git rebase --continue &&
456 test $one = $(git rev-parse HEAD~2)
459 test_expect_success
'interrupted squash works as expected (case 2)' '
460 git checkout -b interrupted-squash2 conflict-branch &&
461 one=$(git rev-parse HEAD~3) &&
463 FAKE_LINES="3 squash 1 2" &&
465 test_must_fail git rebase -i HEAD~3
467 (echo one; echo four) > conflict &&
469 test_must_fail git rebase --continue &&
470 (echo one; echo two; echo four) > conflict &&
472 test_must_fail git rebase --continue &&
473 echo resolved > conflict &&
475 git rebase --continue &&
476 test $one = $(git rev-parse HEAD~2)
479 test_expect_success
'ignore patch if in upstream' '
480 HEAD=$(git rev-parse HEAD) &&
481 git checkout -b has-cherry-picked HEAD^ &&
482 echo unrelated > file7 &&
485 git commit -m "unrelated change" &&
486 git cherry-pick $HEAD &&
487 EXPECT_COUNT=1 git rebase -i $HEAD &&
488 test $HEAD = $(git rev-parse HEAD^)
491 test_expect_success
'--continue tries to commit, even for "edit"' '
492 parent=$(git rev-parse HEAD^) &&
494 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
495 echo edited > file7 &&
497 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
498 test edited = $(git show HEAD:file7) &&
499 git show HEAD | grep chouette &&
500 test $parent = $(git rev-parse HEAD^)
503 test_expect_success
'aborted --continue does not squash commits after "edit"' '
504 old=$(git rev-parse HEAD) &&
506 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
507 echo "edited again" > file7 &&
510 FAKE_COMMIT_MESSAGE=" " &&
511 export FAKE_COMMIT_MESSAGE &&
512 test_must_fail git rebase --continue
514 test $old = $(git rev-parse HEAD) &&
518 test_expect_success
'auto-amend only edited commits after "edit"' '
520 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
521 echo "edited again" > file7 &&
523 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
524 echo "and again" > file7 &&
528 FAKE_COMMIT_MESSAGE="and again" &&
529 export FAKE_COMMIT_MESSAGE &&
530 test_must_fail git rebase --continue
535 test_expect_success
'rebase a detached HEAD' '
536 grandparent=$(git rev-parse HEAD~2) &&
537 git checkout $(git rev-parse HEAD) &&
539 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
540 test $grandparent = $(git rev-parse HEAD~2)
543 test_expect_success
'rebase a commit violating pre-commit' '
545 mkdir -p .git/hooks &&
546 PRE_COMMIT=.git/hooks/pre-commit &&
547 echo "#!/bin/sh" > $PRE_COMMIT &&
548 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
549 chmod a+x $PRE_COMMIT &&
550 echo "monde! " >> file1 &&
552 test_must_fail git commit -m doesnt-verify file1 &&
553 git commit -m doesnt-verify --no-verify file1 &&
555 FAKE_LINES=2 git rebase -i HEAD~2
559 test_expect_success
'rebase with a file named HEAD in worktree' '
563 git checkout -b branch3 A &&
566 GIT_AUTHOR_NAME="Squashed Away" &&
567 export GIT_AUTHOR_NAME &&
570 git commit -m "Add head" &&
573 git commit -m "Add body"
576 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
577 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
581 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
583 git checkout -b branch4 HEAD &&
584 GIT_EDITOR=: git commit --amend \
585 --author="Somebody else <somebody@else.com>" &&
586 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
587 git rebase -i branch3 &&
588 test $(git rev-parse branch3) = $(git rev-parse branch4)
592 test_expect_success
'submodule rebase setup' '
596 cd sub && git init && >elif &&
597 git add elif && git commit -m "submodule initial"
602 git commit -m "One" &&
605 git commit -a -m "Two" &&
607 cd sub && echo 3 >elif &&
608 git commit -a -m "submodule second"
611 git commit -a -m "Three changes submodule"
614 test_expect_success
'submodule rebase -i' '
615 FAKE_LINES="1 squash 2 3" git rebase -i A
618 test_expect_success
'avoid unnecessary reset' '
619 git checkout master &&
620 test-chmtime =123456789 file3 &&
621 git update-index --refresh &&
622 HEAD=$(git rev-parse HEAD) &&
623 git rebase -i HEAD~4 &&
624 test $HEAD = $(git rev-parse HEAD) &&
625 MTIME=$(test-chmtime -v +0 file3 | sed 's
/[^
0-9].
*$
//') &&
626 test 123456789 = $MTIME
629 test_expect_success
'reword' '
630 git checkout -b reword-branch master &&
631 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
632 git show HEAD | grep "E changed" &&
633 test $(git rev-parse master) != $(git rev-parse HEAD) &&
634 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
635 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
636 git show HEAD^ | grep "D changed" &&
637 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
638 git show HEAD~3 | grep "B changed" &&
639 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
640 git show HEAD~2 | grep "C changed"
643 test_expect_success
'rebase -i can copy notes' '
644 git config notes.rewrite.rebase true &&
645 git config notes.rewriteRef "refs/notes/*" &&
649 git notes add -m"a note" n3 &&
650 git rebase --onto n1 n2 &&
651 test "a note" = "$(git notes show HEAD)"
659 test_expect_success
'rebase -i can copy notes over a fixup' '
660 git reset --hard n3 &&
661 git notes add -m"an earlier note" n2 &&
662 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
663 git notes show > output &&
664 test_cmp expect output
667 test_expect_success
'rebase while detaching HEAD' '
668 git symbolic-ref HEAD &&
669 grandparent=$(git rev-parse HEAD~2) &&
671 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
672 test $grandparent = $(git rev-parse HEAD~2) &&
673 test_must_fail git symbolic-ref HEAD
676 test_tick
# Ensure that the rebased commits get a different timestamp.
677 test_expect_success
'always cherry-pick with --no-ff' '
678 git checkout no-ff-branch &&
679 git tag original-no-ff-branch &&
680 git rebase -i --no-ff A &&
684 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
685 git diff HEAD~$p original-no-ff-branch~$p > out &&
688 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
689 git diff HEAD~3 original-no-ff-branch~3 > out &&
693 test_expect_success
'set up commits with funny messages' '
694 git checkout -b funny A &&
697 git commit -a -m "end with slash\\" &&
700 git commit -a -m "something (\000) that looks like octal" &&
703 git commit -a -m "something (\n) that looks like a newline" &&
706 git commit -a -m "another commit"
709 test_expect_success
'rebase-i history with funny messages' '
710 git rev-list A..funny >expect &&
712 FAKE_LINES="1 2 3 4" git rebase -i A &&
713 git rev-list A.. >actual &&
714 test_cmp expect actual