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 test_expect_success
'no changes are a nop' '
68 git checkout branch2 &&
70 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
71 test $(git rev-parse I) = $(git rev-parse HEAD)
74 test_expect_success
'test the [branch] option' '
75 git checkout -b dead-end &&
77 git commit -m "stop here" &&
78 git rebase -i F branch2 &&
79 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
80 test $(git rev-parse I) = $(git rev-parse branch2) &&
81 test $(git rev-parse I) = $(git rev-parse HEAD)
84 test_expect_success
'test --onto <branch>' '
85 git checkout -b test-onto branch2 &&
86 git rebase -i --onto branch1 F &&
87 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
88 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
89 test $(git rev-parse I) = $(git rev-parse branch2)
92 test_expect_success
'rebase on top of a non-conflicting commit' '
93 git checkout branch1 &&
94 git tag original-branch1 &&
95 git rebase -i branch2 &&
96 test file6 = $(git diff --name-only original-branch1) &&
97 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
98 test $(git rev-parse I) = $(git rev-parse branch2) &&
99 test $(git rev-parse I) = $(git rev-parse HEAD~2)
102 test_expect_success
'reflog for the branch shows state before rebase' '
103 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
106 test_expect_success
'exchange two commits' '
107 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
108 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
109 test G = $(git cat-file commit HEAD | sed -ne \$p)
113 diff --git a/file1 b/file1
114 index f70f10e..fd79235 100644
130 test_expect_success
'stop on conflicting pick' '
131 git tag new-branch1 &&
132 test_must_fail git rebase -i master &&
133 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
134 test_cmp expect .git/rebase-merge/patch &&
135 test_cmp expect2 file1 &&
136 test "$(git diff --name-status |
137 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
138 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
139 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
142 test_expect_success
'abort' '
143 git rebase --abort &&
144 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
145 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
146 ! test -d .git/rebase-merge
149 test_expect_success
'abort with error when new base cannot be checked out' '
150 git rm --cached file1 &&
151 git commit -m "remove file in base" &&
152 test_must_fail git rebase -i master > output 2>&1 &&
153 grep "Untracked working tree file .file1. would be overwritten" \
155 ! test -d .git/rebase-merge &&
156 git reset --hard HEAD^
159 test_expect_success
'retain authorship' '
163 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
165 git rebase -i --onto master HEAD^ &&
166 git show HEAD | grep "^Author: Twerp Snog"
169 test_expect_success
'squash' '
170 git reset --hard twerp &&
173 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
174 echo "******************************" &&
175 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
176 git rebase -i --onto master HEAD~2 &&
177 test B = $(cat file7) &&
178 test $(git rev-parse HEAD^) = $(git rev-parse master)
181 test_expect_success
'retain authorship when squashing' '
182 git show HEAD | grep "^Author: Twerp Snog"
185 test_expect_success
'-p handles "no changes" gracefully' '
186 HEAD=$(git rev-parse HEAD) &&
187 git rebase -i -p HEAD^ &&
188 git update-index --refresh &&
189 git diff-files --quiet &&
190 git diff-index --quiet --cached HEAD -- &&
191 test $HEAD = $(git rev-parse HEAD)
194 test_expect_failure
'exchange two commits with -p' '
195 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
196 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
197 test G = $(git cat-file commit HEAD | sed -ne \$p)
200 test_expect_success
'preserve merges with -p' '
201 git checkout -b to-be-preserved master^ &&
202 : > unrelated-file &&
203 git add unrelated-file &&
205 git commit -m "unrelated" &&
206 git checkout -b another-branch master &&
209 git commit -m J file1 &&
211 git merge to-be-preserved &&
214 git commit -m K file1 &&
217 git commit -m L1 file1 &&
218 git checkout HEAD^ &&
219 echo 1 > unrelated-file &&
221 git commit -m L2 unrelated-file &&
223 git merge another-branch &&
226 git commit -m M file1 &&
227 git checkout -b to-be-rebased &&
229 git rebase -i -p --onto branch1 master &&
230 git update-index --refresh &&
231 git diff-files --quiet &&
232 git diff-index --quiet --cached HEAD -- &&
233 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
234 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
235 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
236 test $(git show HEAD~5:file1) = B &&
237 test $(git show HEAD~3:file1) = C &&
238 test $(git show HEAD:file1) = E &&
239 test $(git show HEAD:unrelated-file) = 1
242 test_expect_success
'edit ancestor with -p' '
243 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
244 echo 2 > unrelated-file &&
246 git commit -m L2-modified --amend unrelated-file &&
247 git rebase --continue &&
248 git update-index --refresh &&
249 git diff-files --quiet &&
250 git diff-index --quiet --cached HEAD -- &&
251 test $(git show HEAD:unrelated-file) = 2
254 test_expect_success
'--continue tries to commit' '
256 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
257 echo resolved > file1 &&
259 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
260 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
261 git show HEAD | grep chouette
264 test_expect_success
'verbose flag is heeded, even after --continue' '
265 git reset --hard HEAD@{1} &&
267 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
268 echo resolved > file1 &&
270 git rebase --continue > output &&
271 grep "^ file1 | 2 +-$" output
274 test_expect_success
'multi-squash only fires up editor once' '
275 base=$(git rev-parse HEAD~4) &&
276 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
277 EXPECT_HEADER_COUNT=4 \
278 git rebase -i $base &&
279 test $base = $(git rev-parse HEAD^) &&
280 test 1 = $(git show | grep ONCE | wc -l)
283 test_expect_success
'multi-fixup does not fire up editor' '
284 git checkout -b multi-fixup E &&
285 base=$(git rev-parse HEAD~4) &&
286 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
287 git rebase -i $base &&
288 test $base = $(git rev-parse HEAD^) &&
289 test 0 = $(git show | grep NEVER | wc -l) &&
290 git checkout to-be-rebased &&
291 git branch -D multi-fixup
294 test_expect_success
'commit message used after conflict' '
295 git checkout -b conflict-fixup conflict-branch &&
296 base=$(git rev-parse HEAD~4) &&
298 FAKE_LINES="1 fixup 3 fixup 4" &&
300 test_must_fail git rebase -i $base
302 echo three > conflict &&
304 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
305 git rebase --continue &&
306 test $base = $(git rev-parse HEAD^) &&
307 test 1 = $(git show | grep ONCE | wc -l) &&
308 git checkout to-be-rebased &&
309 git branch -D conflict-fixup
312 test_expect_success
'commit message retained after conflict' '
313 git checkout -b conflict-squash conflict-branch &&
314 base=$(git rev-parse HEAD~4) &&
316 FAKE_LINES="1 fixup 3 squash 4" &&
318 test_must_fail git rebase -i $base
320 echo three > conflict &&
322 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
323 git rebase --continue &&
324 test $base = $(git rev-parse HEAD^) &&
325 test 2 = $(git show | grep TWICE | wc -l) &&
326 git checkout to-be-rebased &&
327 git branch -D conflict-squash
330 cat > expect-squash-fixup
<< EOF
338 test_expect_success
'squash and fixup generate correct log messages' '
339 git checkout -b squash-fixup E &&
340 base=$(git rev-parse HEAD~4) &&
341 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
342 EXPECT_HEADER_COUNT=4 \
343 git rebase -i $base &&
344 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
345 test_cmp expect-squash-fixup actual-squash-fixup &&
346 git checkout to-be-rebased &&
347 git branch -D squash-fixup
350 test_expect_success
'squash ignores comments' '
351 git checkout -b skip-comments E &&
352 base=$(git rev-parse HEAD~4) &&
353 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
354 EXPECT_HEADER_COUNT=4 \
355 git rebase -i $base &&
356 test $base = $(git rev-parse HEAD^) &&
357 test 1 = $(git show | grep ONCE | wc -l) &&
358 git checkout to-be-rebased &&
359 git branch -D skip-comments
362 test_expect_success
'squash ignores blank lines' '
363 git checkout -b skip-blank-lines E &&
364 base=$(git rev-parse HEAD~4) &&
365 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
366 EXPECT_HEADER_COUNT=4 \
367 git rebase -i $base &&
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 skip-blank-lines
374 test_expect_success
'squash works as expected' '
375 git checkout -b squash-works no-conflict-branch &&
376 one=$(git rev-parse HEAD~3) &&
377 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
378 git rebase -i HEAD~3 &&
379 test $one = $(git rev-parse HEAD~2)
382 test_expect_success
'interrupted squash works as expected' '
383 git checkout -b interrupted-squash conflict-branch &&
384 one=$(git rev-parse HEAD~3) &&
386 FAKE_LINES="1 squash 3 2" &&
388 test_must_fail git rebase -i HEAD~3
390 (echo one; echo two; echo four) > conflict &&
392 test_must_fail git rebase --continue &&
393 echo resolved > conflict &&
395 git rebase --continue &&
396 test $one = $(git rev-parse HEAD~2)
399 test_expect_success
'interrupted squash works as expected (case 2)' '
400 git checkout -b interrupted-squash2 conflict-branch &&
401 one=$(git rev-parse HEAD~3) &&
403 FAKE_LINES="3 squash 1 2" &&
405 test_must_fail git rebase -i HEAD~3
407 (echo one; echo four) > conflict &&
409 test_must_fail git rebase --continue &&
410 (echo one; echo two; echo four) > conflict &&
412 test_must_fail git rebase --continue &&
413 echo resolved > conflict &&
415 git rebase --continue &&
416 test $one = $(git rev-parse HEAD~2)
419 test_expect_success
'ignore patch if in upstream' '
420 HEAD=$(git rev-parse HEAD) &&
421 git checkout -b has-cherry-picked HEAD^ &&
422 echo unrelated > file7 &&
425 git commit -m "unrelated change" &&
426 git cherry-pick $HEAD &&
427 EXPECT_COUNT=1 git rebase -i $HEAD &&
428 test $HEAD = $(git rev-parse HEAD^)
431 test_expect_success
'--continue tries to commit, even for "edit"' '
432 parent=$(git rev-parse HEAD^) &&
434 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
435 echo edited > file7 &&
437 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
438 test edited = $(git show HEAD:file7) &&
439 git show HEAD | grep chouette &&
440 test $parent = $(git rev-parse HEAD^)
443 test_expect_success
'aborted --continue does not squash commits after "edit"' '
444 old=$(git rev-parse HEAD) &&
446 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
447 echo "edited again" > file7 &&
450 FAKE_COMMIT_MESSAGE=" " &&
451 export FAKE_COMMIT_MESSAGE &&
452 test_must_fail git rebase --continue
454 test $old = $(git rev-parse HEAD) &&
458 test_expect_success
'auto-amend only edited commits after "edit"' '
460 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
461 echo "edited again" > file7 &&
463 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
464 echo "and again" > file7 &&
468 FAKE_COMMIT_MESSAGE="and again" &&
469 export FAKE_COMMIT_MESSAGE &&
470 test_must_fail git rebase --continue
475 test_expect_success
'rebase a detached HEAD' '
476 grandparent=$(git rev-parse HEAD~2) &&
477 git checkout $(git rev-parse HEAD) &&
479 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
480 test $grandparent = $(git rev-parse HEAD~2)
483 test_expect_success
'rebase a commit violating pre-commit' '
485 mkdir -p .git/hooks &&
486 PRE_COMMIT=.git/hooks/pre-commit &&
487 echo "#!/bin/sh" > $PRE_COMMIT &&
488 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
489 chmod a+x $PRE_COMMIT &&
490 echo "monde! " >> file1 &&
492 test_must_fail git commit -m doesnt-verify file1 &&
493 git commit -m doesnt-verify --no-verify file1 &&
495 FAKE_LINES=2 git rebase -i HEAD~2
499 test_expect_success
'rebase with a file named HEAD in worktree' '
503 git checkout -b branch3 A &&
506 GIT_AUTHOR_NAME="Squashed Away" &&
507 export GIT_AUTHOR_NAME &&
510 git commit -m "Add head" &&
513 git commit -m "Add body"
516 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
517 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
521 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
523 git checkout -b branch4 HEAD &&
524 GIT_EDITOR=: git commit --amend \
525 --author="Somebody else <somebody@else.com>"
526 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
527 git rebase -i branch3 &&
528 test $(git rev-parse branch3) = $(git rev-parse branch4)
532 test_expect_success
'submodule rebase setup' '
536 cd sub && git init && >elif &&
537 git add elif && git commit -m "submodule initial"
542 git commit -m "One" &&
545 git commit -a -m "Two" &&
547 cd sub && echo 3 >elif &&
548 git commit -a -m "submodule second"
551 git commit -a -m "Three changes submodule"
554 test_expect_success
'submodule rebase -i' '
555 FAKE_LINES="1 squash 2 3" git rebase -i A
558 test_expect_success
'avoid unnecessary reset' '
559 git checkout master &&
560 test-chmtime =123456789 file3 &&
561 git update-index --refresh &&
562 HEAD=$(git rev-parse HEAD) &&
563 git rebase -i HEAD~4 &&
564 test $HEAD = $(git rev-parse HEAD) &&
565 MTIME=$(test-chmtime -v +0 file3 | sed 's
/[^
0-9].
*$
//') &&
566 test 123456789 = $MTIME
569 test_expect_success
'reword' '
570 git checkout -b reword-branch master &&
571 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
572 git show HEAD | grep "E changed" &&
573 test $(git rev-parse master) != $(git rev-parse HEAD) &&
574 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
575 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
576 git show HEAD^ | grep "D changed" &&
577 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
578 git show HEAD~3 | grep "B changed" &&
579 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
580 git show HEAD~2 | grep "C changed"
583 test_expect_success
'rebase -i can copy notes' '
584 git config notes.rewrite.rebase true &&
585 git config notes.rewriteRef "refs/notes/*" &&
589 git notes add -m"a note" n3 &&
590 git rebase --onto n1 n2 &&
591 test "a note" = "$(git notes show HEAD)"
599 test_expect_success
'rebase -i can copy notes over a fixup' '
600 git reset --hard n3 &&
601 git notes add -m"an earlier note" n2 &&
602 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
603 git notes show > output &&
604 test_cmp expect output
607 test_expect_success
'rebase while detaching HEAD' '
608 git symbolic-ref HEAD &&
609 grandparent=$(git rev-parse HEAD~2) &&
611 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
612 test $grandparent = $(git rev-parse HEAD~2) &&
613 test_must_fail git symbolic-ref HEAD
616 test_tick
# Ensure that the rebased commits get a different timestamp.
617 test_expect_success
'always cherry-pick with --no-ff' '
618 git checkout no-ff-branch &&
619 git tag original-no-ff-branch &&
620 git rebase -i --no-ff A &&
624 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
625 git diff HEAD~$p original-no-ff-branch~$p > out &&
628 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
629 git diff HEAD~3 original-no-ff-branch~3 > out &&
633 test_expect_success
'set up commits with funny messages' '
634 git checkout -b funny A &&
637 git commit -a -m "end with slash\\" &&
640 git commit -a -m "another commit"
643 test_expect_success
'rebase-i history with funny messages' '
644 git rev-list A..funny >expect &&
646 FAKE_LINES="1 2" git rebase -i A &&
647 git rev-list A.. >actual &&
648 test_cmp expect actual