Merge branch 'es/worktree-repair-copied' into cw/worktrees-relative
[git/gitster.git] / t / t3404-rebase-interactive.sh
blobf171af3061db9538aa0291e642530e098bff7124
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_PASSES_SANITIZE_LEAK=true
30 . ./test-lib.sh
32 . "$TEST_DIRECTORY"/lib-rebase.sh
34 test_expect_success 'setup' '
35 git switch -C primary &&
36 test_commit A file1 &&
37 test_commit B file1 &&
38 test_commit C file2 &&
39 test_commit D file1 &&
40 test_commit E file3 &&
41 git checkout -b branch1 A &&
42 test_commit F file4 &&
43 test_commit G file1 &&
44 test_commit H file5 &&
45 git checkout -b branch2 F &&
46 test_commit I file6 &&
47 git checkout -b conflict-branch A &&
48 test_commit one conflict &&
49 test_commit two conflict &&
50 test_commit three conflict &&
51 test_commit four conflict &&
52 git checkout -b no-conflict-branch A &&
53 test_commit J fileJ &&
54 test_commit K fileK &&
55 test_commit L fileL &&
56 test_commit M fileM &&
57 git checkout -b no-ff-branch A &&
58 test_commit N fileN &&
59 test_commit O fileO &&
60 test_commit P fileP
63 # "exec" commands are run with the user shell by default, but this may
64 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
65 # to create a file. Unsetting SHELL avoids such non-portable behavior
66 # in tests. It must be exported for it to take effect where needed.
67 SHELL=
68 export SHELL
70 test_expect_success 'rebase --keep-empty' '
71 git checkout -b emptybranch primary &&
72 git commit --allow-empty -m "empty" &&
73 git rebase --keep-empty -i HEAD~2 &&
74 git log --oneline >actual &&
75 test_line_count = 6 actual
78 test_expect_success 'rebase -i with empty todo list' '
79 cat >expect <<-\EOF &&
80 error: nothing to do
81 EOF
83 set_fake_editor &&
84 test_must_fail env FAKE_LINES="#" \
85 git rebase -i HEAD^ >output 2>&1
86 ) &&
87 tail -n 1 output >actual && # Ignore output about changing todo list
88 test_cmp expect actual
91 test_expect_success 'rebase -i with the exec command' '
92 git checkout primary &&
94 set_fake_editor &&
95 FAKE_LINES="1 exec_>touch-one
96 2 exec_>touch-two exec_false exec_>touch-three
97 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
98 export FAKE_LINES &&
99 test_must_fail git rebase -i A
100 ) &&
101 test_path_is_file touch-one &&
102 test_path_is_file touch-two &&
103 # Missing because we should have stopped by now.
104 test_path_is_missing touch-three &&
105 test_cmp_rev C HEAD &&
106 git rebase --continue &&
107 test_path_is_file touch-three &&
108 test_path_is_file "touch-file name with spaces" &&
109 test_path_is_file touch-after-semicolon &&
110 test_cmp_rev primary HEAD &&
111 rm -f touch-*
114 test_expect_success 'rebase -i with the exec command runs from tree root' '
115 git checkout primary &&
116 mkdir subdir && (cd subdir &&
117 set_fake_editor &&
118 FAKE_LINES="1 exec_>touch-subdir" \
119 git rebase -i HEAD^
120 ) &&
121 test_path_is_file touch-subdir &&
122 rm -fr subdir
125 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
126 test_when_finished "rm -rf subdir" &&
127 test_when_finished "git rebase --abort ||:" &&
128 git checkout primary &&
129 mkdir subdir && (cd subdir &&
130 set_fake_editor &&
131 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
132 git rebase -i HEAD^
136 test_expect_success 'rebase -i sets work tree properly' '
137 test_when_finished "rm -rf subdir" &&
138 test_when_finished "test_might_fail git rebase --abort" &&
139 mkdir subdir &&
140 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
141 >actual &&
142 ! grep "/subdir$" actual
145 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
146 git checkout primary &&
148 set_fake_editor &&
149 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
150 git rebase -i HEAD^
151 ) &&
152 test_cmp_rev primary^ HEAD &&
153 git reset --hard &&
154 git rebase --continue
157 test_expect_success 'cherry-pick works with rebase --exec' '
158 test_when_finished "git cherry-pick --abort; \
159 git rebase --abort; \
160 git checkout primary" &&
161 echo "exec git cherry-pick G" >todo &&
163 set_replace_editor todo &&
164 test_must_fail git rebase -i D D
165 ) &&
166 test_cmp_rev G CHERRY_PICK_HEAD
169 test_expect_success 'rebase -x with empty command fails' '
170 test_when_finished "git rebase --abort ||:" &&
171 test_must_fail env git rebase -x "" @ 2>actual &&
172 test_write_lines "error: empty exec command" >expected &&
173 test_cmp expected actual &&
174 test_must_fail env git rebase -x " " @ 2>actual &&
175 test_cmp expected actual
178 test_expect_success 'rebase -x with newline in command fails' '
179 test_when_finished "git rebase --abort ||:" &&
180 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
181 test_write_lines "error: exec commands cannot contain newlines" \
182 >expected &&
183 test_cmp expected actual
186 test_expect_success 'rebase -i with exec of inexistent command' '
187 git checkout primary &&
188 test_when_finished "git rebase --abort" &&
190 set_fake_editor &&
191 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
192 git rebase -i HEAD^ >actual 2>&1
193 ) &&
194 ! grep "Maybe git-rebase is broken" actual
197 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
198 test_when_finished "git rebase --abort ||:" &&
199 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
202 test_expect_success 'no changes are a nop' '
203 git checkout branch2 &&
204 git rebase -i F &&
205 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
206 test_cmp_rev I HEAD
209 test_expect_success 'test the [branch] option' '
210 git checkout -b dead-end &&
211 git rm file6 &&
212 git commit -m "stop here" &&
213 git rebase -i F branch2 &&
214 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
215 test_cmp_rev I branch2 &&
216 test_cmp_rev I HEAD
219 test_expect_success 'test --onto <branch>' '
220 git checkout -b test-onto branch2 &&
221 git rebase -i --onto branch1 F &&
222 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
223 test_cmp_rev HEAD^ branch1 &&
224 test_cmp_rev I branch2
227 test_expect_success 'rebase on top of a non-conflicting commit' '
228 git checkout branch1 &&
229 git tag original-branch1 &&
230 git rebase -i branch2 &&
231 test file6 = $(git diff --name-only original-branch1) &&
232 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
233 test_cmp_rev I branch2 &&
234 test_cmp_rev I HEAD~2
237 test_expect_success 'reflog for the branch shows state before rebase' '
238 test_cmp_rev branch1@{1} original-branch1
241 test_expect_success 'reflog for the branch shows correct finish message' '
242 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
243 "$(git rev-parse branch2)" >expected &&
244 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
245 test_cmp expected actual
248 test_expect_success 'exchange two commits' '
250 set_fake_editor &&
251 FAKE_LINES="2 1" git rebase -i HEAD~2
252 ) &&
253 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
254 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
255 blob1=$(git rev-parse --short HEAD^:file1) &&
256 blob2=$(git rev-parse --short HEAD:file1) &&
257 commit=$(git rev-parse --short HEAD)
260 test_expect_success 'stop on conflicting pick' '
261 cat >expect <<-EOF &&
262 diff --git a/file1 b/file1
263 index $blob1..$blob2 100644
264 --- a/file1
265 +++ b/file1
266 @@ -1 +1 @@
270 cat >expect2 <<-EOF &&
271 <<<<<<< HEAD
273 =======
275 >>>>>>> $commit (G)
277 git tag new-branch1 &&
278 test_must_fail git rebase -i primary &&
279 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
280 test_cmp expect .git/rebase-merge/patch &&
281 test_cmp expect2 file1 &&
282 test "$(git diff --name-status |
283 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
284 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
285 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
288 test_expect_success 'show conflicted patch' '
289 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
290 grep "show.*REBASE_HEAD" stderr &&
291 # the original stopped-sha1 is abbreviated
292 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
293 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
296 test_expect_success 'abort' '
297 git rebase --abort &&
298 test_cmp_rev new-branch1 HEAD &&
299 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
300 test_path_is_missing .git/rebase-merge
303 test_expect_success 'abort with error when new base cannot be checked out' '
304 git rm --cached file1 &&
305 git commit -m "remove file in base" &&
306 test_must_fail git rebase -i primary > output 2>&1 &&
307 test_grep "The following untracked working tree files would be overwritten by checkout:" \
308 output &&
309 test_grep "file1" output &&
310 test_path_is_missing .git/rebase-merge &&
311 rm file1 &&
312 git reset --hard HEAD^
315 test_expect_success 'retain authorship' '
316 echo A > file7 &&
317 git add file7 &&
318 test_tick &&
319 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
320 git tag twerp &&
321 git rebase -i --onto primary HEAD^ &&
322 git show HEAD | grep "^Author: Twerp Snog"
325 test_expect_success 'retain authorship w/ conflicts' '
326 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
327 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
329 git reset --hard twerp &&
330 test_commit a conflict a conflict-a &&
331 git reset --hard twerp &&
333 GIT_AUTHOR_NAME=AttributeMe &&
334 export GIT_AUTHOR_NAME &&
335 test_commit b conflict b conflict-b &&
336 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
338 test_must_fail git rebase -i conflict-a &&
339 echo resolved >conflict &&
340 git add conflict &&
341 git rebase --continue &&
342 test_cmp_rev conflict-a^0 HEAD^ &&
343 git show >out &&
344 grep AttributeMe out
347 test_expect_success 'squash' '
348 git reset --hard twerp &&
349 echo B > file7 &&
350 test_tick &&
351 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
352 echo "******************************" &&
354 set_fake_editor &&
355 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
356 git rebase -i --onto primary HEAD~2
357 ) &&
358 test B = $(cat file7) &&
359 test_cmp_rev HEAD^ primary
362 test_expect_success 'retain authorship when squashing' '
363 git show HEAD | grep "^Author: Twerp Snog"
366 test_expect_success '--continue tries to commit' '
367 git reset --hard D &&
368 test_tick &&
370 set_fake_editor &&
371 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
372 echo resolved > file1 &&
373 git add file1 &&
374 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
375 ) &&
376 test_cmp_rev HEAD^ new-branch1 &&
377 git show HEAD | grep chouette
380 test_expect_success 'verbose flag is heeded, even after --continue' '
381 git reset --hard primary@{1} &&
382 test_tick &&
383 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
384 echo resolved > file1 &&
385 git add file1 &&
386 git rebase --continue > output &&
387 grep "^ file1 | 2 +-$" output
390 test_expect_success 'multi-squash only fires up editor once' '
391 base=$(git rev-parse HEAD~4) &&
393 set_fake_editor &&
394 FAKE_COMMIT_AMEND="ONCE" \
395 FAKE_LINES="1 squash 2 squash 3 squash 4" \
396 EXPECT_HEADER_COUNT=4 \
397 git rebase -i $base
398 ) &&
399 test $base = $(git rev-parse HEAD^) &&
400 test 1 = $(git show | grep ONCE | wc -l)
403 test_expect_success 'multi-fixup does not fire up editor' '
404 git checkout -b multi-fixup E &&
405 base=$(git rev-parse HEAD~4) &&
407 set_fake_editor &&
408 FAKE_COMMIT_AMEND="NEVER" \
409 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
410 git rebase -i $base
411 ) &&
412 test $base = $(git rev-parse HEAD^) &&
413 test 0 = $(git show | grep NEVER | wc -l) &&
414 git checkout @{-1} &&
415 git branch -D multi-fixup
418 test_expect_success 'commit message used after conflict' '
419 git checkout -b conflict-fixup conflict-branch &&
420 base=$(git rev-parse HEAD~4) &&
422 set_fake_editor &&
423 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
424 git rebase -i $base &&
425 echo three > conflict &&
426 git add conflict &&
427 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
428 git rebase --continue
429 ) &&
430 test $base = $(git rev-parse HEAD^) &&
431 test 1 = $(git show | grep ONCE | wc -l) &&
432 git checkout @{-1} &&
433 git branch -D conflict-fixup
436 test_expect_success 'commit message retained after conflict' '
437 git checkout -b conflict-squash conflict-branch &&
438 base=$(git rev-parse HEAD~4) &&
440 set_fake_editor &&
441 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
442 git rebase -i $base &&
443 echo three > conflict &&
444 git add conflict &&
445 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
446 git rebase --continue
447 ) &&
448 test $base = $(git rev-parse HEAD^) &&
449 test 2 = $(git show | grep TWICE | wc -l) &&
450 git checkout @{-1} &&
451 git branch -D conflict-squash
454 test_expect_success 'squash and fixup generate correct log messages' '
455 cat >expect-squash-fixup <<-\EOF &&
460 ONCE
462 git checkout -b squash-fixup E &&
463 base=$(git rev-parse HEAD~4) &&
465 set_fake_editor &&
466 FAKE_COMMIT_AMEND="ONCE" \
467 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
468 EXPECT_HEADER_COUNT=4 \
469 git rebase -i $base
470 ) &&
471 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
472 test_cmp expect-squash-fixup actual-squash-fixup &&
473 git cat-file commit HEAD@{2} |
474 grep "^# This is a combination of 3 commits\." &&
475 git cat-file commit HEAD@{3} |
476 grep "^# This is a combination of 2 commits\." &&
477 git checkout @{-1} &&
478 git branch -D squash-fixup
481 test_expect_success 'squash ignores comments' '
482 git checkout -b skip-comments E &&
483 base=$(git rev-parse HEAD~4) &&
485 set_fake_editor &&
486 FAKE_COMMIT_AMEND="ONCE" \
487 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
488 EXPECT_HEADER_COUNT=4 \
489 git rebase -i $base
490 ) &&
491 test $base = $(git rev-parse HEAD^) &&
492 test 1 = $(git show | grep ONCE | wc -l) &&
493 git checkout @{-1} &&
494 git branch -D skip-comments
497 test_expect_success 'squash ignores blank lines' '
498 git checkout -b skip-blank-lines E &&
499 base=$(git rev-parse HEAD~4) &&
501 set_fake_editor &&
502 FAKE_COMMIT_AMEND="ONCE" \
503 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
504 EXPECT_HEADER_COUNT=4 \
505 git rebase -i $base
506 ) &&
507 test $base = $(git rev-parse HEAD^) &&
508 test 1 = $(git show | grep ONCE | wc -l) &&
509 git checkout @{-1} &&
510 git branch -D skip-blank-lines
513 test_expect_success 'squash works as expected' '
514 git checkout -b squash-works no-conflict-branch &&
515 one=$(git rev-parse HEAD~3) &&
517 set_fake_editor &&
518 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
519 ) &&
520 test $one = $(git rev-parse HEAD~2)
523 test_expect_success 'interrupted squash works as expected' '
524 git checkout -b interrupted-squash conflict-branch &&
525 one=$(git rev-parse HEAD~3) &&
527 set_fake_editor &&
528 test_must_fail env FAKE_LINES="1 squash 3 2" \
529 git rebase -i HEAD~3
530 ) &&
531 test_write_lines one two four > conflict &&
532 git add conflict &&
533 test_must_fail git rebase --continue &&
534 echo resolved > conflict &&
535 git add conflict &&
536 git rebase --continue &&
537 test $one = $(git rev-parse HEAD~2)
540 test_expect_success 'interrupted squash works as expected (case 2)' '
541 git checkout -b interrupted-squash2 conflict-branch &&
542 one=$(git rev-parse HEAD~3) &&
544 set_fake_editor &&
545 test_must_fail env FAKE_LINES="3 squash 1 2" \
546 git rebase -i HEAD~3
547 ) &&
548 test_write_lines one four > conflict &&
549 git add conflict &&
550 test_must_fail git rebase --continue &&
551 test_write_lines one two four > conflict &&
552 git add conflict &&
553 test_must_fail git rebase --continue &&
554 echo resolved > conflict &&
555 git add conflict &&
556 git rebase --continue &&
557 test $one = $(git rev-parse HEAD~2)
560 test_expect_success '--continue tries to commit, even for "edit"' '
561 echo unrelated > file7 &&
562 git add file7 &&
563 test_tick &&
564 git commit -m "unrelated change" &&
565 parent=$(git rev-parse HEAD^) &&
566 test_tick &&
568 set_fake_editor &&
569 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
570 echo edited > file7 &&
571 git add file7 &&
572 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
573 ) &&
574 test edited = $(git show HEAD:file7) &&
575 git show HEAD | grep chouette &&
576 test $parent = $(git rev-parse HEAD^)
579 test_expect_success 'aborted --continue does not squash commits after "edit"' '
580 old=$(git rev-parse HEAD) &&
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 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
588 ) &&
589 test $old = $(git rev-parse HEAD) &&
590 git rebase --abort
593 test_expect_success 'auto-amend only edited commits after "edit"' '
594 test_tick &&
596 set_fake_editor &&
597 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
598 echo "edited again" > file7 &&
599 git add file7 &&
600 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
601 echo "and again" > file7 &&
602 git add file7 &&
603 test_tick &&
604 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
605 git rebase --continue
606 ) &&
607 git rebase --abort
610 test_expect_success 'clean error after failed "exec"' '
611 test_tick &&
612 test_when_finished "git rebase --abort || :" &&
614 set_fake_editor &&
615 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
616 ) &&
617 echo "edited again" > file7 &&
618 git add file7 &&
619 test_must_fail git rebase --continue 2>error &&
620 test_grep "you have staged changes in your working tree" error &&
621 test_grep ! "could not open.*for reading" error
624 test_expect_success 'rebase a detached HEAD' '
625 grandparent=$(git rev-parse HEAD~2) &&
626 git checkout $(git rev-parse HEAD) &&
627 test_tick &&
629 set_fake_editor &&
630 FAKE_LINES="2 1" git rebase -i HEAD~2
631 ) &&
632 test $grandparent = $(git rev-parse HEAD~2)
635 test_expect_success 'rebase a commit violating pre-commit' '
636 test_hook pre-commit <<-\EOF &&
637 test -z "$(git diff --cached --check)"
639 echo "monde! " >> file1 &&
640 test_tick &&
641 test_must_fail git commit -m doesnt-verify file1 &&
642 git commit -m doesnt-verify --no-verify file1 &&
643 test_tick &&
645 set_fake_editor &&
646 FAKE_LINES=2 git rebase -i HEAD~2
650 test_expect_success 'rebase with a file named HEAD in worktree' '
651 git reset --hard &&
652 git checkout -b branch3 A &&
655 GIT_AUTHOR_NAME="Squashed Away" &&
656 export GIT_AUTHOR_NAME &&
657 >HEAD &&
658 git add HEAD &&
659 git commit -m "Add head" &&
660 >BODY &&
661 git add BODY &&
662 git commit -m "Add body"
663 ) &&
666 set_fake_editor &&
667 FAKE_LINES="1 squash 2" git rebase -i @{-1}
668 ) &&
669 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
673 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
675 git checkout -b branch4 HEAD &&
676 GIT_EDITOR=: git commit --amend \
677 --author="Somebody else <somebody@else.com>" &&
678 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
679 git rebase -i branch3 &&
680 test_cmp_rev branch3 branch4
684 test_expect_success 'submodule rebase setup' '
685 git checkout A &&
686 mkdir sub &&
688 cd sub && git init && >elif &&
689 git add elif && git commit -m "submodule initial"
690 ) &&
691 echo 1 >file1 &&
692 git add file1 sub &&
693 test_tick &&
694 git commit -m "One" &&
695 echo 2 >file1 &&
696 test_tick &&
697 git commit -a -m "Two" &&
699 cd sub && echo 3 >elif &&
700 git commit -a -m "submodule second"
701 ) &&
702 test_tick &&
703 git commit -a -m "Three changes submodule"
706 test_expect_success 'submodule rebase -i' '
708 set_fake_editor &&
709 FAKE_LINES="1 squash 2 3" git rebase -i A
713 test_expect_success 'submodule conflict setup' '
714 git tag submodule-base &&
715 git checkout HEAD^ &&
717 cd sub && git checkout HEAD^ && echo 4 >elif &&
718 git add elif && git commit -m "submodule conflict"
719 ) &&
720 git add sub &&
721 test_tick &&
722 git commit -m "Conflict in submodule" &&
723 git tag submodule-topic
726 test_expect_success 'rebase -i continue with only submodule staged' '
727 test_must_fail git rebase -i submodule-base &&
728 git add sub &&
729 git rebase --continue &&
730 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
733 test_expect_success 'rebase -i continue with unstaged submodule' '
734 git checkout submodule-topic &&
735 git reset --hard &&
736 test_must_fail git rebase -i submodule-base &&
737 git reset &&
738 git rebase --continue &&
739 test_cmp_rev submodule-base HEAD
742 test_expect_success 'avoid unnecessary reset' '
743 git checkout primary &&
744 git reset --hard &&
745 test-tool chmtime =123456789 file3 &&
746 git update-index --refresh &&
747 HEAD=$(git rev-parse HEAD) &&
748 git rebase -i HEAD~4 &&
749 test $HEAD = $(git rev-parse HEAD) &&
750 MTIME=$(test-tool chmtime --get file3) &&
751 test 123456789 = $MTIME
754 test_expect_success 'reword' '
755 git checkout -b reword-branch primary &&
757 set_fake_editor &&
758 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
759 git rebase -i A &&
760 git show HEAD | grep "E changed" &&
761 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
762 test_cmp_rev primary^ HEAD^ &&
763 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
764 git rebase -i A &&
765 git show HEAD^ | grep "D changed" &&
766 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
767 git rebase -i A &&
768 git show HEAD~3 | grep "B changed" &&
769 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
770 git rebase -i A
771 ) &&
772 git show HEAD~2 | grep "C changed"
775 test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '
776 git checkout E &&
777 test_when_finished "git checkout @{-1}" &&
779 set_fake_editor &&
780 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
781 export GIT_SEQUENCE_EDITOR &&
782 set_reword_editor &&
783 FAKE_LINES="reword 1 reword 2" git rebase -i C
784 ) &&
785 check_reworded_commits D E
788 test_expect_success 'rebase -i can copy notes' '
789 git config notes.rewrite.rebase true &&
790 git config notes.rewriteRef "refs/notes/*" &&
791 test_commit n1 &&
792 test_commit n2 &&
793 test_commit n3 &&
794 git notes add -m"a note" n3 &&
795 git rebase -i --onto n1 n2 &&
796 test "a note" = "$(git notes show HEAD)"
799 test_expect_success 'rebase -i can copy notes over a fixup' '
800 cat >expect <<-\EOF &&
801 an earlier note
803 a note
805 git reset --hard n3 &&
806 git notes add -m"an earlier note" n2 &&
808 set_fake_editor &&
809 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
810 git rebase -i n1
811 ) &&
812 git notes show > output &&
813 test_cmp expect output
816 test_expect_success 'rebase while detaching HEAD' '
817 git symbolic-ref HEAD &&
818 grandparent=$(git rev-parse HEAD~2) &&
819 test_tick &&
821 set_fake_editor &&
822 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
823 ) &&
824 test $grandparent = $(git rev-parse HEAD~2) &&
825 test_must_fail git symbolic-ref HEAD
828 test_tick # Ensure that the rebased commits get a different timestamp.
829 test_expect_success 'always cherry-pick with --no-ff' '
830 git checkout no-ff-branch &&
831 git tag original-no-ff-branch &&
832 git rebase -i --no-ff A &&
833 for p in 0 1 2
835 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
836 git diff HEAD~$p original-no-ff-branch~$p > out &&
837 test_must_be_empty out || return 1
838 done &&
839 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
840 git diff HEAD~3 original-no-ff-branch~3 > out &&
841 test_must_be_empty out
844 test_expect_success 'set up commits with funny messages' '
845 git checkout -b funny A &&
846 echo >>file1 &&
847 test_tick &&
848 git commit -a -m "end with slash\\" &&
849 echo >>file1 &&
850 test_tick &&
851 git commit -a -m "something (\000) that looks like octal" &&
852 echo >>file1 &&
853 test_tick &&
854 git commit -a -m "something (\n) that looks like a newline" &&
855 echo >>file1 &&
856 test_tick &&
857 git commit -a -m "another commit"
860 test_expect_success 'rebase-i history with funny messages' '
861 git rev-list A..funny >expect &&
862 test_tick &&
864 set_fake_editor &&
865 FAKE_LINES="1 2 3 4" git rebase -i A
866 ) &&
867 git rev-list A.. >actual &&
868 test_cmp expect actual
871 test_expect_success 'prepare for rebase -i --exec' '
872 git checkout primary &&
873 git checkout -b execute &&
874 test_commit one_exec main.txt one_exec &&
875 test_commit two_exec main.txt two_exec &&
876 test_commit three_exec main.txt three_exec
879 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
881 set_fake_editor &&
882 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
883 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
884 export FAKE_LINES &&
885 git rebase -i HEAD~2 >expect
886 ) &&
887 sed -e "1,9d" expect >expected &&
888 test_cmp expected actual
891 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
892 git reset --hard execute &&
894 set_fake_editor &&
895 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
896 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
897 export FAKE_LINES &&
898 git rebase -i HEAD~2 >expect
899 ) &&
900 sed -e "1,9d" expect >expected &&
901 test_cmp expected actual
904 test_expect_success 'running "git rebase -ix git show HEAD"' '
905 git reset --hard execute &&
907 set_fake_editor &&
908 git rebase -ix "git show HEAD" HEAD~2 >actual &&
909 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
910 export FAKE_LINES &&
911 git rebase -i HEAD~2 >expect
912 ) &&
913 sed -e "1,9d" expect >expected &&
914 test_cmp expected actual
918 test_expect_success 'rebase -ix with several <CMD>' '
919 git reset --hard execute &&
921 set_fake_editor &&
922 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
923 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
924 export FAKE_LINES &&
925 git rebase -i HEAD~2 >expect
926 ) &&
927 sed -e "1,9d" expect >expected &&
928 test_cmp expected actual
931 test_expect_success 'rebase -ix with several instances of --exec' '
932 git reset --hard execute &&
934 set_fake_editor &&
935 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
936 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
937 exec_git_show_HEAD exec_pwd" &&
938 export FAKE_LINES &&
939 git rebase -i HEAD~2 >expect
940 ) &&
941 sed -e "1,11d" expect >expected &&
942 test_cmp expected actual
945 test_expect_success 'rebase -ix with --autosquash' '
946 git reset --hard execute &&
947 git checkout -b autosquash &&
948 echo second >second.txt &&
949 git add second.txt &&
950 git commit -m "fixup! two_exec" &&
951 echo bis >bis.txt &&
952 git add bis.txt &&
953 git commit -m "fixup! two_exec" &&
954 git checkout -b autosquash_actual &&
955 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
956 git checkout autosquash &&
958 set_fake_editor &&
959 git checkout -b autosquash_expected &&
960 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
961 export FAKE_LINES &&
962 git rebase -i HEAD~4 >expect
963 ) &&
964 sed -e "1,13d" expect >expected &&
965 test_cmp expected actual
968 test_expect_success 'rebase --exec works without -i ' '
969 git reset --hard execute &&
970 rm -rf exec_output &&
971 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
972 test_grep "Successfully rebased and updated" actual &&
973 test_line_count = 2 exec_output &&
974 test_path_is_missing invoked_editor
977 test_expect_success 'rebase -i --exec without <CMD>' '
978 git reset --hard execute &&
979 test_must_fail git rebase -i --exec 2>actual &&
980 test_grep "requires a value" actual &&
981 git checkout primary
984 test_expect_success 'rebase -i --root re-order and drop commits' '
985 git checkout E &&
987 set_fake_editor &&
988 FAKE_LINES="3 1 2 5" git rebase -i --root
989 ) &&
990 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
991 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
992 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
993 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
994 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
997 test_expect_success 'rebase -i --root retain root commit author and message' '
998 git checkout A &&
999 echo B >file7 &&
1000 git add file7 &&
1001 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1003 set_fake_editor &&
1004 FAKE_LINES="2" git rebase -i --root
1005 ) &&
1006 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1007 git cat-file commit HEAD | grep -q "^different author$"
1010 test_expect_success 'rebase -i --root temporary sentinel commit' '
1011 git checkout B &&
1013 set_fake_editor &&
1014 test_must_fail env FAKE_LINES="2" git rebase -i --root
1015 ) &&
1016 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1017 git rebase --abort
1020 test_expect_success 'rebase -i --root fixup root commit' '
1021 git checkout B &&
1023 set_fake_editor &&
1024 FAKE_LINES="1 fixup 2" git rebase -i --root
1025 ) &&
1026 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1027 test B = $(git show HEAD:file1) &&
1028 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1031 test_expect_success 'rebase -i --root reword original root commit' '
1032 test_when_finished "test_might_fail git rebase --abort" &&
1033 git checkout -b reword-original-root-branch primary &&
1035 set_fake_editor &&
1036 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1037 git rebase -i --root
1038 ) &&
1039 git show HEAD^ | grep "A changed" &&
1040 test -z "$(git show -s --format=%p HEAD^)"
1043 test_expect_success 'rebase -i --root reword new root commit' '
1044 test_when_finished "test_might_fail git rebase --abort" &&
1045 git checkout -b reword-now-root-branch primary &&
1047 set_fake_editor &&
1048 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1049 git rebase -i --root
1050 ) &&
1051 git show HEAD^ | grep "C changed" &&
1052 test -z "$(git show -s --format=%p HEAD^)"
1055 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1056 test_when_finished "reset_rebase" &&
1057 git checkout -b failing-root-pick A &&
1058 echo x >file2 &&
1059 git rm file1 &&
1060 git commit -m "remove file 1 add file 2" &&
1061 echo z >file1 &&
1063 set_fake_editor &&
1064 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1065 ) &&
1066 rm file1 &&
1067 git rebase --continue &&
1068 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1069 test "$(git rev-list --count HEAD)" = 2
1072 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1073 test_when_finished "reset_rebase" &&
1074 echo z>file1 &&
1076 set_fake_editor &&
1077 test_must_fail env FAKE_LINES="reword 1 2" \
1078 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1079 rm file1 &&
1080 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1081 ) &&
1082 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1083 test "$(git rev-list --count HEAD)" = 2
1086 test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
1087 git checkout reword-original-root-branch &&
1088 git reset --hard &&
1089 git checkout conflict-branch &&
1091 set_fake_editor &&
1092 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1093 test_must_fail git rebase --edit-todo
1094 ) &&
1095 git rebase --abort
1098 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1099 git reset --hard &&
1100 git checkout no-conflict-branch^0 &&
1102 set_fake_editor &&
1103 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1104 FAKE_LINES="2 1" git rebase --edit-todo &&
1105 git rebase --continue
1106 ) &&
1107 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1108 test L = $(git cat-file commit HEAD | sed -ne \$p)
1111 test_expect_success 'rebase -i produces readable reflog' '
1112 git reset --hard &&
1113 git branch -f branch-reflog-test H &&
1114 git rebase -i --onto I F branch-reflog-test &&
1115 cat >expect <<-\EOF &&
1116 rebase (finish): returning to refs/heads/branch-reflog-test
1117 rebase (pick): H
1118 rebase (pick): G
1119 rebase (start): checkout I
1121 git reflog -n4 HEAD |
1122 sed "s/[^:]*: //" >actual &&
1123 test_cmp expect actual
1126 test_expect_success 'rebase -i respects core.commentchar' '
1127 git reset --hard &&
1128 git checkout E^0 &&
1129 test_config core.commentchar "\\" &&
1130 write_script remove-all-but-first.sh <<-\EOF &&
1131 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1132 mv "$1.tmp" "$1"
1135 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1136 git rebase -i B
1137 ) &&
1138 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1141 test_expect_success 'rebase -i respects core.commentchar=auto' '
1142 test_config core.commentchar auto &&
1143 write_script copy-edit-script.sh <<-\EOF &&
1144 cp "$1" edit-script
1146 test_when_finished "git rebase --abort || :" &&
1148 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1149 git rebase -i HEAD^
1150 ) &&
1151 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1154 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1155 test_when_finished "git branch -D torebase" &&
1156 git checkout -b torebase branch1 &&
1157 upstream=$(git rev-parse ":/J") &&
1158 onto=$(git rev-parse ":/A") &&
1159 git rebase --onto $onto $upstream &&
1160 git reset --hard branch1 &&
1161 git rebase --onto ":/A" ":/J" &&
1162 git checkout branch1
1165 test_expect_success 'rebase -i with --strategy and -X' '
1166 git checkout -b conflict-merge-use-theirs conflict-branch &&
1167 git reset --hard HEAD^ &&
1168 echo five >conflict &&
1169 echo Z >file1 &&
1170 git commit -a -m "one file conflict" &&
1171 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1172 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1173 test $(cat file1) = Z
1176 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1177 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1178 git reset --hard HEAD^ &&
1179 >breakpoint &&
1180 git add breakpoint &&
1181 git commit -m "breakpoint for interactive mode" &&
1182 echo five >conflict &&
1183 echo Z >file1 &&
1184 git commit -a -m "one file conflict" &&
1186 set_fake_editor &&
1187 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1188 -Xours conflict-branch
1189 ) &&
1190 git rebase --continue &&
1191 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1192 test $(cat file1) = Z
1195 test_expect_success 'rebase -i error on commits with \ in message' '
1196 current_head=$(git rev-parse HEAD) &&
1197 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1198 test_commit TO-REMOVE will-conflict old-content &&
1199 test_commit "\temp" will-conflict new-content dummy &&
1200 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1201 test_expect_code 1 grep " emp" error
1204 test_expect_success 'short commit ID setup' '
1205 test_when_finished "git checkout primary" &&
1206 git checkout --orphan collide &&
1207 git rm -rf . &&
1209 unset test_tick &&
1210 test_commit collide1 collide &&
1211 test_commit --notick collide2 collide &&
1212 test_commit --notick collide3 collide
1216 if test -n "$GIT_TEST_FIND_COLLIDER"
1217 then
1218 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1219 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1220 blob="$(git rev-parse collide2:collide)"
1221 from="$(git rev-parse collide1^0)"
1222 repl="commit refs/heads/collider-&\\n"
1223 repl="${repl}author $author\\ncommitter $committer\\n"
1224 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1225 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1226 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1227 git fast-import <script &&
1228 git pack-refs &&
1229 git for-each-ref >refs &&
1230 grep "^$(test_oid t3404_collision)" <refs >matches &&
1231 cat matches &&
1232 test_line_count -gt 2 matches || {
1233 echo "Could not find a collider" >&2
1234 exit 1
1238 test_expect_success 'short commit ID collide' '
1239 test_oid_cache <<-EOF &&
1240 # collision-related constants
1241 t3404_collision sha1:6bcd
1242 t3404_collision sha256:0161
1243 t3404_collider sha1:ac4f2ee
1244 t3404_collider sha256:16697
1246 test_when_finished "reset_rebase && git checkout primary" &&
1247 git checkout collide &&
1248 colliding_id=$(test_oid t3404_collision) &&
1249 hexsz=$(test_oid hexsz) &&
1250 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1251 test_config core.abbrev 4 &&
1253 unset test_tick &&
1254 test_tick &&
1255 set_fake_editor &&
1256 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1257 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1258 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1259 grep "^pick $colliding_id " \
1260 .git/rebase-merge/git-rebase-todo.tmp &&
1261 grep -E "^pick [0-9a-f]{$hexsz}" \
1262 .git/rebase-merge/git-rebase-todo &&
1263 grep -E "^pick [0-9a-f]{$hexsz}" \
1264 .git/rebase-merge/git-rebase-todo.backup &&
1265 git rebase --continue
1266 ) &&
1267 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1268 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1269 test "$collide2" = "$collide3"
1272 test_expect_success 'respect core.abbrev' '
1273 git config core.abbrev 12 &&
1275 set_cat_todo_editor &&
1276 test_must_fail git rebase -i HEAD~4 >todo-list
1277 ) &&
1278 test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list)
1281 test_expect_success 'todo count' '
1282 write_script dump-raw.sh <<-\EOF &&
1283 cat "$1"
1286 test_set_editor "$(pwd)/dump-raw.sh" &&
1287 git rebase -i HEAD~4 >actual
1288 ) &&
1289 test_grep "^# Rebase ..* onto ..* ([0-9]" actual
1292 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1293 git checkout --force A &&
1294 git clean -f &&
1295 cat >todo <<-EOF &&
1296 exec >file2
1297 pick $(git rev-parse B) B
1298 pick $(git rev-parse C) C
1299 pick $(git rev-parse D) D
1300 exec cat .git/rebase-merge/done >actual
1303 set_replace_editor todo &&
1304 test_must_fail git rebase -i A
1305 ) &&
1306 test_cmp_rev HEAD B &&
1307 test_cmp_rev REBASE_HEAD C &&
1308 head -n3 todo >expect &&
1309 test_cmp expect .git/rebase-merge/done &&
1310 rm file2 &&
1311 test_path_is_missing .git/rebase-merge/patch &&
1312 echo changed >file1 &&
1313 git add file1 &&
1314 test_must_fail git rebase --continue 2>err &&
1315 grep "error: you have staged changes in your working tree" err &&
1316 git reset --hard HEAD &&
1317 git rebase --continue &&
1318 test_cmp_rev HEAD D &&
1319 tail -n3 todo >>expect &&
1320 test_cmp expect actual
1323 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1324 git checkout --force branch2 &&
1325 git clean -f &&
1326 git tag original-branch2 &&
1328 set_fake_editor &&
1329 FAKE_LINES="edit 1 squash 2" git rebase -i A
1330 ) &&
1331 test_cmp_rev HEAD F &&
1332 test_path_is_missing file6 &&
1333 >file6 &&
1334 test_must_fail git rebase --continue &&
1335 test_cmp_rev HEAD F &&
1336 test_cmp_rev REBASE_HEAD I &&
1337 rm file6 &&
1338 test_path_is_missing .git/rebase-merge/patch &&
1339 echo changed >file1 &&
1340 git add file1 &&
1341 test_must_fail git rebase --continue 2>err &&
1342 grep "error: you have staged changes in your working tree" err &&
1343 git reset --hard HEAD &&
1344 git rebase --continue &&
1345 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1346 git reset --hard original-branch2
1349 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1350 git checkout --force branch2 &&
1351 git clean -f &&
1353 set_fake_editor &&
1354 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1355 ) &&
1356 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1357 test_path_is_missing file6 &&
1358 >file6 &&
1359 test_must_fail git rebase --continue &&
1360 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1361 test_cmp_rev REBASE_HEAD I &&
1362 rm file6 &&
1363 test_path_is_missing .git/rebase-merge/patch &&
1364 echo changed >file1 &&
1365 git add file1 &&
1366 test_must_fail git rebase --continue 2>err &&
1367 grep "error: you have staged changes in your working tree" err &&
1368 git reset --hard HEAD &&
1369 git rebase --continue &&
1370 test $(git cat-file commit HEAD | sed -ne \$p) = I
1373 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1374 git checkout -b commit-to-skip &&
1375 for double in X 3 1
1377 test_seq 5 | sed "s/$double/&&/" >seq &&
1378 git add seq &&
1379 test_tick &&
1380 git commit -m seq-$double || return 1
1381 done &&
1382 git tag seq-onto &&
1383 git reset --hard HEAD~2 &&
1384 git cherry-pick seq-onto &&
1386 set_fake_editor &&
1387 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1388 ) &&
1389 test -d .git/rebase-merge &&
1390 git rebase --continue &&
1391 git diff --exit-code seq-onto &&
1392 test ! -d .git/rebase-merge &&
1393 test ! -f .git/CHERRY_PICK_HEAD
1396 rebase_setup_and_clean () {
1397 test_when_finished "
1398 git checkout primary &&
1399 test_might_fail git branch -D $1 &&
1400 test_might_fail git rebase --abort
1401 " &&
1402 git checkout -b $1 ${2:-primary}
1405 test_expect_success 'drop' '
1406 rebase_setup_and_clean drop-test &&
1408 set_fake_editor &&
1409 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1410 ) &&
1411 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1412 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1413 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1416 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1417 test_config rebase.missingCommitsCheck ignore &&
1418 rebase_setup_and_clean missing-commit &&
1420 set_fake_editor &&
1421 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1422 ) &&
1423 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1424 test_grep \
1425 "Successfully rebased and updated refs/heads/missing-commit" \
1426 actual
1429 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1430 cat >expect <<-EOF &&
1431 Warning: some commits may have been dropped accidentally.
1432 Dropped commits (newer to older):
1433 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1434 To avoid this message, use "drop" to explicitly remove a commit.
1436 test_config rebase.missingCommitsCheck warn &&
1437 rebase_setup_and_clean missing-commit &&
1439 set_fake_editor &&
1440 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1441 ) &&
1442 head -n4 actual.2 >actual &&
1443 test_cmp expect actual &&
1444 test D = $(git cat-file commit HEAD | sed -ne \$p)
1447 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1448 cat >expect <<-EOF &&
1449 Warning: some commits may have been dropped accidentally.
1450 Dropped commits (newer to older):
1451 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1452 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1453 To avoid this message, use "drop" to explicitly remove a commit.
1455 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1456 The possible behaviours are: ignore, warn, error.
1458 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1459 Or you can abort the rebase with '\''git rebase --abort'\''.
1461 test_config rebase.missingCommitsCheck error &&
1462 rebase_setup_and_clean missing-commit &&
1464 set_fake_editor &&
1465 test_must_fail env FAKE_LINES="1 2 4" \
1466 git rebase -i --root 2>actual &&
1467 test_cmp expect actual &&
1468 cp .git/rebase-merge/git-rebase-todo.backup \
1469 .git/rebase-merge/git-rebase-todo &&
1470 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1471 ) &&
1472 git rebase --continue &&
1473 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1474 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1477 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1478 test_config rebase.missingCommitsCheck ignore &&
1479 rebase_setup_and_clean missing-commit &&
1481 set_fake_editor &&
1482 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1483 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1484 git rebase --continue 2>actual
1485 ) &&
1486 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1487 test_grep \
1488 "Successfully rebased and updated refs/heads/missing-commit" \
1489 actual
1492 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1493 cat >expect <<-EOF &&
1494 error: invalid command '\''pickled'\''
1495 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1496 Warning: some commits may have been dropped accidentally.
1497 Dropped commits (newer to older):
1498 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1499 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1500 To avoid this message, use "drop" to explicitly remove a commit.
1502 head -n5 expect >expect.2 &&
1503 tail -n1 expect >>expect.2 &&
1504 tail -n4 expect.2 >expect.3 &&
1505 test_config rebase.missingCommitsCheck warn &&
1506 rebase_setup_and_clean missing-commit &&
1508 set_fake_editor &&
1509 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1510 git rebase -i --root &&
1511 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1512 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1513 head -n7 actual.2 >actual &&
1514 test_cmp expect actual &&
1515 cp orig .git/rebase-merge/git-rebase-todo &&
1516 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1517 head -n4 actual.2 >actual &&
1518 test_cmp expect.3 actual &&
1519 git rebase --continue 2>actual
1520 ) &&
1521 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1522 test_grep \
1523 "Successfully rebased and updated refs/heads/missing-commit" \
1524 actual
1527 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1528 cat >expect <<-EOF &&
1529 error: invalid command '\''pickled'\''
1530 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1531 Warning: some commits may have been dropped accidentally.
1532 Dropped commits (newer to older):
1533 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1534 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1535 To avoid this message, use "drop" to explicitly remove a commit.
1537 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1538 The possible behaviours are: ignore, warn, error.
1540 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1541 Or you can abort the rebase with '\''git rebase --abort'\''.
1543 tail -n11 expect >expect.2 &&
1544 head -n3 expect.2 >expect.3 &&
1545 tail -n7 expect.2 >>expect.3 &&
1546 test_config rebase.missingCommitsCheck error &&
1547 rebase_setup_and_clean missing-commit &&
1549 set_fake_editor &&
1550 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1551 git rebase -i --root &&
1552 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1553 test_must_fail env FAKE_LINES="2 3 4" \
1554 git rebase --edit-todo 2>actual &&
1555 test_cmp expect actual &&
1556 test_must_fail git rebase --continue 2>actual &&
1557 test_cmp expect.2 actual &&
1558 test_must_fail git rebase --edit-todo &&
1559 cp orig .git/rebase-merge/git-rebase-todo &&
1560 test_must_fail env FAKE_LINES="1 2 3 4" \
1561 git rebase --edit-todo 2>actual &&
1562 test_cmp expect.3 actual &&
1563 test_must_fail git rebase --continue 2>actual &&
1564 test_cmp expect.3 actual &&
1565 cp orig .git/rebase-merge/git-rebase-todo &&
1566 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1567 git rebase --continue 2>actual
1568 ) &&
1569 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1570 test_grep \
1571 "Successfully rebased and updated refs/heads/missing-commit" \
1572 actual
1575 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1576 test_config rebase.missingCommitsCheck error &&
1578 set_fake_editor &&
1579 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1580 ) &&
1581 git rebase --edit-todo &&
1582 test_must_fail git rebase --continue &&
1583 echo x >file1 &&
1584 git add file1 &&
1585 git rebase --continue
1588 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1589 test_config rebase.missingCommitsCheck error &&
1591 set_fake_editor &&
1592 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1593 cp .git/rebase-merge/git-rebase-todo todo &&
1594 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1595 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1596 git rebase --continue
1600 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1601 rebase_setup_and_clean abbrevcmd &&
1602 test_commit "first" file1.txt "first line" first &&
1603 test_commit "second" file1.txt "another line" second &&
1604 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1605 test_commit "squash! second" file1.txt "another line here" second_squash &&
1606 cat >expected <<-EOF &&
1607 p $(git rev-list --abbrev-commit -1 first) first
1608 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1609 x git show HEAD
1610 p $(git rev-list --abbrev-commit -1 second) second
1611 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1612 x git show HEAD
1614 git checkout abbrevcmd &&
1615 test_config rebase.abbreviateCommands true &&
1617 set_cat_todo_editor &&
1618 test_must_fail git rebase -i --exec "git show HEAD" \
1619 --autosquash primary >actual
1620 ) &&
1621 test_cmp expected actual
1624 test_expect_success 'static check of bad command' '
1625 rebase_setup_and_clean bad-cmd &&
1627 set_fake_editor &&
1628 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1629 git rebase -i --root 2>actual &&
1630 test_grep "pickled $(git rev-list --oneline -1 primary~1)" \
1631 actual &&
1632 test_grep "You can fix this with .git rebase --edit-todo.." \
1633 actual &&
1634 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1635 ) &&
1636 git rebase --continue &&
1637 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1638 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1641 test_expect_success 'the first command cannot be a fixup' '
1642 rebase_setup_and_clean fixup-first &&
1644 cat >orig <<-EOF &&
1645 fixup $(git log -1 --format="%h %s" B)
1646 pick $(git log -1 --format="%h %s" C)
1650 set_replace_editor orig &&
1651 test_must_fail git rebase -i A 2>actual
1652 ) &&
1653 grep "cannot .fixup. without a previous commit" actual &&
1654 grep "You can fix this with .git rebase --edit-todo.." actual &&
1655 # verify that the todo list has not been truncated
1656 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1657 test_cmp orig actual &&
1659 test_must_fail git rebase --edit-todo 2>actual &&
1660 grep "cannot .fixup. without a previous commit" actual &&
1661 grep "You can fix this with .git rebase --edit-todo.." actual &&
1662 # verify that the todo list has not been truncated
1663 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1664 test_cmp orig actual
1667 test_expect_success 'tabs and spaces are accepted in the todolist' '
1668 rebase_setup_and_clean indented-comment &&
1669 write_script add-indent.sh <<-\EOF &&
1671 # Turn single spaces into space/tab mix
1672 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1673 printf "\n\t# comment\n #more\n\t # comment\n"
1674 ) >"$1.new"
1675 mv "$1.new" "$1"
1678 test_set_editor "$(pwd)/add-indent.sh" &&
1679 git rebase -i HEAD^^^
1680 ) &&
1681 test E = $(git cat-file commit HEAD | sed -ne \$p)
1684 test_expect_success 'static check of bad SHA-1' '
1685 rebase_setup_and_clean bad-sha &&
1687 set_fake_editor &&
1688 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1689 git rebase -i --root 2>actual &&
1690 test_grep "edit XXXXXXX False commit" actual &&
1691 test_grep "You can fix this with .git rebase --edit-todo.." \
1692 actual &&
1693 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1694 ) &&
1695 git rebase --continue &&
1696 test E = $(git cat-file commit HEAD | sed -ne \$p)
1699 test_expect_success 'editor saves as CR/LF' '
1700 git checkout -b with-crlf &&
1701 write_script add-crs.sh <<-\EOF &&
1702 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1703 mv -f "$1".new "$1"
1706 test_set_editor "$(pwd)/add-crs.sh" &&
1707 git rebase -i HEAD^
1711 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1712 test_when_finished "test_might_fail git rebase --abort" &&
1714 set_fake_editor &&
1715 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1716 HEAD^ >out 2>err
1717 ) &&
1718 test_grep "$SQ-S\"S I Gner\"$SQ" err
1721 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1722 test_when_finished "test_might_fail git rebase --abort" &&
1723 test_config commit.gpgsign true &&
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_grep "$SQ-S\"S I Gner\"$SQ" err
1732 test_expect_success 'valid author header after --root swap' '
1733 rebase_setup_and_clean author-header no-conflict-branch &&
1734 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1735 git cat-file commit HEAD | grep ^author >expected &&
1737 set_fake_editor &&
1738 FAKE_LINES="5 1" git rebase -i --root
1739 ) &&
1740 git cat-file commit HEAD^ | grep ^author >actual &&
1741 test_cmp expected actual
1744 test_expect_success 'valid author header when author contains single quote' '
1745 rebase_setup_and_clean author-header no-conflict-branch &&
1746 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1747 git cat-file commit HEAD | grep ^author >expected &&
1749 set_fake_editor &&
1750 FAKE_LINES="2" git rebase -i HEAD~2
1751 ) &&
1752 git cat-file commit HEAD | grep ^author >actual &&
1753 test_cmp expected actual
1756 test_expect_success 'post-commit hook is called' '
1757 >actual &&
1758 test_hook post-commit <<-\EOS &&
1759 git rev-parse HEAD >>actual
1762 set_fake_editor &&
1763 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1764 echo x>file3 &&
1765 git add file3 &&
1766 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1767 ) &&
1768 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1769 >expect &&
1770 test_cmp expect actual
1773 test_expect_success 'correct error message for partial commit after empty pick' '
1774 test_when_finished "git rebase --abort" &&
1776 set_fake_editor &&
1777 FAKE_LINES="2 1 1" &&
1778 export FAKE_LINES &&
1779 test_must_fail git rebase -i A D
1780 ) &&
1781 echo x >file1 &&
1782 test_must_fail git commit file1 2>err &&
1783 test_grep "cannot do a partial commit during a rebase." err
1786 test_expect_success 'correct error message for commit --amend after empty pick' '
1787 test_when_finished "git rebase --abort" &&
1789 set_fake_editor &&
1790 FAKE_LINES="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 -a --amend 2>err &&
1796 test_grep "middle of a rebase -- cannot amend." err
1799 test_expect_success 'todo has correct onto hash' '
1800 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1801 onto=$(git rev-parse --short HEAD~4) &&
1802 test_grep "^# Rebase ..* onto $onto" actual
1805 test_expect_success 'ORIG_HEAD is updated correctly' '
1806 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1807 git checkout -b test-orig-head A &&
1808 git commit --allow-empty -m A1 &&
1809 git commit --allow-empty -m A2 &&
1810 git commit --allow-empty -m A3 &&
1811 git commit --allow-empty -m A4 &&
1812 git rebase primary &&
1813 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1816 test_expect_success '--update-refs adds label and update-ref commands' '
1817 git checkout -b update-refs no-conflict-branch &&
1818 git branch -f base HEAD~4 &&
1819 git branch -f first HEAD~3 &&
1820 git branch -f second HEAD~3 &&
1821 git branch -f third HEAD~1 &&
1822 git commit --allow-empty --fixup=third &&
1823 git branch -f is-not-reordered &&
1824 git commit --allow-empty --fixup=HEAD~4 &&
1825 git branch -f shared-tip &&
1827 set_cat_todo_editor &&
1829 cat >expect <<-EOF &&
1830 pick $(git log -1 --format=%h J) J
1831 fixup $(git log -1 --format=%h update-refs) fixup! J # empty
1832 update-ref refs/heads/second
1833 update-ref refs/heads/first
1834 pick $(git log -1 --format=%h K) K
1835 pick $(git log -1 --format=%h L) L
1836 fixup $(git log -1 --format=%h is-not-reordered) fixup! L # empty
1837 update-ref refs/heads/third
1838 pick $(git log -1 --format=%h M) M
1839 update-ref refs/heads/no-conflict-branch
1840 update-ref refs/heads/is-not-reordered
1841 update-ref refs/heads/shared-tip
1844 test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
1845 test_cmp expect todo &&
1847 test_must_fail git -c rebase.autosquash=true \
1848 -c rebase.updaterefs=true \
1849 rebase -i primary >todo &&
1851 test_cmp expect todo
1855 test_expect_success '--update-refs adds commands with --rebase-merges' '
1856 git checkout -b update-refs-with-merge no-conflict-branch &&
1857 git branch -f base HEAD~4 &&
1858 git branch -f first HEAD~3 &&
1859 git branch -f second HEAD~3 &&
1860 git branch -f third HEAD~1 &&
1861 git merge -m merge branch2 &&
1862 git branch -f merge-branch &&
1863 git commit --fixup=third --allow-empty &&
1865 set_cat_todo_editor &&
1867 cat >expect <<-EOF &&
1868 label onto
1869 reset onto
1870 pick $(git log -1 --format=%h branch2~1) F
1871 pick $(git log -1 --format=%h branch2) I
1872 update-ref refs/heads/branch2
1873 label merge
1874 reset onto
1875 pick $(git log -1 --format=%h refs/heads/second) J
1876 update-ref refs/heads/second
1877 update-ref refs/heads/first
1878 pick $(git log -1 --format=%h refs/heads/third~1) K
1879 pick $(git log -1 --format=%h refs/heads/third) L
1880 fixup $(git log -1 --format=%h update-refs-with-merge) fixup! L # empty
1881 update-ref refs/heads/third
1882 pick $(git log -1 --format=%h HEAD~2) M
1883 update-ref refs/heads/no-conflict-branch
1884 merge -C $(git log -1 --format=%h HEAD~1) merge # merge
1885 update-ref refs/heads/merge-branch
1888 test_must_fail git rebase -i --autosquash \
1889 --rebase-merges=rebase-cousins \
1890 --update-refs primary >todo &&
1892 test_cmp expect todo &&
1894 test_must_fail git -c rebase.autosquash=true \
1895 -c rebase.updaterefs=true \
1896 rebase -i \
1897 --rebase-merges=rebase-cousins \
1898 primary >todo &&
1900 test_cmp expect todo
1904 test_expect_success '--update-refs updates refs correctly' '
1905 git checkout -B update-refs no-conflict-branch &&
1906 git branch -f base HEAD~4 &&
1907 git branch -f first HEAD~3 &&
1908 git branch -f second HEAD~3 &&
1909 git branch -f third HEAD~1 &&
1910 test_commit extra2 fileX &&
1911 git commit --amend --fixup=L &&
1913 git rebase -i --autosquash --update-refs primary 2>err &&
1915 test_cmp_rev HEAD~3 refs/heads/first &&
1916 test_cmp_rev HEAD~3 refs/heads/second &&
1917 test_cmp_rev HEAD~1 refs/heads/third &&
1918 test_cmp_rev HEAD refs/heads/no-conflict-branch &&
1920 cat >expect <<-\EOF &&
1921 Successfully rebased and updated refs/heads/update-refs.
1922 Updated the following refs with --update-refs:
1923 refs/heads/first
1924 refs/heads/no-conflict-branch
1925 refs/heads/second
1926 refs/heads/third
1929 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
1930 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
1931 <err >err.trimmed &&
1932 test_cmp expect err.trimmed
1935 test_expect_success 'respect user edits to update-ref steps' '
1936 git checkout -B update-refs-break no-conflict-branch &&
1937 git branch -f base HEAD~4 &&
1938 git branch -f first HEAD~3 &&
1939 git branch -f second HEAD~3 &&
1940 git branch -f third HEAD~1 &&
1941 git branch -f unseen base &&
1943 # First, we will add breaks to the expected todo file
1944 cat >fake-todo-1 <<-EOF &&
1945 pick $(git rev-parse HEAD~3)
1946 break
1947 update-ref refs/heads/second
1948 update-ref refs/heads/first
1950 pick $(git rev-parse HEAD~2)
1951 pick $(git rev-parse HEAD~1)
1952 update-ref refs/heads/third
1954 pick $(git rev-parse HEAD)
1955 update-ref refs/heads/no-conflict-branch
1958 # Second, we will drop some update-refs commands (and move one)
1959 cat >fake-todo-2 <<-EOF &&
1960 update-ref refs/heads/second
1962 pick $(git rev-parse HEAD~2)
1963 update-ref refs/heads/third
1964 pick $(git rev-parse HEAD~1)
1965 break
1967 pick $(git rev-parse HEAD)
1970 # Third, we will:
1971 # * insert a new one (new-branch),
1972 # * re-add an old one (first), and
1973 # * add a second instance of a previously-stored one (second)
1974 cat >fake-todo-3 <<-EOF &&
1975 update-ref refs/heads/unseen
1976 update-ref refs/heads/new-branch
1977 pick $(git rev-parse HEAD)
1978 update-ref refs/heads/first
1979 update-ref refs/heads/second
1983 set_replace_editor fake-todo-1 &&
1984 git rebase -i --update-refs primary &&
1986 # These branches are currently locked.
1987 for b in first second third no-conflict-branch
1989 test_must_fail git branch -f $b base || return 1
1990 done &&
1992 set_replace_editor fake-todo-2 &&
1993 git rebase --edit-todo &&
1995 # These branches are currently locked.
1996 for b in second third
1998 test_must_fail git branch -f $b base || return 1
1999 done &&
2001 # These branches are currently unlocked for checkout.
2002 for b in first no-conflict-branch
2004 git worktree add wt-$b $b &&
2005 git worktree remove wt-$b || return 1
2006 done &&
2008 git rebase --continue &&
2010 set_replace_editor fake-todo-3 &&
2011 git rebase --edit-todo &&
2013 # These branches are currently locked.
2014 for b in second third first unseen
2016 test_must_fail git branch -f $b base || return 1
2017 done &&
2019 # These branches are currently unlocked for checkout.
2020 for b in no-conflict-branch
2022 git worktree add wt-$b $b &&
2023 git worktree remove wt-$b || return 1
2024 done &&
2026 git rebase --continue
2027 ) &&
2029 test_cmp_rev HEAD~2 refs/heads/third &&
2030 test_cmp_rev HEAD~1 refs/heads/unseen &&
2031 test_cmp_rev HEAD~1 refs/heads/new-branch &&
2032 test_cmp_rev HEAD refs/heads/first &&
2033 test_cmp_rev HEAD refs/heads/second &&
2034 test_cmp_rev HEAD refs/heads/no-conflict-branch
2037 test_expect_success '--update-refs: all update-ref lines removed' '
2038 git checkout -b test-refs-not-removed no-conflict-branch &&
2039 git branch -f base HEAD~4 &&
2040 git branch -f first HEAD~3 &&
2041 git branch -f second HEAD~3 &&
2042 git branch -f third HEAD~1 &&
2043 git branch -f tip &&
2045 test_commit test-refs-not-removed &&
2046 git commit --amend --fixup first &&
2048 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2051 set_cat_todo_editor &&
2052 test_must_fail git rebase -i --update-refs base >todo.raw &&
2053 sed -e "/^update-ref/d" <todo.raw >todo
2054 ) &&
2056 set_replace_editor todo &&
2057 git rebase -i --update-refs base
2058 ) &&
2060 # Ensure refs are not deleted and their OIDs have not changed
2061 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2062 test_cmp expect-oids actual-oids
2065 test_expect_success '--update-refs: all update-ref lines removed, then some re-added' '
2066 git checkout -b test-refs-not-removed2 no-conflict-branch &&
2067 git branch -f base HEAD~4 &&
2068 git branch -f first HEAD~3 &&
2069 git branch -f second HEAD~3 &&
2070 git branch -f third HEAD~1 &&
2071 git branch -f tip &&
2073 test_commit test-refs-not-removed2 &&
2074 git commit --amend --fixup first &&
2076 git rev-parse first second third >expect-oids &&
2079 set_cat_todo_editor &&
2080 test_must_fail git rebase -i \
2081 --autosquash --update-refs \
2082 base >todo.raw &&
2083 sed -e "/^update-ref/d" <todo.raw >todo
2084 ) &&
2086 # Add a break to the end of the todo so we can edit later
2087 echo "break" >>todo &&
2090 set_replace_editor todo &&
2091 git rebase -i --autosquash --update-refs base &&
2092 echo "update-ref refs/heads/tip" >todo &&
2093 git rebase --edit-todo &&
2094 git rebase --continue
2095 ) &&
2097 # Ensure first/second/third are unchanged, but tip is updated
2098 git rev-parse first second third >actual-oids &&
2099 test_cmp expect-oids actual-oids &&
2100 test_cmp_rev HEAD tip
2103 test_expect_success '--update-refs: --edit-todo with no update-ref lines' '
2104 git checkout -b test-refs-not-removed3 no-conflict-branch &&
2105 git branch -f base HEAD~4 &&
2106 git branch -f first HEAD~3 &&
2107 git branch -f second HEAD~3 &&
2108 git branch -f third HEAD~1 &&
2109 git branch -f tip &&
2111 test_commit test-refs-not-removed3 &&
2112 git commit --amend --fixup first &&
2114 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2117 set_cat_todo_editor &&
2118 test_must_fail git rebase -i \
2119 --autosquash --update-refs \
2120 base >todo.raw &&
2121 sed -e "/^update-ref/d" <todo.raw >todo
2122 ) &&
2124 # Add a break to the beginning of the todo so we can resume with no
2125 # update-ref lines
2126 echo "break" >todo.new &&
2127 cat todo >>todo.new &&
2130 set_replace_editor todo.new &&
2131 git rebase -i --autosquash --update-refs base &&
2133 # Make no changes when editing so update-refs is still empty
2134 cat todo >todo.new &&
2135 git rebase --edit-todo &&
2136 git rebase --continue
2137 ) &&
2139 # Ensure refs are not deleted and their OIDs have not changed
2140 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2141 test_cmp expect-oids actual-oids
2144 test_expect_success '--update-refs: check failed ref update' '
2145 test_when_finished "test_might_fail git rebase --abort" &&
2146 git checkout -B update-refs-error no-conflict-branch &&
2147 git branch -f base HEAD~4 &&
2148 git branch -f first HEAD~3 &&
2149 git branch -f second HEAD~2 &&
2150 git branch -f third HEAD~1 &&
2152 cat >fake-todo <<-EOF &&
2153 pick $(git rev-parse HEAD~3)
2154 break
2155 update-ref refs/heads/first
2157 pick $(git rev-parse HEAD~2)
2158 update-ref refs/heads/second
2160 pick $(git rev-parse HEAD~1)
2161 update-ref refs/heads/third
2163 pick $(git rev-parse HEAD)
2164 update-ref refs/heads/no-conflict-branch
2168 set_replace_editor fake-todo &&
2169 git rebase -i --update-refs base
2170 ) &&
2172 # At this point, the values of first, second, and third are
2173 # recorded in the update-refs file. We will force-update the
2174 # "second" ref, but "git branch -f" will not work because of
2175 # the lock in the update-refs file.
2176 git update-ref refs/heads/second third &&
2178 test_must_fail git rebase --continue 2>err &&
2179 grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
2181 cat >expect <<-\EOF &&
2182 Updated the following refs with --update-refs:
2183 refs/heads/first
2184 refs/heads/no-conflict-branch
2185 refs/heads/third
2186 Failed to update the following refs with --update-refs:
2187 refs/heads/second
2190 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
2191 tail -n 6 err >err.last &&
2192 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
2193 <err.last >err.trimmed &&
2194 test_cmp expect err.trimmed
2197 test_expect_success 'bad labels and refs rejected when parsing todo list' '
2198 test_when_finished "test_might_fail git rebase --abort" &&
2199 cat >todo <<-\EOF &&
2200 exec >execed
2201 label #
2202 label :invalid
2203 update-ref :bad
2204 update-ref topic
2206 rm -f execed &&
2208 set_replace_editor todo &&
2209 test_must_fail git rebase -i HEAD 2>err
2210 ) &&
2211 grep "'\''#'\'' is not a valid label" err &&
2212 grep "'\'':invalid'\'' is not a valid label" err &&
2213 grep "'\'':bad'\'' is not a valid refname" err &&
2214 grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \
2215 err &&
2216 test_path_is_missing execed
2219 test_expect_success 'non-merge commands reject merge commits' '
2220 test_when_finished "test_might_fail git rebase --abort" &&
2221 git checkout E &&
2222 git merge I &&
2223 oid=$(git rev-parse HEAD) &&
2224 cat >todo <<-EOF &&
2225 pick $oid
2226 reword $oid
2227 edit $oid
2228 fixup $oid
2229 squash $oid
2232 set_replace_editor todo &&
2233 test_must_fail git rebase -i HEAD 2>actual
2234 ) &&
2235 cat >expect <<-EOF &&
2236 error: ${SQ}pick${SQ} does not accept merge commits
2237 hint: ${SQ}pick${SQ} does not take a merge commit. If you wanted to
2238 hint: replay the merge, use ${SQ}merge -C${SQ} on the commit.
2239 hint: Disable this message with "git config advice.rebaseTodoError false"
2240 error: invalid line 1: pick $oid
2241 error: ${SQ}reword${SQ} does not accept merge commits
2242 hint: ${SQ}reword${SQ} does not take a merge commit. If you wanted to
2243 hint: replay the merge and reword the commit message, use
2244 hint: ${SQ}merge -c${SQ} on the commit
2245 hint: Disable this message with "git config advice.rebaseTodoError false"
2246 error: invalid line 2: reword $oid
2247 error: ${SQ}edit${SQ} does not accept merge commits
2248 hint: ${SQ}edit${SQ} does not take a merge commit. If you wanted to
2249 hint: replay the merge, use ${SQ}merge -C${SQ} on the commit, and then
2250 hint: ${SQ}break${SQ} to give the control back to you so that you can
2251 hint: do ${SQ}git commit --amend && git rebase --continue${SQ}.
2252 hint: Disable this message with "git config advice.rebaseTodoError false"
2253 error: invalid line 3: edit $oid
2254 error: cannot squash merge commit into another commit
2255 error: invalid line 4: fixup $oid
2256 error: cannot squash merge commit into another commit
2257 error: invalid line 5: squash $oid
2258 You can fix this with ${SQ}git rebase --edit-todo${SQ} and then run ${SQ}git rebase --continue${SQ}.
2259 Or you can abort the rebase with ${SQ}git rebase --abort${SQ}.
2261 test_cmp expect actual
2264 # This must be the last test in this file
2265 test_expect_success '$EDITOR and friends are unchanged' '
2266 test_editor_unchanged
2269 test_done