interpret-trailers: mark unused "unset" parameters in option callbacks
[git.git] / t / t3404-rebase-interactive.sh
blob96a56aafbed67ca7e47ac173c00afdbe17b000ad
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".
29 . ./test-lib.sh
31 . "$TEST_DIRECTORY"/lib-rebase.sh
33 test_expect_success 'setup' '
34 git switch -C primary &&
35 test_commit A file1 &&
36 test_commit B file1 &&
37 test_commit C file2 &&
38 test_commit D file1 &&
39 test_commit E file3 &&
40 git checkout -b branch1 A &&
41 test_commit F file4 &&
42 test_commit G file1 &&
43 test_commit H file5 &&
44 git checkout -b branch2 F &&
45 test_commit I file6 &&
46 git checkout -b conflict-branch A &&
47 test_commit one conflict &&
48 test_commit two conflict &&
49 test_commit three conflict &&
50 test_commit four conflict &&
51 git checkout -b no-conflict-branch A &&
52 test_commit J fileJ &&
53 test_commit K fileK &&
54 test_commit L fileL &&
55 test_commit M fileM &&
56 git checkout -b no-ff-branch A &&
57 test_commit N fileN &&
58 test_commit O fileO &&
59 test_commit P fileP
62 # "exec" commands are run with the user shell by default, but this may
63 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
64 # to create a file. Unsetting SHELL avoids such non-portable behavior
65 # in tests. It must be exported for it to take effect where needed.
66 SHELL=
67 export SHELL
69 test_expect_success 'rebase --keep-empty' '
70 git checkout -b emptybranch primary &&
71 git commit --allow-empty -m "empty" &&
72 git rebase --keep-empty -i HEAD~2 &&
73 git log --oneline >actual &&
74 test_line_count = 6 actual
77 test_expect_success 'rebase -i with empty todo list' '
78 cat >expect <<-\EOF &&
79 error: nothing to do
80 EOF
82 set_fake_editor &&
83 test_must_fail env FAKE_LINES="#" \
84 git rebase -i HEAD^ >output 2>&1
85 ) &&
86 tail -n 1 output >actual && # Ignore output about changing todo list
87 test_cmp expect actual
90 test_expect_success 'rebase -i with the exec command' '
91 git checkout primary &&
93 set_fake_editor &&
94 FAKE_LINES="1 exec_>touch-one
95 2 exec_>touch-two exec_false exec_>touch-three
96 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
97 export FAKE_LINES &&
98 test_must_fail git rebase -i A
99 ) &&
100 test_path_is_file touch-one &&
101 test_path_is_file touch-two &&
102 # Missing because we should have stopped by now.
103 test_path_is_missing touch-three &&
104 test_cmp_rev C HEAD &&
105 git rebase --continue &&
106 test_path_is_file touch-three &&
107 test_path_is_file "touch-file name with spaces" &&
108 test_path_is_file touch-after-semicolon &&
109 test_cmp_rev primary HEAD &&
110 rm -f touch-*
113 test_expect_success 'rebase -i with the exec command runs from tree root' '
114 git checkout primary &&
115 mkdir subdir && (cd subdir &&
116 set_fake_editor &&
117 FAKE_LINES="1 exec_>touch-subdir" \
118 git rebase -i HEAD^
119 ) &&
120 test_path_is_file touch-subdir &&
121 rm -fr subdir
124 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
125 test_when_finished "rm -rf subdir" &&
126 test_when_finished "git rebase --abort ||:" &&
127 git checkout primary &&
128 mkdir subdir && (cd subdir &&
129 set_fake_editor &&
130 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
131 git rebase -i HEAD^
135 test_expect_success 'rebase -i sets work tree properly' '
136 test_when_finished "rm -rf subdir" &&
137 test_when_finished "test_might_fail git rebase --abort" &&
138 mkdir subdir &&
139 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
140 >actual &&
141 ! grep "/subdir$" actual
144 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
145 git checkout primary &&
147 set_fake_editor &&
148 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
149 git rebase -i HEAD^
150 ) &&
151 test_cmp_rev primary^ HEAD &&
152 git reset --hard &&
153 git rebase --continue
156 test_expect_success 'rebase -x with empty command fails' '
157 test_when_finished "git rebase --abort ||:" &&
158 test_must_fail env git rebase -x "" @ 2>actual &&
159 test_write_lines "error: empty exec command" >expected &&
160 test_cmp expected actual &&
161 test_must_fail env git rebase -x " " @ 2>actual &&
162 test_cmp expected actual
165 test_expect_success 'rebase -x with newline in command fails' '
166 test_when_finished "git rebase --abort ||:" &&
167 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
168 test_write_lines "error: exec commands cannot contain newlines" \
169 >expected &&
170 test_cmp expected actual
173 test_expect_success 'rebase -i with exec of inexistent command' '
174 git checkout primary &&
175 test_when_finished "git rebase --abort" &&
177 set_fake_editor &&
178 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
179 git rebase -i HEAD^ >actual 2>&1
180 ) &&
181 ! grep "Maybe git-rebase is broken" actual
184 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
185 test_when_finished "git rebase --abort ||:" &&
186 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
189 test_expect_success 'no changes are a nop' '
190 git checkout branch2 &&
191 git rebase -i F &&
192 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
193 test_cmp_rev I HEAD
196 test_expect_success 'test the [branch] option' '
197 git checkout -b dead-end &&
198 git rm file6 &&
199 git commit -m "stop here" &&
200 git rebase -i F branch2 &&
201 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
202 test_cmp_rev I branch2 &&
203 test_cmp_rev I HEAD
206 test_expect_success 'test --onto <branch>' '
207 git checkout -b test-onto branch2 &&
208 git rebase -i --onto branch1 F &&
209 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
210 test_cmp_rev HEAD^ branch1 &&
211 test_cmp_rev I branch2
214 test_expect_success 'rebase on top of a non-conflicting commit' '
215 git checkout branch1 &&
216 git tag original-branch1 &&
217 git rebase -i branch2 &&
218 test file6 = $(git diff --name-only original-branch1) &&
219 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
220 test_cmp_rev I branch2 &&
221 test_cmp_rev I HEAD~2
224 test_expect_success 'reflog for the branch shows state before rebase' '
225 test_cmp_rev branch1@{1} original-branch1
228 test_expect_success 'reflog for the branch shows correct finish message' '
229 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
230 "$(git rev-parse branch2)" >expected &&
231 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
232 test_cmp expected actual
235 test_expect_success 'exchange two commits' '
237 set_fake_editor &&
238 FAKE_LINES="2 1" git rebase -i HEAD~2
239 ) &&
240 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
241 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
242 blob1=$(git rev-parse --short HEAD^:file1) &&
243 blob2=$(git rev-parse --short HEAD:file1) &&
244 commit=$(git rev-parse --short HEAD)
247 test_expect_success 'stop on conflicting pick' '
248 cat >expect <<-EOF &&
249 diff --git a/file1 b/file1
250 index $blob1..$blob2 100644
251 --- a/file1
252 +++ b/file1
253 @@ -1 +1 @@
257 cat >expect2 <<-EOF &&
258 <<<<<<< HEAD
260 =======
262 >>>>>>> $commit (G)
264 git tag new-branch1 &&
265 test_must_fail git rebase -i primary &&
266 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
267 test_cmp expect .git/rebase-merge/patch &&
268 test_cmp expect2 file1 &&
269 test "$(git diff --name-status |
270 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
271 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
272 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
275 test_expect_success 'show conflicted patch' '
276 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
277 grep "show.*REBASE_HEAD" stderr &&
278 # the original stopped-sha1 is abbreviated
279 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
280 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
283 test_expect_success 'abort' '
284 git rebase --abort &&
285 test_cmp_rev new-branch1 HEAD &&
286 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
287 test_path_is_missing .git/rebase-merge
290 test_expect_success 'abort with error when new base cannot be checked out' '
291 git rm --cached file1 &&
292 git commit -m "remove file in base" &&
293 test_must_fail git rebase -i primary > output 2>&1 &&
294 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
295 output &&
296 test_i18ngrep "file1" output &&
297 test_path_is_missing .git/rebase-merge &&
298 rm file1 &&
299 git reset --hard HEAD^
302 test_expect_success 'retain authorship' '
303 echo A > file7 &&
304 git add file7 &&
305 test_tick &&
306 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
307 git tag twerp &&
308 git rebase -i --onto primary HEAD^ &&
309 git show HEAD | grep "^Author: Twerp Snog"
312 test_expect_success 'retain authorship w/ conflicts' '
313 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
314 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
316 git reset --hard twerp &&
317 test_commit a conflict a conflict-a &&
318 git reset --hard twerp &&
320 GIT_AUTHOR_NAME=AttributeMe &&
321 export GIT_AUTHOR_NAME &&
322 test_commit b conflict b conflict-b &&
323 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
325 test_must_fail git rebase -i conflict-a &&
326 echo resolved >conflict &&
327 git add conflict &&
328 git rebase --continue &&
329 test_cmp_rev conflict-a^0 HEAD^ &&
330 git show >out &&
331 grep AttributeMe out
334 test_expect_success 'squash' '
335 git reset --hard twerp &&
336 echo B > file7 &&
337 test_tick &&
338 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
339 echo "******************************" &&
341 set_fake_editor &&
342 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
343 git rebase -i --onto primary HEAD~2
344 ) &&
345 test B = $(cat file7) &&
346 test_cmp_rev HEAD^ primary
349 test_expect_success 'retain authorship when squashing' '
350 git show HEAD | grep "^Author: Twerp Snog"
353 test_expect_success '--continue tries to commit' '
354 git reset --hard D &&
355 test_tick &&
357 set_fake_editor &&
358 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
359 echo resolved > file1 &&
360 git add file1 &&
361 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
362 ) &&
363 test_cmp_rev HEAD^ new-branch1 &&
364 git show HEAD | grep chouette
367 test_expect_success 'verbose flag is heeded, even after --continue' '
368 git reset --hard primary@{1} &&
369 test_tick &&
370 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
371 echo resolved > file1 &&
372 git add file1 &&
373 git rebase --continue > output &&
374 grep "^ file1 | 2 +-$" output
377 test_expect_success 'multi-squash only fires up editor once' '
378 base=$(git rev-parse HEAD~4) &&
380 set_fake_editor &&
381 FAKE_COMMIT_AMEND="ONCE" \
382 FAKE_LINES="1 squash 2 squash 3 squash 4" \
383 EXPECT_HEADER_COUNT=4 \
384 git rebase -i $base
385 ) &&
386 test $base = $(git rev-parse HEAD^) &&
387 test 1 = $(git show | grep ONCE | wc -l)
390 test_expect_success 'multi-fixup does not fire up editor' '
391 git checkout -b multi-fixup E &&
392 base=$(git rev-parse HEAD~4) &&
394 set_fake_editor &&
395 FAKE_COMMIT_AMEND="NEVER" \
396 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
397 git rebase -i $base
398 ) &&
399 test $base = $(git rev-parse HEAD^) &&
400 test 0 = $(git show | grep NEVER | wc -l) &&
401 git checkout @{-1} &&
402 git branch -D multi-fixup
405 test_expect_success 'commit message used after conflict' '
406 git checkout -b conflict-fixup conflict-branch &&
407 base=$(git rev-parse HEAD~4) &&
409 set_fake_editor &&
410 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
411 git rebase -i $base &&
412 echo three > conflict &&
413 git add conflict &&
414 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
415 git rebase --continue
416 ) &&
417 test $base = $(git rev-parse HEAD^) &&
418 test 1 = $(git show | grep ONCE | wc -l) &&
419 git checkout @{-1} &&
420 git branch -D conflict-fixup
423 test_expect_success 'commit message retained after conflict' '
424 git checkout -b conflict-squash conflict-branch &&
425 base=$(git rev-parse HEAD~4) &&
427 set_fake_editor &&
428 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
429 git rebase -i $base &&
430 echo three > conflict &&
431 git add conflict &&
432 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
433 git rebase --continue
434 ) &&
435 test $base = $(git rev-parse HEAD^) &&
436 test 2 = $(git show | grep TWICE | wc -l) &&
437 git checkout @{-1} &&
438 git branch -D conflict-squash
441 test_expect_success 'squash and fixup generate correct log messages' '
442 cat >expect-squash-fixup <<-\EOF &&
447 ONCE
449 git checkout -b squash-fixup E &&
450 base=$(git rev-parse HEAD~4) &&
452 set_fake_editor &&
453 FAKE_COMMIT_AMEND="ONCE" \
454 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
455 EXPECT_HEADER_COUNT=4 \
456 git rebase -i $base
457 ) &&
458 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
459 test_cmp expect-squash-fixup actual-squash-fixup &&
460 git cat-file commit HEAD@{2} |
461 grep "^# This is a combination of 3 commits\." &&
462 git cat-file commit HEAD@{3} |
463 grep "^# This is a combination of 2 commits\." &&
464 git checkout @{-1} &&
465 git branch -D squash-fixup
468 test_expect_success 'squash ignores comments' '
469 git checkout -b skip-comments E &&
470 base=$(git rev-parse HEAD~4) &&
472 set_fake_editor &&
473 FAKE_COMMIT_AMEND="ONCE" \
474 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
475 EXPECT_HEADER_COUNT=4 \
476 git rebase -i $base
477 ) &&
478 test $base = $(git rev-parse HEAD^) &&
479 test 1 = $(git show | grep ONCE | wc -l) &&
480 git checkout @{-1} &&
481 git branch -D skip-comments
484 test_expect_success 'squash ignores blank lines' '
485 git checkout -b skip-blank-lines E &&
486 base=$(git rev-parse HEAD~4) &&
488 set_fake_editor &&
489 FAKE_COMMIT_AMEND="ONCE" \
490 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
491 EXPECT_HEADER_COUNT=4 \
492 git rebase -i $base
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 skip-blank-lines
500 test_expect_success 'squash works as expected' '
501 git checkout -b squash-works no-conflict-branch &&
502 one=$(git rev-parse HEAD~3) &&
504 set_fake_editor &&
505 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
506 ) &&
507 test $one = $(git rev-parse HEAD~2)
510 test_expect_success 'interrupted squash works as expected' '
511 git checkout -b interrupted-squash conflict-branch &&
512 one=$(git rev-parse HEAD~3) &&
514 set_fake_editor &&
515 test_must_fail env FAKE_LINES="1 squash 3 2" \
516 git rebase -i HEAD~3
517 ) &&
518 test_write_lines one two four > conflict &&
519 git add conflict &&
520 test_must_fail git rebase --continue &&
521 echo resolved > conflict &&
522 git add conflict &&
523 git rebase --continue &&
524 test $one = $(git rev-parse HEAD~2)
527 test_expect_success 'interrupted squash works as expected (case 2)' '
528 git checkout -b interrupted-squash2 conflict-branch &&
529 one=$(git rev-parse HEAD~3) &&
531 set_fake_editor &&
532 test_must_fail env FAKE_LINES="3 squash 1 2" \
533 git rebase -i HEAD~3
534 ) &&
535 test_write_lines one four > conflict &&
536 git add conflict &&
537 test_must_fail git rebase --continue &&
538 test_write_lines one two four > conflict &&
539 git add conflict &&
540 test_must_fail git rebase --continue &&
541 echo resolved > conflict &&
542 git add conflict &&
543 git rebase --continue &&
544 test $one = $(git rev-parse HEAD~2)
547 test_expect_success '--continue tries to commit, even for "edit"' '
548 echo unrelated > file7 &&
549 git add file7 &&
550 test_tick &&
551 git commit -m "unrelated change" &&
552 parent=$(git rev-parse HEAD^) &&
553 test_tick &&
555 set_fake_editor &&
556 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
557 echo edited > file7 &&
558 git add file7 &&
559 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
560 ) &&
561 test edited = $(git show HEAD:file7) &&
562 git show HEAD | grep chouette &&
563 test $parent = $(git rev-parse HEAD^)
566 test_expect_success 'aborted --continue does not squash commits after "edit"' '
567 old=$(git rev-parse HEAD) &&
568 test_tick &&
570 set_fake_editor &&
571 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
572 echo "edited again" > file7 &&
573 git add file7 &&
574 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
575 ) &&
576 test $old = $(git rev-parse HEAD) &&
577 git rebase --abort
580 test_expect_success 'auto-amend only edited commits after "edit"' '
581 test_tick &&
583 set_fake_editor &&
584 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
585 echo "edited again" > file7 &&
586 git add file7 &&
587 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
588 echo "and again" > file7 &&
589 git add file7 &&
590 test_tick &&
591 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
592 git rebase --continue
593 ) &&
594 git rebase --abort
597 test_expect_success 'clean error after failed "exec"' '
598 test_tick &&
599 test_when_finished "git rebase --abort || :" &&
601 set_fake_editor &&
602 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
603 ) &&
604 echo "edited again" > file7 &&
605 git add file7 &&
606 test_must_fail git rebase --continue 2>error &&
607 test_i18ngrep "you have staged changes in your working tree" error
610 test_expect_success 'rebase a detached HEAD' '
611 grandparent=$(git rev-parse HEAD~2) &&
612 git checkout $(git rev-parse HEAD) &&
613 test_tick &&
615 set_fake_editor &&
616 FAKE_LINES="2 1" git rebase -i HEAD~2
617 ) &&
618 test $grandparent = $(git rev-parse HEAD~2)
621 test_expect_success 'rebase a commit violating pre-commit' '
622 test_hook pre-commit <<-\EOF &&
623 test -z "$(git diff --cached --check)"
625 echo "monde! " >> file1 &&
626 test_tick &&
627 test_must_fail git commit -m doesnt-verify file1 &&
628 git commit -m doesnt-verify --no-verify file1 &&
629 test_tick &&
631 set_fake_editor &&
632 FAKE_LINES=2 git rebase -i HEAD~2
636 test_expect_success 'rebase with a file named HEAD in worktree' '
637 git reset --hard &&
638 git checkout -b branch3 A &&
641 GIT_AUTHOR_NAME="Squashed Away" &&
642 export GIT_AUTHOR_NAME &&
643 >HEAD &&
644 git add HEAD &&
645 git commit -m "Add head" &&
646 >BODY &&
647 git add BODY &&
648 git commit -m "Add body"
649 ) &&
652 set_fake_editor &&
653 FAKE_LINES="1 squash 2" git rebase -i @{-1}
654 ) &&
655 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
659 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
661 git checkout -b branch4 HEAD &&
662 GIT_EDITOR=: git commit --amend \
663 --author="Somebody else <somebody@else.com>" &&
664 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
665 git rebase -i branch3 &&
666 test_cmp_rev branch3 branch4
670 test_expect_success 'submodule rebase setup' '
671 git checkout A &&
672 mkdir sub &&
674 cd sub && git init && >elif &&
675 git add elif && git commit -m "submodule initial"
676 ) &&
677 echo 1 >file1 &&
678 git add file1 sub &&
679 test_tick &&
680 git commit -m "One" &&
681 echo 2 >file1 &&
682 test_tick &&
683 git commit -a -m "Two" &&
685 cd sub && echo 3 >elif &&
686 git commit -a -m "submodule second"
687 ) &&
688 test_tick &&
689 git commit -a -m "Three changes submodule"
692 test_expect_success 'submodule rebase -i' '
694 set_fake_editor &&
695 FAKE_LINES="1 squash 2 3" git rebase -i A
699 test_expect_success 'submodule conflict setup' '
700 git tag submodule-base &&
701 git checkout HEAD^ &&
703 cd sub && git checkout HEAD^ && echo 4 >elif &&
704 git add elif && git commit -m "submodule conflict"
705 ) &&
706 git add sub &&
707 test_tick &&
708 git commit -m "Conflict in submodule" &&
709 git tag submodule-topic
712 test_expect_success 'rebase -i continue with only submodule staged' '
713 test_must_fail git rebase -i submodule-base &&
714 git add sub &&
715 git rebase --continue &&
716 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
719 test_expect_success 'rebase -i continue with unstaged submodule' '
720 git checkout submodule-topic &&
721 git reset --hard &&
722 test_must_fail git rebase -i submodule-base &&
723 git reset &&
724 git rebase --continue &&
725 test_cmp_rev submodule-base HEAD
728 test_expect_success 'avoid unnecessary reset' '
729 git checkout primary &&
730 git reset --hard &&
731 test-tool chmtime =123456789 file3 &&
732 git update-index --refresh &&
733 HEAD=$(git rev-parse HEAD) &&
734 git rebase -i HEAD~4 &&
735 test $HEAD = $(git rev-parse HEAD) &&
736 MTIME=$(test-tool chmtime --get file3) &&
737 test 123456789 = $MTIME
740 test_expect_success 'reword' '
741 git checkout -b reword-branch primary &&
743 set_fake_editor &&
744 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
745 git rebase -i A &&
746 git show HEAD | grep "E changed" &&
747 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
748 test_cmp_rev primary^ HEAD^ &&
749 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
750 git rebase -i A &&
751 git show HEAD^ | grep "D changed" &&
752 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
753 git rebase -i A &&
754 git show HEAD~3 | grep "B changed" &&
755 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
756 git rebase -i A
757 ) &&
758 git show HEAD~2 | grep "C changed"
761 test_expect_success 'no uncommited changes when rewording the todo list is reloaded' '
762 git checkout E &&
763 test_when_finished "git checkout @{-1}" &&
765 set_fake_editor &&
766 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
767 export GIT_SEQUENCE_EDITOR &&
768 set_reword_editor &&
769 FAKE_LINES="reword 1 reword 2" git rebase -i C
770 ) &&
771 check_reworded_commits D E
774 test_expect_success 'rebase -i can copy notes' '
775 git config notes.rewrite.rebase true &&
776 git config notes.rewriteRef "refs/notes/*" &&
777 test_commit n1 &&
778 test_commit n2 &&
779 test_commit n3 &&
780 git notes add -m"a note" n3 &&
781 git rebase -i --onto n1 n2 &&
782 test "a note" = "$(git notes show HEAD)"
785 test_expect_success 'rebase -i can copy notes over a fixup' '
786 cat >expect <<-\EOF &&
787 an earlier note
789 a note
791 git reset --hard n3 &&
792 git notes add -m"an earlier note" n2 &&
794 set_fake_editor &&
795 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
796 git rebase -i n1
797 ) &&
798 git notes show > output &&
799 test_cmp expect output
802 test_expect_success 'rebase while detaching HEAD' '
803 git symbolic-ref HEAD &&
804 grandparent=$(git rev-parse HEAD~2) &&
805 test_tick &&
807 set_fake_editor &&
808 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
809 ) &&
810 test $grandparent = $(git rev-parse HEAD~2) &&
811 test_must_fail git symbolic-ref HEAD
814 test_tick # Ensure that the rebased commits get a different timestamp.
815 test_expect_success 'always cherry-pick with --no-ff' '
816 git checkout no-ff-branch &&
817 git tag original-no-ff-branch &&
818 git rebase -i --no-ff A &&
819 for p in 0 1 2
821 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
822 git diff HEAD~$p original-no-ff-branch~$p > out &&
823 test_must_be_empty out || return 1
824 done &&
825 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
826 git diff HEAD~3 original-no-ff-branch~3 > out &&
827 test_must_be_empty out
830 test_expect_success 'set up commits with funny messages' '
831 git checkout -b funny A &&
832 echo >>file1 &&
833 test_tick &&
834 git commit -a -m "end with slash\\" &&
835 echo >>file1 &&
836 test_tick &&
837 git commit -a -m "something (\000) that looks like octal" &&
838 echo >>file1 &&
839 test_tick &&
840 git commit -a -m "something (\n) that looks like a newline" &&
841 echo >>file1 &&
842 test_tick &&
843 git commit -a -m "another commit"
846 test_expect_success 'rebase-i history with funny messages' '
847 git rev-list A..funny >expect &&
848 test_tick &&
850 set_fake_editor &&
851 FAKE_LINES="1 2 3 4" git rebase -i A
852 ) &&
853 git rev-list A.. >actual &&
854 test_cmp expect actual
857 test_expect_success 'prepare for rebase -i --exec' '
858 git checkout primary &&
859 git checkout -b execute &&
860 test_commit one_exec main.txt one_exec &&
861 test_commit two_exec main.txt two_exec &&
862 test_commit three_exec main.txt three_exec
865 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
867 set_fake_editor &&
868 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
869 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
870 export FAKE_LINES &&
871 git rebase -i HEAD~2 >expect
872 ) &&
873 sed -e "1,9d" expect >expected &&
874 test_cmp expected actual
877 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
878 git reset --hard execute &&
880 set_fake_editor &&
881 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
882 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
883 export FAKE_LINES &&
884 git rebase -i HEAD~2 >expect
885 ) &&
886 sed -e "1,9d" expect >expected &&
887 test_cmp expected actual
890 test_expect_success 'running "git rebase -ix git show HEAD"' '
891 git reset --hard execute &&
893 set_fake_editor &&
894 git rebase -ix "git show HEAD" HEAD~2 >actual &&
895 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
896 export FAKE_LINES &&
897 git rebase -i HEAD~2 >expect
898 ) &&
899 sed -e "1,9d" expect >expected &&
900 test_cmp expected actual
904 test_expect_success 'rebase -ix with several <CMD>' '
905 git reset --hard execute &&
907 set_fake_editor &&
908 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
909 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
910 export FAKE_LINES &&
911 git rebase -i HEAD~2 >expect
912 ) &&
913 sed -e "1,9d" expect >expected &&
914 test_cmp expected actual
917 test_expect_success 'rebase -ix with several instances of --exec' '
918 git reset --hard execute &&
920 set_fake_editor &&
921 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
922 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
923 exec_git_show_HEAD exec_pwd" &&
924 export FAKE_LINES &&
925 git rebase -i HEAD~2 >expect
926 ) &&
927 sed -e "1,11d" expect >expected &&
928 test_cmp expected actual
931 test_expect_success 'rebase -ix with --autosquash' '
932 git reset --hard execute &&
933 git checkout -b autosquash &&
934 echo second >second.txt &&
935 git add second.txt &&
936 git commit -m "fixup! two_exec" &&
937 echo bis >bis.txt &&
938 git add bis.txt &&
939 git commit -m "fixup! two_exec" &&
940 git checkout -b autosquash_actual &&
941 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
942 git checkout autosquash &&
944 set_fake_editor &&
945 git checkout -b autosquash_expected &&
946 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
947 export FAKE_LINES &&
948 git rebase -i HEAD~4 >expect
949 ) &&
950 sed -e "1,13d" expect >expected &&
951 test_cmp expected actual
954 test_expect_success 'rebase --exec works without -i ' '
955 git reset --hard execute &&
956 rm -rf exec_output &&
957 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
958 test_i18ngrep "Successfully rebased and updated" actual &&
959 test_line_count = 2 exec_output &&
960 test_path_is_missing invoked_editor
963 test_expect_success 'rebase -i --exec without <CMD>' '
964 git reset --hard execute &&
965 test_must_fail git rebase -i --exec 2>actual &&
966 test_i18ngrep "requires a value" actual &&
967 git checkout primary
970 test_expect_success 'rebase -i --root re-order and drop commits' '
971 git checkout E &&
973 set_fake_editor &&
974 FAKE_LINES="3 1 2 5" git rebase -i --root
975 ) &&
976 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
977 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
978 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
979 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
980 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
983 test_expect_success 'rebase -i --root retain root commit author and message' '
984 git checkout A &&
985 echo B >file7 &&
986 git add file7 &&
987 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
989 set_fake_editor &&
990 FAKE_LINES="2" git rebase -i --root
991 ) &&
992 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
993 git cat-file commit HEAD | grep -q "^different author$"
996 test_expect_success 'rebase -i --root temporary sentinel commit' '
997 git checkout B &&
999 set_fake_editor &&
1000 test_must_fail env FAKE_LINES="2" git rebase -i --root
1001 ) &&
1002 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1003 git rebase --abort
1006 test_expect_success 'rebase -i --root fixup root commit' '
1007 git checkout B &&
1009 set_fake_editor &&
1010 FAKE_LINES="1 fixup 2" git rebase -i --root
1011 ) &&
1012 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1013 test B = $(git show HEAD:file1) &&
1014 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1017 test_expect_success 'rebase -i --root reword original root commit' '
1018 test_when_finished "test_might_fail git rebase --abort" &&
1019 git checkout -b reword-original-root-branch primary &&
1021 set_fake_editor &&
1022 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1023 git rebase -i --root
1024 ) &&
1025 git show HEAD^ | grep "A changed" &&
1026 test -z "$(git show -s --format=%p HEAD^)"
1029 test_expect_success 'rebase -i --root reword new root commit' '
1030 test_when_finished "test_might_fail git rebase --abort" &&
1031 git checkout -b reword-now-root-branch primary &&
1033 set_fake_editor &&
1034 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1035 git rebase -i --root
1036 ) &&
1037 git show HEAD^ | grep "C changed" &&
1038 test -z "$(git show -s --format=%p HEAD^)"
1041 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1042 test_when_finished "reset_rebase" &&
1043 git checkout -b failing-root-pick A &&
1044 echo x >file2 &&
1045 git rm file1 &&
1046 git commit -m "remove file 1 add file 2" &&
1047 echo z >file1 &&
1049 set_fake_editor &&
1050 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1051 ) &&
1052 rm file1 &&
1053 git rebase --continue &&
1054 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1055 test "$(git rev-list --count HEAD)" = 2
1058 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1059 test_when_finished "reset_rebase" &&
1060 echo z>file1 &&
1062 set_fake_editor &&
1063 test_must_fail env FAKE_LINES="reword 1 2" \
1064 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1065 rm file1 &&
1066 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1067 ) &&
1068 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1069 test "$(git rev-list --count HEAD)" = 2
1072 test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
1073 git checkout reword-original-root-branch &&
1074 git reset --hard &&
1075 git checkout conflict-branch &&
1077 set_fake_editor &&
1078 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1079 test_must_fail git rebase --edit-todo
1080 ) &&
1081 git rebase --abort
1084 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1085 git reset --hard &&
1086 git checkout no-conflict-branch^0 &&
1088 set_fake_editor &&
1089 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1090 FAKE_LINES="2 1" git rebase --edit-todo &&
1091 git rebase --continue
1092 ) &&
1093 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1094 test L = $(git cat-file commit HEAD | sed -ne \$p)
1097 test_expect_success 'rebase -i produces readable reflog' '
1098 git reset --hard &&
1099 git branch -f branch-reflog-test H &&
1100 git rebase -i --onto I F branch-reflog-test &&
1101 cat >expect <<-\EOF &&
1102 rebase (finish): returning to refs/heads/branch-reflog-test
1103 rebase (pick): H
1104 rebase (pick): G
1105 rebase (start): checkout I
1107 git reflog -n4 HEAD |
1108 sed "s/[^:]*: //" >actual &&
1109 test_cmp expect actual
1112 test_expect_success 'rebase -i respects core.commentchar' '
1113 git reset --hard &&
1114 git checkout E^0 &&
1115 test_config core.commentchar "\\" &&
1116 write_script remove-all-but-first.sh <<-\EOF &&
1117 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1118 mv "$1.tmp" "$1"
1121 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1122 git rebase -i B
1123 ) &&
1124 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1127 test_expect_success 'rebase -i respects core.commentchar=auto' '
1128 test_config core.commentchar auto &&
1129 write_script copy-edit-script.sh <<-\EOF &&
1130 cp "$1" edit-script
1132 test_when_finished "git rebase --abort || :" &&
1134 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1135 git rebase -i HEAD^
1136 ) &&
1137 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1140 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1141 test_when_finished "git branch -D torebase" &&
1142 git checkout -b torebase branch1 &&
1143 upstream=$(git rev-parse ":/J") &&
1144 onto=$(git rev-parse ":/A") &&
1145 git rebase --onto $onto $upstream &&
1146 git reset --hard branch1 &&
1147 git rebase --onto ":/A" ":/J" &&
1148 git checkout branch1
1151 test_expect_success 'rebase -i with --strategy and -X' '
1152 git checkout -b conflict-merge-use-theirs conflict-branch &&
1153 git reset --hard HEAD^ &&
1154 echo five >conflict &&
1155 echo Z >file1 &&
1156 git commit -a -m "one file conflict" &&
1157 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1158 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1159 test $(cat file1) = Z
1162 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1163 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1164 git reset --hard HEAD^ &&
1165 >breakpoint &&
1166 git add breakpoint &&
1167 git commit -m "breakpoint for interactive mode" &&
1168 echo five >conflict &&
1169 echo Z >file1 &&
1170 git commit -a -m "one file conflict" &&
1172 set_fake_editor &&
1173 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1174 -Xours conflict-branch
1175 ) &&
1176 git rebase --continue &&
1177 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1178 test $(cat file1) = Z
1181 test_expect_success 'rebase -i error on commits with \ in message' '
1182 current_head=$(git rev-parse HEAD) &&
1183 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1184 test_commit TO-REMOVE will-conflict old-content &&
1185 test_commit "\temp" will-conflict new-content dummy &&
1186 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1187 test_expect_code 1 grep " emp" error
1190 test_expect_success 'short commit ID setup' '
1191 test_when_finished "git checkout primary" &&
1192 git checkout --orphan collide &&
1193 git rm -rf . &&
1195 unset test_tick &&
1196 test_commit collide1 collide &&
1197 test_commit --notick collide2 collide &&
1198 test_commit --notick collide3 collide
1202 if test -n "$GIT_TEST_FIND_COLLIDER"
1203 then
1204 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1205 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1206 blob="$(git rev-parse collide2:collide)"
1207 from="$(git rev-parse collide1^0)"
1208 repl="commit refs/heads/collider-&\\n"
1209 repl="${repl}author $author\\ncommitter $committer\\n"
1210 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1211 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1212 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1213 git fast-import <script &&
1214 git pack-refs &&
1215 git for-each-ref >refs &&
1216 grep "^$(test_oid t3404_collision)" <refs >matches &&
1217 cat matches &&
1218 test_line_count -gt 2 matches || {
1219 echo "Could not find a collider" >&2
1220 exit 1
1224 test_expect_success 'short commit ID collide' '
1225 test_oid_cache <<-EOF &&
1226 # collision-related constants
1227 t3404_collision sha1:6bcd
1228 t3404_collision sha256:0161
1229 t3404_collider sha1:ac4f2ee
1230 t3404_collider sha256:16697
1232 test_when_finished "reset_rebase && git checkout primary" &&
1233 git checkout collide &&
1234 colliding_id=$(test_oid t3404_collision) &&
1235 hexsz=$(test_oid hexsz) &&
1236 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1237 test_config core.abbrev 4 &&
1239 unset test_tick &&
1240 test_tick &&
1241 set_fake_editor &&
1242 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1243 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1244 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1245 grep "^pick $colliding_id " \
1246 .git/rebase-merge/git-rebase-todo.tmp &&
1247 grep -E "^pick [0-9a-f]{$hexsz}" \
1248 .git/rebase-merge/git-rebase-todo &&
1249 grep -E "^pick [0-9a-f]{$hexsz}" \
1250 .git/rebase-merge/git-rebase-todo.backup &&
1251 git rebase --continue
1252 ) &&
1253 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1254 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1255 test "$collide2" = "$collide3"
1258 test_expect_success 'respect core.abbrev' '
1259 git config core.abbrev 12 &&
1261 set_cat_todo_editor &&
1262 test_must_fail git rebase -i HEAD~4 >todo-list
1263 ) &&
1264 test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list)
1267 test_expect_success 'todo count' '
1268 write_script dump-raw.sh <<-\EOF &&
1269 cat "$1"
1272 test_set_editor "$(pwd)/dump-raw.sh" &&
1273 git rebase -i HEAD~4 >actual
1274 ) &&
1275 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1278 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1279 git checkout --force branch2 &&
1280 git clean -f &&
1282 set_fake_editor &&
1283 FAKE_LINES="edit 1 2" git rebase -i A
1284 ) &&
1285 test_cmp_rev HEAD F &&
1286 test_path_is_missing file6 &&
1287 >file6 &&
1288 test_must_fail git rebase --continue &&
1289 test_cmp_rev HEAD F &&
1290 rm file6 &&
1291 git rebase --continue &&
1292 test_cmp_rev HEAD I
1295 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1296 git checkout --force branch2 &&
1297 git clean -f &&
1298 git tag original-branch2 &&
1300 set_fake_editor &&
1301 FAKE_LINES="edit 1 squash 2" git rebase -i A
1302 ) &&
1303 test_cmp_rev HEAD F &&
1304 test_path_is_missing file6 &&
1305 >file6 &&
1306 test_must_fail git rebase --continue &&
1307 test_cmp_rev HEAD F &&
1308 rm file6 &&
1309 git rebase --continue &&
1310 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1311 git reset --hard original-branch2
1314 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1315 git checkout --force branch2 &&
1316 git clean -f &&
1318 set_fake_editor &&
1319 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1320 ) &&
1321 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1322 test_path_is_missing file6 &&
1323 >file6 &&
1324 test_must_fail git rebase --continue &&
1325 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1326 rm file6 &&
1327 git rebase --continue &&
1328 test $(git cat-file commit HEAD | sed -ne \$p) = I
1331 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1332 git checkout -b commit-to-skip &&
1333 for double in X 3 1
1335 test_seq 5 | sed "s/$double/&&/" >seq &&
1336 git add seq &&
1337 test_tick &&
1338 git commit -m seq-$double || return 1
1339 done &&
1340 git tag seq-onto &&
1341 git reset --hard HEAD~2 &&
1342 git cherry-pick seq-onto &&
1344 set_fake_editor &&
1345 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1346 ) &&
1347 test -d .git/rebase-merge &&
1348 git rebase --continue &&
1349 git diff --exit-code seq-onto &&
1350 test ! -d .git/rebase-merge &&
1351 test ! -f .git/CHERRY_PICK_HEAD
1354 rebase_setup_and_clean () {
1355 test_when_finished "
1356 git checkout primary &&
1357 test_might_fail git branch -D $1 &&
1358 test_might_fail git rebase --abort
1359 " &&
1360 git checkout -b $1 ${2:-primary}
1363 test_expect_success 'drop' '
1364 rebase_setup_and_clean drop-test &&
1366 set_fake_editor &&
1367 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1368 ) &&
1369 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1370 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1371 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1374 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1375 test_config rebase.missingCommitsCheck ignore &&
1376 rebase_setup_and_clean missing-commit &&
1378 set_fake_editor &&
1379 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1380 ) &&
1381 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1382 test_i18ngrep \
1383 "Successfully rebased and updated refs/heads/missing-commit" \
1384 actual
1387 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1388 cat >expect <<-EOF &&
1389 Warning: some commits may have been dropped accidentally.
1390 Dropped commits (newer to older):
1391 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1392 To avoid this message, use "drop" to explicitly remove a commit.
1394 test_config rebase.missingCommitsCheck warn &&
1395 rebase_setup_and_clean missing-commit &&
1397 set_fake_editor &&
1398 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1399 ) &&
1400 head -n4 actual.2 >actual &&
1401 test_cmp expect actual &&
1402 test D = $(git cat-file commit HEAD | sed -ne \$p)
1405 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1406 cat >expect <<-EOF &&
1407 Warning: some commits may have been dropped accidentally.
1408 Dropped commits (newer to older):
1409 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1410 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1411 To avoid this message, use "drop" to explicitly remove a commit.
1413 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1414 The possible behaviours are: ignore, warn, error.
1416 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1417 Or you can abort the rebase with '\''git rebase --abort'\''.
1419 test_config rebase.missingCommitsCheck error &&
1420 rebase_setup_and_clean missing-commit &&
1422 set_fake_editor &&
1423 test_must_fail env FAKE_LINES="1 2 4" \
1424 git rebase -i --root 2>actual &&
1425 test_cmp expect actual &&
1426 cp .git/rebase-merge/git-rebase-todo.backup \
1427 .git/rebase-merge/git-rebase-todo &&
1428 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1429 ) &&
1430 git rebase --continue &&
1431 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1432 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1435 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1436 test_config rebase.missingCommitsCheck ignore &&
1437 rebase_setup_and_clean missing-commit &&
1439 set_fake_editor &&
1440 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1441 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1442 git rebase --continue 2>actual
1443 ) &&
1444 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1445 test_i18ngrep \
1446 "Successfully rebased and updated refs/heads/missing-commit" \
1447 actual
1450 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1451 cat >expect <<-EOF &&
1452 error: invalid command '\''pickled'\''
1453 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1454 Warning: some commits may have been dropped accidentally.
1455 Dropped commits (newer to older):
1456 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1457 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1458 To avoid this message, use "drop" to explicitly remove a commit.
1460 head -n5 expect >expect.2 &&
1461 tail -n1 expect >>expect.2 &&
1462 tail -n4 expect.2 >expect.3 &&
1463 test_config rebase.missingCommitsCheck warn &&
1464 rebase_setup_and_clean missing-commit &&
1466 set_fake_editor &&
1467 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1468 git rebase -i --root &&
1469 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1470 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1471 head -n7 actual.2 >actual &&
1472 test_cmp expect actual &&
1473 cp orig .git/rebase-merge/git-rebase-todo &&
1474 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1475 head -n4 actual.2 >actual &&
1476 test_cmp expect.3 actual &&
1477 git rebase --continue 2>actual
1478 ) &&
1479 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1480 test_i18ngrep \
1481 "Successfully rebased and updated refs/heads/missing-commit" \
1482 actual
1485 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1486 cat >expect <<-EOF &&
1487 error: invalid command '\''pickled'\''
1488 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1489 Warning: some commits may have been dropped accidentally.
1490 Dropped commits (newer to older):
1491 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1492 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1493 To avoid this message, use "drop" to explicitly remove a commit.
1495 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1496 The possible behaviours are: ignore, warn, error.
1498 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1499 Or you can abort the rebase with '\''git rebase --abort'\''.
1501 tail -n11 expect >expect.2 &&
1502 head -n3 expect.2 >expect.3 &&
1503 tail -n7 expect.2 >>expect.3 &&
1504 test_config rebase.missingCommitsCheck error &&
1505 rebase_setup_and_clean missing-commit &&
1507 set_fake_editor &&
1508 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1509 git rebase -i --root &&
1510 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1511 test_must_fail env FAKE_LINES="2 3 4" \
1512 git rebase --edit-todo 2>actual &&
1513 test_cmp expect actual &&
1514 test_must_fail git rebase --continue 2>actual &&
1515 test_cmp expect.2 actual &&
1516 test_must_fail git rebase --edit-todo &&
1517 cp orig .git/rebase-merge/git-rebase-todo &&
1518 test_must_fail env FAKE_LINES="1 2 3 4" \
1519 git rebase --edit-todo 2>actual &&
1520 test_cmp expect.3 actual &&
1521 test_must_fail git rebase --continue 2>actual &&
1522 test_cmp expect.3 actual &&
1523 cp orig .git/rebase-merge/git-rebase-todo &&
1524 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1525 git rebase --continue 2>actual
1526 ) &&
1527 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1528 test_i18ngrep \
1529 "Successfully rebased and updated refs/heads/missing-commit" \
1530 actual
1533 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1534 test_config rebase.missingCommitsCheck error &&
1536 set_fake_editor &&
1537 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1538 ) &&
1539 git rebase --edit-todo &&
1540 test_must_fail git rebase --continue &&
1541 echo x >file1 &&
1542 git add file1 &&
1543 git rebase --continue
1546 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1547 test_config rebase.missingCommitsCheck error &&
1549 set_fake_editor &&
1550 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1551 cp .git/rebase-merge/git-rebase-todo todo &&
1552 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1553 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1554 git rebase --continue
1558 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1559 rebase_setup_and_clean abbrevcmd &&
1560 test_commit "first" file1.txt "first line" first &&
1561 test_commit "second" file1.txt "another line" second &&
1562 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1563 test_commit "squash! second" file1.txt "another line here" second_squash &&
1564 cat >expected <<-EOF &&
1565 p $(git rev-list --abbrev-commit -1 first) first
1566 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1567 x git show HEAD
1568 p $(git rev-list --abbrev-commit -1 second) second
1569 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1570 x git show HEAD
1572 git checkout abbrevcmd &&
1573 test_config rebase.abbreviateCommands true &&
1575 set_cat_todo_editor &&
1576 test_must_fail git rebase -i --exec "git show HEAD" \
1577 --autosquash primary >actual
1578 ) &&
1579 test_cmp expected actual
1582 test_expect_success 'static check of bad command' '
1583 rebase_setup_and_clean bad-cmd &&
1585 set_fake_editor &&
1586 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1587 git rebase -i --root 2>actual &&
1588 test_i18ngrep "pickled $(git rev-list --oneline -1 primary~1)" \
1589 actual &&
1590 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1591 actual &&
1592 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1593 ) &&
1594 git rebase --continue &&
1595 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1596 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1599 test_expect_success 'the first command cannot be a fixup' '
1600 rebase_setup_and_clean fixup-first &&
1602 cat >orig <<-EOF &&
1603 fixup $(git log -1 --format="%h %s" B)
1604 pick $(git log -1 --format="%h %s" C)
1608 set_replace_editor orig &&
1609 test_must_fail git rebase -i A 2>actual
1610 ) &&
1611 grep "cannot .fixup. without a previous commit" actual &&
1612 grep "You can fix this with .git rebase --edit-todo.." actual &&
1613 # verify that the todo list has not been truncated
1614 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1615 test_cmp orig actual &&
1617 test_must_fail git rebase --edit-todo 2>actual &&
1618 grep "cannot .fixup. without a previous commit" actual &&
1619 grep "You can fix this with .git rebase --edit-todo.." actual &&
1620 # verify that the todo list has not been truncated
1621 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1622 test_cmp orig actual
1625 test_expect_success 'tabs and spaces are accepted in the todolist' '
1626 rebase_setup_and_clean indented-comment &&
1627 write_script add-indent.sh <<-\EOF &&
1629 # Turn single spaces into space/tab mix
1630 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1631 printf "\n\t# comment\n #more\n\t # comment\n"
1632 ) >"$1.new"
1633 mv "$1.new" "$1"
1636 test_set_editor "$(pwd)/add-indent.sh" &&
1637 git rebase -i HEAD^^^
1638 ) &&
1639 test E = $(git cat-file commit HEAD | sed -ne \$p)
1642 test_expect_success 'static check of bad SHA-1' '
1643 rebase_setup_and_clean bad-sha &&
1645 set_fake_editor &&
1646 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1647 git rebase -i --root 2>actual &&
1648 test_i18ngrep "edit XXXXXXX False commit" actual &&
1649 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1650 actual &&
1651 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1652 ) &&
1653 git rebase --continue &&
1654 test E = $(git cat-file commit HEAD | sed -ne \$p)
1657 test_expect_success 'editor saves as CR/LF' '
1658 git checkout -b with-crlf &&
1659 write_script add-crs.sh <<-\EOF &&
1660 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1661 mv -f "$1".new "$1"
1664 test_set_editor "$(pwd)/add-crs.sh" &&
1665 git rebase -i HEAD^
1669 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1670 test_when_finished "test_might_fail git rebase --abort" &&
1672 set_fake_editor &&
1673 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1674 HEAD^ >out 2>err
1675 ) &&
1676 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1679 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1680 test_when_finished "test_might_fail git rebase --abort" &&
1681 test_config commit.gpgsign true &&
1683 set_fake_editor &&
1684 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1685 HEAD^ >out 2>err
1686 ) &&
1687 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1690 test_expect_success 'valid author header after --root swap' '
1691 rebase_setup_and_clean author-header no-conflict-branch &&
1692 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1693 git cat-file commit HEAD | grep ^author >expected &&
1695 set_fake_editor &&
1696 FAKE_LINES="5 1" git rebase -i --root
1697 ) &&
1698 git cat-file commit HEAD^ | grep ^author >actual &&
1699 test_cmp expected actual
1702 test_expect_success 'valid author header when author contains single quote' '
1703 rebase_setup_and_clean author-header no-conflict-branch &&
1704 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1705 git cat-file commit HEAD | grep ^author >expected &&
1707 set_fake_editor &&
1708 FAKE_LINES="2" git rebase -i HEAD~2
1709 ) &&
1710 git cat-file commit HEAD | grep ^author >actual &&
1711 test_cmp expected actual
1714 test_expect_success 'post-commit hook is called' '
1715 >actual &&
1716 test_hook post-commit <<-\EOS &&
1717 git rev-parse HEAD >>actual
1720 set_fake_editor &&
1721 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1722 echo x>file3 &&
1723 git add file3 &&
1724 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1725 ) &&
1726 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1727 >expect &&
1728 test_cmp expect actual
1731 test_expect_success 'correct error message for partial commit after empty pick' '
1732 test_when_finished "git rebase --abort" &&
1734 set_fake_editor &&
1735 FAKE_LINES="2 1 1" &&
1736 export FAKE_LINES &&
1737 test_must_fail git rebase -i A D
1738 ) &&
1739 echo x >file1 &&
1740 test_must_fail git commit file1 2>err &&
1741 test_i18ngrep "cannot do a partial commit during a rebase." err
1744 test_expect_success 'correct error message for commit --amend after empty pick' '
1745 test_when_finished "git rebase --abort" &&
1747 set_fake_editor &&
1748 FAKE_LINES="1 1" &&
1749 export FAKE_LINES &&
1750 test_must_fail git rebase -i A D
1751 ) &&
1752 echo x>file1 &&
1753 test_must_fail git commit -a --amend 2>err &&
1754 test_i18ngrep "middle of a rebase -- cannot amend." err
1757 test_expect_success 'todo has correct onto hash' '
1758 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1759 onto=$(git rev-parse --short HEAD~4) &&
1760 test_i18ngrep "^# Rebase ..* onto $onto" actual
1763 test_expect_success 'ORIG_HEAD is updated correctly' '
1764 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1765 git checkout -b test-orig-head A &&
1766 git commit --allow-empty -m A1 &&
1767 git commit --allow-empty -m A2 &&
1768 git commit --allow-empty -m A3 &&
1769 git commit --allow-empty -m A4 &&
1770 git rebase primary &&
1771 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1774 test_expect_success '--update-refs adds label and update-ref commands' '
1775 git checkout -b update-refs no-conflict-branch &&
1776 git branch -f base HEAD~4 &&
1777 git branch -f first HEAD~3 &&
1778 git branch -f second HEAD~3 &&
1779 git branch -f third HEAD~1 &&
1780 git commit --allow-empty --fixup=third &&
1781 git branch -f is-not-reordered &&
1782 git commit --allow-empty --fixup=HEAD~4 &&
1783 git branch -f shared-tip &&
1785 set_cat_todo_editor &&
1787 cat >expect <<-EOF &&
1788 pick $(git log -1 --format=%h J) J
1789 fixup $(git log -1 --format=%h update-refs) fixup! J # empty
1790 update-ref refs/heads/second
1791 update-ref refs/heads/first
1792 pick $(git log -1 --format=%h K) K
1793 pick $(git log -1 --format=%h L) L
1794 fixup $(git log -1 --format=%h is-not-reordered) fixup! L # empty
1795 update-ref refs/heads/third
1796 pick $(git log -1 --format=%h M) M
1797 update-ref refs/heads/no-conflict-branch
1798 update-ref refs/heads/is-not-reordered
1799 update-ref refs/heads/shared-tip
1802 test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
1803 test_cmp expect todo &&
1805 test_must_fail git -c rebase.autosquash=true \
1806 -c rebase.updaterefs=true \
1807 rebase -i primary >todo &&
1809 test_cmp expect todo
1813 test_expect_success '--update-refs adds commands with --rebase-merges' '
1814 git checkout -b update-refs-with-merge no-conflict-branch &&
1815 git branch -f base HEAD~4 &&
1816 git branch -f first HEAD~3 &&
1817 git branch -f second HEAD~3 &&
1818 git branch -f third HEAD~1 &&
1819 git merge -m merge branch2 &&
1820 git branch -f merge-branch &&
1821 git commit --fixup=third --allow-empty &&
1823 set_cat_todo_editor &&
1825 cat >expect <<-EOF &&
1826 label onto
1827 reset onto
1828 pick $(git log -1 --format=%h branch2~1) F
1829 pick $(git log -1 --format=%h branch2) I
1830 update-ref refs/heads/branch2
1831 label merge
1832 reset onto
1833 pick $(git log -1 --format=%h refs/heads/second) J
1834 update-ref refs/heads/second
1835 update-ref refs/heads/first
1836 pick $(git log -1 --format=%h refs/heads/third~1) K
1837 pick $(git log -1 --format=%h refs/heads/third) L
1838 fixup $(git log -1 --format=%h update-refs-with-merge) fixup! L # empty
1839 update-ref refs/heads/third
1840 pick $(git log -1 --format=%h HEAD~2) M
1841 update-ref refs/heads/no-conflict-branch
1842 merge -C $(git log -1 --format=%h HEAD~1) merge # merge
1843 update-ref refs/heads/merge-branch
1846 test_must_fail git rebase -i --autosquash \
1847 --rebase-merges=rebase-cousins \
1848 --update-refs primary >todo &&
1850 test_cmp expect todo &&
1852 test_must_fail git -c rebase.autosquash=true \
1853 -c rebase.updaterefs=true \
1854 rebase -i \
1855 --rebase-merges=rebase-cousins \
1856 primary >todo &&
1858 test_cmp expect todo
1862 test_expect_success '--update-refs updates refs correctly' '
1863 git checkout -B update-refs no-conflict-branch &&
1864 git branch -f base HEAD~4 &&
1865 git branch -f first HEAD~3 &&
1866 git branch -f second HEAD~3 &&
1867 git branch -f third HEAD~1 &&
1868 test_commit extra2 fileX &&
1869 git commit --amend --fixup=L &&
1871 git rebase -i --autosquash --update-refs primary 2>err &&
1873 test_cmp_rev HEAD~3 refs/heads/first &&
1874 test_cmp_rev HEAD~3 refs/heads/second &&
1875 test_cmp_rev HEAD~1 refs/heads/third &&
1876 test_cmp_rev HEAD refs/heads/no-conflict-branch &&
1878 cat >expect <<-\EOF &&
1879 Successfully rebased and updated refs/heads/update-refs.
1880 Updated the following refs with --update-refs:
1881 refs/heads/first
1882 refs/heads/no-conflict-branch
1883 refs/heads/second
1884 refs/heads/third
1887 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
1888 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
1889 <err >err.trimmed &&
1890 test_cmp expect err.trimmed
1893 test_expect_success 'respect user edits to update-ref steps' '
1894 git checkout -B update-refs-break no-conflict-branch &&
1895 git branch -f base HEAD~4 &&
1896 git branch -f first HEAD~3 &&
1897 git branch -f second HEAD~3 &&
1898 git branch -f third HEAD~1 &&
1899 git branch -f unseen base &&
1901 # First, we will add breaks to the expected todo file
1902 cat >fake-todo-1 <<-EOF &&
1903 pick $(git rev-parse HEAD~3)
1904 break
1905 update-ref refs/heads/second
1906 update-ref refs/heads/first
1908 pick $(git rev-parse HEAD~2)
1909 pick $(git rev-parse HEAD~1)
1910 update-ref refs/heads/third
1912 pick $(git rev-parse HEAD)
1913 update-ref refs/heads/no-conflict-branch
1916 # Second, we will drop some update-refs commands (and move one)
1917 cat >fake-todo-2 <<-EOF &&
1918 update-ref refs/heads/second
1920 pick $(git rev-parse HEAD~2)
1921 update-ref refs/heads/third
1922 pick $(git rev-parse HEAD~1)
1923 break
1925 pick $(git rev-parse HEAD)
1928 # Third, we will:
1929 # * insert a new one (new-branch),
1930 # * re-add an old one (first), and
1931 # * add a second instance of a previously-stored one (second)
1932 cat >fake-todo-3 <<-EOF &&
1933 update-ref refs/heads/unseen
1934 update-ref refs/heads/new-branch
1935 pick $(git rev-parse HEAD)
1936 update-ref refs/heads/first
1937 update-ref refs/heads/second
1941 set_replace_editor fake-todo-1 &&
1942 git rebase -i --update-refs primary &&
1944 # These branches are currently locked.
1945 for b in first second third no-conflict-branch
1947 test_must_fail git branch -f $b base || return 1
1948 done &&
1950 set_replace_editor fake-todo-2 &&
1951 git rebase --edit-todo &&
1953 # These branches are currently locked.
1954 for b in second third
1956 test_must_fail git branch -f $b base || return 1
1957 done &&
1959 # These branches are currently unlocked for checkout.
1960 for b in first no-conflict-branch
1962 git worktree add wt-$b $b &&
1963 git worktree remove wt-$b || return 1
1964 done &&
1966 git rebase --continue &&
1968 set_replace_editor fake-todo-3 &&
1969 git rebase --edit-todo &&
1971 # These branches are currently locked.
1972 for b in second third first unseen
1974 test_must_fail git branch -f $b base || return 1
1975 done &&
1977 # These branches are currently unlocked for checkout.
1978 for b in no-conflict-branch
1980 git worktree add wt-$b $b &&
1981 git worktree remove wt-$b || return 1
1982 done &&
1984 git rebase --continue
1985 ) &&
1987 test_cmp_rev HEAD~2 refs/heads/third &&
1988 test_cmp_rev HEAD~1 refs/heads/unseen &&
1989 test_cmp_rev HEAD~1 refs/heads/new-branch &&
1990 test_cmp_rev HEAD refs/heads/first &&
1991 test_cmp_rev HEAD refs/heads/second &&
1992 test_cmp_rev HEAD refs/heads/no-conflict-branch
1995 test_expect_success '--update-refs: all update-ref lines removed' '
1996 git checkout -b test-refs-not-removed no-conflict-branch &&
1997 git branch -f base HEAD~4 &&
1998 git branch -f first HEAD~3 &&
1999 git branch -f second HEAD~3 &&
2000 git branch -f third HEAD~1 &&
2001 git branch -f tip &&
2003 test_commit test-refs-not-removed &&
2004 git commit --amend --fixup first &&
2006 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2009 set_cat_todo_editor &&
2010 test_must_fail git rebase -i --update-refs base >todo.raw &&
2011 sed -e "/^update-ref/d" <todo.raw >todo
2012 ) &&
2014 set_replace_editor todo &&
2015 git rebase -i --update-refs base
2016 ) &&
2018 # Ensure refs are not deleted and their OIDs have not changed
2019 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2020 test_cmp expect-oids actual-oids
2023 test_expect_success '--update-refs: all update-ref lines removed, then some re-added' '
2024 git checkout -b test-refs-not-removed2 no-conflict-branch &&
2025 git branch -f base HEAD~4 &&
2026 git branch -f first HEAD~3 &&
2027 git branch -f second HEAD~3 &&
2028 git branch -f third HEAD~1 &&
2029 git branch -f tip &&
2031 test_commit test-refs-not-removed2 &&
2032 git commit --amend --fixup first &&
2034 git rev-parse first second third >expect-oids &&
2037 set_cat_todo_editor &&
2038 test_must_fail git rebase -i \
2039 --autosquash --update-refs \
2040 base >todo.raw &&
2041 sed -e "/^update-ref/d" <todo.raw >todo
2042 ) &&
2044 # Add a break to the end of the todo so we can edit later
2045 echo "break" >>todo &&
2048 set_replace_editor todo &&
2049 git rebase -i --autosquash --update-refs base &&
2050 echo "update-ref refs/heads/tip" >todo &&
2051 git rebase --edit-todo &&
2052 git rebase --continue
2053 ) &&
2055 # Ensure first/second/third are unchanged, but tip is updated
2056 git rev-parse first second third >actual-oids &&
2057 test_cmp expect-oids actual-oids &&
2058 test_cmp_rev HEAD tip
2061 test_expect_success '--update-refs: --edit-todo with no update-ref lines' '
2062 git checkout -b test-refs-not-removed3 no-conflict-branch &&
2063 git branch -f base HEAD~4 &&
2064 git branch -f first HEAD~3 &&
2065 git branch -f second HEAD~3 &&
2066 git branch -f third HEAD~1 &&
2067 git branch -f tip &&
2069 test_commit test-refs-not-removed3 &&
2070 git commit --amend --fixup first &&
2072 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2075 set_cat_todo_editor &&
2076 test_must_fail git rebase -i \
2077 --autosquash --update-refs \
2078 base >todo.raw &&
2079 sed -e "/^update-ref/d" <todo.raw >todo
2080 ) &&
2082 # Add a break to the beginning of the todo so we can resume with no
2083 # update-ref lines
2084 echo "break" >todo.new &&
2085 cat todo >>todo.new &&
2088 set_replace_editor todo.new &&
2089 git rebase -i --autosquash --update-refs base &&
2091 # Make no changes when editing so update-refs is still empty
2092 cat todo >todo.new &&
2093 git rebase --edit-todo &&
2094 git rebase --continue
2095 ) &&
2097 # Ensure refs are not deleted and their OIDs have not changed
2098 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2099 test_cmp expect-oids actual-oids
2102 test_expect_success '--update-refs: check failed ref update' '
2103 test_when_finished "test_might_fail git rebase --abort" &&
2104 git checkout -B update-refs-error no-conflict-branch &&
2105 git branch -f base HEAD~4 &&
2106 git branch -f first HEAD~3 &&
2107 git branch -f second HEAD~2 &&
2108 git branch -f third HEAD~1 &&
2110 cat >fake-todo <<-EOF &&
2111 pick $(git rev-parse HEAD~3)
2112 break
2113 update-ref refs/heads/first
2115 pick $(git rev-parse HEAD~2)
2116 update-ref refs/heads/second
2118 pick $(git rev-parse HEAD~1)
2119 update-ref refs/heads/third
2121 pick $(git rev-parse HEAD)
2122 update-ref refs/heads/no-conflict-branch
2126 set_replace_editor fake-todo &&
2127 git rebase -i --update-refs base
2128 ) &&
2130 # At this point, the values of first, second, and third are
2131 # recorded in the update-refs file. We will force-update the
2132 # "second" ref, but "git branch -f" will not work because of
2133 # the lock in the update-refs file.
2134 git rev-parse third >.git/refs/heads/second &&
2136 test_must_fail git rebase --continue 2>err &&
2137 grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
2139 cat >expect <<-\EOF &&
2140 Updated the following refs with --update-refs:
2141 refs/heads/first
2142 refs/heads/no-conflict-branch
2143 refs/heads/third
2144 Failed to update the following refs with --update-refs:
2145 refs/heads/second
2148 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
2149 tail -n 6 err >err.last &&
2150 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
2151 <err.last >err.trimmed &&
2152 test_cmp expect err.trimmed
2155 test_expect_success 'bad labels and refs rejected when parsing todo list' '
2156 test_when_finished "test_might_fail git rebase --abort" &&
2157 cat >todo <<-\EOF &&
2158 exec >execed
2159 label #
2160 label :invalid
2161 update-ref :bad
2162 update-ref topic
2164 rm -f execed &&
2166 set_replace_editor todo &&
2167 test_must_fail git rebase -i HEAD 2>err
2168 ) &&
2169 grep "'\''#'\'' is not a valid label" err &&
2170 grep "'\'':invalid'\'' is not a valid label" err &&
2171 grep "'\'':bad'\'' is not a valid refname" err &&
2172 grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \
2173 err &&
2174 test_path_is_missing execed
2177 # This must be the last test in this file
2178 test_expect_success '$EDITOR and friends are unchanged' '
2179 test_editor_unchanged
2182 test_done