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 "The following untracked working tree files would be overwritten by checkout:" \
155 grep "file1" output &&
156 ! test -d .git/rebase-merge &&
157 git reset --hard HEAD^
160 test_expect_success
'retain authorship' '
164 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
166 git rebase -i --onto master HEAD^ &&
167 git show HEAD | grep "^Author: Twerp Snog"
170 test_expect_success
'squash' '
171 git reset --hard twerp &&
174 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
175 echo "******************************" &&
176 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
177 git rebase -i --onto master HEAD~2 &&
178 test B = $(cat file7) &&
179 test $(git rev-parse HEAD^) = $(git rev-parse master)
182 test_expect_success
'retain authorship when squashing' '
183 git show HEAD | grep "^Author: Twerp Snog"
186 test_expect_success
'-p handles "no changes" gracefully' '
187 HEAD=$(git rev-parse HEAD) &&
188 git rebase -i -p HEAD^ &&
189 git update-index --refresh &&
190 git diff-files --quiet &&
191 git diff-index --quiet --cached HEAD -- &&
192 test $HEAD = $(git rev-parse HEAD)
195 test_expect_failure
'exchange two commits with -p' '
196 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
197 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
198 test G = $(git cat-file commit HEAD | sed -ne \$p)
201 test_expect_success
'preserve merges with -p' '
202 git checkout -b to-be-preserved master^ &&
203 : > unrelated-file &&
204 git add unrelated-file &&
206 git commit -m "unrelated" &&
207 git checkout -b another-branch master &&
210 git commit -m J file1 &&
212 git merge to-be-preserved &&
215 git commit -m K file1 &&
218 git commit -m L1 file1 &&
219 git checkout HEAD^ &&
220 echo 1 > unrelated-file &&
222 git commit -m L2 unrelated-file &&
224 git merge another-branch &&
227 git commit -m M file1 &&
228 git checkout -b to-be-rebased &&
230 git rebase -i -p --onto branch1 master &&
231 git update-index --refresh &&
232 git diff-files --quiet &&
233 git diff-index --quiet --cached HEAD -- &&
234 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
235 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
236 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
237 test $(git show HEAD~5:file1) = B &&
238 test $(git show HEAD~3:file1) = C &&
239 test $(git show HEAD:file1) = E &&
240 test $(git show HEAD:unrelated-file) = 1
243 test_expect_success
'edit ancestor with -p' '
244 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
245 echo 2 > unrelated-file &&
247 git commit -m L2-modified --amend unrelated-file &&
248 git rebase --continue &&
249 git update-index --refresh &&
250 git diff-files --quiet &&
251 git diff-index --quiet --cached HEAD -- &&
252 test $(git show HEAD:unrelated-file) = 2
255 test_expect_success
'--continue tries to commit' '
257 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
258 echo resolved > file1 &&
260 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
261 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
262 git show HEAD | grep chouette
265 test_expect_success
'verbose flag is heeded, even after --continue' '
266 git reset --hard HEAD@{1} &&
268 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
269 echo resolved > file1 &&
271 git rebase --continue > output &&
272 grep "^ file1 | 2 +-$" output
275 test_expect_success
'multi-squash only fires up editor once' '
276 base=$(git rev-parse HEAD~4) &&
277 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
278 EXPECT_HEADER_COUNT=4 \
279 git rebase -i $base &&
280 test $base = $(git rev-parse HEAD^) &&
281 test 1 = $(git show | grep ONCE | wc -l)
284 test_expect_success
'multi-fixup does not fire up editor' '
285 git checkout -b multi-fixup E &&
286 base=$(git rev-parse HEAD~4) &&
287 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
288 git rebase -i $base &&
289 test $base = $(git rev-parse HEAD^) &&
290 test 0 = $(git show | grep NEVER | wc -l) &&
291 git checkout to-be-rebased &&
292 git branch -D multi-fixup
295 test_expect_success
'commit message used after conflict' '
296 git checkout -b conflict-fixup conflict-branch &&
297 base=$(git rev-parse HEAD~4) &&
299 FAKE_LINES="1 fixup 3 fixup 4" &&
301 test_must_fail git rebase -i $base
303 echo three > conflict &&
305 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
306 git rebase --continue &&
307 test $base = $(git rev-parse HEAD^) &&
308 test 1 = $(git show | grep ONCE | wc -l) &&
309 git checkout to-be-rebased &&
310 git branch -D conflict-fixup
313 test_expect_success
'commit message retained after conflict' '
314 git checkout -b conflict-squash conflict-branch &&
315 base=$(git rev-parse HEAD~4) &&
317 FAKE_LINES="1 fixup 3 squash 4" &&
319 test_must_fail git rebase -i $base
321 echo three > conflict &&
323 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
324 git rebase --continue &&
325 test $base = $(git rev-parse HEAD^) &&
326 test 2 = $(git show | grep TWICE | wc -l) &&
327 git checkout to-be-rebased &&
328 git branch -D conflict-squash
331 cat > expect-squash-fixup
<< EOF
339 test_expect_success
'squash and fixup generate correct log messages' '
340 git checkout -b squash-fixup E &&
341 base=$(git rev-parse HEAD~4) &&
342 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
343 EXPECT_HEADER_COUNT=4 \
344 git rebase -i $base &&
345 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
346 test_cmp expect-squash-fixup actual-squash-fixup &&
347 git checkout to-be-rebased &&
348 git branch -D squash-fixup
351 test_expect_success
'squash ignores comments' '
352 git checkout -b skip-comments E &&
353 base=$(git rev-parse HEAD~4) &&
354 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
355 EXPECT_HEADER_COUNT=4 \
356 git rebase -i $base &&
357 test $base = $(git rev-parse HEAD^) &&
358 test 1 = $(git show | grep ONCE | wc -l) &&
359 git checkout to-be-rebased &&
360 git branch -D skip-comments
363 test_expect_success
'squash ignores blank lines' '
364 git checkout -b skip-blank-lines E &&
365 base=$(git rev-parse HEAD~4) &&
366 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
367 EXPECT_HEADER_COUNT=4 \
368 git rebase -i $base &&
369 test $base = $(git rev-parse HEAD^) &&
370 test 1 = $(git show | grep ONCE | wc -l) &&
371 git checkout to-be-rebased &&
372 git branch -D skip-blank-lines
375 test_expect_success
'squash works as expected' '
376 git checkout -b squash-works no-conflict-branch &&
377 one=$(git rev-parse HEAD~3) &&
378 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
379 git rebase -i HEAD~3 &&
380 test $one = $(git rev-parse HEAD~2)
383 test_expect_success
'interrupted squash works as expected' '
384 git checkout -b interrupted-squash conflict-branch &&
385 one=$(git rev-parse HEAD~3) &&
387 FAKE_LINES="1 squash 3 2" &&
389 test_must_fail git rebase -i HEAD~3
391 (echo one; echo two; echo four) > conflict &&
393 test_must_fail git rebase --continue &&
394 echo resolved > conflict &&
396 git rebase --continue &&
397 test $one = $(git rev-parse HEAD~2)
400 test_expect_success
'interrupted squash works as expected (case 2)' '
401 git checkout -b interrupted-squash2 conflict-branch &&
402 one=$(git rev-parse HEAD~3) &&
404 FAKE_LINES="3 squash 1 2" &&
406 test_must_fail git rebase -i HEAD~3
408 (echo one; echo four) > conflict &&
410 test_must_fail git rebase --continue &&
411 (echo one; echo two; echo four) > conflict &&
413 test_must_fail git rebase --continue &&
414 echo resolved > conflict &&
416 git rebase --continue &&
417 test $one = $(git rev-parse HEAD~2)
420 test_expect_success
'ignore patch if in upstream' '
421 HEAD=$(git rev-parse HEAD) &&
422 git checkout -b has-cherry-picked HEAD^ &&
423 echo unrelated > file7 &&
426 git commit -m "unrelated change" &&
427 git cherry-pick $HEAD &&
428 EXPECT_COUNT=1 git rebase -i $HEAD &&
429 test $HEAD = $(git rev-parse HEAD^)
432 test_expect_success
'--continue tries to commit, even for "edit"' '
433 parent=$(git rev-parse HEAD^) &&
435 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
436 echo edited > file7 &&
438 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
439 test edited = $(git show HEAD:file7) &&
440 git show HEAD | grep chouette &&
441 test $parent = $(git rev-parse HEAD^)
444 test_expect_success
'aborted --continue does not squash commits after "edit"' '
445 old=$(git rev-parse HEAD) &&
447 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
448 echo "edited again" > file7 &&
451 FAKE_COMMIT_MESSAGE=" " &&
452 export FAKE_COMMIT_MESSAGE &&
453 test_must_fail git rebase --continue
455 test $old = $(git rev-parse HEAD) &&
459 test_expect_success
'auto-amend only edited commits after "edit"' '
461 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
462 echo "edited again" > file7 &&
464 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
465 echo "and again" > file7 &&
469 FAKE_COMMIT_MESSAGE="and again" &&
470 export FAKE_COMMIT_MESSAGE &&
471 test_must_fail git rebase --continue
476 test_expect_success
'rebase a detached HEAD' '
477 grandparent=$(git rev-parse HEAD~2) &&
478 git checkout $(git rev-parse HEAD) &&
480 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
481 test $grandparent = $(git rev-parse HEAD~2)
484 test_expect_success
'rebase a commit violating pre-commit' '
486 mkdir -p .git/hooks &&
487 PRE_COMMIT=.git/hooks/pre-commit &&
488 echo "#!/bin/sh" > $PRE_COMMIT &&
489 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
490 chmod a+x $PRE_COMMIT &&
491 echo "monde! " >> file1 &&
493 test_must_fail git commit -m doesnt-verify file1 &&
494 git commit -m doesnt-verify --no-verify file1 &&
496 FAKE_LINES=2 git rebase -i HEAD~2
500 test_expect_success
'rebase with a file named HEAD in worktree' '
504 git checkout -b branch3 A &&
507 GIT_AUTHOR_NAME="Squashed Away" &&
508 export GIT_AUTHOR_NAME &&
511 git commit -m "Add head" &&
514 git commit -m "Add body"
517 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
518 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
522 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
524 git checkout -b branch4 HEAD &&
525 GIT_EDITOR=: git commit --amend \
526 --author="Somebody else <somebody@else.com>"
527 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
528 git rebase -i branch3 &&
529 test $(git rev-parse branch3) = $(git rev-parse branch4)
533 test_expect_success
'submodule rebase setup' '
537 cd sub && git init && >elif &&
538 git add elif && git commit -m "submodule initial"
543 git commit -m "One" &&
546 git commit -a -m "Two" &&
548 cd sub && echo 3 >elif &&
549 git commit -a -m "submodule second"
552 git commit -a -m "Three changes submodule"
555 test_expect_success
'submodule rebase -i' '
556 FAKE_LINES="1 squash 2 3" git rebase -i A
559 test_expect_success
'avoid unnecessary reset' '
560 git checkout master &&
561 test-chmtime =123456789 file3 &&
562 git update-index --refresh &&
563 HEAD=$(git rev-parse HEAD) &&
564 git rebase -i HEAD~4 &&
565 test $HEAD = $(git rev-parse HEAD) &&
566 MTIME=$(test-chmtime -v +0 file3 | sed 's
/[^
0-9].
*$
//') &&
567 test 123456789 = $MTIME
570 test_expect_success
'reword' '
571 git checkout -b reword-branch master &&
572 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
573 git show HEAD | grep "E changed" &&
574 test $(git rev-parse master) != $(git rev-parse HEAD) &&
575 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
576 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
577 git show HEAD^ | grep "D changed" &&
578 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
579 git show HEAD~3 | grep "B changed" &&
580 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
581 git show HEAD~2 | grep "C changed"
584 test_expect_success
'rebase -i can copy notes' '
585 git config notes.rewrite.rebase true &&
586 git config notes.rewriteRef "refs/notes/*" &&
590 git notes add -m"a note" n3 &&
591 git rebase --onto n1 n2 &&
592 test "a note" = "$(git notes show HEAD)"
600 test_expect_success
'rebase -i can copy notes over a fixup' '
601 git reset --hard n3 &&
602 git notes add -m"an earlier note" n2 &&
603 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
604 git notes show > output &&
605 test_cmp expect output
608 test_expect_success
'rebase while detaching HEAD' '
609 git symbolic-ref HEAD &&
610 grandparent=$(git rev-parse HEAD~2) &&
612 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
613 test $grandparent = $(git rev-parse HEAD~2) &&
614 test_must_fail git symbolic-ref HEAD
617 test_tick
# Ensure that the rebased commits get a different timestamp.
618 test_expect_success
'always cherry-pick with --no-ff' '
619 git checkout no-ff-branch &&
620 git tag original-no-ff-branch &&
621 git rebase -i --no-ff A &&
625 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
626 git diff HEAD~$p original-no-ff-branch~$p > out &&
629 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
630 git diff HEAD~3 original-no-ff-branch~3 > out &&
634 test_expect_success
'set up commits with funny messages' '
635 git checkout -b funny A &&
638 git commit -a -m "end with slash\\" &&
641 git commit -a -m "another commit"
644 test_expect_success
'rebase-i history with funny messages' '
645 git rev-list A..funny >expect &&
647 FAKE_LINES="1 2" git rebase -i A &&
648 git rev-list A.. >actual &&
649 test_cmp expect actual