]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3404-rebase-interactive.sh
Git 2.35.8
[thirdparty/git.git] / t / t3404-rebase-interactive.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes E. Schindelin
4 #
5
6 test_description='git rebase interactive
7
8 This test runs git rebase "interactively", by faking an edit, and verifies
9 that the result still makes sense.
10
11 Initial setup:
12
13 one - two - three - four (conflict-branch)
14 /
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)
22 \
23 N - O - P (no-ff-branch)
24
25 where A, B, D and G all touch file1, and one, two, three, four all
26 touch file "conflict".
27 '
28
29 . ./test-lib.sh
30
31 . "$TEST_DIRECTORY"/lib-rebase.sh
32
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
60 '
61
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
68
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
75 '
76
77 test_expect_success 'rebase -i with empty todo list' '
78 cat >expect <<-\EOF &&
79 error: nothing to do
80 EOF
81 (
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
88 '
89
90 test_expect_success 'rebase -i with the exec command' '
91 git checkout primary &&
92 (
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-*
111 '
112
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
122 '
123
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^
132 )
133 '
134
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
142 '
143
144 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
145 git checkout primary &&
146 (
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
154 '
155
156 test_expect_success 'rebase -x with empty command fails' '
157 test_when_finished "git rebase --abort ||:" &&
158 test_must_fail env git rebase -x "" @ 2>actual &&
159 test_write_lines "error: empty exec command" >expected &&
160 test_cmp expected actual &&
161 test_must_fail env git rebase -x " " @ 2>actual &&
162 test_cmp expected actual
163 '
164
165 test_expect_success 'rebase -x with newline in command fails' '
166 test_when_finished "git rebase --abort ||:" &&
167 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
168 test_write_lines "error: exec commands cannot contain newlines" \
169 >expected &&
170 test_cmp expected actual
171 '
172
173 test_expect_success 'rebase -i with exec of inexistent command' '
174 git checkout primary &&
175 test_when_finished "git rebase --abort" &&
176 (
177 set_fake_editor &&
178 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
179 git rebase -i HEAD^ >actual 2>&1
180 ) &&
181 ! grep "Maybe git-rebase is broken" actual
182 '
183
184 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
185 test_when_finished "git rebase --abort ||:" &&
186 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
187 '
188
189 test_expect_success 'no changes are a nop' '
190 git checkout branch2 &&
191 git rebase -i F &&
192 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
193 test_cmp_rev I HEAD
194 '
195
196 test_expect_success 'test the [branch] option' '
197 git checkout -b dead-end &&
198 git rm file6 &&
199 git commit -m "stop here" &&
200 git rebase -i F branch2 &&
201 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
202 test_cmp_rev I branch2 &&
203 test_cmp_rev I HEAD
204 '
205
206 test_expect_success 'test --onto <branch>' '
207 git checkout -b test-onto branch2 &&
208 git rebase -i --onto branch1 F &&
209 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
210 test_cmp_rev HEAD^ branch1 &&
211 test_cmp_rev I branch2
212 '
213
214 test_expect_success 'rebase on top of a non-conflicting commit' '
215 git checkout branch1 &&
216 git tag original-branch1 &&
217 git rebase -i branch2 &&
218 test file6 = $(git diff --name-only original-branch1) &&
219 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
220 test_cmp_rev I branch2 &&
221 test_cmp_rev I HEAD~2
222 '
223
224 test_expect_success 'reflog for the branch shows state before rebase' '
225 test_cmp_rev branch1@{1} original-branch1
226 '
227
228 test_expect_success 'reflog for the branch shows correct finish message' '
229 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
230 "$(git rev-parse branch2)" >expected &&
231 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
232 test_cmp expected actual
233 '
234
235 test_expect_success 'exchange two commits' '
236 (
237 set_fake_editor &&
238 FAKE_LINES="2 1" git rebase -i HEAD~2
239 ) &&
240 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
241 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
242 blob1=$(git rev-parse --short HEAD^:file1) &&
243 blob2=$(git rev-parse --short HEAD:file1) &&
244 commit=$(git rev-parse --short HEAD)
245 '
246
247 test_expect_success 'stop on conflicting pick' '
248 cat >expect <<-EOF &&
249 diff --git a/file1 b/file1
250 index $blob1..$blob2 100644
251 --- a/file1
252 +++ b/file1
253 @@ -1 +1 @@
254 -A
255 +G
256 EOF
257 cat >expect2 <<-EOF &&
258 <<<<<<< HEAD
259 D
260 =======
261 G
262 >>>>>>> $commit (G)
263 EOF
264 git tag new-branch1 &&
265 test_must_fail git rebase -i primary &&
266 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
267 test_cmp expect .git/rebase-merge/patch &&
268 test_cmp expect2 file1 &&
269 test "$(git diff --name-status |
270 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
271 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
272 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
273 '
274
275 test_expect_success 'show conflicted patch' '
276 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
277 grep "show.*REBASE_HEAD" stderr &&
278 # the original stopped-sha1 is abbreviated
279 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
280 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
281 '
282
283 test_expect_success 'abort' '
284 git rebase --abort &&
285 test_cmp_rev new-branch1 HEAD &&
286 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
287 test_path_is_missing .git/rebase-merge
288 '
289
290 test_expect_success 'abort with error when new base cannot be checked out' '
291 git rm --cached file1 &&
292 git commit -m "remove file in base" &&
293 test_must_fail git rebase -i primary > output 2>&1 &&
294 test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
295 output &&
296 test_i18ngrep "file1" output &&
297 test_path_is_missing .git/rebase-merge &&
298 rm file1 &&
299 git reset --hard HEAD^
300 '
301
302 test_expect_success 'retain authorship' '
303 echo A > file7 &&
304 git add file7 &&
305 test_tick &&
306 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
307 git tag twerp &&
308 git rebase -i --onto primary HEAD^ &&
309 git show HEAD | grep "^Author: Twerp Snog"
310 '
311
312 test_expect_success 'retain authorship w/ conflicts' '
313 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
314 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
315
316 git reset --hard twerp &&
317 test_commit a conflict a conflict-a &&
318 git reset --hard twerp &&
319
320 GIT_AUTHOR_NAME=AttributeMe &&
321 export GIT_AUTHOR_NAME &&
322 test_commit b conflict b conflict-b &&
323 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
324
325 test_must_fail git rebase -i conflict-a &&
326 echo resolved >conflict &&
327 git add conflict &&
328 git rebase --continue &&
329 test_cmp_rev conflict-a^0 HEAD^ &&
330 git show >out &&
331 grep AttributeMe out
332 '
333
334 test_expect_success 'squash' '
335 git reset --hard twerp &&
336 echo B > file7 &&
337 test_tick &&
338 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
339 echo "******************************" &&
340 (
341 set_fake_editor &&
342 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
343 git rebase -i --onto primary HEAD~2
344 ) &&
345 test B = $(cat file7) &&
346 test_cmp_rev HEAD^ primary
347 '
348
349 test_expect_success 'retain authorship when squashing' '
350 git show HEAD | grep "^Author: Twerp Snog"
351 '
352
353 test_expect_success '--continue tries to commit' '
354 git reset --hard D &&
355 test_tick &&
356 (
357 set_fake_editor &&
358 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
359 echo resolved > file1 &&
360 git add file1 &&
361 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
362 ) &&
363 test_cmp_rev HEAD^ new-branch1 &&
364 git show HEAD | grep chouette
365 '
366
367 test_expect_success 'verbose flag is heeded, even after --continue' '
368 git reset --hard primary@{1} &&
369 test_tick &&
370 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
371 echo resolved > file1 &&
372 git add file1 &&
373 git rebase --continue > output &&
374 grep "^ file1 | 2 +-$" output
375 '
376
377 test_expect_success 'multi-squash only fires up editor once' '
378 base=$(git rev-parse HEAD~4) &&
379 (
380 set_fake_editor &&
381 FAKE_COMMIT_AMEND="ONCE" \
382 FAKE_LINES="1 squash 2 squash 3 squash 4" \
383 EXPECT_HEADER_COUNT=4 \
384 git rebase -i $base
385 ) &&
386 test $base = $(git rev-parse HEAD^) &&
387 test 1 = $(git show | grep ONCE | wc -l)
388 '
389
390 test_expect_success 'multi-fixup does not fire up editor' '
391 git checkout -b multi-fixup E &&
392 base=$(git rev-parse HEAD~4) &&
393 (
394 set_fake_editor &&
395 FAKE_COMMIT_AMEND="NEVER" \
396 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
397 git rebase -i $base
398 ) &&
399 test $base = $(git rev-parse HEAD^) &&
400 test 0 = $(git show | grep NEVER | wc -l) &&
401 git checkout @{-1} &&
402 git branch -D multi-fixup
403 '
404
405 test_expect_success 'commit message used after conflict' '
406 git checkout -b conflict-fixup conflict-branch &&
407 base=$(git rev-parse HEAD~4) &&
408 (
409 set_fake_editor &&
410 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
411 git rebase -i $base &&
412 echo three > conflict &&
413 git add conflict &&
414 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
415 git rebase --continue
416 ) &&
417 test $base = $(git rev-parse HEAD^) &&
418 test 1 = $(git show | grep ONCE | wc -l) &&
419 git checkout @{-1} &&
420 git branch -D conflict-fixup
421 '
422
423 test_expect_success 'commit message retained after conflict' '
424 git checkout -b conflict-squash conflict-branch &&
425 base=$(git rev-parse HEAD~4) &&
426 (
427 set_fake_editor &&
428 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
429 git rebase -i $base &&
430 echo three > conflict &&
431 git add conflict &&
432 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
433 git rebase --continue
434 ) &&
435 test $base = $(git rev-parse HEAD^) &&
436 test 2 = $(git show | grep TWICE | wc -l) &&
437 git checkout @{-1} &&
438 git branch -D conflict-squash
439 '
440
441 test_expect_success 'squash and fixup generate correct log messages' '
442 cat >expect-squash-fixup <<-\EOF &&
443 B
444
445 D
446
447 ONCE
448 EOF
449 git checkout -b squash-fixup E &&
450 base=$(git rev-parse HEAD~4) &&
451 (
452 set_fake_editor &&
453 FAKE_COMMIT_AMEND="ONCE" \
454 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
455 EXPECT_HEADER_COUNT=4 \
456 git rebase -i $base
457 ) &&
458 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
459 test_cmp expect-squash-fixup actual-squash-fixup &&
460 git cat-file commit HEAD@{2} |
461 grep "^# This is a combination of 3 commits\." &&
462 git cat-file commit HEAD@{3} |
463 grep "^# This is a combination of 2 commits\." &&
464 git checkout @{-1} &&
465 git branch -D squash-fixup
466 '
467
468 test_expect_success 'squash ignores comments' '
469 git checkout -b skip-comments E &&
470 base=$(git rev-parse HEAD~4) &&
471 (
472 set_fake_editor &&
473 FAKE_COMMIT_AMEND="ONCE" \
474 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
475 EXPECT_HEADER_COUNT=4 \
476 git rebase -i $base
477 ) &&
478 test $base = $(git rev-parse HEAD^) &&
479 test 1 = $(git show | grep ONCE | wc -l) &&
480 git checkout @{-1} &&
481 git branch -D skip-comments
482 '
483
484 test_expect_success 'squash ignores blank lines' '
485 git checkout -b skip-blank-lines E &&
486 base=$(git rev-parse HEAD~4) &&
487 (
488 set_fake_editor &&
489 FAKE_COMMIT_AMEND="ONCE" \
490 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
491 EXPECT_HEADER_COUNT=4 \
492 git rebase -i $base
493 ) &&
494 test $base = $(git rev-parse HEAD^) &&
495 test 1 = $(git show | grep ONCE | wc -l) &&
496 git checkout @{-1} &&
497 git branch -D skip-blank-lines
498 '
499
500 test_expect_success 'squash works as expected' '
501 git checkout -b squash-works no-conflict-branch &&
502 one=$(git rev-parse HEAD~3) &&
503 (
504 set_fake_editor &&
505 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
506 ) &&
507 test $one = $(git rev-parse HEAD~2)
508 '
509
510 test_expect_success 'interrupted squash works as expected' '
511 git checkout -b interrupted-squash conflict-branch &&
512 one=$(git rev-parse HEAD~3) &&
513 (
514 set_fake_editor &&
515 test_must_fail env FAKE_LINES="1 squash 3 2" \
516 git rebase -i HEAD~3
517 ) &&
518 test_write_lines one two four > conflict &&
519 git add conflict &&
520 test_must_fail git rebase --continue &&
521 echo resolved > conflict &&
522 git add conflict &&
523 git rebase --continue &&
524 test $one = $(git rev-parse HEAD~2)
525 '
526
527 test_expect_success 'interrupted squash works as expected (case 2)' '
528 git checkout -b interrupted-squash2 conflict-branch &&
529 one=$(git rev-parse HEAD~3) &&
530 (
531 set_fake_editor &&
532 test_must_fail env FAKE_LINES="3 squash 1 2" \
533 git rebase -i HEAD~3
534 ) &&
535 test_write_lines one four > conflict &&
536 git add conflict &&
537 test_must_fail git rebase --continue &&
538 test_write_lines one two four > conflict &&
539 git add conflict &&
540 test_must_fail git rebase --continue &&
541 echo resolved > conflict &&
542 git add conflict &&
543 git rebase --continue &&
544 test $one = $(git rev-parse HEAD~2)
545 '
546
547 test_expect_success '--continue tries to commit, even for "edit"' '
548 echo unrelated > file7 &&
549 git add file7 &&
550 test_tick &&
551 git commit -m "unrelated change" &&
552 parent=$(git rev-parse HEAD^) &&
553 test_tick &&
554 (
555 set_fake_editor &&
556 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
557 echo edited > file7 &&
558 git add file7 &&
559 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
560 ) &&
561 test edited = $(git show HEAD:file7) &&
562 git show HEAD | grep chouette &&
563 test $parent = $(git rev-parse HEAD^)
564 '
565
566 test_expect_success 'aborted --continue does not squash commits after "edit"' '
567 old=$(git rev-parse HEAD) &&
568 test_tick &&
569 (
570 set_fake_editor &&
571 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
572 echo "edited again" > file7 &&
573 git add file7 &&
574 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
575 ) &&
576 test $old = $(git rev-parse HEAD) &&
577 git rebase --abort
578 '
579
580 test_expect_success 'auto-amend only edited commits after "edit"' '
581 test_tick &&
582 (
583 set_fake_editor &&
584 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
585 echo "edited again" > file7 &&
586 git add file7 &&
587 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
588 echo "and again" > file7 &&
589 git add file7 &&
590 test_tick &&
591 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
592 git rebase --continue
593 ) &&
594 git rebase --abort
595 '
596
597 test_expect_success 'clean error after failed "exec"' '
598 test_tick &&
599 test_when_finished "git rebase --abort || :" &&
600 (
601 set_fake_editor &&
602 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
603 ) &&
604 echo "edited again" > file7 &&
605 git add file7 &&
606 test_must_fail git rebase --continue 2>error &&
607 test_i18ngrep "you have staged changes in your working tree" error
608 '
609
610 test_expect_success 'rebase a detached HEAD' '
611 grandparent=$(git rev-parse HEAD~2) &&
612 git checkout $(git rev-parse HEAD) &&
613 test_tick &&
614 (
615 set_fake_editor &&
616 FAKE_LINES="2 1" git rebase -i HEAD~2
617 ) &&
618 test $grandparent = $(git rev-parse HEAD~2)
619 '
620
621 test_expect_success 'rebase a commit violating pre-commit' '
622
623 mkdir -p .git/hooks &&
624 write_script .git/hooks/pre-commit <<-\EOF &&
625 test -z "$(git diff --cached --check)"
626 EOF
627 echo "monde! " >> file1 &&
628 test_tick &&
629 test_must_fail git commit -m doesnt-verify file1 &&
630 git commit -m doesnt-verify --no-verify file1 &&
631 test_tick &&
632 (
633 set_fake_editor &&
634 FAKE_LINES=2 git rebase -i HEAD~2
635 )
636 '
637
638 test_expect_success 'rebase with a file named HEAD in worktree' '
639
640 rm -fr .git/hooks &&
641 git reset --hard &&
642 git checkout -b branch3 A &&
643
644 (
645 GIT_AUTHOR_NAME="Squashed Away" &&
646 export GIT_AUTHOR_NAME &&
647 >HEAD &&
648 git add HEAD &&
649 git commit -m "Add head" &&
650 >BODY &&
651 git add BODY &&
652 git commit -m "Add body"
653 ) &&
654
655 (
656 set_fake_editor &&
657 FAKE_LINES="1 squash 2" git rebase -i @{-1}
658 ) &&
659 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
660
661 '
662
663 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
664
665 git checkout -b branch4 HEAD &&
666 GIT_EDITOR=: git commit --amend \
667 --author="Somebody else <somebody@else.com>" &&
668 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
669 git rebase -i branch3 &&
670 test_cmp_rev branch3 branch4
671
672 '
673
674 test_expect_success 'submodule rebase setup' '
675 git checkout A &&
676 mkdir sub &&
677 (
678 cd sub && git init && >elif &&
679 git add elif && git commit -m "submodule initial"
680 ) &&
681 echo 1 >file1 &&
682 git add file1 sub &&
683 test_tick &&
684 git commit -m "One" &&
685 echo 2 >file1 &&
686 test_tick &&
687 git commit -a -m "Two" &&
688 (
689 cd sub && echo 3 >elif &&
690 git commit -a -m "submodule second"
691 ) &&
692 test_tick &&
693 git commit -a -m "Three changes submodule"
694 '
695
696 test_expect_success 'submodule rebase -i' '
697 (
698 set_fake_editor &&
699 FAKE_LINES="1 squash 2 3" git rebase -i A
700 )
701 '
702
703 test_expect_success 'submodule conflict setup' '
704 git tag submodule-base &&
705 git checkout HEAD^ &&
706 (
707 cd sub && git checkout HEAD^ && echo 4 >elif &&
708 git add elif && git commit -m "submodule conflict"
709 ) &&
710 git add sub &&
711 test_tick &&
712 git commit -m "Conflict in submodule" &&
713 git tag submodule-topic
714 '
715
716 test_expect_success 'rebase -i continue with only submodule staged' '
717 test_must_fail git rebase -i submodule-base &&
718 git add sub &&
719 git rebase --continue &&
720 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
721 '
722
723 test_expect_success 'rebase -i continue with unstaged submodule' '
724 git checkout submodule-topic &&
725 git reset --hard &&
726 test_must_fail git rebase -i submodule-base &&
727 git reset &&
728 git rebase --continue &&
729 test_cmp_rev submodule-base HEAD
730 '
731
732 test_expect_success 'avoid unnecessary reset' '
733 git checkout primary &&
734 git reset --hard &&
735 test-tool chmtime =123456789 file3 &&
736 git update-index --refresh &&
737 HEAD=$(git rev-parse HEAD) &&
738 git rebase -i HEAD~4 &&
739 test $HEAD = $(git rev-parse HEAD) &&
740 MTIME=$(test-tool chmtime --get file3) &&
741 test 123456789 = $MTIME
742 '
743
744 test_expect_success 'reword' '
745 git checkout -b reword-branch primary &&
746 (
747 set_fake_editor &&
748 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
749 git rebase -i A &&
750 git show HEAD | grep "E changed" &&
751 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
752 test_cmp_rev primary^ HEAD^ &&
753 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
754 git rebase -i A &&
755 git show HEAD^ | grep "D changed" &&
756 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
757 git rebase -i A &&
758 git show HEAD~3 | grep "B changed" &&
759 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
760 git rebase -i A
761 ) &&
762 git show HEAD~2 | grep "C changed"
763 '
764
765 test_expect_success 'no uncommited changes when rewording the todo list is reloaded' '
766 git checkout E &&
767 test_when_finished "git checkout @{-1}" &&
768 (
769 set_fake_editor &&
770 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
771 export GIT_SEQUENCE_EDITOR &&
772 set_reword_editor &&
773 FAKE_LINES="reword 1 reword 2" git rebase -i C
774 ) &&
775 check_reworded_commits D E
776 '
777
778 test_expect_success 'rebase -i can copy notes' '
779 git config notes.rewrite.rebase true &&
780 git config notes.rewriteRef "refs/notes/*" &&
781 test_commit n1 &&
782 test_commit n2 &&
783 test_commit n3 &&
784 git notes add -m"a note" n3 &&
785 git rebase -i --onto n1 n2 &&
786 test "a note" = "$(git notes show HEAD)"
787 '
788
789 test_expect_success 'rebase -i can copy notes over a fixup' '
790 cat >expect <<-\EOF &&
791 an earlier note
792
793 a note
794 EOF
795 git reset --hard n3 &&
796 git notes add -m"an earlier note" n2 &&
797 (
798 set_fake_editor &&
799 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
800 git rebase -i n1
801 ) &&
802 git notes show > output &&
803 test_cmp expect output
804 '
805
806 test_expect_success 'rebase while detaching HEAD' '
807 git symbolic-ref HEAD &&
808 grandparent=$(git rev-parse HEAD~2) &&
809 test_tick &&
810 (
811 set_fake_editor &&
812 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
813 ) &&
814 test $grandparent = $(git rev-parse HEAD~2) &&
815 test_must_fail git symbolic-ref HEAD
816 '
817
818 test_tick # Ensure that the rebased commits get a different timestamp.
819 test_expect_success 'always cherry-pick with --no-ff' '
820 git checkout no-ff-branch &&
821 git tag original-no-ff-branch &&
822 git rebase -i --no-ff A &&
823 for p in 0 1 2
824 do
825 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
826 git diff HEAD~$p original-no-ff-branch~$p > out &&
827 test_must_be_empty out || return 1
828 done &&
829 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
830 git diff HEAD~3 original-no-ff-branch~3 > out &&
831 test_must_be_empty out
832 '
833
834 test_expect_success 'set up commits with funny messages' '
835 git checkout -b funny A &&
836 echo >>file1 &&
837 test_tick &&
838 git commit -a -m "end with slash\\" &&
839 echo >>file1 &&
840 test_tick &&
841 git commit -a -m "something (\000) that looks like octal" &&
842 echo >>file1 &&
843 test_tick &&
844 git commit -a -m "something (\n) that looks like a newline" &&
845 echo >>file1 &&
846 test_tick &&
847 git commit -a -m "another commit"
848 '
849
850 test_expect_success 'rebase-i history with funny messages' '
851 git rev-list A..funny >expect &&
852 test_tick &&
853 (
854 set_fake_editor &&
855 FAKE_LINES="1 2 3 4" git rebase -i A
856 ) &&
857 git rev-list A.. >actual &&
858 test_cmp expect actual
859 '
860
861 test_expect_success 'prepare for rebase -i --exec' '
862 git checkout primary &&
863 git checkout -b execute &&
864 test_commit one_exec main.txt one_exec &&
865 test_commit two_exec main.txt two_exec &&
866 test_commit three_exec main.txt three_exec
867 '
868
869 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
870 (
871 set_fake_editor &&
872 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
873 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
874 export FAKE_LINES &&
875 git rebase -i HEAD~2 >expect
876 ) &&
877 sed -e "1,9d" expect >expected &&
878 test_cmp expected actual
879 '
880
881 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
882 git reset --hard execute &&
883 (
884 set_fake_editor &&
885 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
886 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
887 export FAKE_LINES &&
888 git rebase -i HEAD~2 >expect
889 ) &&
890 sed -e "1,9d" expect >expected &&
891 test_cmp expected actual
892 '
893
894 test_expect_success 'running "git rebase -ix git show HEAD"' '
895 git reset --hard execute &&
896 (
897 set_fake_editor &&
898 git rebase -ix "git show HEAD" HEAD~2 >actual &&
899 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
900 export FAKE_LINES &&
901 git rebase -i HEAD~2 >expect
902 ) &&
903 sed -e "1,9d" expect >expected &&
904 test_cmp expected actual
905 '
906
907
908 test_expect_success 'rebase -ix with several <CMD>' '
909 git reset --hard execute &&
910 (
911 set_fake_editor &&
912 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
913 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
914 export FAKE_LINES &&
915 git rebase -i HEAD~2 >expect
916 ) &&
917 sed -e "1,9d" expect >expected &&
918 test_cmp expected actual
919 '
920
921 test_expect_success 'rebase -ix with several instances of --exec' '
922 git reset --hard execute &&
923 (
924 set_fake_editor &&
925 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
926 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
927 exec_git_show_HEAD exec_pwd" &&
928 export FAKE_LINES &&
929 git rebase -i HEAD~2 >expect
930 ) &&
931 sed -e "1,11d" expect >expected &&
932 test_cmp expected actual
933 '
934
935 test_expect_success 'rebase -ix with --autosquash' '
936 git reset --hard execute &&
937 git checkout -b autosquash &&
938 echo second >second.txt &&
939 git add second.txt &&
940 git commit -m "fixup! two_exec" &&
941 echo bis >bis.txt &&
942 git add bis.txt &&
943 git commit -m "fixup! two_exec" &&
944 git checkout -b autosquash_actual &&
945 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
946 git checkout autosquash &&
947 (
948 set_fake_editor &&
949 git checkout -b autosquash_expected &&
950 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
951 export FAKE_LINES &&
952 git rebase -i HEAD~4 >expect
953 ) &&
954 sed -e "1,13d" expect >expected &&
955 test_cmp expected actual
956 '
957
958 test_expect_success 'rebase --exec works without -i ' '
959 git reset --hard execute &&
960 rm -rf exec_output &&
961 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
962 test_i18ngrep "Successfully rebased and updated" actual &&
963 test_line_count = 2 exec_output &&
964 test_path_is_missing invoked_editor
965 '
966
967 test_expect_success 'rebase -i --exec without <CMD>' '
968 git reset --hard execute &&
969 test_must_fail git rebase -i --exec 2>actual &&
970 test_i18ngrep "requires a value" actual &&
971 git checkout primary
972 '
973
974 test_expect_success 'rebase -i --root re-order and drop commits' '
975 git checkout E &&
976 (
977 set_fake_editor &&
978 FAKE_LINES="3 1 2 5" git rebase -i --root
979 ) &&
980 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
981 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
982 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
983 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
984 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
985 '
986
987 test_expect_success 'rebase -i --root retain root commit author and message' '
988 git checkout A &&
989 echo B >file7 &&
990 git add file7 &&
991 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
992 (
993 set_fake_editor &&
994 FAKE_LINES="2" git rebase -i --root
995 ) &&
996 git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
997 git cat-file commit HEAD | grep -q "^different author$"
998 '
999
1000 test_expect_success 'rebase -i --root temporary sentinel commit' '
1001 git checkout B &&
1002 (
1003 set_fake_editor &&
1004 test_must_fail env FAKE_LINES="2" git rebase -i --root
1005 ) &&
1006 git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
1007 git rebase --abort
1008 '
1009
1010 test_expect_success 'rebase -i --root fixup root commit' '
1011 git checkout B &&
1012 (
1013 set_fake_editor &&
1014 FAKE_LINES="1 fixup 2" git rebase -i --root
1015 ) &&
1016 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1017 test B = $(git show HEAD:file1) &&
1018 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1019 '
1020
1021 test_expect_success 'rebase -i --root reword original root commit' '
1022 test_when_finished "test_might_fail git rebase --abort" &&
1023 git checkout -b reword-original-root-branch primary &&
1024 (
1025 set_fake_editor &&
1026 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1027 git rebase -i --root
1028 ) &&
1029 git show HEAD^ | grep "A changed" &&
1030 test -z "$(git show -s --format=%p HEAD^)"
1031 '
1032
1033 test_expect_success 'rebase -i --root reword new root commit' '
1034 test_when_finished "test_might_fail git rebase --abort" &&
1035 git checkout -b reword-now-root-branch primary &&
1036 (
1037 set_fake_editor &&
1038 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1039 git rebase -i --root
1040 ) &&
1041 git show HEAD^ | grep "C changed" &&
1042 test -z "$(git show -s --format=%p HEAD^)"
1043 '
1044
1045 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1046 test_when_finished "reset_rebase" &&
1047 git checkout -b failing-root-pick A &&
1048 echo x >file2 &&
1049 git rm file1 &&
1050 git commit -m "remove file 1 add file 2" &&
1051 echo z >file1 &&
1052 (
1053 set_fake_editor &&
1054 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1055 ) &&
1056 rm file1 &&
1057 git rebase --continue &&
1058 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1059 test "$(git rev-list --count HEAD)" = 2
1060 '
1061
1062 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1063 test_when_finished "reset_rebase" &&
1064 echo z>file1 &&
1065 (
1066 set_fake_editor &&
1067 test_must_fail env FAKE_LINES="reword 1 2" \
1068 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1069 rm file1 &&
1070 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1071 ) &&
1072 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1073 test "$(git rev-list --count HEAD)" = 2
1074 '
1075
1076 test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
1077 git checkout reword-original-root-branch &&
1078 git reset --hard &&
1079 git checkout conflict-branch &&
1080 (
1081 set_fake_editor &&
1082 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1083 test_must_fail git rebase --edit-todo
1084 ) &&
1085 git rebase --abort
1086 '
1087
1088 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1089 git reset --hard &&
1090 git checkout no-conflict-branch^0 &&
1091 (
1092 set_fake_editor &&
1093 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1094 FAKE_LINES="2 1" git rebase --edit-todo &&
1095 git rebase --continue
1096 ) &&
1097 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1098 test L = $(git cat-file commit HEAD | sed -ne \$p)
1099 '
1100
1101 test_expect_success 'rebase -i produces readable reflog' '
1102 git reset --hard &&
1103 git branch -f branch-reflog-test H &&
1104 git rebase -i --onto I F branch-reflog-test &&
1105 cat >expect <<-\EOF &&
1106 rebase (finish): returning to refs/heads/branch-reflog-test
1107 rebase (pick): H
1108 rebase (pick): G
1109 rebase (start): checkout I
1110 EOF
1111 git reflog -n4 HEAD |
1112 sed "s/[^:]*: //" >actual &&
1113 test_cmp expect actual
1114 '
1115
1116 test_expect_success 'rebase -i respects core.commentchar' '
1117 git reset --hard &&
1118 git checkout E^0 &&
1119 test_config core.commentchar "\\" &&
1120 write_script remove-all-but-first.sh <<-\EOF &&
1121 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1122 mv "$1.tmp" "$1"
1123 EOF
1124 (
1125 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1126 git rebase -i B
1127 ) &&
1128 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1129 '
1130
1131 test_expect_success 'rebase -i respects core.commentchar=auto' '
1132 test_config core.commentchar auto &&
1133 write_script copy-edit-script.sh <<-\EOF &&
1134 cp "$1" edit-script
1135 EOF
1136 test_when_finished "git rebase --abort || :" &&
1137 (
1138 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1139 git rebase -i HEAD^
1140 ) &&
1141 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1142 '
1143
1144 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1145 test_when_finished "git branch -D torebase" &&
1146 git checkout -b torebase branch1 &&
1147 upstream=$(git rev-parse ":/J") &&
1148 onto=$(git rev-parse ":/A") &&
1149 git rebase --onto $onto $upstream &&
1150 git reset --hard branch1 &&
1151 git rebase --onto ":/A" ":/J" &&
1152 git checkout branch1
1153 '
1154
1155 test_expect_success 'rebase -i with --strategy and -X' '
1156 git checkout -b conflict-merge-use-theirs conflict-branch &&
1157 git reset --hard HEAD^ &&
1158 echo five >conflict &&
1159 echo Z >file1 &&
1160 git commit -a -m "one file conflict" &&
1161 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1162 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1163 test $(cat file1) = Z
1164 '
1165
1166 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1167 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1168 git reset --hard HEAD^ &&
1169 >breakpoint &&
1170 git add breakpoint &&
1171 git commit -m "breakpoint for interactive mode" &&
1172 echo five >conflict &&
1173 echo Z >file1 &&
1174 git commit -a -m "one file conflict" &&
1175 (
1176 set_fake_editor &&
1177 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1178 -Xours conflict-branch
1179 ) &&
1180 git rebase --continue &&
1181 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1182 test $(cat file1) = Z
1183 '
1184
1185 test_expect_success 'rebase -i error on commits with \ in message' '
1186 current_head=$(git rev-parse HEAD) &&
1187 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1188 test_commit TO-REMOVE will-conflict old-content &&
1189 test_commit "\temp" will-conflict new-content dummy &&
1190 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1191 test_expect_code 1 grep " emp" error
1192 '
1193
1194 test_expect_success 'short commit ID setup' '
1195 test_when_finished "git checkout primary" &&
1196 git checkout --orphan collide &&
1197 git rm -rf . &&
1198 (
1199 unset test_tick &&
1200 test_commit collide1 collide &&
1201 test_commit --notick collide2 collide &&
1202 test_commit --notick collide3 collide
1203 )
1204 '
1205
1206 if test -n "$GIT_TEST_FIND_COLLIDER"
1207 then
1208 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1209 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1210 blob="$(git rev-parse collide2:collide)"
1211 from="$(git rev-parse collide1^0)"
1212 repl="commit refs/heads/collider-&\\n"
1213 repl="${repl}author $author\\ncommitter $committer\\n"
1214 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1215 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1216 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1217 git fast-import <script &&
1218 git pack-refs &&
1219 git for-each-ref >refs &&
1220 grep "^$(test_oid t3404_collision)" <refs >matches &&
1221 cat matches &&
1222 test_line_count -gt 2 matches || {
1223 echo "Could not find a collider" >&2
1224 exit 1
1225 }
1226 fi
1227
1228 test_expect_success 'short commit ID collide' '
1229 test_oid_cache <<-EOF &&
1230 # collision-related constants
1231 t3404_collision sha1:6bcd
1232 t3404_collision sha256:0161
1233 t3404_collider sha1:ac4f2ee
1234 t3404_collider sha256:16697
1235 EOF
1236 test_when_finished "reset_rebase && git checkout primary" &&
1237 git checkout collide &&
1238 colliding_id=$(test_oid t3404_collision) &&
1239 hexsz=$(test_oid hexsz) &&
1240 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1241 test_config core.abbrev 4 &&
1242 (
1243 unset test_tick &&
1244 test_tick &&
1245 set_fake_editor &&
1246 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1247 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1248 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1249 grep "^pick $colliding_id " \
1250 .git/rebase-merge/git-rebase-todo.tmp &&
1251 grep "^pick [0-9a-f]\{$hexsz\}" \
1252 .git/rebase-merge/git-rebase-todo &&
1253 grep "^pick [0-9a-f]\{$hexsz\}" \
1254 .git/rebase-merge/git-rebase-todo.backup &&
1255 git rebase --continue
1256 ) &&
1257 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1258 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1259 test "$collide2" = "$collide3"
1260 '
1261
1262 test_expect_success 'respect core.abbrev' '
1263 git config core.abbrev 12 &&
1264 (
1265 set_cat_todo_editor &&
1266 test_must_fail git rebase -i HEAD~4 >todo-list
1267 ) &&
1268 test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1269 '
1270
1271 test_expect_success 'todo count' '
1272 write_script dump-raw.sh <<-\EOF &&
1273 cat "$1"
1274 EOF
1275 (
1276 test_set_editor "$(pwd)/dump-raw.sh" &&
1277 git rebase -i HEAD~4 >actual
1278 ) &&
1279 test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1280 '
1281
1282 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1283 git checkout --force branch2 &&
1284 git clean -f &&
1285 (
1286 set_fake_editor &&
1287 FAKE_LINES="edit 1 2" git rebase -i A
1288 ) &&
1289 test_cmp_rev HEAD F &&
1290 test_path_is_missing file6 &&
1291 >file6 &&
1292 test_must_fail git rebase --continue &&
1293 test_cmp_rev HEAD F &&
1294 rm file6 &&
1295 git rebase --continue &&
1296 test_cmp_rev HEAD I
1297 '
1298
1299 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1300 git checkout --force branch2 &&
1301 git clean -f &&
1302 git tag original-branch2 &&
1303 (
1304 set_fake_editor &&
1305 FAKE_LINES="edit 1 squash 2" git rebase -i A
1306 ) &&
1307 test_cmp_rev HEAD F &&
1308 test_path_is_missing file6 &&
1309 >file6 &&
1310 test_must_fail git rebase --continue &&
1311 test_cmp_rev HEAD F &&
1312 rm file6 &&
1313 git rebase --continue &&
1314 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1315 git reset --hard original-branch2
1316 '
1317
1318 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1319 git checkout --force branch2 &&
1320 git clean -f &&
1321 (
1322 set_fake_editor &&
1323 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1324 ) &&
1325 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1326 test_path_is_missing file6 &&
1327 >file6 &&
1328 test_must_fail git rebase --continue &&
1329 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1330 rm file6 &&
1331 git rebase --continue &&
1332 test $(git cat-file commit HEAD | sed -ne \$p) = I
1333 '
1334
1335 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1336 git checkout -b commit-to-skip &&
1337 for double in X 3 1
1338 do
1339 test_seq 5 | sed "s/$double/&&/" >seq &&
1340 git add seq &&
1341 test_tick &&
1342 git commit -m seq-$double || return 1
1343 done &&
1344 git tag seq-onto &&
1345 git reset --hard HEAD~2 &&
1346 git cherry-pick seq-onto &&
1347 (
1348 set_fake_editor &&
1349 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1350 ) &&
1351 test -d .git/rebase-merge &&
1352 git rebase --continue &&
1353 git diff --exit-code seq-onto &&
1354 test ! -d .git/rebase-merge &&
1355 test ! -f .git/CHERRY_PICK_HEAD
1356 '
1357
1358 rebase_setup_and_clean () {
1359 test_when_finished "
1360 git checkout primary &&
1361 test_might_fail git branch -D $1 &&
1362 test_might_fail git rebase --abort
1363 " &&
1364 git checkout -b $1 ${2:-primary}
1365 }
1366
1367 test_expect_success 'drop' '
1368 rebase_setup_and_clean drop-test &&
1369 (
1370 set_fake_editor &&
1371 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1372 ) &&
1373 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1374 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1375 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1376 '
1377
1378 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1379 test_config rebase.missingCommitsCheck ignore &&
1380 rebase_setup_and_clean missing-commit &&
1381 (
1382 set_fake_editor &&
1383 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1384 ) &&
1385 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1386 test_i18ngrep \
1387 "Successfully rebased and updated refs/heads/missing-commit" \
1388 actual
1389 '
1390
1391 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1392 cat >expect <<-EOF &&
1393 Warning: some commits may have been dropped accidentally.
1394 Dropped commits (newer to older):
1395 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1396 To avoid this message, use "drop" to explicitly remove a commit.
1397 EOF
1398 test_config rebase.missingCommitsCheck warn &&
1399 rebase_setup_and_clean missing-commit &&
1400 (
1401 set_fake_editor &&
1402 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1403 ) &&
1404 head -n4 actual.2 >actual &&
1405 test_cmp expect actual &&
1406 test D = $(git cat-file commit HEAD | sed -ne \$p)
1407 '
1408
1409 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1410 cat >expect <<-EOF &&
1411 Warning: some commits may have been dropped accidentally.
1412 Dropped commits (newer to older):
1413 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1414 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
1415 To avoid this message, use "drop" to explicitly remove a commit.
1416
1417 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1418 The possible behaviours are: ignore, warn, error.
1419
1420 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1421 Or you can abort the rebase with '\''git rebase --abort'\''.
1422 EOF
1423 test_config rebase.missingCommitsCheck error &&
1424 rebase_setup_and_clean missing-commit &&
1425 (
1426 set_fake_editor &&
1427 test_must_fail env FAKE_LINES="1 2 4" \
1428 git rebase -i --root 2>actual &&
1429 test_cmp expect actual &&
1430 cp .git/rebase-merge/git-rebase-todo.backup \
1431 .git/rebase-merge/git-rebase-todo &&
1432 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1433 ) &&
1434 git rebase --continue &&
1435 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1436 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1437 '
1438
1439 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1440 test_config rebase.missingCommitsCheck ignore &&
1441 rebase_setup_and_clean missing-commit &&
1442 (
1443 set_fake_editor &&
1444 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1445 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1446 git rebase --continue 2>actual
1447 ) &&
1448 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1449 test_i18ngrep \
1450 "Successfully rebased and updated refs/heads/missing-commit" \
1451 actual
1452 '
1453
1454 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1455 cat >expect <<-EOF &&
1456 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1457 Warning: some commits may have been dropped accidentally.
1458 Dropped commits (newer to older):
1459 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1460 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1461 To avoid this message, use "drop" to explicitly remove a commit.
1462 EOF
1463 head -n4 expect >expect.2 &&
1464 tail -n1 expect >>expect.2 &&
1465 tail -n4 expect.2 >expect.3 &&
1466 test_config rebase.missingCommitsCheck warn &&
1467 rebase_setup_and_clean missing-commit &&
1468 (
1469 set_fake_editor &&
1470 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1471 git rebase -i --root &&
1472 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1473 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1474 head -n6 actual.2 >actual &&
1475 test_cmp expect actual &&
1476 cp orig .git/rebase-merge/git-rebase-todo &&
1477 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1478 head -n4 actual.2 >actual &&
1479 test_cmp expect.3 actual &&
1480 git rebase --continue 2>actual
1481 ) &&
1482 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1483 test_i18ngrep \
1484 "Successfully rebased and updated refs/heads/missing-commit" \
1485 actual
1486 '
1487
1488 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1489 cat >expect <<-EOF &&
1490 error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1491 Warning: some commits may have been dropped accidentally.
1492 Dropped commits (newer to older):
1493 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
1494 - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
1495 To avoid this message, use "drop" to explicitly remove a commit.
1496
1497 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1498 The possible behaviours are: ignore, warn, error.
1499
1500 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1501 Or you can abort the rebase with '\''git rebase --abort'\''.
1502 EOF
1503 tail -n11 expect >expect.2 &&
1504 head -n3 expect.2 >expect.3 &&
1505 tail -n7 expect.2 >>expect.3 &&
1506 test_config rebase.missingCommitsCheck error &&
1507 rebase_setup_and_clean missing-commit &&
1508 (
1509 set_fake_editor &&
1510 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1511 git rebase -i --root &&
1512 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1513 test_must_fail env FAKE_LINES="2 3 4" \
1514 git rebase --edit-todo 2>actual &&
1515 test_cmp expect actual &&
1516 test_must_fail git rebase --continue 2>actual &&
1517 test_cmp expect.2 actual &&
1518 test_must_fail git rebase --edit-todo &&
1519 cp orig .git/rebase-merge/git-rebase-todo &&
1520 test_must_fail env FAKE_LINES="1 2 3 4" \
1521 git rebase --edit-todo 2>actual &&
1522 test_cmp expect.3 actual &&
1523 test_must_fail git rebase --continue 2>actual &&
1524 test_cmp expect.3 actual &&
1525 cp orig .git/rebase-merge/git-rebase-todo &&
1526 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1527 git rebase --continue 2>actual
1528 ) &&
1529 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1530 test_i18ngrep \
1531 "Successfully rebased and updated refs/heads/missing-commit" \
1532 actual
1533 '
1534
1535 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1536 test_config rebase.missingCommitsCheck error &&
1537 (
1538 set_fake_editor &&
1539 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1540 ) &&
1541 git rebase --edit-todo &&
1542 test_must_fail git rebase --continue &&
1543 echo x >file1 &&
1544 git add file1 &&
1545 git rebase --continue
1546 '
1547
1548 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1549 test_config rebase.missingCommitsCheck error &&
1550 (
1551 set_fake_editor &&
1552 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1553 cp .git/rebase-merge/git-rebase-todo todo &&
1554 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1555 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1556 git rebase --continue
1557 )
1558 '
1559
1560 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1561 rebase_setup_and_clean abbrevcmd &&
1562 test_commit "first" file1.txt "first line" first &&
1563 test_commit "second" file1.txt "another line" second &&
1564 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1565 test_commit "squash! second" file1.txt "another line here" second_squash &&
1566 cat >expected <<-EOF &&
1567 p $(git rev-list --abbrev-commit -1 first) first
1568 f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1569 x git show HEAD
1570 p $(git rev-list --abbrev-commit -1 second) second
1571 s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1572 x git show HEAD
1573 EOF
1574 git checkout abbrevcmd &&
1575 test_config rebase.abbreviateCommands true &&
1576 (
1577 set_cat_todo_editor &&
1578 test_must_fail git rebase -i --exec "git show HEAD" \
1579 --autosquash primary >actual
1580 ) &&
1581 test_cmp expected actual
1582 '
1583
1584 test_expect_success 'static check of bad command' '
1585 rebase_setup_and_clean bad-cmd &&
1586 (
1587 set_fake_editor &&
1588 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1589 git rebase -i --root 2>actual &&
1590 test_i18ngrep "badcmd $(git rev-list --oneline -1 primary~1)" \
1591 actual &&
1592 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1593 actual &&
1594 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1595 ) &&
1596 git rebase --continue &&
1597 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1598 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1599 '
1600
1601 test_expect_success 'tabs and spaces are accepted in the todolist' '
1602 rebase_setup_and_clean indented-comment &&
1603 write_script add-indent.sh <<-\EOF &&
1604 (
1605 # Turn single spaces into space/tab mix
1606 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1607 printf "\n\t# comment\n #more\n\t # comment\n"
1608 ) >"$1.new"
1609 mv "$1.new" "$1"
1610 EOF
1611 (
1612 test_set_editor "$(pwd)/add-indent.sh" &&
1613 git rebase -i HEAD^^^
1614 ) &&
1615 test E = $(git cat-file commit HEAD | sed -ne \$p)
1616 '
1617
1618 test_expect_success 'static check of bad SHA-1' '
1619 rebase_setup_and_clean bad-sha &&
1620 (
1621 set_fake_editor &&
1622 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1623 git rebase -i --root 2>actual &&
1624 test_i18ngrep "edit XXXXXXX False commit" actual &&
1625 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1626 actual &&
1627 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1628 ) &&
1629 git rebase --continue &&
1630 test E = $(git cat-file commit HEAD | sed -ne \$p)
1631 '
1632
1633 test_expect_success 'editor saves as CR/LF' '
1634 git checkout -b with-crlf &&
1635 write_script add-crs.sh <<-\EOF &&
1636 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1637 mv -f "$1".new "$1"
1638 EOF
1639 (
1640 test_set_editor "$(pwd)/add-crs.sh" &&
1641 git rebase -i HEAD^
1642 )
1643 '
1644
1645 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1646 test_when_finished "test_might_fail git rebase --abort" &&
1647 (
1648 set_fake_editor &&
1649 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1650 HEAD^ >out 2>err
1651 ) &&
1652 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1653 '
1654
1655 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1656 test_when_finished "test_might_fail git rebase --abort" &&
1657 test_config commit.gpgsign true &&
1658 (
1659 set_fake_editor &&
1660 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1661 HEAD^ >out 2>err
1662 ) &&
1663 test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1664 '
1665
1666 test_expect_success 'valid author header after --root swap' '
1667 rebase_setup_and_clean author-header no-conflict-branch &&
1668 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1669 git cat-file commit HEAD | grep ^author >expected &&
1670 (
1671 set_fake_editor &&
1672 FAKE_LINES="5 1" git rebase -i --root
1673 ) &&
1674 git cat-file commit HEAD^ | grep ^author >actual &&
1675 test_cmp expected actual
1676 '
1677
1678 test_expect_success 'valid author header when author contains single quote' '
1679 rebase_setup_and_clean author-header no-conflict-branch &&
1680 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1681 git cat-file commit HEAD | grep ^author >expected &&
1682 (
1683 set_fake_editor &&
1684 FAKE_LINES="2" git rebase -i HEAD~2
1685 ) &&
1686 git cat-file commit HEAD | grep ^author >actual &&
1687 test_cmp expected actual
1688 '
1689
1690 test_expect_success 'post-commit hook is called' '
1691 test_when_finished "rm -f .git/hooks/post-commit" &&
1692 >actual &&
1693 mkdir -p .git/hooks &&
1694 write_script .git/hooks/post-commit <<-\EOS &&
1695 git rev-parse HEAD >>actual
1696 EOS
1697 (
1698 set_fake_editor &&
1699 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1700 echo x>file3 &&
1701 git add file3 &&
1702 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1703 ) &&
1704 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1705 >expect &&
1706 test_cmp expect actual
1707 '
1708
1709 test_expect_success 'correct error message for partial commit after empty pick' '
1710 test_when_finished "git rebase --abort" &&
1711 (
1712 set_fake_editor &&
1713 FAKE_LINES="2 1 1" &&
1714 export FAKE_LINES &&
1715 test_must_fail git rebase -i A D
1716 ) &&
1717 echo x >file1 &&
1718 test_must_fail git commit file1 2>err &&
1719 test_i18ngrep "cannot do a partial commit during a rebase." err
1720 '
1721
1722 test_expect_success 'correct error message for commit --amend after empty pick' '
1723 test_when_finished "git rebase --abort" &&
1724 (
1725 set_fake_editor &&
1726 FAKE_LINES="1 1" &&
1727 export FAKE_LINES &&
1728 test_must_fail git rebase -i A D
1729 ) &&
1730 echo x>file1 &&
1731 test_must_fail git commit -a --amend 2>err &&
1732 test_i18ngrep "middle of a rebase -- cannot amend." err
1733 '
1734
1735 test_expect_success 'todo has correct onto hash' '
1736 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1737 onto=$(git rev-parse --short HEAD~4) &&
1738 test_i18ngrep "^# Rebase ..* onto $onto" actual
1739 '
1740
1741 test_expect_success 'ORIG_HEAD is updated correctly' '
1742 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1743 git checkout -b test-orig-head A &&
1744 git commit --allow-empty -m A1 &&
1745 git commit --allow-empty -m A2 &&
1746 git commit --allow-empty -m A3 &&
1747 git commit --allow-empty -m A4 &&
1748 git rebase primary &&
1749 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1750 '
1751
1752 # This must be the last test in this file
1753 test_expect_success '$EDITOR and friends are unchanged' '
1754 test_editor_unchanged
1755 '
1756
1757 test_done