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_success 'preserve merges with -p' '
195 git checkout -b to-be-preserved master^ &&
196 : > unrelated-file &&
197 git add unrelated-file &&
199 git commit -m "unrelated" &&
200 git checkout -b another-branch master &&
203 git commit -m J file1 &&
205 git merge to-be-preserved &&
208 git commit -m K file1 &&
211 git commit -m L1 file1 &&
212 git checkout HEAD^ &&
213 echo 1 > unrelated-file &&
215 git commit -m L2 unrelated-file &&
217 git merge another-branch &&
220 git commit -m M file1 &&
221 git checkout -b to-be-rebased &&
223 git rebase -i -p --onto branch1 master &&
224 git update-index --refresh &&
225 git diff-files --quiet &&
226 git diff-index --quiet --cached HEAD -- &&
227 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
228 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
229 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
230 test $(git show HEAD~5:file1) = B &&
231 test $(git show HEAD~3:file1) = C &&
232 test $(git show HEAD:file1) = E &&
233 test $(git show HEAD:unrelated-file) = 1
236 test_expect_success 'edit ancestor with -p' '
237 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
238 echo 2 > unrelated-file &&
240 git commit -m L2-modified --amend unrelated-file &&
241 git rebase --continue &&
242 git update-index --refresh &&
243 git diff-files --quiet &&
244 git diff-index --quiet --cached HEAD -- &&
245 test $(git show HEAD:unrelated-file) = 2
248 test_expect_success '--continue tries to commit' '
250 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
251 echo resolved > file1 &&
253 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
254 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
255 git show HEAD | grep chouette
258 test_expect_success 'verbose flag is heeded, even after --continue' '
259 git reset --hard HEAD@{1} &&
261 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
262 echo resolved > file1 &&
264 git rebase --continue > output &&
265 grep "^ file1 | 2 +-$" output
268 test_expect_success 'multi-squash only fires up editor once' '
269 base=$(git rev-parse HEAD~4) &&
270 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
271 EXPECT_HEADER_COUNT=4 \
272 git rebase -i $base &&
273 test $base = $(git rev-parse HEAD^) &&
274 test 1 = $(git show | grep ONCE | wc -l)
277 test_expect_success 'multi-fixup does not fire up editor' '
278 git checkout -b multi-fixup E &&
279 base=$(git rev-parse HEAD~4) &&
280 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
281 git rebase -i $base &&
282 test $base = $(git rev-parse HEAD^) &&
283 test 0 = $(git show | grep NEVER | wc -l) &&
284 git checkout to-be-rebased &&
285 git branch -D multi-fixup
288 test_expect_success 'commit message used after conflict' '
289 git checkout -b conflict-fixup conflict-branch &&
290 base=$(git rev-parse HEAD~4) &&
292 FAKE_LINES="1 fixup 3 fixup 4" &&
294 test_must_fail git rebase -i $base
296 echo three > conflict &&
298 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
299 git rebase --continue &&
300 test $base = $(git rev-parse HEAD^) &&
301 test 1 = $(git show | grep ONCE | wc -l) &&
302 git checkout to-be-rebased &&
303 git branch -D conflict-fixup
306 test_expect_success 'commit message retained after conflict' '
307 git checkout -b conflict-squash conflict-branch &&
308 base=$(git rev-parse HEAD~4) &&
310 FAKE_LINES="1 fixup 3 squash 4" &&
312 test_must_fail git rebase -i $base
314 echo three > conflict &&
316 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
317 git rebase --continue &&
318 test $base = $(git rev-parse HEAD^) &&
319 test 2 = $(git show | grep TWICE | wc -l) &&
320 git checkout to-be-rebased &&
321 git branch -D conflict-squash
324 cat > expect-squash-fixup << EOF
332 test_expect_success 'squash and fixup generate correct log messages' '
333 git checkout -b squash-fixup E &&
334 base=$(git rev-parse HEAD~4) &&
335 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
336 EXPECT_HEADER_COUNT=4 \
337 git rebase -i $base &&
338 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
339 test_cmp expect-squash-fixup actual-squash-fixup &&
340 git checkout to-be-rebased &&
341 git branch -D squash-fixup
344 test_expect_success 'squash ignores comments' '
345 git checkout -b skip-comments E &&
346 base=$(git rev-parse HEAD~4) &&
347 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
348 EXPECT_HEADER_COUNT=4 \
349 git rebase -i $base &&
350 test $base = $(git rev-parse HEAD^) &&
351 test 1 = $(git show | grep ONCE | wc -l) &&
352 git checkout to-be-rebased &&
353 git branch -D skip-comments
356 test_expect_success 'squash ignores blank lines' '
357 git checkout -b skip-blank-lines E &&
358 base=$(git rev-parse HEAD~4) &&
359 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
360 EXPECT_HEADER_COUNT=4 \
361 git rebase -i $base &&
362 test $base = $(git rev-parse HEAD^) &&
363 test 1 = $(git show | grep ONCE | wc -l) &&
364 git checkout to-be-rebased &&
365 git branch -D skip-blank-lines
368 test_expect_success 'squash works as expected' '
369 git checkout -b squash-works no-conflict-branch &&
370 one=$(git rev-parse HEAD~3) &&
371 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
372 git rebase -i HEAD~3 &&
373 test $one = $(git rev-parse HEAD~2)
376 test_expect_success 'interrupted squash works as expected' '
377 git checkout -b interrupted-squash conflict-branch &&
378 one=$(git rev-parse HEAD~3) &&
380 FAKE_LINES="1 squash 3 2" &&
382 test_must_fail git rebase -i HEAD~3
384 (echo one; echo two; echo four) > conflict &&
386 test_must_fail git rebase --continue &&
387 echo resolved > conflict &&
389 git rebase --continue &&
390 test $one = $(git rev-parse HEAD~2)
393 test_expect_success 'interrupted squash works as expected (case 2)' '
394 git checkout -b interrupted-squash2 conflict-branch &&
395 one=$(git rev-parse HEAD~3) &&
397 FAKE_LINES="3 squash 1 2" &&
399 test_must_fail git rebase -i HEAD~3
401 (echo one; echo four) > conflict &&
403 test_must_fail git rebase --continue &&
404 (echo one; echo two; echo four) > conflict &&
406 test_must_fail git rebase --continue &&
407 echo resolved > conflict &&
409 git rebase --continue &&
410 test $one = $(git rev-parse HEAD~2)
413 test_expect_success 'ignore patch if in upstream' '
414 HEAD=$(git rev-parse HEAD) &&
415 git checkout -b has-cherry-picked HEAD^ &&
416 echo unrelated > file7 &&
419 git commit -m "unrelated change" &&
420 git cherry-pick $HEAD &&
421 EXPECT_COUNT=1 git rebase -i $HEAD &&
422 test $HEAD = $(git rev-parse HEAD^)
425 test_expect_success '--continue tries to commit, even for "edit"' '
426 parent=$(git rev-parse HEAD^) &&
428 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
429 echo edited > file7 &&
431 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
432 test edited = $(git show HEAD:file7) &&
433 git show HEAD | grep chouette &&
434 test $parent = $(git rev-parse HEAD^)
437 test_expect_success 'aborted --continue does not squash commits after "edit"' '
438 old=$(git rev-parse HEAD) &&
440 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
441 echo "edited again" > file7 &&
444 FAKE_COMMIT_MESSAGE=" " &&
445 export FAKE_COMMIT_MESSAGE &&
446 test_must_fail git rebase --continue
448 test $old = $(git rev-parse HEAD) &&
452 test_expect_success 'auto-amend only edited commits after "edit"' '
454 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
455 echo "edited again" > file7 &&
457 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
458 echo "and again" > file7 &&
462 FAKE_COMMIT_MESSAGE="and again" &&
463 export FAKE_COMMIT_MESSAGE &&
464 test_must_fail git rebase --continue
469 test_expect_success 'rebase a detached HEAD' '
470 grandparent=$(git rev-parse HEAD~2) &&
471 git checkout $(git rev-parse HEAD) &&
473 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
474 test $grandparent = $(git rev-parse HEAD~2)
477 test_expect_success 'rebase a commit violating pre-commit' '
479 mkdir -p .git/hooks &&
480 PRE_COMMIT=.git/hooks/pre-commit &&
481 echo "#!/bin/sh" > $PRE_COMMIT &&
482 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
483 chmod a+x $PRE_COMMIT &&
484 echo "monde! " >> file1 &&
486 test_must_fail git commit -m doesnt-verify file1 &&
487 git commit -m doesnt-verify --no-verify file1 &&
489 FAKE_LINES=2 git rebase -i HEAD~2
493 test_expect_success 'rebase with a file named HEAD in worktree' '
497 git checkout -b branch3 A &&
500 GIT_AUTHOR_NAME="Squashed Away" &&
501 export GIT_AUTHOR_NAME &&
504 git commit -m "Add head" &&
507 git commit -m "Add body"
510 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
511 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
515 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
517 git checkout -b branch4 HEAD &&
518 GIT_EDITOR=: git commit --amend \
519 --author="Somebody else <somebody@else.com>"
520 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
521 git rebase -i branch3 &&
522 test $(git rev-parse branch3) = $(git rev-parse branch4)
526 test_expect_success 'submodule rebase setup' '
530 cd sub && git init && >elif &&
531 git add elif && git commit -m "submodule initial"
536 git commit -m "One" &&
539 git commit -a -m "Two" &&
541 cd sub && echo 3 >elif &&
542 git commit -a -m "submodule second"
545 git commit -a -m "Three changes submodule"
548 test_expect_success 'submodule rebase -i' '
549 FAKE_LINES="1 squash 2 3" git rebase -i A
552 test_expect_success 'avoid unnecessary reset' '
553 git checkout master &&
554 test-chmtime =123456789 file3 &&
555 git update-index --refresh &&
556 HEAD=$(git rev-parse HEAD) &&
557 git rebase -i HEAD~4 &&
558 test $HEAD = $(git rev-parse HEAD) &&
559 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
560 test 123456789 = $MTIME
563 test_expect_success 'reword' '
564 git checkout -b reword-branch master &&
565 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
566 git show HEAD | grep "E changed" &&
567 test $(git rev-parse master) != $(git rev-parse HEAD) &&
568 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
569 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
570 git show HEAD^ | grep "D changed" &&
571 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
572 git show HEAD~3 | grep "B changed" &&
573 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
574 git show HEAD~2 | grep "C changed"
577 test_expect_success 'rebase -i can copy notes' '
578 git config notes.rewrite.rebase true &&
579 git config notes.rewriteRef "refs/notes/*" &&
583 git notes add -m"a note" n3 &&
584 git rebase --onto n1 n2 &&
585 test "a note" = "$(git notes show HEAD)"
593 test_expect_success 'rebase -i can copy notes over a fixup' '
594 git reset --hard n3 &&
595 git notes add -m"an earlier note" n2 &&
596 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
597 git notes show > output &&
598 test_cmp expect output
601 test_expect_success 'rebase while detaching HEAD' '
602 git symbolic-ref HEAD &&
603 grandparent=$(git rev-parse HEAD~2) &&
605 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
606 test $grandparent = $(git rev-parse HEAD~2) &&
607 test_must_fail git symbolic-ref HEAD
610 test_tick # Ensure that the rebased commits get a different timestamp.
611 test_expect_success 'always cherry-pick with --no-ff' '
612 git checkout no-ff-branch &&
613 git tag original-no-ff-branch &&
614 git rebase -i --no-ff A &&
618 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
619 git diff HEAD~$p original-no-ff-branch~$p > out &&
622 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
623 git diff HEAD~3 original-no-ff-branch~3 > out &&