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 test_commit one conflict &&
52 test_commit two conflict &&
53 test_commit three conflict &&
54 test_commit four conflict &&
55 git checkout -b no-conflict-branch A &&
56 test_commit J fileJ &&
57 test_commit K fileK &&
58 test_commit L fileL &&
59 test_commit M fileM &&
60 git checkout -b no-ff-branch A &&
61 test_commit N fileN &&
62 test_commit O fileO &&
66 # "exec" commands are ran with the user shell by default, but this may
67 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
68 # to create a file. Unseting SHELL avoids such non-portable behavior
72 test_expect_success
'rebase -i with the exec command' '
73 git checkout master &&
75 FAKE_LINES="1 exec_>touch-one
76 2 exec_>touch-two exec_false exec_>touch-three
77 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
79 test_must_fail git rebase -i A
81 test_path_is_file touch-one &&
82 test_path_is_file touch-two &&
83 test_path_is_missing touch-three " (should have stopped before)" &&
84 test $(git rev-parse C) = $(git rev-parse HEAD) || {
85 echo "Stopped at wrong revision:"
86 echo "($(git describe --tags HEAD) instead of C)"
89 git rebase --continue &&
90 test_path_is_file touch-three &&
91 test_path_is_file "touch-file name with spaces" &&
92 test_path_is_file touch-after-semicolon &&
93 test $(git rev-parse master) = $(git rev-parse HEAD) || {
94 echo "Stopped at wrong revision:"
95 echo "($(git describe --tags HEAD) instead of master)"
101 test_expect_success
'rebase -i with the exec command runs from tree root' '
102 git checkout master &&
103 mkdir subdir && (cd subdir &&
104 FAKE_LINES="1 exec_>touch-subdir" \
107 test_path_is_file touch-subdir &&
111 test_expect_success
'rebase -i with the exec command checks tree cleanness' '
112 git checkout master &&
114 FAKE_LINES="exec_echo_foo_>file1 1" &&
116 test_must_fail git rebase -i HEAD^
118 test $(git rev-parse master^) = $(git rev-parse HEAD) || {
119 echo "Stopped at wrong revision:"
120 echo "($(git describe --tags HEAD) instead of master^)"
124 git rebase --continue
127 test_expect_success
'no changes are a nop' '
128 git checkout branch2 &&
130 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
131 test $(git rev-parse I) = $(git rev-parse HEAD)
134 test_expect_success
'test the [branch] option' '
135 git checkout -b dead-end &&
137 git commit -m "stop here" &&
138 git rebase -i F branch2 &&
139 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
140 test $(git rev-parse I) = $(git rev-parse branch2) &&
141 test $(git rev-parse I) = $(git rev-parse HEAD)
144 test_expect_success
'test --onto <branch>' '
145 git checkout -b test-onto branch2 &&
146 git rebase -i --onto branch1 F &&
147 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
148 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
149 test $(git rev-parse I) = $(git rev-parse branch2)
152 test_expect_success
'rebase on top of a non-conflicting commit' '
153 git checkout branch1 &&
154 git tag original-branch1 &&
155 git rebase -i branch2 &&
156 test file6 = $(git diff --name-only original-branch1) &&
157 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
158 test $(git rev-parse I) = $(git rev-parse branch2) &&
159 test $(git rev-parse I) = $(git rev-parse HEAD~2)
162 test_expect_success
'reflog for the branch shows state before rebase' '
163 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
166 test_expect_success
'exchange two commits' '
167 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
168 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
169 test G = $(git cat-file commit HEAD | sed -ne \$p)
173 diff --git a/file1 b/file1
174 index f70f10e..fd79235 100644
190 test_expect_success
'stop on conflicting pick' '
191 git tag new-branch1 &&
192 test_must_fail git rebase -i master &&
193 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
194 test_cmp expect .git/rebase-merge/patch &&
195 test_cmp expect2 file1 &&
196 test "$(git diff --name-status |
197 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
198 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
199 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
202 test_expect_success
'abort' '
203 git rebase --abort &&
204 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
205 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
206 test_path_is_missing .git/rebase-merge
209 test_expect_success
'abort with error when new base cannot be checked out' '
210 git rm --cached file1 &&
211 git commit -m "remove file in base" &&
212 test_must_fail git rebase -i master > output 2>&1 &&
213 grep "The following untracked working tree files would be overwritten by checkout:" \
215 grep "file1" output &&
216 test_path_is_missing .git/rebase-merge &&
217 git reset --hard HEAD^
220 test_expect_success
'retain authorship' '
224 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
226 git rebase -i --onto master HEAD^ &&
227 git show HEAD | grep "^Author: Twerp Snog"
230 test_expect_success
'squash' '
231 git reset --hard twerp &&
234 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
235 echo "******************************" &&
236 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
237 git rebase -i --onto master HEAD~2 &&
238 test B = $(cat file7) &&
239 test $(git rev-parse HEAD^) = $(git rev-parse master)
242 test_expect_success
'retain authorship when squashing' '
243 git show HEAD | grep "^Author: Twerp Snog"
246 test_expect_success
'-p handles "no changes" gracefully' '
247 HEAD=$(git rev-parse HEAD) &&
248 git rebase -i -p HEAD^ &&
249 git update-index --refresh &&
250 git diff-files --quiet &&
251 git diff-index --quiet --cached HEAD -- &&
252 test $HEAD = $(git rev-parse HEAD)
255 test_expect_failure
'exchange two commits with -p' '
256 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
257 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
258 test G = $(git cat-file commit HEAD | sed -ne \$p)
261 test_expect_success
'preserve merges with -p' '
262 git checkout -b to-be-preserved master^ &&
263 : > unrelated-file &&
264 git add unrelated-file &&
266 git commit -m "unrelated" &&
267 git checkout -b another-branch master &&
270 git commit -m J file1 &&
272 git merge to-be-preserved &&
275 git commit -m K file1 &&
278 git commit -m L1 file1 &&
279 git checkout HEAD^ &&
280 echo 1 > unrelated-file &&
282 git commit -m L2 unrelated-file &&
284 git merge another-branch &&
287 git commit -m M file1 &&
288 git checkout -b to-be-rebased &&
290 git rebase -i -p --onto branch1 master &&
291 git update-index --refresh &&
292 git diff-files --quiet &&
293 git diff-index --quiet --cached HEAD -- &&
294 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
295 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
296 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
297 test $(git show HEAD~5:file1) = B &&
298 test $(git show HEAD~3:file1) = C &&
299 test $(git show HEAD:file1) = E &&
300 test $(git show HEAD:unrelated-file) = 1
303 test_expect_success
'edit ancestor with -p' '
304 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
305 echo 2 > unrelated-file &&
307 git commit -m L2-modified --amend unrelated-file &&
308 git rebase --continue &&
309 git update-index --refresh &&
310 git diff-files --quiet &&
311 git diff-index --quiet --cached HEAD -- &&
312 test $(git show HEAD:unrelated-file) = 2
315 test_expect_success
'--continue tries to commit' '
317 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
318 echo resolved > file1 &&
320 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
321 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
322 git show HEAD | grep chouette
325 test_expect_success
'verbose flag is heeded, even after --continue' '
326 git reset --hard HEAD@{1} &&
328 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
329 echo resolved > file1 &&
331 git rebase --continue > output &&
332 grep "^ file1 | 2 +-$" output
335 test_expect_success
'multi-squash only fires up editor once' '
336 base=$(git rev-parse HEAD~4) &&
337 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
338 EXPECT_HEADER_COUNT=4 \
339 git rebase -i $base &&
340 test $base = $(git rev-parse HEAD^) &&
341 test 1 = $(git show | grep ONCE | wc -l)
344 test_expect_success
'multi-fixup does not fire up editor' '
345 git checkout -b multi-fixup E &&
346 base=$(git rev-parse HEAD~4) &&
347 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
348 git rebase -i $base &&
349 test $base = $(git rev-parse HEAD^) &&
350 test 0 = $(git show | grep NEVER | wc -l) &&
351 git checkout to-be-rebased &&
352 git branch -D multi-fixup
355 test_expect_success
'commit message used after conflict' '
356 git checkout -b conflict-fixup conflict-branch &&
357 base=$(git rev-parse HEAD~4) &&
359 FAKE_LINES="1 fixup 3 fixup 4" &&
361 test_must_fail git rebase -i $base
363 echo three > conflict &&
365 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
366 git rebase --continue &&
367 test $base = $(git rev-parse HEAD^) &&
368 test 1 = $(git show | grep ONCE | wc -l) &&
369 git checkout to-be-rebased &&
370 git branch -D conflict-fixup
373 test_expect_success
'commit message retained after conflict' '
374 git checkout -b conflict-squash conflict-branch &&
375 base=$(git rev-parse HEAD~4) &&
377 FAKE_LINES="1 fixup 3 squash 4" &&
379 test_must_fail git rebase -i $base
381 echo three > conflict &&
383 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
384 git rebase --continue &&
385 test $base = $(git rev-parse HEAD^) &&
386 test 2 = $(git show | grep TWICE | wc -l) &&
387 git checkout to-be-rebased &&
388 git branch -D conflict-squash
391 cat > expect-squash-fixup
<< EOF
399 test_expect_success
'squash and fixup generate correct log messages' '
400 git checkout -b squash-fixup E &&
401 base=$(git rev-parse HEAD~4) &&
402 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
403 EXPECT_HEADER_COUNT=4 \
404 git rebase -i $base &&
405 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
406 test_cmp expect-squash-fixup actual-squash-fixup &&
407 git checkout to-be-rebased &&
408 git branch -D squash-fixup
411 test_expect_success
'squash ignores comments' '
412 git checkout -b skip-comments E &&
413 base=$(git rev-parse HEAD~4) &&
414 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
415 EXPECT_HEADER_COUNT=4 \
416 git rebase -i $base &&
417 test $base = $(git rev-parse HEAD^) &&
418 test 1 = $(git show | grep ONCE | wc -l) &&
419 git checkout to-be-rebased &&
420 git branch -D skip-comments
423 test_expect_success
'squash ignores blank lines' '
424 git checkout -b skip-blank-lines E &&
425 base=$(git rev-parse HEAD~4) &&
426 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
427 EXPECT_HEADER_COUNT=4 \
428 git rebase -i $base &&
429 test $base = $(git rev-parse HEAD^) &&
430 test 1 = $(git show | grep ONCE | wc -l) &&
431 git checkout to-be-rebased &&
432 git branch -D skip-blank-lines
435 test_expect_success
'squash works as expected' '
436 git checkout -b squash-works no-conflict-branch &&
437 one=$(git rev-parse HEAD~3) &&
438 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
439 git rebase -i HEAD~3 &&
440 test $one = $(git rev-parse HEAD~2)
443 test_expect_success
'interrupted squash works as expected' '
444 git checkout -b interrupted-squash conflict-branch &&
445 one=$(git rev-parse HEAD~3) &&
447 FAKE_LINES="1 squash 3 2" &&
449 test_must_fail git rebase -i HEAD~3
451 (echo one; echo two; echo four) > conflict &&
453 test_must_fail git rebase --continue &&
454 echo resolved > conflict &&
456 git rebase --continue &&
457 test $one = $(git rev-parse HEAD~2)
460 test_expect_success
'interrupted squash works as expected (case 2)' '
461 git checkout -b interrupted-squash2 conflict-branch &&
462 one=$(git rev-parse HEAD~3) &&
464 FAKE_LINES="3 squash 1 2" &&
466 test_must_fail git rebase -i HEAD~3
468 (echo one; echo four) > conflict &&
470 test_must_fail git rebase --continue &&
471 (echo one; echo two; echo four) > conflict &&
473 test_must_fail git rebase --continue &&
474 echo resolved > conflict &&
476 git rebase --continue &&
477 test $one = $(git rev-parse HEAD~2)
480 test_expect_success
'ignore patch if in upstream' '
481 HEAD=$(git rev-parse HEAD) &&
482 git checkout -b has-cherry-picked HEAD^ &&
483 echo unrelated > file7 &&
486 git commit -m "unrelated change" &&
487 git cherry-pick $HEAD &&
488 EXPECT_COUNT=1 git rebase -i $HEAD &&
489 test $HEAD = $(git rev-parse HEAD^)
492 test_expect_success
'--continue tries to commit, even for "edit"' '
493 parent=$(git rev-parse HEAD^) &&
495 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
496 echo edited > file7 &&
498 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
499 test edited = $(git show HEAD:file7) &&
500 git show HEAD | grep chouette &&
501 test $parent = $(git rev-parse HEAD^)
504 test_expect_success
'aborted --continue does not squash commits after "edit"' '
505 old=$(git rev-parse HEAD) &&
507 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
508 echo "edited again" > file7 &&
511 FAKE_COMMIT_MESSAGE=" " &&
512 export FAKE_COMMIT_MESSAGE &&
513 test_must_fail git rebase --continue
515 test $old = $(git rev-parse HEAD) &&
519 test_expect_success
'auto-amend only edited commits after "edit"' '
521 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
522 echo "edited again" > file7 &&
524 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
525 echo "and again" > file7 &&
529 FAKE_COMMIT_MESSAGE="and again" &&
530 export FAKE_COMMIT_MESSAGE &&
531 test_must_fail git rebase --continue
536 test_expect_success
'rebase a detached HEAD' '
537 grandparent=$(git rev-parse HEAD~2) &&
538 git checkout $(git rev-parse HEAD) &&
540 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
541 test $grandparent = $(git rev-parse HEAD~2)
544 test_expect_success
'rebase a commit violating pre-commit' '
546 mkdir -p .git/hooks &&
547 PRE_COMMIT=.git/hooks/pre-commit &&
548 echo "#!/bin/sh" > $PRE_COMMIT &&
549 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
550 chmod a+x $PRE_COMMIT &&
551 echo "monde! " >> file1 &&
553 test_must_fail git commit -m doesnt-verify file1 &&
554 git commit -m doesnt-verify --no-verify file1 &&
556 FAKE_LINES=2 git rebase -i HEAD~2
560 test_expect_success
'rebase with a file named HEAD in worktree' '
564 git checkout -b branch3 A &&
567 GIT_AUTHOR_NAME="Squashed Away" &&
568 export GIT_AUTHOR_NAME &&
571 git commit -m "Add head" &&
574 git commit -m "Add body"
577 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
578 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
582 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
584 git checkout -b branch4 HEAD &&
585 GIT_EDITOR=: git commit --amend \
586 --author="Somebody else <somebody@else.com>" &&
587 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
588 git rebase -i branch3 &&
589 test $(git rev-parse branch3) = $(git rev-parse branch4)
593 test_expect_success
'submodule rebase setup' '
597 cd sub && git init && >elif &&
598 git add elif && git commit -m "submodule initial"
603 git commit -m "One" &&
606 git commit -a -m "Two" &&
608 cd sub && echo 3 >elif &&
609 git commit -a -m "submodule second"
612 git commit -a -m "Three changes submodule"
615 test_expect_success
'submodule rebase -i' '
616 FAKE_LINES="1 squash 2 3" git rebase -i A
619 test_expect_success
'avoid unnecessary reset' '
620 git checkout master &&
621 test-chmtime =123456789 file3 &&
622 git update-index --refresh &&
623 HEAD=$(git rev-parse HEAD) &&
624 git rebase -i HEAD~4 &&
625 test $HEAD = $(git rev-parse HEAD) &&
626 MTIME=$(test-chmtime -v +0 file3 | sed 's
/[^
0-9].
*$
//') &&
627 test 123456789 = $MTIME
630 test_expect_success
'reword' '
631 git checkout -b reword-branch master &&
632 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
633 git show HEAD | grep "E changed" &&
634 test $(git rev-parse master) != $(git rev-parse HEAD) &&
635 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
636 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
637 git show HEAD^ | grep "D changed" &&
638 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
639 git show HEAD~3 | grep "B changed" &&
640 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
641 git show HEAD~2 | grep "C changed"
644 test_expect_success
'rebase -i can copy notes' '
645 git config notes.rewrite.rebase true &&
646 git config notes.rewriteRef "refs/notes/*" &&
650 git notes add -m"a note" n3 &&
651 git rebase --onto n1 n2 &&
652 test "a note" = "$(git notes show HEAD)"
660 test_expect_success
'rebase -i can copy notes over a fixup' '
661 git reset --hard n3 &&
662 git notes add -m"an earlier note" n2 &&
663 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
664 git notes show > output &&
665 test_cmp expect output
668 test_expect_success
'rebase while detaching HEAD' '
669 git symbolic-ref HEAD &&
670 grandparent=$(git rev-parse HEAD~2) &&
672 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
673 test $grandparent = $(git rev-parse HEAD~2) &&
674 test_must_fail git symbolic-ref HEAD
677 test_tick
# Ensure that the rebased commits get a different timestamp.
678 test_expect_success
'always cherry-pick with --no-ff' '
679 git checkout no-ff-branch &&
680 git tag original-no-ff-branch &&
681 git rebase -i --no-ff A &&
685 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
686 git diff HEAD~$p original-no-ff-branch~$p > out &&
689 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
690 git diff HEAD~3 original-no-ff-branch~3 > out &&
694 test_expect_success
'set up commits with funny messages' '
695 git checkout -b funny A &&
698 git commit -a -m "end with slash\\" &&
701 git commit -a -m "something (\000) that looks like octal" &&
704 git commit -a -m "something (\n) that looks like a newline" &&
707 git commit -a -m "another commit"
710 test_expect_success
'rebase-i history with funny messages' '
711 git rev-list A..funny >expect &&
713 FAKE_LINES="1 2 3 4" git rebase -i A &&
714 git rev-list A.. >actual &&
715 test_cmp expect actual