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 'retain authorship' '
153 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
155 git rebase -i --onto master HEAD^ &&
156 git show HEAD | grep "^Author: Twerp Snog"
159 test_expect_success 'squash' '
160 git reset --hard twerp &&
163 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
164 echo "******************************" &&
165 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
166 git rebase -i --onto master HEAD~2 &&
167 test B = $(cat file7) &&
168 test $(git rev-parse HEAD^) = $(git rev-parse master)
171 test_expect_success 'retain authorship when squashing' '
172 git show HEAD | grep "^Author: Twerp Snog"
175 test_expect_success '-p handles "no changes" gracefully' '
176 HEAD=$(git rev-parse HEAD) &&
177 git rebase -i -p HEAD^ &&
178 git update-index --refresh &&
179 git diff-files --quiet &&
180 git diff-index --quiet --cached HEAD -- &&
181 test $HEAD = $(git rev-parse HEAD)
184 test_expect_success 'preserve merges with -p' '
185 git checkout -b to-be-preserved master^ &&
186 : > unrelated-file &&
187 git add unrelated-file &&
189 git commit -m "unrelated" &&
190 git checkout -b another-branch master &&
193 git commit -m J file1 &&
195 git merge to-be-preserved &&
198 git commit -m K file1 &&
201 git commit -m L1 file1 &&
202 git checkout HEAD^ &&
203 echo 1 > unrelated-file &&
205 git commit -m L2 unrelated-file &&
207 git merge another-branch &&
210 git commit -m M file1 &&
211 git checkout -b to-be-rebased &&
213 git rebase -i -p --onto branch1 master &&
214 git update-index --refresh &&
215 git diff-files --quiet &&
216 git diff-index --quiet --cached HEAD -- &&
217 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
218 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
219 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
220 test $(git show HEAD~5:file1) = B &&
221 test $(git show HEAD~3:file1) = C &&
222 test $(git show HEAD:file1) = E &&
223 test $(git show HEAD:unrelated-file) = 1
226 test_expect_success 'edit ancestor with -p' '
227 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
228 echo 2 > unrelated-file &&
230 git commit -m L2-modified --amend unrelated-file &&
231 git rebase --continue &&
232 git update-index --refresh &&
233 git diff-files --quiet &&
234 git diff-index --quiet --cached HEAD -- &&
235 test $(git show HEAD:unrelated-file) = 2
238 test_expect_success '--continue tries to commit' '
240 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
241 echo resolved > file1 &&
243 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
244 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
245 git show HEAD | grep chouette
248 test_expect_success 'verbose flag is heeded, even after --continue' '
249 git reset --hard HEAD@{1} &&
251 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
252 echo resolved > file1 &&
254 git rebase --continue > output &&
255 grep "^ file1 | 2 +-$" output
258 test_expect_success 'multi-squash only fires up editor once' '
259 base=$(git rev-parse HEAD~4) &&
260 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
261 EXPECT_HEADER_COUNT=4 \
262 git rebase -i $base &&
263 test $base = $(git rev-parse HEAD^) &&
264 test 1 = $(git show | grep ONCE | wc -l)
267 test_expect_success 'multi-fixup does not fire up editor' '
268 git checkout -b multi-fixup E &&
269 base=$(git rev-parse HEAD~4) &&
270 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
271 git rebase -i $base &&
272 test $base = $(git rev-parse HEAD^) &&
273 test 0 = $(git show | grep NEVER | wc -l) &&
274 git checkout to-be-rebased &&
275 git branch -D multi-fixup
278 test_expect_success 'commit message used after conflict' '
279 git checkout -b conflict-fixup conflict-branch &&
280 base=$(git rev-parse HEAD~4) &&
282 FAKE_LINES="1 fixup 3 fixup 4" &&
284 test_must_fail git rebase -i $base
286 echo three > conflict &&
288 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
289 git rebase --continue &&
290 test $base = $(git rev-parse HEAD^) &&
291 test 1 = $(git show | grep ONCE | wc -l) &&
292 git checkout to-be-rebased &&
293 git branch -D conflict-fixup
296 test_expect_success 'commit message retained after conflict' '
297 git checkout -b conflict-squash conflict-branch &&
298 base=$(git rev-parse HEAD~4) &&
300 FAKE_LINES="1 fixup 3 squash 4" &&
302 test_must_fail git rebase -i $base
304 echo three > conflict &&
306 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
307 git rebase --continue &&
308 test $base = $(git rev-parse HEAD^) &&
309 test 2 = $(git show | grep TWICE | wc -l) &&
310 git checkout to-be-rebased &&
311 git branch -D conflict-squash
314 cat > expect-squash-fixup << EOF
322 test_expect_success 'squash and fixup generate correct log messages' '
323 git checkout -b squash-fixup E &&
324 base=$(git rev-parse HEAD~4) &&
325 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
326 EXPECT_HEADER_COUNT=4 \
327 git rebase -i $base &&
328 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
329 test_cmp expect-squash-fixup actual-squash-fixup &&
330 git checkout to-be-rebased &&
331 git branch -D squash-fixup
334 test_expect_success 'squash ignores comments' '
335 git checkout -b skip-comments E &&
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) &&
342 git checkout to-be-rebased &&
343 git branch -D skip-comments
346 test_expect_success 'squash ignores blank lines' '
347 git checkout -b skip-blank-lines E &&
348 base=$(git rev-parse HEAD~4) &&
349 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
350 EXPECT_HEADER_COUNT=4 \
351 git rebase -i $base &&
352 test $base = $(git rev-parse HEAD^) &&
353 test 1 = $(git show | grep ONCE | wc -l) &&
354 git checkout to-be-rebased &&
355 git branch -D skip-blank-lines
358 test_expect_success 'squash works as expected' '
359 git checkout -b squash-works no-conflict-branch &&
360 one=$(git rev-parse HEAD~3) &&
361 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
362 git rebase -i HEAD~3 &&
363 test $one = $(git rev-parse HEAD~2)
366 test_expect_success 'interrupted squash works as expected' '
367 git checkout -b interrupted-squash conflict-branch &&
368 one=$(git rev-parse HEAD~3) &&
370 FAKE_LINES="1 squash 3 2" &&
372 test_must_fail git rebase -i HEAD~3
374 (echo one; echo two; echo four) > conflict &&
376 test_must_fail git rebase --continue &&
377 echo resolved > conflict &&
379 git rebase --continue &&
380 test $one = $(git rev-parse HEAD~2)
383 test_expect_success 'interrupted squash works as expected (case 2)' '
384 git checkout -b interrupted-squash2 conflict-branch &&
385 one=$(git rev-parse HEAD~3) &&
387 FAKE_LINES="3 squash 1 2" &&
389 test_must_fail git rebase -i HEAD~3
391 (echo one; echo four) > conflict &&
393 test_must_fail git rebase --continue &&
394 (echo one; echo two; echo four) > conflict &&
396 test_must_fail git rebase --continue &&
397 echo resolved > conflict &&
399 git rebase --continue &&
400 test $one = $(git rev-parse HEAD~2)
403 test_expect_success 'ignore patch if in upstream' '
404 HEAD=$(git rev-parse HEAD) &&
405 git checkout -b has-cherry-picked HEAD^ &&
406 echo unrelated > file7 &&
409 git commit -m "unrelated change" &&
410 git cherry-pick $HEAD &&
411 EXPECT_COUNT=1 git rebase -i $HEAD &&
412 test $HEAD = $(git rev-parse HEAD^)
415 test_expect_success '--continue tries to commit, even for "edit"' '
416 parent=$(git rev-parse HEAD^) &&
418 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
419 echo edited > file7 &&
421 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
422 test edited = $(git show HEAD:file7) &&
423 git show HEAD | grep chouette &&
424 test $parent = $(git rev-parse HEAD^)
427 test_expect_success 'aborted --continue does not squash commits after "edit"' '
428 old=$(git rev-parse HEAD) &&
430 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
431 echo "edited again" > file7 &&
434 FAKE_COMMIT_MESSAGE=" " &&
435 export FAKE_COMMIT_MESSAGE &&
436 test_must_fail git rebase --continue
438 test $old = $(git rev-parse HEAD) &&
442 test_expect_success 'auto-amend only edited commits after "edit"' '
444 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
445 echo "edited again" > file7 &&
447 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
448 echo "and again" > file7 &&
452 FAKE_COMMIT_MESSAGE="and again" &&
453 export FAKE_COMMIT_MESSAGE &&
454 test_must_fail git rebase --continue
459 test_expect_success 'rebase a detached HEAD' '
460 grandparent=$(git rev-parse HEAD~2) &&
461 git checkout $(git rev-parse HEAD) &&
463 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
464 test $grandparent = $(git rev-parse HEAD~2)
467 test_expect_success 'rebase a commit violating pre-commit' '
469 mkdir -p .git/hooks &&
470 PRE_COMMIT=.git/hooks/pre-commit &&
471 echo "#!/bin/sh" > $PRE_COMMIT &&
472 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
473 chmod a+x $PRE_COMMIT &&
474 echo "monde! " >> file1 &&
476 test_must_fail git commit -m doesnt-verify file1 &&
477 git commit -m doesnt-verify --no-verify file1 &&
479 FAKE_LINES=2 git rebase -i HEAD~2
483 test_expect_success 'rebase with a file named HEAD in worktree' '
487 git checkout -b branch3 A &&
490 GIT_AUTHOR_NAME="Squashed Away" &&
491 export GIT_AUTHOR_NAME &&
494 git commit -m "Add head" &&
497 git commit -m "Add body"
500 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
501 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
505 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
507 git checkout -b branch4 HEAD &&
508 GIT_EDITOR=: git commit --amend \
509 --author="Somebody else <somebody@else.com>"
510 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
511 git rebase -i branch3 &&
512 test $(git rev-parse branch3) = $(git rev-parse branch4)
516 test_expect_success 'submodule rebase setup' '
520 cd sub && git init && >elif &&
521 git add elif && git commit -m "submodule initial"
526 git commit -m "One" &&
529 git commit -a -m "Two" &&
531 cd sub && echo 3 >elif &&
532 git commit -a -m "submodule second"
535 git commit -a -m "Three changes submodule"
538 test_expect_success 'submodule rebase -i' '
539 FAKE_LINES="1 squash 2 3" git rebase -i A
542 test_expect_success 'avoid unnecessary reset' '
543 git checkout master &&
544 test-chmtime =123456789 file3 &&
545 git update-index --refresh &&
546 HEAD=$(git rev-parse HEAD) &&
547 git rebase -i HEAD~4 &&
548 test $HEAD = $(git rev-parse HEAD) &&
549 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
550 test 123456789 = $MTIME
553 test_expect_success 'reword' '
554 git checkout -b reword-branch master &&
555 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
556 git show HEAD | grep "E changed" &&
557 test $(git rev-parse master) != $(git rev-parse HEAD) &&
558 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
559 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
560 git show HEAD^ | grep "D changed" &&
561 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
562 git show HEAD~3 | grep "B changed" &&
563 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
564 git show HEAD~2 | grep "C changed"
567 test_expect_success 'rebase -i can copy notes' '
568 git config notes.rewrite.rebase true &&
569 git config notes.rewriteRef "refs/notes/*" &&
573 git notes add -m"a note" n3 &&
574 git rebase --onto n1 n2 &&
575 test "a note" = "$(git notes show HEAD)"
583 test_expect_success 'rebase -i can copy notes over a fixup' '
584 git reset --hard n3 &&
585 git notes add -m"an earlier note" n2 &&
586 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
587 git notes show > output &&
588 test_cmp expect output
591 test_expect_success 'rebase while detaching HEAD' '
592 git symbolic-ref HEAD &&
593 grandparent=$(git rev-parse HEAD~2) &&
595 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
596 test $grandparent = $(git rev-parse HEAD~2) &&
597 test_must_fail git symbolic-ref HEAD
600 test_tick # Ensure that the rebased commits get a different timestamp.
601 test_expect_success 'always cherry-pick with --no-ff' '
602 git checkout no-ff-branch &&
603 git tag original-no-ff-branch &&
604 git rebase -i --no-ff A &&
608 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
609 git diff HEAD~$p original-no-ff-branch~$p > out &&
612 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
613 git diff HEAD~3 original-no-ff-branch~3 > out &&