advice: omit trailing whitespace
[git.git] / t / t3404-rebase-interactive.sh
blobd1bead61fad03d8a847d75062ede8ad852f7d8e6
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 'cherry-pick works with rebase --exec' '
157 test_when_finished "git cherry-pick --abort; \
158 git rebase --abort; \
159 git checkout primary" &&
160 echo "exec git cherry-pick G" >todo &&
162 set_replace_editor todo &&
163 test_must_fail git rebase -i D D
164 ) &&
165 test_cmp_rev G CHERRY_PICK_HEAD
168 test_expect_success 'rebase -x with empty command fails' '
169 test_when_finished "git rebase --abort ||:" &&
170 test_must_fail env git rebase -x "" @ 2>actual &&
171 test_write_lines "error: empty exec command" >expected &&
172 test_cmp expected actual &&
173 test_must_fail env git rebase -x " " @ 2>actual &&
174 test_cmp expected actual
177 test_expect_success 'rebase -x with newline in command fails' '
178 test_when_finished "git rebase --abort ||:" &&
179 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
180 test_write_lines "error: exec commands cannot contain newlines" \
181 >expected &&
182 test_cmp expected actual
185 test_expect_success 'rebase -i with exec of inexistent command' '
186 git checkout primary &&
187 test_when_finished "git rebase --abort" &&
189 set_fake_editor &&
190 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
191 git rebase -i HEAD^ >actual 2>&1
192 ) &&
193 ! grep "Maybe git-rebase is broken" actual
196 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
197 test_when_finished "git rebase --abort ||:" &&
198 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
201 test_expect_success 'no changes are a nop' '
202 git checkout branch2 &&
203 git rebase -i F &&
204 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
205 test_cmp_rev I HEAD
208 test_expect_success 'test the [branch] option' '
209 git checkout -b dead-end &&
210 git rm file6 &&
211 git commit -m "stop here" &&
212 git rebase -i F branch2 &&
213 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
214 test_cmp_rev I branch2 &&
215 test_cmp_rev I HEAD
218 test_expect_success 'test --onto <branch>' '
219 git checkout -b test-onto branch2 &&
220 git rebase -i --onto branch1 F &&
221 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
222 test_cmp_rev HEAD^ branch1 &&
223 test_cmp_rev I branch2
226 test_expect_success 'rebase on top of a non-conflicting commit' '
227 git checkout branch1 &&
228 git tag original-branch1 &&
229 git rebase -i branch2 &&
230 test file6 = $(git diff --name-only original-branch1) &&
231 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
232 test_cmp_rev I branch2 &&
233 test_cmp_rev I HEAD~2
236 test_expect_success 'reflog for the branch shows state before rebase' '
237 test_cmp_rev branch1@{1} original-branch1
240 test_expect_success 'reflog for the branch shows correct finish message' '
241 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
242 "$(git rev-parse branch2)" >expected &&
243 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
244 test_cmp expected actual
247 test_expect_success 'exchange two commits' '
249 set_fake_editor &&
250 FAKE_LINES="2 1" git rebase -i HEAD~2
251 ) &&
252 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
253 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
254 blob1=$(git rev-parse --short HEAD^:file1) &&
255 blob2=$(git rev-parse --short HEAD:file1) &&
256 commit=$(git rev-parse --short HEAD)
259 test_expect_success 'stop on conflicting pick' '
260 cat >expect <<-EOF &&
261 diff --git a/file1 b/file1
262 index $blob1..$blob2 100644
263 --- a/file1
264 +++ b/file1
265 @@ -1 +1 @@
269 cat >expect2 <<-EOF &&
270 <<<<<<< HEAD
272 =======
274 >>>>>>> $commit (G)
276 git tag new-branch1 &&
277 test_must_fail git rebase -i primary &&
278 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
279 test_cmp expect .git/rebase-merge/patch &&
280 test_cmp expect2 file1 &&
281 test "$(git diff --name-status |
282 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
283 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
284 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
287 test_expect_success 'show conflicted patch' '
288 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
289 grep "show.*REBASE_HEAD" stderr &&
290 # the original stopped-sha1 is abbreviated
291 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
292 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
295 test_expect_success 'abort' '
296 git rebase --abort &&
297 test_cmp_rev new-branch1 HEAD &&
298 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
299 test_path_is_missing .git/rebase-merge
302 test_expect_success 'abort with error when new base cannot be checked out' '
303 git rm --cached file1 &&
304 git commit -m "remove file in base" &&
305 test_must_fail git rebase -i primary > output 2>&1 &&
306 test_grep "The following untracked working tree files would be overwritten by checkout:" \
307 output &&
308 test_grep "file1" output &&
309 test_path_is_missing .git/rebase-merge &&
310 rm file1 &&
311 git reset --hard HEAD^
314 test_expect_success 'retain authorship' '
315 echo A > file7 &&
316 git add file7 &&
317 test_tick &&
318 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
319 git tag twerp &&
320 git rebase -i --onto primary HEAD^ &&
321 git show HEAD | grep "^Author: Twerp Snog"
324 test_expect_success 'retain authorship w/ conflicts' '
325 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
326 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
328 git reset --hard twerp &&
329 test_commit a conflict a conflict-a &&
330 git reset --hard twerp &&
332 GIT_AUTHOR_NAME=AttributeMe &&
333 export GIT_AUTHOR_NAME &&
334 test_commit b conflict b conflict-b &&
335 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
337 test_must_fail git rebase -i conflict-a &&
338 echo resolved >conflict &&
339 git add conflict &&
340 git rebase --continue &&
341 test_cmp_rev conflict-a^0 HEAD^ &&
342 git show >out &&
343 grep AttributeMe out
346 test_expect_success 'squash' '
347 git reset --hard twerp &&
348 echo B > file7 &&
349 test_tick &&
350 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
351 echo "******************************" &&
353 set_fake_editor &&
354 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
355 git rebase -i --onto primary HEAD~2
356 ) &&
357 test B = $(cat file7) &&
358 test_cmp_rev HEAD^ primary
361 test_expect_success 'retain authorship when squashing' '
362 git show HEAD | grep "^Author: Twerp Snog"
365 test_expect_success '--continue tries to commit' '
366 git reset --hard D &&
367 test_tick &&
369 set_fake_editor &&
370 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
371 echo resolved > file1 &&
372 git add file1 &&
373 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
374 ) &&
375 test_cmp_rev HEAD^ new-branch1 &&
376 git show HEAD | grep chouette
379 test_expect_success 'verbose flag is heeded, even after --continue' '
380 git reset --hard primary@{1} &&
381 test_tick &&
382 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
383 echo resolved > file1 &&
384 git add file1 &&
385 git rebase --continue > output &&
386 grep "^ file1 | 2 +-$" output
389 test_expect_success 'multi-squash only fires up editor once' '
390 base=$(git rev-parse HEAD~4) &&
392 set_fake_editor &&
393 FAKE_COMMIT_AMEND="ONCE" \
394 FAKE_LINES="1 squash 2 squash 3 squash 4" \
395 EXPECT_HEADER_COUNT=4 \
396 git rebase -i $base
397 ) &&
398 test $base = $(git rev-parse HEAD^) &&
399 test 1 = $(git show | grep ONCE | wc -l)
402 test_expect_success 'multi-fixup does not fire up editor' '
403 git checkout -b multi-fixup E &&
404 base=$(git rev-parse HEAD~4) &&
406 set_fake_editor &&
407 FAKE_COMMIT_AMEND="NEVER" \
408 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
409 git rebase -i $base
410 ) &&
411 test $base = $(git rev-parse HEAD^) &&
412 test 0 = $(git show | grep NEVER | wc -l) &&
413 git checkout @{-1} &&
414 git branch -D multi-fixup
417 test_expect_success 'commit message used after conflict' '
418 git checkout -b conflict-fixup conflict-branch &&
419 base=$(git rev-parse HEAD~4) &&
421 set_fake_editor &&
422 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
423 git rebase -i $base &&
424 echo three > conflict &&
425 git add conflict &&
426 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
427 git rebase --continue
428 ) &&
429 test $base = $(git rev-parse HEAD^) &&
430 test 1 = $(git show | grep ONCE | wc -l) &&
431 git checkout @{-1} &&
432 git branch -D conflict-fixup
435 test_expect_success 'commit message retained after conflict' '
436 git checkout -b conflict-squash conflict-branch &&
437 base=$(git rev-parse HEAD~4) &&
439 set_fake_editor &&
440 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
441 git rebase -i $base &&
442 echo three > conflict &&
443 git add conflict &&
444 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
445 git rebase --continue
446 ) &&
447 test $base = $(git rev-parse HEAD^) &&
448 test 2 = $(git show | grep TWICE | wc -l) &&
449 git checkout @{-1} &&
450 git branch -D conflict-squash
453 test_expect_success 'squash and fixup generate correct log messages' '
454 cat >expect-squash-fixup <<-\EOF &&
459 ONCE
461 git checkout -b squash-fixup E &&
462 base=$(git rev-parse HEAD~4) &&
464 set_fake_editor &&
465 FAKE_COMMIT_AMEND="ONCE" \
466 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
467 EXPECT_HEADER_COUNT=4 \
468 git rebase -i $base
469 ) &&
470 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
471 test_cmp expect-squash-fixup actual-squash-fixup &&
472 git cat-file commit HEAD@{2} |
473 grep "^# This is a combination of 3 commits\." &&
474 git cat-file commit HEAD@{3} |
475 grep "^# This is a combination of 2 commits\." &&
476 git checkout @{-1} &&
477 git branch -D squash-fixup
480 test_expect_success 'squash ignores comments' '
481 git checkout -b skip-comments E &&
482 base=$(git rev-parse HEAD~4) &&
484 set_fake_editor &&
485 FAKE_COMMIT_AMEND="ONCE" \
486 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
487 EXPECT_HEADER_COUNT=4 \
488 git rebase -i $base
489 ) &&
490 test $base = $(git rev-parse HEAD^) &&
491 test 1 = $(git show | grep ONCE | wc -l) &&
492 git checkout @{-1} &&
493 git branch -D skip-comments
496 test_expect_success 'squash ignores blank lines' '
497 git checkout -b skip-blank-lines E &&
498 base=$(git rev-parse HEAD~4) &&
500 set_fake_editor &&
501 FAKE_COMMIT_AMEND="ONCE" \
502 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
503 EXPECT_HEADER_COUNT=4 \
504 git rebase -i $base
505 ) &&
506 test $base = $(git rev-parse HEAD^) &&
507 test 1 = $(git show | grep ONCE | wc -l) &&
508 git checkout @{-1} &&
509 git branch -D skip-blank-lines
512 test_expect_success 'squash works as expected' '
513 git checkout -b squash-works no-conflict-branch &&
514 one=$(git rev-parse HEAD~3) &&
516 set_fake_editor &&
517 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
518 ) &&
519 test $one = $(git rev-parse HEAD~2)
522 test_expect_success 'interrupted squash works as expected' '
523 git checkout -b interrupted-squash conflict-branch &&
524 one=$(git rev-parse HEAD~3) &&
526 set_fake_editor &&
527 test_must_fail env FAKE_LINES="1 squash 3 2" \
528 git rebase -i HEAD~3
529 ) &&
530 test_write_lines one two four > conflict &&
531 git add conflict &&
532 test_must_fail git rebase --continue &&
533 echo resolved > conflict &&
534 git add conflict &&
535 git rebase --continue &&
536 test $one = $(git rev-parse HEAD~2)
539 test_expect_success 'interrupted squash works as expected (case 2)' '
540 git checkout -b interrupted-squash2 conflict-branch &&
541 one=$(git rev-parse HEAD~3) &&
543 set_fake_editor &&
544 test_must_fail env FAKE_LINES="3 squash 1 2" \
545 git rebase -i HEAD~3
546 ) &&
547 test_write_lines one four > conflict &&
548 git add conflict &&
549 test_must_fail git rebase --continue &&
550 test_write_lines one two four > conflict &&
551 git add conflict &&
552 test_must_fail git rebase --continue &&
553 echo resolved > conflict &&
554 git add conflict &&
555 git rebase --continue &&
556 test $one = $(git rev-parse HEAD~2)
559 test_expect_success '--continue tries to commit, even for "edit"' '
560 echo unrelated > file7 &&
561 git add file7 &&
562 test_tick &&
563 git commit -m "unrelated change" &&
564 parent=$(git rev-parse HEAD^) &&
565 test_tick &&
567 set_fake_editor &&
568 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
569 echo edited > file7 &&
570 git add file7 &&
571 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
572 ) &&
573 test edited = $(git show HEAD:file7) &&
574 git show HEAD | grep chouette &&
575 test $parent = $(git rev-parse HEAD^)
578 test_expect_success 'aborted --continue does not squash commits after "edit"' '
579 old=$(git rev-parse HEAD) &&
580 test_tick &&
582 set_fake_editor &&
583 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
584 echo "edited again" > file7 &&
585 git add file7 &&
586 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
587 ) &&
588 test $old = $(git rev-parse HEAD) &&
589 git rebase --abort
592 test_expect_success 'auto-amend only edited commits after "edit"' '
593 test_tick &&
595 set_fake_editor &&
596 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
597 echo "edited again" > file7 &&
598 git add file7 &&
599 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
600 echo "and again" > file7 &&
601 git add file7 &&
602 test_tick &&
603 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
604 git rebase --continue
605 ) &&
606 git rebase --abort
609 test_expect_success 'clean error after failed "exec"' '
610 test_tick &&
611 test_when_finished "git rebase --abort || :" &&
613 set_fake_editor &&
614 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
615 ) &&
616 echo "edited again" > file7 &&
617 git add file7 &&
618 test_must_fail git rebase --continue 2>error &&
619 test_grep "you have staged changes in your working tree" error &&
620 test_grep ! "could not open.*for reading" error
623 test_expect_success 'rebase a detached HEAD' '
624 grandparent=$(git rev-parse HEAD~2) &&
625 git checkout $(git rev-parse HEAD) &&
626 test_tick &&
628 set_fake_editor &&
629 FAKE_LINES="2 1" git rebase -i HEAD~2
630 ) &&
631 test $grandparent = $(git rev-parse HEAD~2)
634 test_expect_success 'rebase a commit violating pre-commit' '
635 test_hook pre-commit <<-\EOF &&
636 test -z "$(git diff --cached --check)"
638 echo "monde! " >> file1 &&
639 test_tick &&
640 test_must_fail git commit -m doesnt-verify file1 &&
641 git commit -m doesnt-verify --no-verify file1 &&
642 test_tick &&
644 set_fake_editor &&
645 FAKE_LINES=2 git rebase -i HEAD~2
649 test_expect_success 'rebase with a file named HEAD in worktree' '
650 git reset --hard &&
651 git checkout -b branch3 A &&
654 GIT_AUTHOR_NAME="Squashed Away" &&
655 export GIT_AUTHOR_NAME &&
656 >HEAD &&
657 git add HEAD &&
658 git commit -m "Add head" &&
659 >BODY &&
660 git add BODY &&
661 git commit -m "Add body"
662 ) &&
665 set_fake_editor &&
666 FAKE_LINES="1 squash 2" git rebase -i @{-1}
667 ) &&
668 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
672 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
674 git checkout -b branch4 HEAD &&
675 GIT_EDITOR=: git commit --amend \
676 --author="Somebody else <somebody@else.com>" &&
677 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
678 git rebase -i branch3 &&
679 test_cmp_rev branch3 branch4
683 test_expect_success 'submodule rebase setup' '
684 git checkout A &&
685 mkdir sub &&
687 cd sub && git init && >elif &&
688 git add elif && git commit -m "submodule initial"
689 ) &&
690 echo 1 >file1 &&
691 git add file1 sub &&
692 test_tick &&
693 git commit -m "One" &&
694 echo 2 >file1 &&
695 test_tick &&
696 git commit -a -m "Two" &&
698 cd sub && echo 3 >elif &&
699 git commit -a -m "submodule second"
700 ) &&
701 test_tick &&
702 git commit -a -m "Three changes submodule"
705 test_expect_success 'submodule rebase -i' '
707 set_fake_editor &&
708 FAKE_LINES="1 squash 2 3" git rebase -i A
712 test_expect_success 'submodule conflict setup' '
713 git tag submodule-base &&
714 git checkout HEAD^ &&
716 cd sub && git checkout HEAD^ && echo 4 >elif &&
717 git add elif && git commit -m "submodule conflict"
718 ) &&
719 git add sub &&
720 test_tick &&
721 git commit -m "Conflict in submodule" &&
722 git tag submodule-topic
725 test_expect_success 'rebase -i continue with only submodule staged' '
726 test_must_fail git rebase -i submodule-base &&
727 git add sub &&
728 git rebase --continue &&
729 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
732 test_expect_success 'rebase -i continue with unstaged submodule' '
733 git checkout submodule-topic &&
734 git reset --hard &&
735 test_must_fail git rebase -i submodule-base &&
736 git reset &&
737 git rebase --continue &&
738 test_cmp_rev submodule-base HEAD
741 test_expect_success 'avoid unnecessary reset' '
742 git checkout primary &&
743 git reset --hard &&
744 test-tool chmtime =123456789 file3 &&
745 git update-index --refresh &&
746 HEAD=$(git rev-parse HEAD) &&
747 git rebase -i HEAD~4 &&
748 test $HEAD = $(git rev-parse HEAD) &&
749 MTIME=$(test-tool chmtime --get file3) &&
750 test 123456789 = $MTIME
753 test_expect_success 'reword' '
754 git checkout -b reword-branch primary &&
756 set_fake_editor &&
757 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
758 git rebase -i A &&
759 git show HEAD | grep "E changed" &&
760 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
761 test_cmp_rev primary^ HEAD^ &&
762 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
763 git rebase -i A &&
764 git show HEAD^ | grep "D changed" &&
765 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
766 git rebase -i A &&
767 git show HEAD~3 | grep "B changed" &&
768 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
769 git rebase -i A
770 ) &&
771 git show HEAD~2 | grep "C changed"
774 test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '
775 git checkout E &&
776 test_when_finished "git checkout @{-1}" &&
778 set_fake_editor &&
779 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
780 export GIT_SEQUENCE_EDITOR &&
781 set_reword_editor &&
782 FAKE_LINES="reword 1 reword 2" git rebase -i C
783 ) &&
784 check_reworded_commits D E
787 test_expect_success 'rebase -i can copy notes' '
788 git config notes.rewrite.rebase true &&
789 git config notes.rewriteRef "refs/notes/*" &&
790 test_commit n1 &&
791 test_commit n2 &&
792 test_commit n3 &&
793 git notes add -m"a note" n3 &&
794 git rebase -i --onto n1 n2 &&
795 test "a note" = "$(git notes show HEAD)"
798 test_expect_success 'rebase -i can copy notes over a fixup' '
799 cat >expect <<-\EOF &&
800 an earlier note
802 a note
804 git reset --hard n3 &&
805 git notes add -m"an earlier note" n2 &&
807 set_fake_editor &&
808 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
809 git rebase -i n1
810 ) &&
811 git notes show > output &&
812 test_cmp expect output
815 test_expect_success 'rebase while detaching HEAD' '
816 git symbolic-ref HEAD &&
817 grandparent=$(git rev-parse HEAD~2) &&
818 test_tick &&
820 set_fake_editor &&
821 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
822 ) &&
823 test $grandparent = $(git rev-parse HEAD~2) &&
824 test_must_fail git symbolic-ref HEAD
827 test_tick # Ensure that the rebased commits get a different timestamp.
828 test_expect_success 'always cherry-pick with --no-ff' '
829 git checkout no-ff-branch &&
830 git tag original-no-ff-branch &&
831 git rebase -i --no-ff A &&
832 for p in 0 1 2
834 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
835 git diff HEAD~$p original-no-ff-branch~$p > out &&
836 test_must_be_empty out || return 1
837 done &&
838 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
839 git diff HEAD~3 original-no-ff-branch~3 > out &&
840 test_must_be_empty out
843 test_expect_success 'set up commits with funny messages' '
844 git checkout -b funny A &&
845 echo >>file1 &&
846 test_tick &&
847 git commit -a -m "end with slash\\" &&
848 echo >>file1 &&
849 test_tick &&
850 git commit -a -m "something (\000) that looks like octal" &&
851 echo >>file1 &&
852 test_tick &&
853 git commit -a -m "something (\n) that looks like a newline" &&
854 echo >>file1 &&
855 test_tick &&
856 git commit -a -m "another commit"
859 test_expect_success 'rebase-i history with funny messages' '
860 git rev-list A..funny >expect &&
861 test_tick &&
863 set_fake_editor &&
864 FAKE_LINES="1 2 3 4" git rebase -i A
865 ) &&
866 git rev-list A.. >actual &&
867 test_cmp expect actual
870 test_expect_success 'prepare for rebase -i --exec' '
871 git checkout primary &&
872 git checkout -b execute &&
873 test_commit one_exec main.txt one_exec &&
874 test_commit two_exec main.txt two_exec &&
875 test_commit three_exec main.txt three_exec
878 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
880 set_fake_editor &&
881 git rebase -i --exec "git show HEAD" 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 --exec git show HEAD -i"' '
891 git reset --hard execute &&
893 set_fake_editor &&
894 git rebase --exec "git show HEAD" -i 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
903 test_expect_success 'running "git rebase -ix git show HEAD"' '
904 git reset --hard execute &&
906 set_fake_editor &&
907 git rebase -ix "git show HEAD" HEAD~2 >actual &&
908 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
909 export FAKE_LINES &&
910 git rebase -i HEAD~2 >expect
911 ) &&
912 sed -e "1,9d" expect >expected &&
913 test_cmp expected actual
917 test_expect_success 'rebase -ix with several <CMD>' '
918 git reset --hard execute &&
920 set_fake_editor &&
921 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
922 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
923 export FAKE_LINES &&
924 git rebase -i HEAD~2 >expect
925 ) &&
926 sed -e "1,9d" expect >expected &&
927 test_cmp expected actual
930 test_expect_success 'rebase -ix with several instances of --exec' '
931 git reset --hard execute &&
933 set_fake_editor &&
934 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
935 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
936 exec_git_show_HEAD exec_pwd" &&
937 export FAKE_LINES &&
938 git rebase -i HEAD~2 >expect
939 ) &&
940 sed -e "1,11d" expect >expected &&
941 test_cmp expected actual
944 test_expect_success 'rebase -ix with --autosquash' '
945 git reset --hard execute &&
946 git checkout -b autosquash &&
947 echo second >second.txt &&
948 git add second.txt &&
949 git commit -m "fixup! two_exec" &&
950 echo bis >bis.txt &&
951 git add bis.txt &&
952 git commit -m "fixup! two_exec" &&
953 git checkout -b autosquash_actual &&
954 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
955 git checkout autosquash &&
957 set_fake_editor &&
958 git checkout -b autosquash_expected &&
959 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
960 export FAKE_LINES &&
961 git rebase -i HEAD~4 >expect
962 ) &&
963 sed -e "1,13d" expect >expected &&
964 test_cmp expected actual
967 test_expect_success 'rebase --exec works without -i ' '
968 git reset --hard execute &&
969 rm -rf exec_output &&
970 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
971 test_grep "Successfully rebased and updated" actual &&
972 test_line_count = 2 exec_output &&
973 test_path_is_missing invoked_editor
976 test_expect_success 'rebase -i --exec without <CMD>' '
977 git reset --hard execute &&
978 test_must_fail git rebase -i --exec 2>actual &&
979 test_grep "requires a value" actual &&
980 git checkout primary
983 test_expect_success 'rebase -i --root re-order and drop commits' '
984 git checkout E &&
986 set_fake_editor &&
987 FAKE_LINES="3 1 2 5" git rebase -i --root
988 ) &&
989 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
990 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
991 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
992 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
993 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
996 test_expect_success 'rebase -i --root retain root commit author and message' '
997 git checkout A &&
998 echo B >file7 &&
999 git add file7 &&
1000 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1002 set_fake_editor &&
1003 FAKE_LINES="2" git rebase -i --root
1004 ) &&
1005 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
1006 git cat-file commit HEAD | grep -q "^different author$"
1009 test_expect_success 'rebase -i --root temporary sentinel commit' '
1010 git checkout B &&
1012 set_fake_editor &&
1013 test_must_fail env FAKE_LINES="2" git rebase -i --root
1014 ) &&
1015 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1016 git rebase --abort
1019 test_expect_success 'rebase -i --root fixup root commit' '
1020 git checkout B &&
1022 set_fake_editor &&
1023 FAKE_LINES="1 fixup 2" git rebase -i --root
1024 ) &&
1025 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1026 test B = $(git show HEAD:file1) &&
1027 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1030 test_expect_success 'rebase -i --root reword original root commit' '
1031 test_when_finished "test_might_fail git rebase --abort" &&
1032 git checkout -b reword-original-root-branch primary &&
1034 set_fake_editor &&
1035 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1036 git rebase -i --root
1037 ) &&
1038 git show HEAD^ | grep "A changed" &&
1039 test -z "$(git show -s --format=%p HEAD^)"
1042 test_expect_success 'rebase -i --root reword new root commit' '
1043 test_when_finished "test_might_fail git rebase --abort" &&
1044 git checkout -b reword-now-root-branch primary &&
1046 set_fake_editor &&
1047 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1048 git rebase -i --root
1049 ) &&
1050 git show HEAD^ | grep "C changed" &&
1051 test -z "$(git show -s --format=%p HEAD^)"
1054 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1055 test_when_finished "reset_rebase" &&
1056 git checkout -b failing-root-pick A &&
1057 echo x >file2 &&
1058 git rm file1 &&
1059 git commit -m "remove file 1 add file 2" &&
1060 echo z >file1 &&
1062 set_fake_editor &&
1063 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1064 ) &&
1065 rm file1 &&
1066 git rebase --continue &&
1067 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1068 test "$(git rev-list --count HEAD)" = 2
1071 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1072 test_when_finished "reset_rebase" &&
1073 echo z>file1 &&
1075 set_fake_editor &&
1076 test_must_fail env FAKE_LINES="reword 1 2" \
1077 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1078 rm file1 &&
1079 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1080 ) &&
1081 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1082 test "$(git rev-list --count HEAD)" = 2
1085 test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
1086 git checkout reword-original-root-branch &&
1087 git reset --hard &&
1088 git checkout conflict-branch &&
1090 set_fake_editor &&
1091 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1092 test_must_fail git rebase --edit-todo
1093 ) &&
1094 git rebase --abort
1097 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1098 git reset --hard &&
1099 git checkout no-conflict-branch^0 &&
1101 set_fake_editor &&
1102 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1103 FAKE_LINES="2 1" git rebase --edit-todo &&
1104 git rebase --continue
1105 ) &&
1106 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1107 test L = $(git cat-file commit HEAD | sed -ne \$p)
1110 test_expect_success 'rebase -i produces readable reflog' '
1111 git reset --hard &&
1112 git branch -f branch-reflog-test H &&
1113 git rebase -i --onto I F branch-reflog-test &&
1114 cat >expect <<-\EOF &&
1115 rebase (finish): returning to refs/heads/branch-reflog-test
1116 rebase (pick): H
1117 rebase (pick): G
1118 rebase (start): checkout I
1120 git reflog -n4 HEAD |
1121 sed "s/[^:]*: //" >actual &&
1122 test_cmp expect actual
1125 test_expect_success 'rebase -i respects core.commentchar' '
1126 git reset --hard &&
1127 git checkout E^0 &&
1128 test_config core.commentchar "\\" &&
1129 write_script remove-all-but-first.sh <<-\EOF &&
1130 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1131 mv "$1.tmp" "$1"
1134 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1135 git rebase -i B
1136 ) &&
1137 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1140 test_expect_success 'rebase -i respects core.commentchar=auto' '
1141 test_config core.commentchar auto &&
1142 write_script copy-edit-script.sh <<-\EOF &&
1143 cp "$1" edit-script
1145 test_when_finished "git rebase --abort || :" &&
1147 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1148 git rebase -i HEAD^
1149 ) &&
1150 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1153 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1154 test_when_finished "git branch -D torebase" &&
1155 git checkout -b torebase branch1 &&
1156 upstream=$(git rev-parse ":/J") &&
1157 onto=$(git rev-parse ":/A") &&
1158 git rebase --onto $onto $upstream &&
1159 git reset --hard branch1 &&
1160 git rebase --onto ":/A" ":/J" &&
1161 git checkout branch1
1164 test_expect_success 'rebase -i with --strategy and -X' '
1165 git checkout -b conflict-merge-use-theirs conflict-branch &&
1166 git reset --hard HEAD^ &&
1167 echo five >conflict &&
1168 echo Z >file1 &&
1169 git commit -a -m "one file conflict" &&
1170 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1171 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1172 test $(cat file1) = Z
1175 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1176 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1177 git reset --hard HEAD^ &&
1178 >breakpoint &&
1179 git add breakpoint &&
1180 git commit -m "breakpoint for interactive mode" &&
1181 echo five >conflict &&
1182 echo Z >file1 &&
1183 git commit -a -m "one file conflict" &&
1185 set_fake_editor &&
1186 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1187 -Xours conflict-branch
1188 ) &&
1189 git rebase --continue &&
1190 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1191 test $(cat file1) = Z
1194 test_expect_success 'rebase -i error on commits with \ in message' '
1195 current_head=$(git rev-parse HEAD) &&
1196 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1197 test_commit TO-REMOVE will-conflict old-content &&
1198 test_commit "\temp" will-conflict new-content dummy &&
1199 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1200 test_expect_code 1 grep " emp" error
1203 test_expect_success 'short commit ID setup' '
1204 test_when_finished "git checkout primary" &&
1205 git checkout --orphan collide &&
1206 git rm -rf . &&
1208 unset test_tick &&
1209 test_commit collide1 collide &&
1210 test_commit --notick collide2 collide &&
1211 test_commit --notick collide3 collide
1215 if test -n "$GIT_TEST_FIND_COLLIDER"
1216 then
1217 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1218 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1219 blob="$(git rev-parse collide2:collide)"
1220 from="$(git rev-parse collide1^0)"
1221 repl="commit refs/heads/collider-&\\n"
1222 repl="${repl}author $author\\ncommitter $committer\\n"
1223 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1224 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1225 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1226 git fast-import <script &&
1227 git pack-refs &&
1228 git for-each-ref >refs &&
1229 grep "^$(test_oid t3404_collision)" <refs >matches &&
1230 cat matches &&
1231 test_line_count -gt 2 matches || {
1232 echo "Could not find a collider" >&2
1233 exit 1
1237 test_expect_success 'short commit ID collide' '
1238 test_oid_cache <<-EOF &&
1239 # collision-related constants
1240 t3404_collision sha1:6bcd
1241 t3404_collision sha256:0161
1242 t3404_collider sha1:ac4f2ee
1243 t3404_collider sha256:16697
1245 test_when_finished "reset_rebase && git checkout primary" &&
1246 git checkout collide &&
1247 colliding_id=$(test_oid t3404_collision) &&
1248 hexsz=$(test_oid hexsz) &&
1249 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1250 test_config core.abbrev 4 &&
1252 unset test_tick &&
1253 test_tick &&
1254 set_fake_editor &&
1255 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1256 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1257 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1258 grep "^pick $colliding_id " \
1259 .git/rebase-merge/git-rebase-todo.tmp &&
1260 grep -E "^pick [0-9a-f]{$hexsz}" \
1261 .git/rebase-merge/git-rebase-todo &&
1262 grep -E "^pick [0-9a-f]{$hexsz}" \
1263 .git/rebase-merge/git-rebase-todo.backup &&
1264 git rebase --continue
1265 ) &&
1266 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1267 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1268 test "$collide2" = "$collide3"
1271 test_expect_success 'respect core.abbrev' '
1272 git config core.abbrev 12 &&
1274 set_cat_todo_editor &&
1275 test_must_fail git rebase -i HEAD~4 >todo-list
1276 ) &&
1277 test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list)
1280 test_expect_success 'todo count' '
1281 write_script dump-raw.sh <<-\EOF &&
1282 cat "$1"
1285 test_set_editor "$(pwd)/dump-raw.sh" &&
1286 git rebase -i HEAD~4 >actual
1287 ) &&
1288 test_grep "^# Rebase ..* onto ..* ([0-9]" actual
1291 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1292 git checkout --force A &&
1293 git clean -f &&
1294 cat >todo <<-EOF &&
1295 exec >file2
1296 pick $(git rev-parse B) B
1297 pick $(git rev-parse C) C
1298 pick $(git rev-parse D) D
1299 exec cat .git/rebase-merge/done >actual
1302 set_replace_editor todo &&
1303 test_must_fail git rebase -i A
1304 ) &&
1305 test_cmp_rev HEAD B &&
1306 test_cmp_rev REBASE_HEAD C &&
1307 head -n3 todo >expect &&
1308 test_cmp expect .git/rebase-merge/done &&
1309 rm file2 &&
1310 test_path_is_missing .git/rebase-merge/patch &&
1311 echo changed >file1 &&
1312 git add file1 &&
1313 test_must_fail git rebase --continue 2>err &&
1314 grep "error: you have staged changes in your working tree" err &&
1315 git reset --hard HEAD &&
1316 git rebase --continue &&
1317 test_cmp_rev HEAD D &&
1318 tail -n3 todo >>expect &&
1319 test_cmp expect actual
1322 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1323 git checkout --force branch2 &&
1324 git clean -f &&
1325 git tag original-branch2 &&
1327 set_fake_editor &&
1328 FAKE_LINES="edit 1 squash 2" git rebase -i A
1329 ) &&
1330 test_cmp_rev HEAD F &&
1331 test_path_is_missing file6 &&
1332 >file6 &&
1333 test_must_fail git rebase --continue &&
1334 test_cmp_rev HEAD F &&
1335 test_cmp_rev REBASE_HEAD I &&
1336 rm file6 &&
1337 test_path_is_missing .git/rebase-merge/patch &&
1338 echo changed >file1 &&
1339 git add file1 &&
1340 test_must_fail git rebase --continue 2>err &&
1341 grep "error: you have staged changes in your working tree" err &&
1342 git reset --hard HEAD &&
1343 git rebase --continue &&
1344 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1345 git reset --hard original-branch2
1348 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1349 git checkout --force branch2 &&
1350 git clean -f &&
1352 set_fake_editor &&
1353 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1354 ) &&
1355 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1356 test_path_is_missing file6 &&
1357 >file6 &&
1358 test_must_fail git rebase --continue &&
1359 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1360 test_cmp_rev REBASE_HEAD I &&
1361 rm file6 &&
1362 test_path_is_missing .git/rebase-merge/patch &&
1363 echo changed >file1 &&
1364 git add file1 &&
1365 test_must_fail git rebase --continue 2>err &&
1366 grep "error: you have staged changes in your working tree" err &&
1367 git reset --hard HEAD &&
1368 git rebase --continue &&
1369 test $(git cat-file commit HEAD | sed -ne \$p) = I
1372 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1373 git checkout -b commit-to-skip &&
1374 for double in X 3 1
1376 test_seq 5 | sed "s/$double/&&/" >seq &&
1377 git add seq &&
1378 test_tick &&
1379 git commit -m seq-$double || return 1
1380 done &&
1381 git tag seq-onto &&
1382 git reset --hard HEAD~2 &&
1383 git cherry-pick seq-onto &&
1385 set_fake_editor &&
1386 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1387 ) &&
1388 test -d .git/rebase-merge &&
1389 git rebase --continue &&
1390 git diff --exit-code seq-onto &&
1391 test ! -d .git/rebase-merge &&
1392 test ! -f .git/CHERRY_PICK_HEAD
1395 rebase_setup_and_clean () {
1396 test_when_finished "
1397 git checkout primary &&
1398 test_might_fail git branch -D $1 &&
1399 test_might_fail git rebase --abort
1400 " &&
1401 git checkout -b $1 ${2:-primary}
1404 test_expect_success 'drop' '
1405 rebase_setup_and_clean drop-test &&
1407 set_fake_editor &&
1408 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1409 ) &&
1410 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1411 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1412 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1415 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1416 test_config rebase.missingCommitsCheck ignore &&
1417 rebase_setup_and_clean missing-commit &&
1419 set_fake_editor &&
1420 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1421 ) &&
1422 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1423 test_grep \
1424 "Successfully rebased and updated refs/heads/missing-commit" \
1425 actual
1428 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1429 cat >expect <<-EOF &&
1430 Warning: some commits may have been dropped accidentally.
1431 Dropped commits (newer to older):
1432 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1433 To avoid this message, use "drop" to explicitly remove a commit.
1435 test_config rebase.missingCommitsCheck warn &&
1436 rebase_setup_and_clean missing-commit &&
1438 set_fake_editor &&
1439 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1440 ) &&
1441 head -n4 actual.2 >actual &&
1442 test_cmp expect actual &&
1443 test D = $(git cat-file commit HEAD | sed -ne \$p)
1446 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1447 cat >expect <<-EOF &&
1448 Warning: some commits may have been dropped accidentally.
1449 Dropped commits (newer to older):
1450 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1451 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1452 To avoid this message, use "drop" to explicitly remove a commit.
1454 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1455 The possible behaviours are: ignore, warn, error.
1457 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1458 Or you can abort the rebase with '\''git rebase --abort'\''.
1460 test_config rebase.missingCommitsCheck error &&
1461 rebase_setup_and_clean missing-commit &&
1463 set_fake_editor &&
1464 test_must_fail env FAKE_LINES="1 2 4" \
1465 git rebase -i --root 2>actual &&
1466 test_cmp expect actual &&
1467 cp .git/rebase-merge/git-rebase-todo.backup \
1468 .git/rebase-merge/git-rebase-todo &&
1469 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1470 ) &&
1471 git rebase --continue &&
1472 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1473 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1476 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1477 test_config rebase.missingCommitsCheck ignore &&
1478 rebase_setup_and_clean missing-commit &&
1480 set_fake_editor &&
1481 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1482 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1483 git rebase --continue 2>actual
1484 ) &&
1485 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1486 test_grep \
1487 "Successfully rebased and updated refs/heads/missing-commit" \
1488 actual
1491 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1492 cat >expect <<-EOF &&
1493 error: invalid command '\''pickled'\''
1494 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1495 Warning: some commits may have been dropped accidentally.
1496 Dropped commits (newer to older):
1497 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1498 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1499 To avoid this message, use "drop" to explicitly remove a commit.
1501 head -n5 expect >expect.2 &&
1502 tail -n1 expect >>expect.2 &&
1503 tail -n4 expect.2 >expect.3 &&
1504 test_config rebase.missingCommitsCheck warn &&
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 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1512 head -n7 actual.2 >actual &&
1513 test_cmp expect actual &&
1514 cp orig .git/rebase-merge/git-rebase-todo &&
1515 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1516 head -n4 actual.2 >actual &&
1517 test_cmp expect.3 actual &&
1518 git rebase --continue 2>actual
1519 ) &&
1520 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1521 test_grep \
1522 "Successfully rebased and updated refs/heads/missing-commit" \
1523 actual
1526 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1527 cat >expect <<-EOF &&
1528 error: invalid command '\''pickled'\''
1529 error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1530 Warning: some commits may have been dropped accidentally.
1531 Dropped commits (newer to older):
1532 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1533 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1534 To avoid this message, use "drop" to explicitly remove a commit.
1536 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1537 The possible behaviours are: ignore, warn, error.
1539 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1540 Or you can abort the rebase with '\''git rebase --abort'\''.
1542 tail -n11 expect >expect.2 &&
1543 head -n3 expect.2 >expect.3 &&
1544 tail -n7 expect.2 >>expect.3 &&
1545 test_config rebase.missingCommitsCheck error &&
1546 rebase_setup_and_clean missing-commit &&
1548 set_fake_editor &&
1549 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1550 git rebase -i --root &&
1551 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1552 test_must_fail env FAKE_LINES="2 3 4" \
1553 git rebase --edit-todo 2>actual &&
1554 test_cmp expect actual &&
1555 test_must_fail git rebase --continue 2>actual &&
1556 test_cmp expect.2 actual &&
1557 test_must_fail git rebase --edit-todo &&
1558 cp orig .git/rebase-merge/git-rebase-todo &&
1559 test_must_fail env FAKE_LINES="1 2 3 4" \
1560 git rebase --edit-todo 2>actual &&
1561 test_cmp expect.3 actual &&
1562 test_must_fail git rebase --continue 2>actual &&
1563 test_cmp expect.3 actual &&
1564 cp orig .git/rebase-merge/git-rebase-todo &&
1565 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1566 git rebase --continue 2>actual
1567 ) &&
1568 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1569 test_grep \
1570 "Successfully rebased and updated refs/heads/missing-commit" \
1571 actual
1574 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1575 test_config rebase.missingCommitsCheck error &&
1577 set_fake_editor &&
1578 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1579 ) &&
1580 git rebase --edit-todo &&
1581 test_must_fail git rebase --continue &&
1582 echo x >file1 &&
1583 git add file1 &&
1584 git rebase --continue
1587 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1588 test_config rebase.missingCommitsCheck error &&
1590 set_fake_editor &&
1591 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1592 cp .git/rebase-merge/git-rebase-todo todo &&
1593 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1594 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1595 git rebase --continue
1599 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1600 rebase_setup_and_clean abbrevcmd &&
1601 test_commit "first" file1.txt "first line" first &&
1602 test_commit "second" file1.txt "another line" second &&
1603 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1604 test_commit "squash! second" file1.txt "another line here" second_squash &&
1605 cat >expected <<-EOF &&
1606 p $(git rev-list --abbrev-commit -1 first) first
1607 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1608 x git show HEAD
1609 p $(git rev-list --abbrev-commit -1 second) second
1610 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1611 x git show HEAD
1613 git checkout abbrevcmd &&
1614 test_config rebase.abbreviateCommands true &&
1616 set_cat_todo_editor &&
1617 test_must_fail git rebase -i --exec "git show HEAD" \
1618 --autosquash primary >actual
1619 ) &&
1620 test_cmp expected actual
1623 test_expect_success 'static check of bad command' '
1624 rebase_setup_and_clean bad-cmd &&
1626 set_fake_editor &&
1627 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1628 git rebase -i --root 2>actual &&
1629 test_grep "pickled $(git rev-list --oneline -1 primary~1)" \
1630 actual &&
1631 test_grep "You can fix this with .git rebase --edit-todo.." \
1632 actual &&
1633 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1634 ) &&
1635 git rebase --continue &&
1636 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1637 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1640 test_expect_success 'the first command cannot be a fixup' '
1641 rebase_setup_and_clean fixup-first &&
1643 cat >orig <<-EOF &&
1644 fixup $(git log -1 --format="%h %s" B)
1645 pick $(git log -1 --format="%h %s" C)
1649 set_replace_editor orig &&
1650 test_must_fail git rebase -i A 2>actual
1651 ) &&
1652 grep "cannot .fixup. without a previous commit" actual &&
1653 grep "You can fix this with .git rebase --edit-todo.." actual &&
1654 # verify that the todo list has not been truncated
1655 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1656 test_cmp orig actual &&
1658 test_must_fail git rebase --edit-todo 2>actual &&
1659 grep "cannot .fixup. without a previous commit" actual &&
1660 grep "You can fix this with .git rebase --edit-todo.." actual &&
1661 # verify that the todo list has not been truncated
1662 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1663 test_cmp orig actual
1666 test_expect_success 'tabs and spaces are accepted in the todolist' '
1667 rebase_setup_and_clean indented-comment &&
1668 write_script add-indent.sh <<-\EOF &&
1670 # Turn single spaces into space/tab mix
1671 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1672 printf "\n\t# comment\n #more\n\t # comment\n"
1673 ) >"$1.new"
1674 mv "$1.new" "$1"
1677 test_set_editor "$(pwd)/add-indent.sh" &&
1678 git rebase -i HEAD^^^
1679 ) &&
1680 test E = $(git cat-file commit HEAD | sed -ne \$p)
1683 test_expect_success 'static check of bad SHA-1' '
1684 rebase_setup_and_clean bad-sha &&
1686 set_fake_editor &&
1687 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1688 git rebase -i --root 2>actual &&
1689 test_grep "edit XXXXXXX False commit" actual &&
1690 test_grep "You can fix this with .git rebase --edit-todo.." \
1691 actual &&
1692 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1693 ) &&
1694 git rebase --continue &&
1695 test E = $(git cat-file commit HEAD | sed -ne \$p)
1698 test_expect_success 'editor saves as CR/LF' '
1699 git checkout -b with-crlf &&
1700 write_script add-crs.sh <<-\EOF &&
1701 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1702 mv -f "$1".new "$1"
1705 test_set_editor "$(pwd)/add-crs.sh" &&
1706 git rebase -i HEAD^
1710 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1711 test_when_finished "test_might_fail git rebase --abort" &&
1713 set_fake_editor &&
1714 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1715 HEAD^ >out 2>err
1716 ) &&
1717 test_grep "$SQ-S\"S I Gner\"$SQ" err
1720 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1721 test_when_finished "test_might_fail git rebase --abort" &&
1722 test_config commit.gpgsign true &&
1724 set_fake_editor &&
1725 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1726 HEAD^ >out 2>err
1727 ) &&
1728 test_grep "$SQ-S\"S I Gner\"$SQ" err
1731 test_expect_success 'valid author header after --root swap' '
1732 rebase_setup_and_clean author-header no-conflict-branch &&
1733 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1734 git cat-file commit HEAD | grep ^author >expected &&
1736 set_fake_editor &&
1737 FAKE_LINES="5 1" git rebase -i --root
1738 ) &&
1739 git cat-file commit HEAD^ | grep ^author >actual &&
1740 test_cmp expected actual
1743 test_expect_success 'valid author header when author contains single quote' '
1744 rebase_setup_and_clean author-header no-conflict-branch &&
1745 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1746 git cat-file commit HEAD | grep ^author >expected &&
1748 set_fake_editor &&
1749 FAKE_LINES="2" git rebase -i HEAD~2
1750 ) &&
1751 git cat-file commit HEAD | grep ^author >actual &&
1752 test_cmp expected actual
1755 test_expect_success 'post-commit hook is called' '
1756 >actual &&
1757 test_hook post-commit <<-\EOS &&
1758 git rev-parse HEAD >>actual
1761 set_fake_editor &&
1762 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1763 echo x>file3 &&
1764 git add file3 &&
1765 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1766 ) &&
1767 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1768 >expect &&
1769 test_cmp expect actual
1772 test_expect_success 'correct error message for partial commit after empty pick' '
1773 test_when_finished "git rebase --abort" &&
1775 set_fake_editor &&
1776 FAKE_LINES="2 1 1" &&
1777 export FAKE_LINES &&
1778 test_must_fail git rebase -i A D
1779 ) &&
1780 echo x >file1 &&
1781 test_must_fail git commit file1 2>err &&
1782 test_grep "cannot do a partial commit during a rebase." err
1785 test_expect_success 'correct error message for commit --amend after empty pick' '
1786 test_when_finished "git rebase --abort" &&
1788 set_fake_editor &&
1789 FAKE_LINES="1 1" &&
1790 export FAKE_LINES &&
1791 test_must_fail git rebase -i A D
1792 ) &&
1793 echo x>file1 &&
1794 test_must_fail git commit -a --amend 2>err &&
1795 test_grep "middle of a rebase -- cannot amend." err
1798 test_expect_success 'todo has correct onto hash' '
1799 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1800 onto=$(git rev-parse --short HEAD~4) &&
1801 test_grep "^# Rebase ..* onto $onto" actual
1804 test_expect_success 'ORIG_HEAD is updated correctly' '
1805 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1806 git checkout -b test-orig-head A &&
1807 git commit --allow-empty -m A1 &&
1808 git commit --allow-empty -m A2 &&
1809 git commit --allow-empty -m A3 &&
1810 git commit --allow-empty -m A4 &&
1811 git rebase primary &&
1812 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1815 test_expect_success '--update-refs adds label and update-ref commands' '
1816 git checkout -b update-refs no-conflict-branch &&
1817 git branch -f base HEAD~4 &&
1818 git branch -f first HEAD~3 &&
1819 git branch -f second HEAD~3 &&
1820 git branch -f third HEAD~1 &&
1821 git commit --allow-empty --fixup=third &&
1822 git branch -f is-not-reordered &&
1823 git commit --allow-empty --fixup=HEAD~4 &&
1824 git branch -f shared-tip &&
1826 set_cat_todo_editor &&
1828 cat >expect <<-EOF &&
1829 pick $(git log -1 --format=%h J) J
1830 fixup $(git log -1 --format=%h update-refs) fixup! J # empty
1831 update-ref refs/heads/second
1832 update-ref refs/heads/first
1833 pick $(git log -1 --format=%h K) K
1834 pick $(git log -1 --format=%h L) L
1835 fixup $(git log -1 --format=%h is-not-reordered) fixup! L # empty
1836 update-ref refs/heads/third
1837 pick $(git log -1 --format=%h M) M
1838 update-ref refs/heads/no-conflict-branch
1839 update-ref refs/heads/is-not-reordered
1840 update-ref refs/heads/shared-tip
1843 test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
1844 test_cmp expect todo &&
1846 test_must_fail git -c rebase.autosquash=true \
1847 -c rebase.updaterefs=true \
1848 rebase -i primary >todo &&
1850 test_cmp expect todo
1854 test_expect_success '--update-refs adds commands with --rebase-merges' '
1855 git checkout -b update-refs-with-merge no-conflict-branch &&
1856 git branch -f base HEAD~4 &&
1857 git branch -f first HEAD~3 &&
1858 git branch -f second HEAD~3 &&
1859 git branch -f third HEAD~1 &&
1860 git merge -m merge branch2 &&
1861 git branch -f merge-branch &&
1862 git commit --fixup=third --allow-empty &&
1864 set_cat_todo_editor &&
1866 cat >expect <<-EOF &&
1867 label onto
1868 reset onto
1869 pick $(git log -1 --format=%h branch2~1) F
1870 pick $(git log -1 --format=%h branch2) I
1871 update-ref refs/heads/branch2
1872 label merge
1873 reset onto
1874 pick $(git log -1 --format=%h refs/heads/second) J
1875 update-ref refs/heads/second
1876 update-ref refs/heads/first
1877 pick $(git log -1 --format=%h refs/heads/third~1) K
1878 pick $(git log -1 --format=%h refs/heads/third) L
1879 fixup $(git log -1 --format=%h update-refs-with-merge) fixup! L # empty
1880 update-ref refs/heads/third
1881 pick $(git log -1 --format=%h HEAD~2) M
1882 update-ref refs/heads/no-conflict-branch
1883 merge -C $(git log -1 --format=%h HEAD~1) merge # merge
1884 update-ref refs/heads/merge-branch
1887 test_must_fail git rebase -i --autosquash \
1888 --rebase-merges=rebase-cousins \
1889 --update-refs primary >todo &&
1891 test_cmp expect todo &&
1893 test_must_fail git -c rebase.autosquash=true \
1894 -c rebase.updaterefs=true \
1895 rebase -i \
1896 --rebase-merges=rebase-cousins \
1897 primary >todo &&
1899 test_cmp expect todo
1903 test_expect_success '--update-refs updates refs correctly' '
1904 git checkout -B update-refs no-conflict-branch &&
1905 git branch -f base HEAD~4 &&
1906 git branch -f first HEAD~3 &&
1907 git branch -f second HEAD~3 &&
1908 git branch -f third HEAD~1 &&
1909 test_commit extra2 fileX &&
1910 git commit --amend --fixup=L &&
1912 git rebase -i --autosquash --update-refs primary 2>err &&
1914 test_cmp_rev HEAD~3 refs/heads/first &&
1915 test_cmp_rev HEAD~3 refs/heads/second &&
1916 test_cmp_rev HEAD~1 refs/heads/third &&
1917 test_cmp_rev HEAD refs/heads/no-conflict-branch &&
1919 cat >expect <<-\EOF &&
1920 Successfully rebased and updated refs/heads/update-refs.
1921 Updated the following refs with --update-refs:
1922 refs/heads/first
1923 refs/heads/no-conflict-branch
1924 refs/heads/second
1925 refs/heads/third
1928 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
1929 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
1930 <err >err.trimmed &&
1931 test_cmp expect err.trimmed
1934 test_expect_success 'respect user edits to update-ref steps' '
1935 git checkout -B update-refs-break no-conflict-branch &&
1936 git branch -f base HEAD~4 &&
1937 git branch -f first HEAD~3 &&
1938 git branch -f second HEAD~3 &&
1939 git branch -f third HEAD~1 &&
1940 git branch -f unseen base &&
1942 # First, we will add breaks to the expected todo file
1943 cat >fake-todo-1 <<-EOF &&
1944 pick $(git rev-parse HEAD~3)
1945 break
1946 update-ref refs/heads/second
1947 update-ref refs/heads/first
1949 pick $(git rev-parse HEAD~2)
1950 pick $(git rev-parse HEAD~1)
1951 update-ref refs/heads/third
1953 pick $(git rev-parse HEAD)
1954 update-ref refs/heads/no-conflict-branch
1957 # Second, we will drop some update-refs commands (and move one)
1958 cat >fake-todo-2 <<-EOF &&
1959 update-ref refs/heads/second
1961 pick $(git rev-parse HEAD~2)
1962 update-ref refs/heads/third
1963 pick $(git rev-parse HEAD~1)
1964 break
1966 pick $(git rev-parse HEAD)
1969 # Third, we will:
1970 # * insert a new one (new-branch),
1971 # * re-add an old one (first), and
1972 # * add a second instance of a previously-stored one (second)
1973 cat >fake-todo-3 <<-EOF &&
1974 update-ref refs/heads/unseen
1975 update-ref refs/heads/new-branch
1976 pick $(git rev-parse HEAD)
1977 update-ref refs/heads/first
1978 update-ref refs/heads/second
1982 set_replace_editor fake-todo-1 &&
1983 git rebase -i --update-refs primary &&
1985 # These branches are currently locked.
1986 for b in first second third no-conflict-branch
1988 test_must_fail git branch -f $b base || return 1
1989 done &&
1991 set_replace_editor fake-todo-2 &&
1992 git rebase --edit-todo &&
1994 # These branches are currently locked.
1995 for b in second third
1997 test_must_fail git branch -f $b base || return 1
1998 done &&
2000 # These branches are currently unlocked for checkout.
2001 for b in first no-conflict-branch
2003 git worktree add wt-$b $b &&
2004 git worktree remove wt-$b || return 1
2005 done &&
2007 git rebase --continue &&
2009 set_replace_editor fake-todo-3 &&
2010 git rebase --edit-todo &&
2012 # These branches are currently locked.
2013 for b in second third first unseen
2015 test_must_fail git branch -f $b base || return 1
2016 done &&
2018 # These branches are currently unlocked for checkout.
2019 for b in no-conflict-branch
2021 git worktree add wt-$b $b &&
2022 git worktree remove wt-$b || return 1
2023 done &&
2025 git rebase --continue
2026 ) &&
2028 test_cmp_rev HEAD~2 refs/heads/third &&
2029 test_cmp_rev HEAD~1 refs/heads/unseen &&
2030 test_cmp_rev HEAD~1 refs/heads/new-branch &&
2031 test_cmp_rev HEAD refs/heads/first &&
2032 test_cmp_rev HEAD refs/heads/second &&
2033 test_cmp_rev HEAD refs/heads/no-conflict-branch
2036 test_expect_success '--update-refs: all update-ref lines removed' '
2037 git checkout -b test-refs-not-removed no-conflict-branch &&
2038 git branch -f base HEAD~4 &&
2039 git branch -f first HEAD~3 &&
2040 git branch -f second HEAD~3 &&
2041 git branch -f third HEAD~1 &&
2042 git branch -f tip &&
2044 test_commit test-refs-not-removed &&
2045 git commit --amend --fixup first &&
2047 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2050 set_cat_todo_editor &&
2051 test_must_fail git rebase -i --update-refs base >todo.raw &&
2052 sed -e "/^update-ref/d" <todo.raw >todo
2053 ) &&
2055 set_replace_editor todo &&
2056 git rebase -i --update-refs base
2057 ) &&
2059 # Ensure refs are not deleted and their OIDs have not changed
2060 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2061 test_cmp expect-oids actual-oids
2064 test_expect_success '--update-refs: all update-ref lines removed, then some re-added' '
2065 git checkout -b test-refs-not-removed2 no-conflict-branch &&
2066 git branch -f base HEAD~4 &&
2067 git branch -f first HEAD~3 &&
2068 git branch -f second HEAD~3 &&
2069 git branch -f third HEAD~1 &&
2070 git branch -f tip &&
2072 test_commit test-refs-not-removed2 &&
2073 git commit --amend --fixup first &&
2075 git rev-parse first second third >expect-oids &&
2078 set_cat_todo_editor &&
2079 test_must_fail git rebase -i \
2080 --autosquash --update-refs \
2081 base >todo.raw &&
2082 sed -e "/^update-ref/d" <todo.raw >todo
2083 ) &&
2085 # Add a break to the end of the todo so we can edit later
2086 echo "break" >>todo &&
2089 set_replace_editor todo &&
2090 git rebase -i --autosquash --update-refs base &&
2091 echo "update-ref refs/heads/tip" >todo &&
2092 git rebase --edit-todo &&
2093 git rebase --continue
2094 ) &&
2096 # Ensure first/second/third are unchanged, but tip is updated
2097 git rev-parse first second third >actual-oids &&
2098 test_cmp expect-oids actual-oids &&
2099 test_cmp_rev HEAD tip
2102 test_expect_success '--update-refs: --edit-todo with no update-ref lines' '
2103 git checkout -b test-refs-not-removed3 no-conflict-branch &&
2104 git branch -f base HEAD~4 &&
2105 git branch -f first HEAD~3 &&
2106 git branch -f second HEAD~3 &&
2107 git branch -f third HEAD~1 &&
2108 git branch -f tip &&
2110 test_commit test-refs-not-removed3 &&
2111 git commit --amend --fixup first &&
2113 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2116 set_cat_todo_editor &&
2117 test_must_fail git rebase -i \
2118 --autosquash --update-refs \
2119 base >todo.raw &&
2120 sed -e "/^update-ref/d" <todo.raw >todo
2121 ) &&
2123 # Add a break to the beginning of the todo so we can resume with no
2124 # update-ref lines
2125 echo "break" >todo.new &&
2126 cat todo >>todo.new &&
2129 set_replace_editor todo.new &&
2130 git rebase -i --autosquash --update-refs base &&
2132 # Make no changes when editing so update-refs is still empty
2133 cat todo >todo.new &&
2134 git rebase --edit-todo &&
2135 git rebase --continue
2136 ) &&
2138 # Ensure refs are not deleted and their OIDs have not changed
2139 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2140 test_cmp expect-oids actual-oids
2143 test_expect_success '--update-refs: check failed ref update' '
2144 test_when_finished "test_might_fail git rebase --abort" &&
2145 git checkout -B update-refs-error no-conflict-branch &&
2146 git branch -f base HEAD~4 &&
2147 git branch -f first HEAD~3 &&
2148 git branch -f second HEAD~2 &&
2149 git branch -f third HEAD~1 &&
2151 cat >fake-todo <<-EOF &&
2152 pick $(git rev-parse HEAD~3)
2153 break
2154 update-ref refs/heads/first
2156 pick $(git rev-parse HEAD~2)
2157 update-ref refs/heads/second
2159 pick $(git rev-parse HEAD~1)
2160 update-ref refs/heads/third
2162 pick $(git rev-parse HEAD)
2163 update-ref refs/heads/no-conflict-branch
2167 set_replace_editor fake-todo &&
2168 git rebase -i --update-refs base
2169 ) &&
2171 # At this point, the values of first, second, and third are
2172 # recorded in the update-refs file. We will force-update the
2173 # "second" ref, but "git branch -f" will not work because of
2174 # the lock in the update-refs file.
2175 git update-ref refs/heads/second third &&
2177 test_must_fail git rebase --continue 2>err &&
2178 grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
2180 cat >expect <<-\EOF &&
2181 Updated the following refs with --update-refs:
2182 refs/heads/first
2183 refs/heads/no-conflict-branch
2184 refs/heads/third
2185 Failed to update the following refs with --update-refs:
2186 refs/heads/second
2189 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
2190 tail -n 6 err >err.last &&
2191 sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
2192 <err.last >err.trimmed &&
2193 test_cmp expect err.trimmed
2196 test_expect_success 'bad labels and refs rejected when parsing todo list' '
2197 test_when_finished "test_might_fail git rebase --abort" &&
2198 cat >todo <<-\EOF &&
2199 exec >execed
2200 label #
2201 label :invalid
2202 update-ref :bad
2203 update-ref topic
2205 rm -f execed &&
2207 set_replace_editor todo &&
2208 test_must_fail git rebase -i HEAD 2>err
2209 ) &&
2210 grep "'\''#'\'' is not a valid label" err &&
2211 grep "'\'':invalid'\'' is not a valid label" err &&
2212 grep "'\'':bad'\'' is not a valid refname" err &&
2213 grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \
2214 err &&
2215 test_path_is_missing execed
2218 # This must be the last test in this file
2219 test_expect_success '$EDITOR and friends are unchanged' '
2220 test_editor_unchanged
2223 test_done