Merge branch 'ar/submodule-add-config'
[git/debian.git] / t / t3404-rebase-interactive.sh
blobd877872e8f4ad15f3af4ad8e2bf22948fae34a6c
1 #!/bin/sh
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.
11 Initial setup:
13 one - two - three - four (conflict-branch)
15 A - B - C - D - E (primary)
16 | \
17 | F - G - H (branch1)
18 | \
19 |\ I (branch2)
20 | \
21 | J - K - L - M (no-conflict-branch)
23 N - O - P (no-ff-branch)
25 where A, B, D and G all touch file1, and one, two, three, four all
26 touch file "conflict".
28 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
29 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
31 . ./test-lib.sh
33 . "$TEST_DIRECTORY"/lib-rebase.sh
35 test_expect_success 'setup' '
36 git switch -C primary &&
37 test_commit A file1 &&
38 test_commit B file1 &&
39 test_commit C file2 &&
40 test_commit D file1 &&
41 test_commit E file3 &&
42 git checkout -b branch1 A &&
43 test_commit F file4 &&
44 test_commit G file1 &&
45 test_commit H file5 &&
46 git checkout -b branch2 F &&
47 test_commit I file6 &&
48 git checkout -b conflict-branch A &&
49 test_commit one conflict &&
50 test_commit two conflict &&
51 test_commit three conflict &&
52 test_commit four conflict &&
53 git checkout -b no-conflict-branch A &&
54 test_commit J fileJ &&
55 test_commit K fileK &&
56 test_commit L fileL &&
57 test_commit M fileM &&
58 git checkout -b no-ff-branch A &&
59 test_commit N fileN &&
60 test_commit O fileO &&
61 test_commit P fileP
64 # "exec" commands are run with the user shell by default, but this may
65 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
66 # to create a file. Unsetting SHELL avoids such non-portable behavior
67 # in tests. It must be exported for it to take effect where needed.
68 SHELL=
69 export SHELL
71 test_expect_success 'rebase --keep-empty' '
72 git checkout -b emptybranch primary &&
73 git commit --allow-empty -m "empty" &&
74 git rebase --keep-empty -i HEAD~2 &&
75 git log --oneline >actual &&
76 test_line_count = 6 actual
79 test_expect_success 'rebase -i with empty todo list' '
80 cat >expect <<-\EOF &&
81 error: nothing to do
82 EOF
84 set_fake_editor &&
85 test_must_fail env FAKE_LINES="#" \
86 git rebase -i HEAD^ >output 2>&1
87 ) &&
88 tail -n 1 output >actual && # Ignore output about changing todo list
89 test_cmp expect actual
92 test_expect_success 'rebase -i with the exec command' '
93 git checkout primary &&
95 set_fake_editor &&
96 FAKE_LINES="1 exec_>touch-one
97 2 exec_>touch-two exec_false exec_>touch-three
98 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
99 export FAKE_LINES &&
100 test_must_fail git rebase -i A
101 ) &&
102 test_path_is_file touch-one &&
103 test_path_is_file touch-two &&
104 # Missing because we should have stopped by now.
105 test_path_is_missing touch-three &&
106 test_cmp_rev C HEAD &&
107 git rebase --continue &&
108 test_path_is_file touch-three &&
109 test_path_is_file "touch-file name with spaces" &&
110 test_path_is_file touch-after-semicolon &&
111 test_cmp_rev primary HEAD &&
112 rm -f touch-*
115 test_expect_success 'rebase -i with the exec command runs from tree root' '
116 git checkout primary &&
117 mkdir subdir && (cd subdir &&
118 set_fake_editor &&
119 FAKE_LINES="1 exec_>touch-subdir" \
120 git rebase -i HEAD^
121 ) &&
122 test_path_is_file touch-subdir &&
123 rm -fr subdir
126 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
127 test_when_finished "rm -rf subdir" &&
128 test_when_finished "git rebase --abort ||:" &&
129 git checkout primary &&
130 mkdir subdir && (cd subdir &&
131 set_fake_editor &&
132 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
133 git rebase -i HEAD^
137 test_expect_success 'rebase -i sets work tree properly' '
138 test_when_finished "rm -rf subdir" &&
139 test_when_finished "test_might_fail git rebase --abort" &&
140 mkdir subdir &&
141 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
142 >actual &&
143 ! grep "/subdir$" actual
146 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
147 git checkout primary &&
149 set_fake_editor &&
150 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
151 git rebase -i HEAD^
152 ) &&
153 test_cmp_rev primary^ HEAD &&
154 git reset --hard &&
155 git rebase --continue
158 test_expect_success 'rebase -x with empty command fails' '
159 test_when_finished "git rebase --abort ||:" &&
160 test_must_fail env git rebase -x "" @ 2>actual &&
161 test_write_lines "error: empty exec command" >expected &&
162 test_cmp expected actual &&
163 test_must_fail env git rebase -x " " @ 2>actual &&
164 test_cmp expected actual
167 test_expect_success 'rebase -x with newline in command fails' '
168 test_when_finished "git rebase --abort ||:" &&
169 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
170 test_write_lines "error: exec commands cannot contain newlines" \
171 >expected &&
172 test_cmp expected actual
175 test_expect_success 'rebase -i with exec of inexistent command' '
176 git checkout primary &&
177 test_when_finished "git rebase --abort" &&
179 set_fake_editor &&
180 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
181 git rebase -i HEAD^ >actual 2>&1
182 ) &&
183 ! grep "Maybe git-rebase is broken" actual
186 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
187 test_when_finished "git rebase --abort ||:" &&
188 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
191 test_expect_success 'no changes are a nop' '
192 git checkout branch2 &&
193 git rebase -i F &&
194 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
195 test_cmp_rev I HEAD
198 test_expect_success 'test the [branch] option' '
199 git checkout -b dead-end &&
200 git rm file6 &&
201 git commit -m "stop here" &&
202 git rebase -i F branch2 &&
203 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
204 test_cmp_rev I branch2 &&
205 test_cmp_rev I HEAD
208 test_expect_success 'test --onto <branch>' '
209 git checkout -b test-onto branch2 &&
210 git rebase -i --onto branch1 F &&
211 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
212 test_cmp_rev HEAD^ branch1 &&
213 test_cmp_rev I branch2
216 test_expect_success 'rebase on top of a non-conflicting commit' '
217 git checkout branch1 &&
218 git tag original-branch1 &&
219 git rebase -i branch2 &&
220 test file6 = $(git diff --name-only original-branch1) &&
221 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
222 test_cmp_rev I branch2 &&
223 test_cmp_rev I HEAD~2
226 test_expect_success 'reflog for the branch shows state before rebase' '
227 test_cmp_rev branch1@{1} original-branch1
230 test_expect_success 'reflog for the branch shows correct finish message' '
231 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
232 "$(git rev-parse branch2)" >expected &&
233 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
234 test_cmp expected actual
237 test_expect_success 'exchange two commits' '
239 set_fake_editor &&
240 FAKE_LINES="2 1" git rebase -i HEAD~2
241 ) &&
242 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
243 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
244 blob1=$(git rev-parse --short HEAD^:file1) &&
245 blob2=$(git rev-parse --short HEAD:file1) &&
246 commit=$(git rev-parse --short HEAD)
249 test_expect_success 'stop on conflicting pick' '
250 cat >expect <<-EOF &&
251 diff --git a/file1 b/file1
252 index $blob1..$blob2 100644
253 --- a/file1
254 +++ b/file1
255 @@ -1 +1 @@
259 cat >expect2 <<-EOF &&
260 <<<<<<< HEAD
262 =======
264 >>>>>>> $commit (G)
266 git tag new-branch1 &&
267 test_must_fail git rebase -i primary &&
268 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
269 test_cmp expect .git/rebase-merge/patch &&
270 test_cmp expect2 file1 &&
271 test "$(git diff --name-status |
272 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
273 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
274 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
277 test_expect_success 'show conflicted patch' '
278 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
279 grep "show.*REBASE_HEAD" stderr &&
280 # the original stopped-sha1 is abbreviated
281 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
282 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
285 test_expect_success 'abort' '
286 git rebase --abort &&
287 test_cmp_rev new-branch1 HEAD &&
288 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
289 test_path_is_missing .git/rebase-merge
292 test_expect_success 'abort with error when new base cannot be checked out' '
293 git rm --cached file1 &&
294 git commit -m "remove file in base" &&
295 test_must_fail git rebase -i primary > output 2>&1 &&
296 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
297 output &&
298 test_i18ngrep "file1" output &&
299 test_path_is_missing .git/rebase-merge &&
300 git reset --hard HEAD^
303 test_expect_success 'retain authorship' '
304 echo A > file7 &&
305 git add file7 &&
306 test_tick &&
307 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
308 git tag twerp &&
309 git rebase -i --onto primary HEAD^ &&
310 git show HEAD | grep "^Author: Twerp Snog"
313 test_expect_success 'retain authorship w/ conflicts' '
314 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
315 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
317 git reset --hard twerp &&
318 test_commit a conflict a conflict-a &&
319 git reset --hard twerp &&
321 GIT_AUTHOR_NAME=AttributeMe &&
322 export GIT_AUTHOR_NAME &&
323 test_commit b conflict b conflict-b &&
324 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
326 test_must_fail git rebase -i conflict-a &&
327 echo resolved >conflict &&
328 git add conflict &&
329 git rebase --continue &&
330 test_cmp_rev conflict-a^0 HEAD^ &&
331 git show >out &&
332 grep AttributeMe out
335 test_expect_success 'squash' '
336 git reset --hard twerp &&
337 echo B > file7 &&
338 test_tick &&
339 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
340 echo "******************************" &&
342 set_fake_editor &&
343 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
344 git rebase -i --onto primary HEAD~2
345 ) &&
346 test B = $(cat file7) &&
347 test_cmp_rev HEAD^ primary
350 test_expect_success 'retain authorship when squashing' '
351 git show HEAD | grep "^Author: Twerp Snog"
354 test_expect_success REBASE_P '-p handles "no changes" gracefully' '
355 HEAD=$(git rev-parse HEAD) &&
356 git rebase -i -p HEAD^ &&
357 git update-index --refresh &&
358 git diff-files --quiet &&
359 git diff-index --quiet --cached HEAD -- &&
360 test $HEAD = $(git rev-parse HEAD)
363 test_expect_failure REBASE_P 'exchange two commits with -p' '
364 git checkout H &&
366 set_fake_editor &&
367 FAKE_LINES="2 1" git rebase -i -p HEAD~2
368 ) &&
369 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
370 test G = $(git cat-file commit HEAD | sed -ne \$p)
373 test_expect_success REBASE_P 'preserve merges with -p' '
374 git checkout -b to-be-preserved primary^ &&
375 : > unrelated-file &&
376 git add unrelated-file &&
377 test_tick &&
378 git commit -m "unrelated" &&
379 git checkout -b another-branch primary &&
380 echo B > file1 &&
381 test_tick &&
382 git commit -m J file1 &&
383 test_tick &&
384 git merge to-be-preserved &&
385 echo C > file1 &&
386 test_tick &&
387 git commit -m K file1 &&
388 echo D > file1 &&
389 test_tick &&
390 git commit -m L1 file1 &&
391 git checkout HEAD^ &&
392 echo 1 > unrelated-file &&
393 test_tick &&
394 git commit -m L2 unrelated-file &&
395 test_tick &&
396 git merge another-branch &&
397 echo E > file1 &&
398 test_tick &&
399 git commit -m M file1 &&
400 git checkout -b to-be-rebased &&
401 test_tick &&
402 git rebase -i -p --onto branch1 primary &&
403 git update-index --refresh &&
404 git diff-files --quiet &&
405 git diff-index --quiet --cached HEAD -- &&
406 test_cmp_rev HEAD~6 branch1 &&
407 test_cmp_rev HEAD~4^2 to-be-preserved &&
408 test_cmp_rev HEAD^^2^ HEAD^^^ &&
409 test $(git show HEAD~5:file1) = B &&
410 test $(git show HEAD~3:file1) = C &&
411 test $(git show HEAD:file1) = E &&
412 test $(git show HEAD:unrelated-file) = 1
415 test_expect_success REBASE_P 'edit ancestor with -p' '
417 set_fake_editor &&
418 FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3
419 ) &&
420 echo 2 > unrelated-file &&
421 test_tick &&
422 git commit -m L2-modified --amend unrelated-file &&
423 git rebase --continue &&
424 git update-index --refresh &&
425 git diff-files --quiet &&
426 git diff-index --quiet --cached HEAD -- &&
427 test $(git show HEAD:unrelated-file) = 2
430 test_expect_success '--continue tries to commit' '
431 git reset --hard D &&
432 test_tick &&
434 set_fake_editor &&
435 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
436 echo resolved > file1 &&
437 git add file1 &&
438 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
439 ) &&
440 test_cmp_rev HEAD^ new-branch1 &&
441 git show HEAD | grep chouette
444 test_expect_success 'verbose flag is heeded, even after --continue' '
445 git reset --hard primary@{1} &&
446 test_tick &&
447 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
448 echo resolved > file1 &&
449 git add file1 &&
450 git rebase --continue > output &&
451 grep "^ file1 | 2 +-$" output
454 test_expect_success 'multi-squash only fires up editor once' '
455 base=$(git rev-parse HEAD~4) &&
457 set_fake_editor &&
458 FAKE_COMMIT_AMEND="ONCE" \
459 FAKE_LINES="1 squash 2 squash 3 squash 4" \
460 EXPECT_HEADER_COUNT=4 \
461 git rebase -i $base
462 ) &&
463 test $base = $(git rev-parse HEAD^) &&
464 test 1 = $(git show | grep ONCE | wc -l)
467 test_expect_success 'multi-fixup does not fire up editor' '
468 git checkout -b multi-fixup E &&
469 base=$(git rev-parse HEAD~4) &&
471 set_fake_editor &&
472 FAKE_COMMIT_AMEND="NEVER" \
473 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
474 git rebase -i $base
475 ) &&
476 test $base = $(git rev-parse HEAD^) &&
477 test 0 = $(git show | grep NEVER | wc -l) &&
478 git checkout @{-1} &&
479 git branch -D multi-fixup
482 test_expect_success 'commit message used after conflict' '
483 git checkout -b conflict-fixup conflict-branch &&
484 base=$(git rev-parse HEAD~4) &&
486 set_fake_editor &&
487 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
488 git rebase -i $base &&
489 echo three > conflict &&
490 git add conflict &&
491 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
492 git rebase --continue
493 ) &&
494 test $base = $(git rev-parse HEAD^) &&
495 test 1 = $(git show | grep ONCE | wc -l) &&
496 git checkout @{-1} &&
497 git branch -D conflict-fixup
500 test_expect_success 'commit message retained after conflict' '
501 git checkout -b conflict-squash conflict-branch &&
502 base=$(git rev-parse HEAD~4) &&
504 set_fake_editor &&
505 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
506 git rebase -i $base &&
507 echo three > conflict &&
508 git add conflict &&
509 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
510 git rebase --continue
511 ) &&
512 test $base = $(git rev-parse HEAD^) &&
513 test 2 = $(git show | grep TWICE | wc -l) &&
514 git checkout @{-1} &&
515 git branch -D conflict-squash
518 test_expect_success 'squash and fixup generate correct log messages' '
519 cat >expect-squash-fixup <<-\EOF &&
524 ONCE
526 git checkout -b squash-fixup E &&
527 base=$(git rev-parse HEAD~4) &&
529 set_fake_editor &&
530 FAKE_COMMIT_AMEND="ONCE" \
531 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
532 EXPECT_HEADER_COUNT=4 \
533 git rebase -i $base
534 ) &&
535 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
536 test_cmp expect-squash-fixup actual-squash-fixup &&
537 git cat-file commit HEAD@{2} |
538 grep "^# This is a combination of 3 commits\." &&
539 git cat-file commit HEAD@{3} |
540 grep "^# This is a combination of 2 commits\." &&
541 git checkout @{-1} &&
542 git branch -D squash-fixup
545 test_expect_success 'squash ignores comments' '
546 git checkout -b skip-comments E &&
547 base=$(git rev-parse HEAD~4) &&
549 set_fake_editor &&
550 FAKE_COMMIT_AMEND="ONCE" \
551 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
552 EXPECT_HEADER_COUNT=4 \
553 git rebase -i $base
554 ) &&
555 test $base = $(git rev-parse HEAD^) &&
556 test 1 = $(git show | grep ONCE | wc -l) &&
557 git checkout @{-1} &&
558 git branch -D skip-comments
561 test_expect_success 'squash ignores blank lines' '
562 git checkout -b skip-blank-lines E &&
563 base=$(git rev-parse HEAD~4) &&
565 set_fake_editor &&
566 FAKE_COMMIT_AMEND="ONCE" \
567 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
568 EXPECT_HEADER_COUNT=4 \
569 git rebase -i $base
570 ) &&
571 test $base = $(git rev-parse HEAD^) &&
572 test 1 = $(git show | grep ONCE | wc -l) &&
573 git checkout @{-1} &&
574 git branch -D skip-blank-lines
577 test_expect_success 'squash works as expected' '
578 git checkout -b squash-works no-conflict-branch &&
579 one=$(git rev-parse HEAD~3) &&
581 set_fake_editor &&
582 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
583 ) &&
584 test $one = $(git rev-parse HEAD~2)
587 test_expect_success 'interrupted squash works as expected' '
588 git checkout -b interrupted-squash conflict-branch &&
589 one=$(git rev-parse HEAD~3) &&
591 set_fake_editor &&
592 test_must_fail env FAKE_LINES="1 squash 3 2" \
593 git rebase -i HEAD~3
594 ) &&
595 test_write_lines one two four > conflict &&
596 git add conflict &&
597 test_must_fail git rebase --continue &&
598 echo resolved > conflict &&
599 git add conflict &&
600 git rebase --continue &&
601 test $one = $(git rev-parse HEAD~2)
604 test_expect_success 'interrupted squash works as expected (case 2)' '
605 git checkout -b interrupted-squash2 conflict-branch &&
606 one=$(git rev-parse HEAD~3) &&
608 set_fake_editor &&
609 test_must_fail env FAKE_LINES="3 squash 1 2" \
610 git rebase -i HEAD~3
611 ) &&
612 test_write_lines one four > conflict &&
613 git add conflict &&
614 test_must_fail git rebase --continue &&
615 test_write_lines one two four > conflict &&
616 git add conflict &&
617 test_must_fail git rebase --continue &&
618 echo resolved > conflict &&
619 git add conflict &&
620 git rebase --continue &&
621 test $one = $(git rev-parse HEAD~2)
624 test_expect_success '--continue tries to commit, even for "edit"' '
625 echo unrelated > file7 &&
626 git add file7 &&
627 test_tick &&
628 git commit -m "unrelated change" &&
629 parent=$(git rev-parse HEAD^) &&
630 test_tick &&
632 set_fake_editor &&
633 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
634 echo edited > file7 &&
635 git add file7 &&
636 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
637 ) &&
638 test edited = $(git show HEAD:file7) &&
639 git show HEAD | grep chouette &&
640 test $parent = $(git rev-parse HEAD^)
643 test_expect_success 'aborted --continue does not squash commits after "edit"' '
644 old=$(git rev-parse HEAD) &&
645 test_tick &&
647 set_fake_editor &&
648 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
649 echo "edited again" > file7 &&
650 git add file7 &&
651 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
652 ) &&
653 test $old = $(git rev-parse HEAD) &&
654 git rebase --abort
657 test_expect_success 'auto-amend only edited commits after "edit"' '
658 test_tick &&
660 set_fake_editor &&
661 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
662 echo "edited again" > file7 &&
663 git add file7 &&
664 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
665 echo "and again" > file7 &&
666 git add file7 &&
667 test_tick &&
668 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
669 git rebase --continue
670 ) &&
671 git rebase --abort
674 test_expect_success 'clean error after failed "exec"' '
675 test_tick &&
676 test_when_finished "git rebase --abort || :" &&
678 set_fake_editor &&
679 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
680 ) &&
681 echo "edited again" > file7 &&
682 git add file7 &&
683 test_must_fail git rebase --continue 2>error &&
684 test_i18ngrep "you have staged changes in your working tree" error
687 test_expect_success 'rebase a detached HEAD' '
688 grandparent=$(git rev-parse HEAD~2) &&
689 git checkout $(git rev-parse HEAD) &&
690 test_tick &&
692 set_fake_editor &&
693 FAKE_LINES="2 1" git rebase -i HEAD~2
694 ) &&
695 test $grandparent = $(git rev-parse HEAD~2)
698 test_expect_success 'rebase a commit violating pre-commit' '
700 mkdir -p .git/hooks &&
701 write_script .git/hooks/pre-commit <<-\EOF &&
702 test -z "$(git diff --cached --check)"
704 echo "monde! " >> file1 &&
705 test_tick &&
706 test_must_fail git commit -m doesnt-verify file1 &&
707 git commit -m doesnt-verify --no-verify file1 &&
708 test_tick &&
710 set_fake_editor &&
711 FAKE_LINES=2 git rebase -i HEAD~2
715 test_expect_success 'rebase with a file named HEAD in worktree' '
717 rm -fr .git/hooks &&
718 git reset --hard &&
719 git checkout -b branch3 A &&
722 GIT_AUTHOR_NAME="Squashed Away" &&
723 export GIT_AUTHOR_NAME &&
724 >HEAD &&
725 git add HEAD &&
726 git commit -m "Add head" &&
727 >BODY &&
728 git add BODY &&
729 git commit -m "Add body"
730 ) &&
733 set_fake_editor &&
734 FAKE_LINES="1 squash 2" git rebase -i @{-1}
735 ) &&
736 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
740 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
742 git checkout -b branch4 HEAD &&
743 GIT_EDITOR=: git commit --amend \
744 --author="Somebody else <somebody@else.com>" &&
745 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
746 git rebase -i branch3 &&
747 test_cmp_rev branch3 branch4
751 test_expect_success 'submodule rebase setup' '
752 git checkout A &&
753 mkdir sub &&
755 cd sub && git init && >elif &&
756 git add elif && git commit -m "submodule initial"
757 ) &&
758 echo 1 >file1 &&
759 git add file1 sub &&
760 test_tick &&
761 git commit -m "One" &&
762 echo 2 >file1 &&
763 test_tick &&
764 git commit -a -m "Two" &&
766 cd sub && echo 3 >elif &&
767 git commit -a -m "submodule second"
768 ) &&
769 test_tick &&
770 git commit -a -m "Three changes submodule"
773 test_expect_success 'submodule rebase -i' '
775 set_fake_editor &&
776 FAKE_LINES="1 squash 2 3" git rebase -i A
780 test_expect_success 'submodule conflict setup' '
781 git tag submodule-base &&
782 git checkout HEAD^ &&
784 cd sub && git checkout HEAD^ && echo 4 >elif &&
785 git add elif && git commit -m "submodule conflict"
786 ) &&
787 git add sub &&
788 test_tick &&
789 git commit -m "Conflict in submodule" &&
790 git tag submodule-topic
793 test_expect_success 'rebase -i continue with only submodule staged' '
794 test_must_fail git rebase -i submodule-base &&
795 git add sub &&
796 git rebase --continue &&
797 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
800 test_expect_success 'rebase -i continue with unstaged submodule' '
801 git checkout submodule-topic &&
802 git reset --hard &&
803 test_must_fail git rebase -i submodule-base &&
804 git reset &&
805 git rebase --continue &&
806 test_cmp_rev submodule-base HEAD
809 test_expect_success 'avoid unnecessary reset' '
810 git checkout primary &&
811 git reset --hard &&
812 test-tool chmtime =123456789 file3 &&
813 git update-index --refresh &&
814 HEAD=$(git rev-parse HEAD) &&
815 git rebase -i HEAD~4 &&
816 test $HEAD = $(git rev-parse HEAD) &&
817 MTIME=$(test-tool chmtime --get file3) &&
818 test 123456789 = $MTIME
821 test_expect_success 'reword' '
822 git checkout -b reword-branch primary &&
824 set_fake_editor &&
825 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
826 git rebase -i A &&
827 git show HEAD | grep "E changed" &&
828 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
829 test_cmp_rev primary^ HEAD^ &&
830 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
831 git rebase -i A &&
832 git show HEAD^ | grep "D changed" &&
833 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
834 git rebase -i A &&
835 git show HEAD~3 | grep "B changed" &&
836 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
837 git rebase -i A
838 ) &&
839 git show HEAD~2 | grep "C changed"
842 test_expect_success 'no uncommited changes when rewording the todo list is reloaded' '
843 git checkout E &&
844 test_when_finished "git checkout @{-1}" &&
846 set_fake_editor &&
847 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
848 export GIT_SEQUENCE_EDITOR &&
849 set_reword_editor &&
850 FAKE_LINES="reword 1 reword 2" git rebase -i C
851 ) &&
852 check_reworded_commits D E
855 test_expect_success 'rebase -i can copy notes' '
856 git config notes.rewrite.rebase true &&
857 git config notes.rewriteRef "refs/notes/*" &&
858 test_commit n1 &&
859 test_commit n2 &&
860 test_commit n3 &&
861 git notes add -m"a note" n3 &&
862 git rebase -i --onto n1 n2 &&
863 test "a note" = "$(git notes show HEAD)"
866 test_expect_success 'rebase -i can copy notes over a fixup' '
867 cat >expect <<-\EOF &&
868 an earlier note
870 a note
872 git reset --hard n3 &&
873 git notes add -m"an earlier note" n2 &&
875 set_fake_editor &&
876 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
877 git rebase -i n1
878 ) &&
879 git notes show > output &&
880 test_cmp expect output
883 test_expect_success 'rebase while detaching HEAD' '
884 git symbolic-ref HEAD &&
885 grandparent=$(git rev-parse HEAD~2) &&
886 test_tick &&
888 set_fake_editor &&
889 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
890 ) &&
891 test $grandparent = $(git rev-parse HEAD~2) &&
892 test_must_fail git symbolic-ref HEAD
895 test_tick # Ensure that the rebased commits get a different timestamp.
896 test_expect_success 'always cherry-pick with --no-ff' '
897 git checkout no-ff-branch &&
898 git tag original-no-ff-branch &&
899 git rebase -i --no-ff A &&
900 for p in 0 1 2
902 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
903 git diff HEAD~$p original-no-ff-branch~$p > out &&
904 test_must_be_empty out
905 done &&
906 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
907 git diff HEAD~3 original-no-ff-branch~3 > out &&
908 test_must_be_empty out
911 test_expect_success 'set up commits with funny messages' '
912 git checkout -b funny A &&
913 echo >>file1 &&
914 test_tick &&
915 git commit -a -m "end with slash\\" &&
916 echo >>file1 &&
917 test_tick &&
918 git commit -a -m "something (\000) that looks like octal" &&
919 echo >>file1 &&
920 test_tick &&
921 git commit -a -m "something (\n) that looks like a newline" &&
922 echo >>file1 &&
923 test_tick &&
924 git commit -a -m "another commit"
927 test_expect_success 'rebase-i history with funny messages' '
928 git rev-list A..funny >expect &&
929 test_tick &&
931 set_fake_editor &&
932 FAKE_LINES="1 2 3 4" git rebase -i A
933 ) &&
934 git rev-list A.. >actual &&
935 test_cmp expect actual
938 test_expect_success 'prepare for rebase -i --exec' '
939 git checkout primary &&
940 git checkout -b execute &&
941 test_commit one_exec main.txt one_exec &&
942 test_commit two_exec main.txt two_exec &&
943 test_commit three_exec main.txt three_exec
946 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
948 set_fake_editor &&
949 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
950 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
951 export FAKE_LINES &&
952 git rebase -i HEAD~2 >expect
953 ) &&
954 sed -e "1,9d" expect >expected &&
955 test_cmp expected actual
958 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
959 git reset --hard execute &&
961 set_fake_editor &&
962 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
963 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
964 export FAKE_LINES &&
965 git rebase -i HEAD~2 >expect
966 ) &&
967 sed -e "1,9d" expect >expected &&
968 test_cmp expected actual
971 test_expect_success 'running "git rebase -ix git show HEAD"' '
972 git reset --hard execute &&
974 set_fake_editor &&
975 git rebase -ix "git show HEAD" HEAD~2 >actual &&
976 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
977 export FAKE_LINES &&
978 git rebase -i HEAD~2 >expect
979 ) &&
980 sed -e "1,9d" expect >expected &&
981 test_cmp expected actual
985 test_expect_success 'rebase -ix with several <CMD>' '
986 git reset --hard execute &&
988 set_fake_editor &&
989 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
990 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
991 export FAKE_LINES &&
992 git rebase -i HEAD~2 >expect
993 ) &&
994 sed -e "1,9d" expect >expected &&
995 test_cmp expected actual
998 test_expect_success 'rebase -ix with several instances of --exec' '
999 git reset --hard execute &&
1001 set_fake_editor &&
1002 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
1003 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
1004 exec_git_show_HEAD exec_pwd" &&
1005 export FAKE_LINES &&
1006 git rebase -i HEAD~2 >expect
1007 ) &&
1008 sed -e "1,11d" expect >expected &&
1009 test_cmp expected actual
1012 test_expect_success 'rebase -ix with --autosquash' '
1013 git reset --hard execute &&
1014 git checkout -b autosquash &&
1015 echo second >second.txt &&
1016 git add second.txt &&
1017 git commit -m "fixup! two_exec" &&
1018 echo bis >bis.txt &&
1019 git add bis.txt &&
1020 git commit -m "fixup! two_exec" &&
1021 git checkout -b autosquash_actual &&
1022 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
1023 git checkout autosquash &&
1025 set_fake_editor &&
1026 git checkout -b autosquash_expected &&
1027 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
1028 export FAKE_LINES &&
1029 git rebase -i HEAD~4 >expect
1030 ) &&
1031 sed -e "1,13d" expect >expected &&
1032 test_cmp expected actual
1035 test_expect_success 'rebase --exec works without -i ' '
1036 git reset --hard execute &&
1037 rm -rf exec_output &&
1038 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
1039 test_i18ngrep "Successfully rebased and updated" actual &&
1040 test_line_count = 2 exec_output &&
1041 test_path_is_missing invoked_editor
1044 test_expect_success 'rebase -i --exec without <CMD>' '
1045 git reset --hard execute &&
1046 test_must_fail git rebase -i --exec 2>actual &&
1047 test_i18ngrep "requires a value" actual &&
1048 git checkout primary
1051 test_expect_success 'rebase -i --root re-order and drop commits' '
1052 git checkout E &&
1054 set_fake_editor &&
1055 FAKE_LINES="3 1 2 5" git rebase -i --root
1056 ) &&
1057 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1058 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1059 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
1060 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
1061 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
1064 test_expect_success 'rebase -i --root retain root commit author and message' '
1065 git checkout A &&
1066 echo B >file7 &&
1067 git add file7 &&
1068 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1070 set_fake_editor &&
1071 FAKE_LINES="2" git rebase -i --root
1072 ) &&
1073 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1074 git cat-file commit HEAD | grep -q "^different author$"
1077 test_expect_success 'rebase -i --root temporary sentinel commit' '
1078 git checkout B &&
1080 set_fake_editor &&
1081 test_must_fail env FAKE_LINES="2" git rebase -i --root
1082 ) &&
1083 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1084 git rebase --abort
1087 test_expect_success 'rebase -i --root fixup root commit' '
1088 git checkout B &&
1090 set_fake_editor &&
1091 FAKE_LINES="1 fixup 2" git rebase -i --root
1092 ) &&
1093 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1094 test B = $(git show HEAD:file1) &&
1095 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1098 test_expect_success 'rebase -i --root reword original root commit' '
1099 test_when_finished "test_might_fail git rebase --abort" &&
1100 git checkout -b reword-original-root-branch primary &&
1102 set_fake_editor &&
1103 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1104 git rebase -i --root
1105 ) &&
1106 git show HEAD^ | grep "A changed" &&
1107 test -z "$(git show -s --format=%p HEAD^)"
1110 test_expect_success 'rebase -i --root reword new root commit' '
1111 test_when_finished "test_might_fail git rebase --abort" &&
1112 git checkout -b reword-now-root-branch primary &&
1114 set_fake_editor &&
1115 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1116 git rebase -i --root
1117 ) &&
1118 git show HEAD^ | grep "C changed" &&
1119 test -z "$(git show -s --format=%p HEAD^)"
1122 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1123 test_when_finished "reset_rebase" &&
1124 git checkout -b failing-root-pick A &&
1125 echo x >file2 &&
1126 git rm file1 &&
1127 git commit -m "remove file 1 add file 2" &&
1128 echo z >file1 &&
1130 set_fake_editor &&
1131 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1132 ) &&
1133 rm file1 &&
1134 git rebase --continue &&
1135 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1136 test "$(git rev-list --count HEAD)" = 2
1139 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1140 test_when_finished "reset_rebase" &&
1141 echo z>file1 &&
1143 set_fake_editor &&
1144 test_must_fail env FAKE_LINES="reword 1 2" \
1145 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1146 rm file1 &&
1147 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1148 ) &&
1149 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1150 test "$(git rev-list --count HEAD)" = 2
1153 test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
1154 git checkout reword-original-root-branch &&
1155 git reset --hard &&
1156 git checkout conflict-branch &&
1158 set_fake_editor &&
1159 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1160 test_must_fail git rebase --edit-todo
1161 ) &&
1162 git rebase --abort
1165 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1166 git reset --hard &&
1167 git checkout no-conflict-branch^0 &&
1169 set_fake_editor &&
1170 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1171 FAKE_LINES="2 1" git rebase --edit-todo &&
1172 git rebase --continue
1173 ) &&
1174 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1175 test L = $(git cat-file commit HEAD | sed -ne \$p)
1178 test_expect_success 'rebase -i produces readable reflog' '
1179 git reset --hard &&
1180 git branch -f branch-reflog-test H &&
1181 git rebase -i --onto I F branch-reflog-test &&
1182 cat >expect <<-\EOF &&
1183 rebase (finish): returning to refs/heads/branch-reflog-test
1184 rebase (pick): H
1185 rebase (pick): G
1186 rebase (start): checkout I
1188 git reflog -n4 HEAD |
1189 sed "s/[^:]*: //" >actual &&
1190 test_cmp expect actual
1193 test_expect_success 'rebase -i respects core.commentchar' '
1194 git reset --hard &&
1195 git checkout E^0 &&
1196 test_config core.commentchar "\\" &&
1197 write_script remove-all-but-first.sh <<-\EOF &&
1198 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1199 mv "$1.tmp" "$1"
1202 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1203 git rebase -i B
1204 ) &&
1205 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1208 test_expect_success 'rebase -i respects core.commentchar=auto' '
1209 test_config core.commentchar auto &&
1210 write_script copy-edit-script.sh <<-\EOF &&
1211 cp "$1" edit-script
1213 test_when_finished "git rebase --abort || :" &&
1215 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1216 git rebase -i HEAD^
1217 ) &&
1218 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1221 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1222 test_when_finished "git branch -D torebase" &&
1223 git checkout -b torebase branch1 &&
1224 upstream=$(git rev-parse ":/J") &&
1225 onto=$(git rev-parse ":/A") &&
1226 git rebase --onto $onto $upstream &&
1227 git reset --hard branch1 &&
1228 git rebase --onto ":/A" ":/J" &&
1229 git checkout branch1
1232 test_expect_success 'rebase -i with --strategy and -X' '
1233 git checkout -b conflict-merge-use-theirs conflict-branch &&
1234 git reset --hard HEAD^ &&
1235 echo five >conflict &&
1236 echo Z >file1 &&
1237 git commit -a -m "one file conflict" &&
1238 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1239 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1240 test $(cat file1) = Z
1243 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1244 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1245 git reset --hard HEAD^ &&
1246 >breakpoint &&
1247 git add breakpoint &&
1248 git commit -m "breakpoint for interactive mode" &&
1249 echo five >conflict &&
1250 echo Z >file1 &&
1251 git commit -a -m "one file conflict" &&
1253 set_fake_editor &&
1254 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1255 -Xours conflict-branch
1256 ) &&
1257 git rebase --continue &&
1258 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1259 test $(cat file1) = Z
1262 test_expect_success 'rebase -i error on commits with \ in message' '
1263 current_head=$(git rev-parse HEAD) &&
1264 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1265 test_commit TO-REMOVE will-conflict old-content &&
1266 test_commit "\temp" will-conflict new-content dummy &&
1267 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1268 test_expect_code 1 grep " emp" error
1271 test_expect_success 'short commit ID setup' '
1272 test_when_finished "git checkout primary" &&
1273 git checkout --orphan collide &&
1274 git rm -rf . &&
1276 unset test_tick &&
1277 test_commit collide1 collide &&
1278 test_commit --notick collide2 collide &&
1279 test_commit --notick collide3 collide
1283 if test -n "$GIT_TEST_FIND_COLLIDER"
1284 then
1285 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1286 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1287 blob="$(git rev-parse collide2:collide)"
1288 from="$(git rev-parse collide1^0)"
1289 repl="commit refs/heads/collider-&\\n"
1290 repl="${repl}author $author\\ncommitter $committer\\n"
1291 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1292 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1293 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1294 git fast-import <script &&
1295 git pack-refs &&
1296 git for-each-ref >refs &&
1297 grep "^$(test_oid t3404_collision)" <refs >matches &&
1298 cat matches &&
1299 test_line_count -gt 2 matches || {
1300 echo "Could not find a collider" >&2
1301 exit 1
1305 test_expect_success 'short commit ID collide' '
1306 test_oid_cache <<-EOF &&
1307 # collision-related constants
1308 t3404_collision sha1:6bcd
1309 t3404_collision sha256:0161
1310 t3404_collider sha1:ac4f2ee
1311 t3404_collider sha256:16697
1313 test_when_finished "reset_rebase && git checkout primary" &&
1314 git checkout collide &&
1315 colliding_id=$(test_oid t3404_collision) &&
1316 hexsz=$(test_oid hexsz) &&
1317 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1318 test_config core.abbrev 4 &&
1320 unset test_tick &&
1321 test_tick &&
1322 set_fake_editor &&
1323 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1324 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1325 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1326 grep "^pick $colliding_id " \
1327 .git/rebase-merge/git-rebase-todo.tmp &&
1328 grep "^pick [0-9a-f]\{$hexsz\}" \
1329 .git/rebase-merge/git-rebase-todo &&
1330 grep "^pick [0-9a-f]\{$hexsz\}" \
1331 .git/rebase-merge/git-rebase-todo.backup &&
1332 git rebase --continue
1333 ) &&
1334 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1335 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1336 test "$collide2" = "$collide3"
1339 test_expect_success 'respect core.abbrev' '
1340 git config core.abbrev 12 &&
1342 set_cat_todo_editor &&
1343 test_must_fail git rebase -i HEAD~4 >todo-list
1344 ) &&
1345 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1348 test_expect_success 'todo count' '
1349 write_script dump-raw.sh <<-\EOF &&
1350 cat "$1"
1353 test_set_editor "$(pwd)/dump-raw.sh" &&
1354 git rebase -i HEAD~4 >actual
1355 ) &&
1356 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1359 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1360 git checkout --force branch2 &&
1361 git clean -f &&
1363 set_fake_editor &&
1364 FAKE_LINES="edit 1 2" git rebase -i A
1365 ) &&
1366 test_cmp_rev HEAD F &&
1367 test_path_is_missing file6 &&
1368 >file6 &&
1369 test_must_fail git rebase --continue &&
1370 test_cmp_rev HEAD F &&
1371 rm file6 &&
1372 git rebase --continue &&
1373 test_cmp_rev HEAD I
1376 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1377 git checkout --force branch2 &&
1378 git clean -f &&
1379 git tag original-branch2 &&
1381 set_fake_editor &&
1382 FAKE_LINES="edit 1 squash 2" git rebase -i A
1383 ) &&
1384 test_cmp_rev HEAD F &&
1385 test_path_is_missing file6 &&
1386 >file6 &&
1387 test_must_fail git rebase --continue &&
1388 test_cmp_rev HEAD F &&
1389 rm file6 &&
1390 git rebase --continue &&
1391 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1392 git reset --hard original-branch2
1395 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1396 git checkout --force branch2 &&
1397 git clean -f &&
1399 set_fake_editor &&
1400 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1401 ) &&
1402 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1403 test_path_is_missing file6 &&
1404 >file6 &&
1405 test_must_fail git rebase --continue &&
1406 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1407 rm file6 &&
1408 git rebase --continue &&
1409 test $(git cat-file commit HEAD | sed -ne \$p) = I
1412 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1413 git checkout -b commit-to-skip &&
1414 for double in X 3 1
1416 test_seq 5 | sed "s/$double/&&/" >seq &&
1417 git add seq &&
1418 test_tick &&
1419 git commit -m seq-$double
1420 done &&
1421 git tag seq-onto &&
1422 git reset --hard HEAD~2 &&
1423 git cherry-pick seq-onto &&
1425 set_fake_editor &&
1426 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1427 ) &&
1428 test -d .git/rebase-merge &&
1429 git rebase --continue &&
1430 git diff --exit-code seq-onto &&
1431 test ! -d .git/rebase-merge &&
1432 test ! -f .git/CHERRY_PICK_HEAD
1435 rebase_setup_and_clean () {
1436 test_when_finished "
1437 git checkout primary &&
1438 test_might_fail git branch -D $1 &&
1439 test_might_fail git rebase --abort
1440 " &&
1441 git checkout -b $1 ${2:-primary}
1444 test_expect_success 'drop' '
1445 rebase_setup_and_clean drop-test &&
1447 set_fake_editor &&
1448 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1449 ) &&
1450 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1451 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1452 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1455 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1456 test_config rebase.missingCommitsCheck ignore &&
1457 rebase_setup_and_clean missing-commit &&
1459 set_fake_editor &&
1460 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1461 ) &&
1462 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1463 test_i18ngrep \
1464 "Successfully rebased and updated refs/heads/missing-commit" \
1465 actual
1468 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1469 cat >expect <<-EOF &&
1470 Warning: some commits may have been dropped accidentally.
1471 Dropped commits (newer to older):
1472 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1473 To avoid this message, use "drop" to explicitly remove a commit.
1475 test_config rebase.missingCommitsCheck warn &&
1476 rebase_setup_and_clean missing-commit &&
1478 set_fake_editor &&
1479 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1480 ) &&
1481 head -n4 actual.2 >actual &&
1482 test_cmp expect actual &&
1483 test D = $(git cat-file commit HEAD | sed -ne \$p)
1486 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1487 cat >expect <<-EOF &&
1488 Warning: some commits may have been dropped accidentally.
1489 Dropped commits (newer to older):
1490 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1491 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1492 To avoid this message, use "drop" to explicitly remove a commit.
1494 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1495 The possible behaviours are: ignore, warn, error.
1497 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1498 Or you can abort the rebase with '\''git rebase --abort'\''.
1500 test_config rebase.missingCommitsCheck error &&
1501 rebase_setup_and_clean missing-commit &&
1503 set_fake_editor &&
1504 test_must_fail env FAKE_LINES="1 2 4" \
1505 git rebase -i --root 2>actual &&
1506 test_cmp expect actual &&
1507 cp .git/rebase-merge/git-rebase-todo.backup \
1508 .git/rebase-merge/git-rebase-todo &&
1509 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1510 ) &&
1511 git rebase --continue &&
1512 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1513 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1516 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1517 test_config rebase.missingCommitsCheck ignore &&
1518 rebase_setup_and_clean missing-commit &&
1520 set_fake_editor &&
1521 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1522 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1523 git rebase --continue 2>actual
1524 ) &&
1525 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1526 test_i18ngrep \
1527 "Successfully rebased and updated refs/heads/missing-commit" \
1528 actual
1531 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1532 cat >expect <<-EOF &&
1533 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1534 Warning: some commits may have been dropped accidentally.
1535 Dropped commits (newer to older):
1536 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1537 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1538 To avoid this message, use "drop" to explicitly remove a commit.
1540 head -n4 expect >expect.2 &&
1541 tail -n1 expect >>expect.2 &&
1542 tail -n4 expect.2 >expect.3 &&
1543 test_config rebase.missingCommitsCheck warn &&
1544 rebase_setup_and_clean missing-commit &&
1546 set_fake_editor &&
1547 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1548 git rebase -i --root &&
1549 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1550 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1551 head -n6 actual.2 >actual &&
1552 test_cmp expect actual &&
1553 cp orig .git/rebase-merge/git-rebase-todo &&
1554 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1555 head -n4 actual.2 >actual &&
1556 test_cmp expect.3 actual &&
1557 git rebase --continue 2>actual
1558 ) &&
1559 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1560 test_i18ngrep \
1561 "Successfully rebased and updated refs/heads/missing-commit" \
1562 actual
1565 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1566 cat >expect <<-EOF &&
1567 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1568 Warning: some commits may have been dropped accidentally.
1569 Dropped commits (newer to older):
1570 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1571 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1572 To avoid this message, use "drop" to explicitly remove a commit.
1574 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1575 The possible behaviours are: ignore, warn, error.
1577 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1578 Or you can abort the rebase with '\''git rebase --abort'\''.
1580 tail -n11 expect >expect.2 &&
1581 head -n3 expect.2 >expect.3 &&
1582 tail -n7 expect.2 >>expect.3 &&
1583 test_config rebase.missingCommitsCheck error &&
1584 rebase_setup_and_clean missing-commit &&
1586 set_fake_editor &&
1587 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1588 git rebase -i --root &&
1589 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1590 test_must_fail env FAKE_LINES="2 3 4" \
1591 git rebase --edit-todo 2>actual &&
1592 test_cmp expect actual &&
1593 test_must_fail git rebase --continue 2>actual &&
1594 test_cmp expect.2 actual &&
1595 test_must_fail git rebase --edit-todo &&
1596 cp orig .git/rebase-merge/git-rebase-todo &&
1597 test_must_fail env FAKE_LINES="1 2 3 4" \
1598 git rebase --edit-todo 2>actual &&
1599 test_cmp expect.3 actual &&
1600 test_must_fail git rebase --continue 2>actual &&
1601 test_cmp expect.3 actual &&
1602 cp orig .git/rebase-merge/git-rebase-todo &&
1603 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1604 git rebase --continue 2>actual
1605 ) &&
1606 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1607 test_i18ngrep \
1608 "Successfully rebased and updated refs/heads/missing-commit" \
1609 actual
1612 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1613 test_config rebase.missingCommitsCheck error &&
1615 set_fake_editor &&
1616 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1617 ) &&
1618 git rebase --edit-todo &&
1619 test_must_fail git rebase --continue &&
1620 echo x >file1 &&
1621 git add file1 &&
1622 git rebase --continue
1625 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1626 test_config rebase.missingCommitsCheck error &&
1628 set_fake_editor &&
1629 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1630 cp .git/rebase-merge/git-rebase-todo todo &&
1631 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1632 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1633 git rebase --continue
1637 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1638 rebase_setup_and_clean abbrevcmd &&
1639 test_commit "first" file1.txt "first line" first &&
1640 test_commit "second" file1.txt "another line" second &&
1641 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1642 test_commit "squash! second" file1.txt "another line here" second_squash &&
1643 cat >expected <<-EOF &&
1644 p $(git rev-list --abbrev-commit -1 first) first
1645 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1646 x git show HEAD
1647 p $(git rev-list --abbrev-commit -1 second) second
1648 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1649 x git show HEAD
1651 git checkout abbrevcmd &&
1652 test_config rebase.abbreviateCommands true &&
1654 set_cat_todo_editor &&
1655 test_must_fail git rebase -i --exec "git show HEAD" \
1656 --autosquash primary >actual
1657 ) &&
1658 test_cmp expected actual
1661 test_expect_success 'static check of bad command' '
1662 rebase_setup_and_clean bad-cmd &&
1664 set_fake_editor &&
1665 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1666 git rebase -i --root 2>actual &&
1667 test_i18ngrep "badcmd $(git rev-list --oneline -1 primary~1)" \
1668 actual &&
1669 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1670 actual &&
1671 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1672 ) &&
1673 git rebase --continue &&
1674 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1675 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1678 test_expect_success 'tabs and spaces are accepted in the todolist' '
1679 rebase_setup_and_clean indented-comment &&
1680 write_script add-indent.sh <<-\EOF &&
1682 # Turn single spaces into space/tab mix
1683 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1684 printf "\n\t# comment\n #more\n\t # comment\n"
1685 ) >"$1.new"
1686 mv "$1.new" "$1"
1689 test_set_editor "$(pwd)/add-indent.sh" &&
1690 git rebase -i HEAD^^^
1691 ) &&
1692 test E = $(git cat-file commit HEAD | sed -ne \$p)
1695 test_expect_success 'static check of bad SHA-1' '
1696 rebase_setup_and_clean bad-sha &&
1698 set_fake_editor &&
1699 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1700 git rebase -i --root 2>actual &&
1701 test_i18ngrep "edit XXXXXXX False commit" actual &&
1702 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1703 actual &&
1704 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1705 ) &&
1706 git rebase --continue &&
1707 test E = $(git cat-file commit HEAD | sed -ne \$p)
1710 test_expect_success 'editor saves as CR/LF' '
1711 git checkout -b with-crlf &&
1712 write_script add-crs.sh <<-\EOF &&
1713 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1714 mv -f "$1".new "$1"
1717 test_set_editor "$(pwd)/add-crs.sh" &&
1718 git rebase -i HEAD^
1722 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1723 test_when_finished "test_might_fail git rebase --abort" &&
1725 set_fake_editor &&
1726 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1727 HEAD^ >out 2>err
1728 ) &&
1729 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1732 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1733 test_when_finished "test_might_fail git rebase --abort" &&
1734 test_config commit.gpgsign true &&
1736 set_fake_editor &&
1737 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1738 HEAD^ >out 2>err
1739 ) &&
1740 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1743 test_expect_success 'valid author header after --root swap' '
1744 rebase_setup_and_clean author-header no-conflict-branch &&
1745 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1746 git cat-file commit HEAD | grep ^author >expected &&
1748 set_fake_editor &&
1749 FAKE_LINES="5 1" git rebase -i --root
1750 ) &&
1751 git cat-file commit HEAD^ | grep ^author >actual &&
1752 test_cmp expected actual
1755 test_expect_success 'valid author header when author contains single quote' '
1756 rebase_setup_and_clean author-header no-conflict-branch &&
1757 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1758 git cat-file commit HEAD | grep ^author >expected &&
1760 set_fake_editor &&
1761 FAKE_LINES="2" git rebase -i HEAD~2
1762 ) &&
1763 git cat-file commit HEAD | grep ^author >actual &&
1764 test_cmp expected actual
1767 test_expect_success 'post-commit hook is called' '
1768 test_when_finished "rm -f .git/hooks/post-commit" &&
1769 >actual &&
1770 mkdir -p .git/hooks &&
1771 write_script .git/hooks/post-commit <<-\EOS &&
1772 git rev-parse HEAD >>actual
1775 set_fake_editor &&
1776 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1777 echo x>file3 &&
1778 git add file3 &&
1779 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1780 ) &&
1781 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1782 >expect &&
1783 test_cmp expect actual
1786 test_expect_success 'correct error message for partial commit after empty pick' '
1787 test_when_finished "git rebase --abort" &&
1789 set_fake_editor &&
1790 FAKE_LINES="2 1 1" &&
1791 export FAKE_LINES &&
1792 test_must_fail git rebase -i A D
1793 ) &&
1794 echo x >file1 &&
1795 test_must_fail git commit file1 2>err &&
1796 test_i18ngrep "cannot do a partial commit during a rebase." err
1799 test_expect_success 'correct error message for commit --amend after empty pick' '
1800 test_when_finished "git rebase --abort" &&
1802 set_fake_editor &&
1803 FAKE_LINES="1 1" &&
1804 export FAKE_LINES &&
1805 test_must_fail git rebase -i A D
1806 ) &&
1807 echo x>file1 &&
1808 test_must_fail git commit -a --amend 2>err &&
1809 test_i18ngrep "middle of a rebase -- cannot amend." err
1812 test_expect_success 'todo has correct onto hash' '
1813 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1814 onto=$(git rev-parse --short HEAD~4) &&
1815 test_i18ngrep "^# Rebase ..* onto $onto" actual
1818 test_expect_success 'ORIG_HEAD is updated correctly' '
1819 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1820 git checkout -b test-orig-head A &&
1821 git commit --allow-empty -m A1 &&
1822 git commit --allow-empty -m A2 &&
1823 git commit --allow-empty -m A3 &&
1824 git commit --allow-empty -m A4 &&
1825 git rebase primary &&
1826 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1829 # This must be the last test in this file
1830 test_expect_success '$EDITOR and friends are unchanged' '
1831 test_editor_unchanged
1834 test_done