Raw
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 # Commit dates are hardcoded to 2005, and the reflog entries will have
35 # a matching timestamp. Maintenance may thus immediately expire
36 # reflogs if it was running.
37 git config set gc.reflogExpire never &&
38 git config set gc.reflogExpireUnreachable never &&
39
40 git switch -C primary &&
41 test_commit A file1 &&
42 test_commit B file1 &&
43 test_commit C file2 &&
44 test_commit D file1 &&
45 test_commit E file3 &&
46 git checkout -b branch1 A &&
47 test_commit F file4 &&
48 test_commit G file1 &&
49 test_commit H file5 &&
50 git checkout -b branch2 F &&
51 test_commit I file6 &&
52 git checkout -b conflict-branch A &&
53 test_commit one conflict &&
54 test_commit two conflict &&
55 test_commit three conflict &&
56 test_commit four conflict &&
57 git checkout -b no-conflict-branch A &&
58 test_commit J fileJ &&
59 test_commit K fileK &&
60 test_commit L fileL &&
61 test_commit M fileM &&
62 git checkout -b no-ff-branch A &&
63 test_commit N fileN &&
64 test_commit O fileO &&
65 test_commit P fileP
66 '
67
68 # "exec" commands are run with the user shell by default, but this may
69 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
70 # to create a file. Unsetting SHELL avoids such non-portable behavior
71 # in tests. It must be exported for it to take effect where needed.
72 SHELL=
73 export SHELL
74
75 test_expect_success 'rebase --keep-empty' '
76 git checkout -b emptybranch primary &&
77 git commit --allow-empty -m "empty" &&
78 git rebase --keep-empty -i HEAD~2 &&
79 git log --oneline >actual &&
80 test_line_count = 6 actual
81 '
82
83 test_expect_success 'rebase -i with empty todo list' '
84 cat >expect <<-\EOF &&
85 error: nothing to do
86 EOF
87 (
88 set_fake_editor &&
89 test_must_fail env FAKE_LINES="#" \
90 git rebase -i HEAD^ >output 2>&1
91 ) &&
92 tail -n 1 output >actual && # Ignore output about changing todo list
93 test_cmp expect actual
94 '
95
96 test_expect_success 'rebase -i with the exec command' '
97 git checkout primary &&
98 (
99 set_fake_editor &&
100 FAKE_LINES="1 exec_>touch-one
101 2 exec_>touch-two exec_false exec_>touch-three
102 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
103 export FAKE_LINES &&
104 test_must_fail git rebase -i A
105 ) &&
106 test_path_is_file touch-one &&
107 test_path_is_file touch-two &&
108 # Missing because we should have stopped by now.
109 test_path_is_missing touch-three &&
110 test_cmp_rev C HEAD &&
111 git rebase --continue &&
112 test_path_is_file touch-three &&
113 test_path_is_file "touch-file name with spaces" &&
114 test_path_is_file touch-after-semicolon &&
115 test_cmp_rev primary HEAD &&
116 rm -f touch-*
117 '
118
119 test_expect_success 'rebase -i with the exec command runs from tree root' '
120 git checkout primary &&
121 mkdir subdir && (cd subdir &&
122 set_fake_editor &&
123 FAKE_LINES="1 exec_>touch-subdir" \
124 git rebase -i HEAD^
125 ) &&
126 test_path_is_file touch-subdir &&
127 rm -fr subdir
128 '
129
130 test_expect_success 'rebase -i with exec allows git commands in subdirs' '
131 test_when_finished "rm -rf subdir" &&
132 test_when_finished "git rebase --abort ||:" &&
133 git checkout primary &&
134 mkdir subdir && (cd subdir &&
135 set_fake_editor &&
136 FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
137 git rebase -i HEAD^
138 )
139 '
140
141 test_expect_success 'rebase -i sets work tree properly' '
142 test_when_finished "rm -rf subdir" &&
143 test_when_finished "test_might_fail git rebase --abort" &&
144 mkdir subdir &&
145 git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
146 >actual &&
147 ! grep "/subdir$" actual
148 '
149
150 test_expect_success 'rebase -i with the exec command checks tree cleanness' '
151 git checkout primary &&
152 (
153 set_fake_editor &&
154 test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
155 git rebase -i HEAD^
156 ) &&
157 test_cmp_rev primary^ HEAD &&
158 git reset --hard &&
159 git rebase --continue
160 '
161
162 test_expect_success 'cherry-pick works with rebase --exec' '
163 test_when_finished "git cherry-pick --abort; \
164 git rebase --abort; \
165 git checkout primary" &&
166 echo "exec git cherry-pick G" >todo &&
167 (
168 set_replace_editor todo &&
169 test_must_fail git rebase -i D D
170 ) &&
171 test_cmp_rev G CHERRY_PICK_HEAD
172 '
173
174 test_expect_success 'rebase -x with empty command fails' '
175 test_when_finished "git rebase --abort ||:" &&
176 test_must_fail env git rebase -x "" @ 2>actual &&
177 test_write_lines "error: empty exec command" >expected &&
178 test_cmp expected actual &&
179 test_must_fail env git rebase -x " " @ 2>actual &&
180 test_cmp expected actual
181 '
182
183 test_expect_success 'rebase -x with newline in command fails' '
184 test_when_finished "git rebase --abort ||:" &&
185 test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
186 test_write_lines "error: exec commands cannot contain newlines" \
187 >expected &&
188 test_cmp expected actual
189 '
190
191 test_expect_success 'rebase -i with exec of inexistent command' '
192 git checkout primary &&
193 test_when_finished "git rebase --abort" &&
194 (
195 set_fake_editor &&
196 test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
197 git rebase -i HEAD^ >actual 2>&1
198 ) &&
199 ! grep "Maybe git-rebase is broken" actual
200 '
201
202 test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
203 test_when_finished "git rebase --abort ||:" &&
204 GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
205 '
206
207 test_expect_success 'no changes are a nop' '
208 git checkout branch2 &&
209 git rebase -i F &&
210 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
211 test_cmp_rev I HEAD
212 '
213
214 test_expect_success 'test the [branch] option' '
215 git checkout -b dead-end &&
216 git rm file6 &&
217 git commit -m "stop here" &&
218 git rebase -i F branch2 &&
219 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
220 test_cmp_rev I branch2 &&
221 test_cmp_rev I HEAD
222 '
223
224 test_expect_success 'test --onto <branch>' '
225 git checkout -b test-onto branch2 &&
226 git rebase -i --onto branch1 F &&
227 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
228 test_cmp_rev HEAD^ branch1 &&
229 test_cmp_rev I branch2
230 '
231
232 test_expect_success 'rebase on top of a non-conflicting commit' '
233 git checkout branch1 &&
234 git tag original-branch1 &&
235 git rebase -i branch2 &&
236 test file6 = $(git diff --name-only original-branch1) &&
237 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
238 test_cmp_rev I branch2 &&
239 test_cmp_rev I HEAD~2
240 '
241
242 test_expect_success 'reflog for the branch shows state before rebase' '
243 test_cmp_rev branch1@{1} original-branch1
244 '
245
246 test_expect_success 'reflog for the branch shows correct finish message' '
247 printf "rebase (finish): refs/heads/branch1 onto %s\n" \
248 "$(git rev-parse branch2)" >expected &&
249 git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
250 test_cmp expected actual
251 '
252
253 test_expect_success 'exchange two commits' '
254 (
255 set_fake_editor &&
256 FAKE_LINES="2 1" git rebase -i HEAD~2
257 ) &&
258 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
259 test G = $(git cat-file commit HEAD | sed -ne \$p) &&
260 blob1=$(git rev-parse --short HEAD^:file1) &&
261 blob2=$(git rev-parse --short HEAD:file1) &&
262 commit=$(git rev-parse --short HEAD)
263 '
264
265 test_expect_success 'stop on conflicting pick' '
266 cat >expect <<-EOF &&
267 diff --git a/file1 b/file1
268 index $blob1..$blob2 100644
269 --- a/file1
270 +++ b/file1
271 @@ -1 +1 @@
272 -A
273 +G
274 EOF
275 cat >expect2 <<-EOF &&
276 <<<<<<< HEAD
277 D
278 =======
279 G
280 >>>>>>> $commit (G)
281 EOF
282 git tag new-branch1 &&
283 test_must_fail git rebase -i primary &&
284 test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
285 test_cmp expect .git/rebase-merge/patch &&
286 test_cmp expect2 file1 &&
287 test "$(git diff --name-status |
288 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
289 grep -v "^#" <.git/rebase-merge/done >actual &&
290 test_line_count = 4 actual &&
291 test 0 = $(grep -c "^[^#]" <.git/rebase-merge/git-rebase-todo)
292 '
293
294 test_expect_success 'show conflicted patch' '
295 GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
296 grep "show.*REBASE_HEAD" stderr &&
297 # the original stopped-sha1 is abbreviated
298 stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
299 test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
300 '
301
302 test_expect_success 'abort' '
303 git rebase --abort &&
304 test_cmp_rev new-branch1 HEAD &&
305 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
306 test_path_is_missing .git/rebase-merge
307 '
308
309 test_expect_success 'abort with error when new base cannot be checked out' '
310 git rm --cached file1 &&
311 git commit -m "remove file in base" &&
312 test_must_fail git rebase -i primary > output 2>&1 &&
313 test_grep "The following untracked working tree files would be overwritten by checkout:" \
314 output &&
315 test_grep "file1" output &&
316 test_path_is_missing .git/rebase-merge &&
317 rm file1 &&
318 git reset --hard HEAD^
319 '
320
321 test_expect_success 'retain authorship' '
322 echo A > file7 &&
323 git add file7 &&
324 test_tick &&
325 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
326 git tag twerp &&
327 git rebase -i --onto primary HEAD^ &&
328 git show HEAD >actual &&
329 grep "^Author: Twerp Snog" actual
330 '
331
332 test_expect_success 'retain authorship w/ conflicts' '
333 oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
334 test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
335
336 git reset --hard twerp &&
337 test_commit a conflict a conflict-a &&
338 git reset --hard twerp &&
339
340 GIT_AUTHOR_NAME=AttributeMe &&
341 export GIT_AUTHOR_NAME &&
342 test_commit b conflict b conflict-b &&
343 GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
344
345 test_must_fail git rebase -i conflict-a &&
346 echo resolved >conflict &&
347 git add conflict &&
348 git rebase --continue &&
349 test_cmp_rev conflict-a^0 HEAD^ &&
350 git show >out &&
351 grep AttributeMe out
352 '
353
354 test_expect_success 'squash' '
355 git reset --hard twerp &&
356 echo B > file7 &&
357 test_tick &&
358 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
359 echo "******************************" &&
360 (
361 set_fake_editor &&
362 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
363 git rebase -i --onto primary HEAD~2
364 ) &&
365 test B = $(cat file7) &&
366 test_cmp_rev HEAD^ primary
367 '
368
369 test_expect_success 'retain authorship when squashing' '
370 git show HEAD >actual &&
371 grep "^Author: Twerp Snog" actual
372 '
373
374 test_expect_success '--continue tries to commit' '
375 git reset --hard D &&
376 test_tick &&
377 (
378 set_fake_editor &&
379 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
380 echo resolved > file1 &&
381 git add file1 &&
382 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
383 ) &&
384 test_cmp_rev HEAD^ new-branch1 &&
385 git show HEAD >actual &&
386 grep chouette actual
387 '
388
389 test_expect_success 'verbose flag is heeded, even after --continue' '
390 git reset --hard primary@{1} &&
391 test_tick &&
392 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
393 echo resolved > file1 &&
394 git add file1 &&
395 git rebase --continue > output &&
396 grep "^ file1 | 2 +-$" output
397 '
398
399 test_expect_success 'multi-squash only fires up editor once' '
400 base=$(git rev-parse HEAD~4) &&
401 (
402 set_fake_editor &&
403 FAKE_COMMIT_AMEND="ONCE" \
404 FAKE_LINES="1 squash 2 squash 3 squash 4" \
405 EXPECT_HEADER_COUNT=4 \
406 git rebase -i $base
407 ) &&
408 test $base = $(git rev-parse HEAD^) &&
409 git show >output &&
410 grep ONCE output >actual &&
411 test_line_count = 1 actual
412 '
413
414 test_expect_success 'multi-fixup does not fire up editor' '
415 git checkout -b multi-fixup E &&
416 base=$(git rev-parse HEAD~4) &&
417 (
418 set_fake_editor &&
419 FAKE_COMMIT_AMEND="NEVER" \
420 FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
421 git rebase -i $base
422 ) &&
423 test $base = $(git rev-parse HEAD^) &&
424 git show >output &&
425 ! grep NEVER output &&
426 git checkout @{-1} &&
427 git branch -D multi-fixup
428 '
429
430 test_expect_success 'commit message used after conflict' '
431 git checkout -b conflict-fixup conflict-branch &&
432 base=$(git rev-parse HEAD~4) &&
433 (
434 set_fake_editor &&
435 test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
436 git rebase -i $base &&
437 echo three > conflict &&
438 git add conflict &&
439 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
440 git rebase --continue
441 ) &&
442 test $base = $(git rev-parse HEAD^) &&
443 git show >output &&
444 grep ONCE output >actual &&
445 test_line_count = 1 actual &&
446 git checkout @{-1} &&
447 git branch -D conflict-fixup
448 '
449
450 test_expect_success 'commit message retained after conflict' '
451 git checkout -b conflict-squash conflict-branch &&
452 base=$(git rev-parse HEAD~4) &&
453 (
454 set_fake_editor &&
455 test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
456 git rebase -i $base &&
457 echo three > conflict &&
458 git add conflict &&
459 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
460 git rebase --continue
461 ) &&
462 test $base = $(git rev-parse HEAD^) &&
463 git show >output &&
464 grep TWICE output >actual &&
465 test_line_count = 2 actual &&
466 git checkout @{-1} &&
467 git branch -D conflict-squash
468 '
469
470 test_expect_success 'squash and fixup generate correct log messages' '
471 cat >expect-squash-fixup <<-\EOF &&
472 B
473
474 D
475
476 ONCE
477 EOF
478 git checkout -b squash-fixup E &&
479 base=$(git rev-parse HEAD~4) &&
480 (
481 set_fake_editor &&
482 FAKE_COMMIT_AMEND="ONCE" \
483 FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
484 EXPECT_HEADER_COUNT=4 \
485 git rebase -i $base
486 ) &&
487 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
488 test_cmp expect-squash-fixup actual-squash-fixup &&
489 git cat-file commit HEAD@{2} >actual &&
490 grep "^# This is a combination of 3 commits\." actual &&
491 git cat-file commit HEAD@{3} >actual &&
492 grep "^# This is a combination of 2 commits\." actual &&
493 git checkout @{-1} &&
494 git branch -D squash-fixup
495 '
496
497 test_expect_success 'squash ignores comments' '
498 git checkout -b skip-comments E &&
499 base=$(git rev-parse HEAD~4) &&
500 (
501 set_fake_editor &&
502 FAKE_COMMIT_AMEND="ONCE" \
503 FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
504 EXPECT_HEADER_COUNT=4 \
505 git rebase -i $base
506 ) &&
507 test $base = $(git rev-parse HEAD^) &&
508 git show >output &&
509 grep ONCE output >actual &&
510 test_line_count = 1 actual &&
511 git checkout @{-1} &&
512 git branch -D skip-comments
513 '
514
515 test_expect_success 'squash ignores blank lines' '
516 git checkout -b skip-blank-lines E &&
517 base=$(git rev-parse HEAD~4) &&
518 (
519 set_fake_editor &&
520 FAKE_COMMIT_AMEND="ONCE" \
521 FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
522 EXPECT_HEADER_COUNT=4 \
523 git rebase -i $base
524 ) &&
525 test $base = $(git rev-parse HEAD^) &&
526 git show >output &&
527 grep ONCE output >actual &&
528 test_line_count = 1 actual &&
529 git checkout @{-1} &&
530 git branch -D skip-blank-lines
531 '
532
533 test_expect_success 'squash works as expected' '
534 git checkout -b squash-works no-conflict-branch &&
535 one=$(git rev-parse HEAD~3) &&
536 (
537 set_fake_editor &&
538 FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
539 ) &&
540 test $one = $(git rev-parse HEAD~2)
541 '
542
543 test_expect_success 'interrupted squash works as expected' '
544 git checkout -b interrupted-squash conflict-branch &&
545 one=$(git rev-parse HEAD~3) &&
546 (
547 set_fake_editor &&
548 test_must_fail env FAKE_LINES="1 squash 3 2" \
549 git rebase -i HEAD~3
550 ) &&
551 test_write_lines one two four > conflict &&
552 git add conflict &&
553 test_must_fail git rebase --continue &&
554 echo resolved > conflict &&
555 git add conflict &&
556 git rebase --continue &&
557 test $one = $(git rev-parse HEAD~2)
558 '
559
560 test_expect_success 'interrupted squash works as expected (case 2)' '
561 git checkout -b interrupted-squash2 conflict-branch &&
562 one=$(git rev-parse HEAD~3) &&
563 (
564 set_fake_editor &&
565 test_must_fail env FAKE_LINES="3 squash 1 2" \
566 git rebase -i HEAD~3
567 ) &&
568 test_write_lines one four > conflict &&
569 git add conflict &&
570 test_must_fail git rebase --continue &&
571 test_write_lines one two four > conflict &&
572 git add conflict &&
573 test_must_fail git rebase --continue &&
574 echo resolved > conflict &&
575 git add conflict &&
576 git rebase --continue &&
577 test $one = $(git rev-parse HEAD~2)
578 '
579
580 test_expect_success '--continue tries to commit, even for "edit"' '
581 echo unrelated > file7 &&
582 git add file7 &&
583 test_tick &&
584 git commit -m "unrelated change" &&
585 parent=$(git rev-parse HEAD^) &&
586 test_tick &&
587 (
588 set_fake_editor &&
589 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
590 echo edited > file7 &&
591 git add file7 &&
592 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
593 ) &&
594 test edited = $(git show HEAD:file7) &&
595 git show HEAD >actual &&
596 grep chouette actual &&
597 test $parent = $(git rev-parse HEAD^)
598 '
599
600 test_expect_success 'aborted --continue does not squash commits after "edit"' '
601 old=$(git rev-parse HEAD) &&
602 test_tick &&
603 (
604 set_fake_editor &&
605 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
606 echo "edited again" > file7 &&
607 git add file7 &&
608 test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
609 ) &&
610 test $old = $(git rev-parse HEAD) &&
611 git rebase --abort
612 '
613
614 test_expect_success 'auto-amend only edited commits after "edit"' '
615 test_tick &&
616 (
617 set_fake_editor &&
618 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
619 echo "edited again" > file7 &&
620 git add file7 &&
621 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
622 echo "and again" > file7 &&
623 git add file7 &&
624 test_tick &&
625 test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
626 git rebase --continue
627 ) &&
628 git rebase --abort
629 '
630
631 test_expect_success 'clean error after failed "exec"' '
632 test_tick &&
633 test_when_finished "git rebase --abort || :" &&
634 (
635 set_fake_editor &&
636 test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
637 ) &&
638 echo "edited again" > file7 &&
639 git add file7 &&
640 test_must_fail git rebase --continue 2>error &&
641 test_grep "you have staged changes in your working tree" error &&
642 test_grep ! "could not open.*for reading" error
643 '
644
645 test_expect_success 'rebase a detached HEAD' '
646 grandparent=$(git rev-parse HEAD~2) &&
647 git checkout $(git rev-parse HEAD) &&
648 test_tick &&
649 (
650 set_fake_editor &&
651 FAKE_LINES="2 1" git rebase -i HEAD~2
652 ) &&
653 test $grandparent = $(git rev-parse HEAD~2)
654 '
655
656 test_expect_success 'rebase a commit violating pre-commit' '
657 test_hook pre-commit <<-\EOF &&
658 test -z "$(git diff --cached --check)"
659 EOF
660 echo "monde! " >> file1 &&
661 test_tick &&
662 test_must_fail git commit -m doesnt-verify file1 &&
663 git commit -m doesnt-verify --no-verify file1 &&
664 test_tick &&
665 (
666 set_fake_editor &&
667 FAKE_LINES=2 git rebase -i HEAD~2
668 )
669 '
670
671 test_expect_success 'rebase with a file named HEAD in worktree' '
672 git reset --hard &&
673 git checkout -b branch3 A &&
674
675 (
676 GIT_AUTHOR_NAME="Squashed Away" &&
677 export GIT_AUTHOR_NAME &&
678 >HEAD &&
679 git add HEAD &&
680 git commit -m "Add head" &&
681 >BODY &&
682 git add BODY &&
683 git commit -m "Add body"
684 ) &&
685
686 (
687 set_fake_editor &&
688 FAKE_LINES="1 squash 2" git rebase -i @{-1}
689 ) &&
690 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
691
692 '
693
694 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
695
696 git checkout -b branch4 HEAD &&
697 GIT_EDITOR=: git commit --amend \
698 --author="Somebody else <somebody@else.com>" &&
699 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
700 git rebase -i branch3 &&
701 test_cmp_rev branch3 branch4
702
703 '
704
705 test_expect_success 'submodule rebase setup' '
706 git checkout A &&
707 mkdir sub &&
708 (
709 cd sub && git init && >elif &&
710 git add elif && git commit -m "submodule initial"
711 ) &&
712 echo 1 >file1 &&
713 git add file1 sub &&
714 test_tick &&
715 git commit -m "One" &&
716 echo 2 >file1 &&
717 test_tick &&
718 git commit -a -m "Two" &&
719 (
720 cd sub && echo 3 >elif &&
721 git commit -a -m "submodule second"
722 ) &&
723 test_tick &&
724 git commit -a -m "Three changes submodule"
725 '
726
727 test_expect_success 'submodule rebase -i' '
728 (
729 set_fake_editor &&
730 FAKE_LINES="1 squash 2 3" git rebase -i A
731 )
732 '
733
734 test_expect_success 'submodule conflict setup' '
735 git tag submodule-base &&
736 git checkout HEAD^ &&
737 (
738 cd sub && git checkout HEAD^ && echo 4 >elif &&
739 git add elif && git commit -m "submodule conflict"
740 ) &&
741 git add sub &&
742 test_tick &&
743 git commit -m "Conflict in submodule" &&
744 git tag submodule-topic
745 '
746
747 test_expect_success 'rebase -i continue with only submodule staged' '
748 test_must_fail git rebase -i submodule-base &&
749 git add sub &&
750 git rebase --continue &&
751 test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
752 '
753
754 test_expect_success 'rebase -i continue with unstaged submodule' '
755 git checkout submodule-topic &&
756 git reset --hard &&
757 test_must_fail git rebase -i submodule-base &&
758 git reset &&
759 git rebase --continue &&
760 test_cmp_rev submodule-base HEAD
761 '
762
763 test_expect_success 'avoid unnecessary reset' '
764 git checkout primary &&
765 git reset --hard &&
766 test-tool chmtime =123456789 file3 &&
767 git update-index --refresh &&
768 HEAD=$(git rev-parse HEAD) &&
769 git rebase -i HEAD~4 &&
770 test $HEAD = $(git rev-parse HEAD) &&
771 MTIME=$(test-tool chmtime --get file3) &&
772 test 123456789 = $MTIME
773 '
774
775 test_expect_success 'reword' '
776 git checkout -b reword-branch primary &&
777 (
778 set_fake_editor &&
779 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
780 git rebase -i A &&
781 git show HEAD >actual &&
782 grep "E changed" actual &&
783 test $(git rev-parse primary) != $(git rev-parse HEAD) &&
784 test_cmp_rev primary^ HEAD^ &&
785 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
786 git rebase -i A &&
787 git show HEAD^ >actual &&
788 grep "D changed" actual &&
789 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
790 git rebase -i A &&
791 git show HEAD~3 >actual &&
792 grep "B changed" actual &&
793 FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
794 git rebase -i A
795 ) &&
796 git show HEAD~2 >actual &&
797 grep "C changed" actual
798 '
799
800 test_expect_success 'reword fast-forwarded empty commit' '
801 git commit --allow-empty -m "empty commit" --only &&
802 (
803 set_fake_editor &&
804 FAKE_COMMIT_AMEND=edited FAKE_LINES="reword 1" \
805 git rebase -i HEAD^
806 ) &&
807 test_commit_message HEAD <<-\EOF
808 empty commit
809
810 edited
811 EOF
812 '
813
814 test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '
815 git checkout E &&
816 test_when_finished "git checkout @{-1}" &&
817 (
818 set_fake_editor &&
819 GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
820 export GIT_SEQUENCE_EDITOR &&
821 set_reword_editor &&
822 FAKE_LINES="reword 1 reword 2" git rebase -i C
823 ) &&
824 check_reworded_commits D E
825 '
826
827 test_expect_success 'rebase -i can copy notes' '
828 git config notes.rewrite.rebase true &&
829 git config notes.rewriteRef "refs/notes/*" &&
830 test_commit n1 &&
831 test_commit n2 &&
832 test_commit n3 &&
833 git notes add -m"a note" n3 &&
834 git rebase -i --onto n1 n2 &&
835 test "a note" = "$(git notes show HEAD)"
836 '
837
838 test_expect_success 'rebase -i can copy notes over a fixup' '
839 cat >expect <<-\EOF &&
840 an earlier note
841
842 a note
843 EOF
844 git reset --hard n3 &&
845 git notes add -m"an earlier note" n2 &&
846 (
847 set_fake_editor &&
848 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
849 git rebase -i n1
850 ) &&
851 git notes show > output &&
852 test_cmp expect output
853 '
854
855 test_expect_success 'rebase while detaching HEAD' '
856 git symbolic-ref HEAD &&
857 grandparent=$(git rev-parse HEAD~2) &&
858 test_tick &&
859 (
860 set_fake_editor &&
861 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
862 ) &&
863 test $grandparent = $(git rev-parse HEAD~2) &&
864 test_must_fail git symbolic-ref HEAD
865 '
866
867 test_tick # Ensure that the rebased commits get a different timestamp.
868 test_expect_success 'always cherry-pick with --no-ff' '
869 git checkout no-ff-branch &&
870 git tag original-no-ff-branch &&
871 git rebase -i --no-ff A &&
872 for p in 0 1 2
873 do
874 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
875 git diff HEAD~$p original-no-ff-branch~$p > out &&
876 test_must_be_empty out || return 1
877 done &&
878 test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
879 git diff HEAD~3 original-no-ff-branch~3 > out &&
880 test_must_be_empty out
881 '
882
883 test_expect_success 'set up commits with funny messages' '
884 git checkout -b funny A &&
885 echo >>file1 &&
886 test_tick &&
887 git commit -a -m "end with slash\\" &&
888 echo >>file1 &&
889 test_tick &&
890 git commit -a -m "something (\000) that looks like octal" &&
891 echo >>file1 &&
892 test_tick &&
893 git commit -a -m "something (\n) that looks like a newline" &&
894 echo >>file1 &&
895 test_tick &&
896 git commit -a -m "another commit"
897 '
898
899 test_expect_success 'rebase-i history with funny messages' '
900 git rev-list A..funny >expect &&
901 test_tick &&
902 (
903 set_fake_editor &&
904 FAKE_LINES="1 2 3 4" git rebase -i A
905 ) &&
906 git rev-list A.. >actual &&
907 test_cmp expect actual
908 '
909
910 test_expect_success 'prepare for rebase -i --exec' '
911 git checkout primary &&
912 git checkout -b execute &&
913 test_commit one_exec main.txt one_exec &&
914 test_commit two_exec main.txt two_exec &&
915 test_commit three_exec main.txt three_exec
916 '
917
918 test_expect_success 'running "git rebase -i --exec git show HEAD"' '
919 (
920 set_fake_editor &&
921 git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
922 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
923 export FAKE_LINES &&
924 git rebase -i HEAD~2 >expect
925 ) &&
926 sed -e "1,9d" expect >expected &&
927 test_cmp expected actual
928 '
929
930 test_expect_success 'running "git rebase --exec git show HEAD -i"' '
931 git reset --hard execute &&
932 (
933 set_fake_editor &&
934 git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
935 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
936 export FAKE_LINES &&
937 git rebase -i HEAD~2 >expect
938 ) &&
939 sed -e "1,9d" expect >expected &&
940 test_cmp expected actual
941 '
942
943 test_expect_success 'running "git rebase -ix git show HEAD"' '
944 git reset --hard execute &&
945 (
946 set_fake_editor &&
947 git rebase -ix "git show HEAD" HEAD~2 >actual &&
948 FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
949 export FAKE_LINES &&
950 git rebase -i HEAD~2 >expect
951 ) &&
952 sed -e "1,9d" expect >expected &&
953 test_cmp expected actual
954 '
955
956
957 test_expect_success 'rebase -ix with several <CMD>' '
958 git reset --hard execute &&
959 (
960 set_fake_editor &&
961 git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
962 FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
963 export FAKE_LINES &&
964 git rebase -i HEAD~2 >expect
965 ) &&
966 sed -e "1,9d" expect >expected &&
967 test_cmp expected actual
968 '
969
970 test_expect_success 'rebase -ix with several instances of --exec' '
971 git reset --hard execute &&
972 (
973 set_fake_editor &&
974 git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
975 FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
976 exec_git_show_HEAD exec_pwd" &&
977 export FAKE_LINES &&
978 git rebase -i HEAD~2 >expect
979 ) &&
980 sed -e "1,11d" expect >expected &&
981 test_cmp expected actual
982 '
983
984 test_expect_success 'rebase -ix with --autosquash' '
985 git reset --hard execute &&
986 git checkout -b autosquash &&
987 echo second >second.txt &&
988 git add second.txt &&
989 git commit -m "fixup! two_exec" &&
990 echo bis >bis.txt &&
991 git add bis.txt &&
992 git commit -m "fixup! two_exec" &&
993 git checkout -b autosquash_actual &&
994 git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
995 git checkout autosquash &&
996 (
997 set_fake_editor &&
998 git checkout -b autosquash_expected &&
999 FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
1000 export FAKE_LINES &&
1001 git rebase -i HEAD~4 >expect
1002 ) &&
1003 sed -e "1,13d" expect >expected &&
1004 test_cmp expected actual
1005 '
1006
1007 test_expect_success 'rebase --exec works without -i ' '
1008 git reset --hard execute &&
1009 rm -rf exec_output &&
1010 EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
1011 test_grep "Successfully rebased and updated" actual &&
1012 test_line_count = 2 exec_output &&
1013 test_path_is_missing invoked_editor
1014 '
1015
1016 test_expect_success 'rebase -i --exec without <CMD>' '
1017 git reset --hard execute &&
1018 test_must_fail git rebase -i --exec 2>actual &&
1019 test_grep "requires a value" actual &&
1020 git checkout primary
1021 '
1022
1023 test_expect_success 'rebase -i --root re-order and drop commits' '
1024 git checkout E &&
1025 (
1026 set_fake_editor &&
1027 FAKE_LINES="3 1 2 5" git rebase -i --root
1028 ) &&
1029 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1030 test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1031 test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
1032 test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
1033 test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
1034 '
1035
1036 test_expect_success 'rebase -i --root retain root commit author and message' '
1037 git checkout A &&
1038 echo B >file7 &&
1039 git add file7 &&
1040 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
1041 (
1042 set_fake_editor &&
1043 FAKE_LINES="2" git rebase -i --root
1044 ) &&
1045 git cat-file commit HEAD >output &&
1046 grep -q "^author Twerp Snog" output &&
1047 git cat-file commit HEAD >actual &&
1048 grep -q "^different author$" actual
1049 '
1050
1051 test_expect_success 'rebase -i --root temporary sentinel commit' '
1052 git checkout B &&
1053 (
1054 set_fake_editor &&
1055 test_must_fail env FAKE_LINES="2" git rebase -i --root
1056 ) &&
1057 git cat-file commit HEAD >actual &&
1058 grep "^tree $EMPTY_TREE" actual &&
1059 git rebase --abort
1060 '
1061
1062 test_expect_success 'rebase -i --root fixup root commit' '
1063 git checkout B &&
1064 (
1065 set_fake_editor &&
1066 FAKE_LINES="1 fixup 2" git rebase -i --root
1067 ) &&
1068 test A = $(git cat-file commit HEAD | sed -ne \$p) &&
1069 test B = $(git show HEAD:file1) &&
1070 test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
1071 '
1072
1073 test_expect_success 'rebase -i --root reword original root commit' '
1074 test_when_finished "test_might_fail git rebase --abort" &&
1075 git checkout -b reword-original-root-branch primary &&
1076 (
1077 set_fake_editor &&
1078 FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1079 git rebase -i --root
1080 ) &&
1081 git show HEAD^ >actual &&
1082 grep "A changed" actual &&
1083 test -z "$(git show -s --format=%p HEAD^)"
1084 '
1085
1086 test_expect_success 'rebase -i --root reword new root commit' '
1087 test_when_finished "test_might_fail git rebase --abort" &&
1088 git checkout -b reword-now-root-branch primary &&
1089 (
1090 set_fake_editor &&
1091 FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
1092 git rebase -i --root
1093 ) &&
1094 git show HEAD^ >actual &&
1095 grep "C changed" actual &&
1096 test -z "$(git show -s --format=%p HEAD^)"
1097 '
1098
1099 test_expect_success 'rebase -i --root when root has untracked file conflict' '
1100 test_when_finished "reset_rebase" &&
1101 git checkout -b failing-root-pick A &&
1102 echo x >file2 &&
1103 git rm file1 &&
1104 git commit -m "remove file 1 add file 2" &&
1105 echo z >file1 &&
1106 (
1107 set_fake_editor &&
1108 test_must_fail env FAKE_LINES="1 2" git rebase -i --root
1109 ) &&
1110 rm file1 &&
1111 git rebase --continue &&
1112 test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1113 test "$(git rev-list --count HEAD)" = 2
1114 '
1115
1116 test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1117 test_when_finished "reset_rebase" &&
1118 echo z>file1 &&
1119 (
1120 set_fake_editor &&
1121 test_must_fail env FAKE_LINES="reword 1 2" \
1122 FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1123 rm file1 &&
1124 FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
1125 ) &&
1126 test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1127 test "$(git rev-list --count HEAD)" = 2
1128 '
1129
1130 test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
1131 git checkout reword-original-root-branch &&
1132 git reset --hard &&
1133 git checkout conflict-branch &&
1134 (
1135 set_fake_editor &&
1136 test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
1137 test_must_fail git rebase --edit-todo
1138 ) &&
1139 git rebase --abort
1140 '
1141
1142 test_expect_success 'rebase --edit-todo can be used to modify todo' '
1143 git reset --hard &&
1144 git checkout no-conflict-branch^0 &&
1145 (
1146 set_fake_editor &&
1147 FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1148 FAKE_LINES="2 1" git rebase --edit-todo &&
1149 git rebase --continue
1150 ) &&
1151 test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1152 test L = $(git cat-file commit HEAD | sed -ne \$p)
1153 '
1154
1155 test_expect_success 'rebase -i produces readable reflog' '
1156 git reset --hard &&
1157 git branch -f branch-reflog-test H &&
1158 git rebase -i --onto I F branch-reflog-test &&
1159 cat >expect <<-\EOF &&
1160 rebase (finish): returning to refs/heads/branch-reflog-test
1161 rebase (pick): H
1162 rebase (pick): G
1163 rebase (start): checkout I
1164 EOF
1165 git reflog -n4 HEAD |
1166 sed "s/[^:]*: //" >actual &&
1167 test_cmp expect actual
1168 '
1169
1170 test_expect_success 'rebase -i respects core.commentchar' '
1171 git reset --hard &&
1172 git checkout E^0 &&
1173 test_config core.commentchar "\\" &&
1174 write_script remove-all-but-first.sh <<-\EOF &&
1175 sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1176 mv "$1.tmp" "$1"
1177 EOF
1178 (
1179 test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1180 git rebase -i B
1181 ) &&
1182 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1183 '
1184
1185 test_expect_success !WITH_BREAKING_CHANGES 'rebase -i respects core.commentchar=auto' '
1186 test_config core.commentchar auto &&
1187 write_script copy-edit-script.sh <<-\EOF &&
1188 cp "$1" edit-script
1189 EOF
1190 test_when_finished "git rebase --abort || :" &&
1191 (
1192 test_set_editor "$(pwd)/copy-edit-script.sh" &&
1193 git rebase -i HEAD^ 2>err
1194 ) &&
1195 sed -n "s/^hint: *\$//p; s/^hint: //p; s/^warning: //p" err >actual &&
1196 cat >expect <<-EOF &&
1197 Support for ${SQ}core.commentChar=auto${SQ} is deprecated and will be removed in Git 3.0
1198
1199 To use the default comment string (#) please run
1200
1201 git config unset core.commentChar
1202
1203 To set a custom comment string please run
1204
1205 git config set core.commentChar <comment string>
1206
1207 where ${SQ}<comment string>${SQ} is the string you wish to use.
1208 EOF
1209 test_cmp expect actual &&
1210 test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1211 '
1212
1213 test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1214 test_when_finished "git branch -D torebase" &&
1215 git checkout -b torebase branch1 &&
1216 upstream=$(git rev-parse ":/J") &&
1217 onto=$(git rev-parse ":/A") &&
1218 git rebase --onto $onto $upstream &&
1219 git reset --hard branch1 &&
1220 git rebase --onto ":/A" ":/J" &&
1221 git checkout branch1
1222 '
1223
1224 test_expect_success 'rebase -i with --strategy and -X' '
1225 git checkout -b conflict-merge-use-theirs conflict-branch &&
1226 git reset --hard HEAD^ &&
1227 echo five >conflict &&
1228 echo Z >file1 &&
1229 git commit -a -m "one file conflict" &&
1230 EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1231 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1232 test $(cat file1) = Z
1233 '
1234
1235 test_expect_success 'interrupted rebase -i with --strategy and -X' '
1236 git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1237 git reset --hard HEAD^ &&
1238 >breakpoint &&
1239 git add breakpoint &&
1240 git commit -m "breakpoint for interactive mode" &&
1241 echo five >conflict &&
1242 echo Z >file1 &&
1243 git commit -a -m "one file conflict" &&
1244 (
1245 set_fake_editor &&
1246 FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
1247 -Xours conflict-branch
1248 ) &&
1249 git rebase --continue &&
1250 test $(git show conflict-branch:conflict) = $(cat conflict) &&
1251 test $(cat file1) = Z
1252 '
1253
1254 test_expect_success 'rebase -i error on commits with \ in message' '
1255 current_head=$(git rev-parse HEAD) &&
1256 test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1257 test_commit TO-REMOVE will-conflict old-content &&
1258 test_commit "\temp" will-conflict new-content dummy &&
1259 test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1260 test_expect_code 1 grep " emp" error
1261 '
1262
1263 test_expect_success 'short commit ID setup' '
1264 test_when_finished "git checkout primary" &&
1265 git checkout --orphan collide &&
1266 git rm -rf . &&
1267 (
1268 unset test_tick &&
1269 test_commit collide1 collide &&
1270 test_commit --notick collide2 collide &&
1271 test_commit --notick collide3 collide
1272 )
1273 '
1274
1275 if test -n "$GIT_TEST_FIND_COLLIDER"
1276 then
1277 author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
1278 committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
1279 blob="$(git rev-parse collide2:collide)"
1280 from="$(git rev-parse collide1^0)"
1281 repl="commit refs/heads/collider-&\\n"
1282 repl="${repl}author $author\\ncommitter $committer\\n"
1283 repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
1284 repl="${repl}from $from\\nM 100644 $blob collide\\n"
1285 test_seq 1 32768 | sed "s|.*|$repl|" >script &&
1286 git fast-import <script &&
1287 git pack-refs &&
1288 git for-each-ref >refs &&
1289 grep "^$(test_oid t3404_collision)" <refs >matches &&
1290 cat matches &&
1291 test_line_count -gt 2 matches || {
1292 echo "Could not find a collider" >&2
1293 exit 1
1294 }
1295 fi
1296
1297 test_expect_success 'short commit ID collide' '
1298 test_oid_cache <<-EOF &&
1299 # collision-related constants
1300 t3404_collision sha1:6bcd
1301 t3404_collision sha256:0161
1302 t3404_collider sha1:ac4f2ee
1303 t3404_collider sha256:16697
1304 EOF
1305 test_when_finished "reset_rebase && git checkout primary" &&
1306 git checkout collide &&
1307 colliding_id=$(test_oid t3404_collision) &&
1308 hexsz=$(test_oid hexsz) &&
1309 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1310 test_config core.abbrev 4 &&
1311 (
1312 unset test_tick &&
1313 test_tick &&
1314 set_fake_editor &&
1315 FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
1316 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1317 test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
1318 grep "^pick $colliding_id " \
1319 .git/rebase-merge/git-rebase-todo.tmp &&
1320 grep -E "^pick [0-9a-f]{$hexsz}" \
1321 .git/rebase-merge/git-rebase-todo &&
1322 grep -E "^pick [0-9a-f]{$hexsz}" \
1323 .git/rebase-merge/git-rebase-todo.backup &&
1324 git rebase --continue
1325 ) &&
1326 collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1327 collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1328 test "$collide2" = "$collide3"
1329 '
1330
1331 test_expect_success 'respect core.abbrev' '
1332 git config core.abbrev 12 &&
1333 (
1334 set_cat_todo_editor &&
1335 test_must_fail git rebase -i HEAD~4 >todo-list
1336 ) &&
1337 test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list)
1338 '
1339
1340 test_expect_success 'todo count' '
1341 write_script dump-raw.sh <<-\EOF &&
1342 cat "$1"
1343 EOF
1344 (
1345 test_set_editor "$(pwd)/dump-raw.sh" &&
1346 git rebase -i HEAD~4 >actual
1347 ) &&
1348 test_grep "^# Rebase ..* onto ..* ([0-9]" actual
1349 '
1350
1351 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1352 git checkout --force A &&
1353 git clean -f &&
1354 cat >todo <<-EOF &&
1355 exec >file2
1356 pick $(git rev-parse B) B
1357 pick $(git rev-parse C) C
1358 pick $(git rev-parse D) D
1359 exec cat .git/rebase-merge/done >actual
1360 EOF
1361 (
1362 set_replace_editor todo &&
1363 test_must_fail git rebase -i A
1364 ) &&
1365 test_cmp_rev HEAD B &&
1366 test_cmp_rev REBASE_HEAD C &&
1367 head -n3 todo >expect &&
1368 test_cmp expect .git/rebase-merge/done &&
1369 rm file2 &&
1370 test_path_is_missing .git/rebase-merge/patch &&
1371 echo changed >file1 &&
1372 git add file1 &&
1373 test_must_fail git rebase --continue 2>err &&
1374 grep "error: you have staged changes in your working tree" err &&
1375 git reset --hard HEAD &&
1376 git rebase --continue &&
1377 test_cmp_rev HEAD D &&
1378 tail -n3 todo >>expect &&
1379 test_cmp expect actual
1380 '
1381
1382 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1383 git checkout --force branch2 &&
1384 git clean -f &&
1385 git tag original-branch2 &&
1386 (
1387 set_fake_editor &&
1388 FAKE_LINES="edit 1 squash 2" git rebase -i A
1389 ) &&
1390 test_cmp_rev HEAD F &&
1391 test_path_is_missing file6 &&
1392 >file6 &&
1393 test_must_fail git rebase --continue &&
1394 test_cmp_rev HEAD F &&
1395 test_cmp_rev REBASE_HEAD I &&
1396 rm file6 &&
1397 test_path_is_missing .git/rebase-merge/patch &&
1398 echo changed >file1 &&
1399 git add file1 &&
1400 test_must_fail git rebase --continue 2>err &&
1401 grep "error: you have staged changes in your working tree" err &&
1402 git reset --hard HEAD &&
1403 git rebase --continue &&
1404 test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1405 git reset --hard original-branch2
1406 '
1407
1408 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1409 git checkout --force branch2 &&
1410 git clean -f &&
1411 (
1412 set_fake_editor &&
1413 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1414 ) &&
1415 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1416 test_path_is_missing file6 &&
1417 >file6 &&
1418 test_must_fail git rebase --continue &&
1419 test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1420 test_cmp_rev REBASE_HEAD I &&
1421 rm file6 &&
1422 test_path_is_missing .git/rebase-merge/patch &&
1423 echo changed >file1 &&
1424 git add file1 &&
1425 test_must_fail git rebase --continue 2>err &&
1426 grep "error: you have staged changes in your working tree" err &&
1427 git reset --hard HEAD &&
1428 git rebase --continue &&
1429 test $(git cat-file commit HEAD | sed -ne \$p) = I
1430 '
1431
1432 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1433 git checkout -b commit-to-skip &&
1434 for double in X 3 1
1435 do
1436 test_seq 5 | sed "s/$double/&&/" >seq &&
1437 git add seq &&
1438 test_tick &&
1439 git commit -m seq-$double || return 1
1440 done &&
1441 git tag seq-onto &&
1442 git reset --hard HEAD~2 &&
1443 git cherry-pick seq-onto &&
1444 (
1445 set_fake_editor &&
1446 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1447 ) &&
1448 test -d .git/rebase-merge &&
1449 git rebase --continue &&
1450 git diff --exit-code seq-onto &&
1451 test ! -d .git/rebase-merge &&
1452 test ! -f .git/CHERRY_PICK_HEAD
1453 '
1454
1455 rebase_setup_and_clean () {
1456 test_when_finished "
1457 git checkout primary &&
1458 test_might_fail git branch -D $1 &&
1459 test_might_fail git rebase --abort
1460 " &&
1461 git checkout -b $1 ${2:-primary}
1462 }
1463
1464 test_expect_success 'drop' '
1465 rebase_setup_and_clean drop-test &&
1466 (
1467 set_fake_editor &&
1468 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1469 ) &&
1470 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1471 test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1472 test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1473 '
1474
1475 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1476 test_config rebase.missingCommitsCheck ignore &&
1477 rebase_setup_and_clean missing-commit &&
1478 (
1479 set_fake_editor &&
1480 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1481 ) &&
1482 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1483 test_grep \
1484 "Successfully rebased and updated refs/heads/missing-commit" \
1485 actual
1486 '
1487
1488 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1489 cat >expect <<-EOF &&
1490 Warning: some commits may have been dropped accidentally.
1491 Dropped commits (newer to older):
1492 - $(git log --format="%h # %s" -1 primary)
1493 To avoid this message, use "drop" to explicitly remove a commit.
1494 EOF
1495 test_config rebase.missingCommitsCheck warn &&
1496 rebase_setup_and_clean missing-commit &&
1497 (
1498 set_fake_editor &&
1499 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1500 ) &&
1501 head -n4 actual.2 >actual &&
1502 test_cmp expect actual &&
1503 test D = $(git cat-file commit HEAD | sed -ne \$p)
1504 '
1505
1506 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1507 cat >expect <<-EOF &&
1508 Warning: some commits may have been dropped accidentally.
1509 Dropped commits (newer to older):
1510 - $(git log --format="%h # %s" -1 primary)
1511 - $(git log --format="%h # %s" -1 primary~2)
1512 To avoid this message, use "drop" to explicitly remove a commit.
1513
1514 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1515 The possible behaviours are: ignore, warn, error.
1516
1517 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1518 Or you can abort the rebase with '\''git rebase --abort'\''.
1519 EOF
1520 test_config rebase.missingCommitsCheck error &&
1521 rebase_setup_and_clean missing-commit &&
1522 (
1523 set_fake_editor &&
1524 test_must_fail env FAKE_LINES="1 2 4" \
1525 git rebase -i --root 2>actual &&
1526 test_cmp expect actual &&
1527 cp .git/rebase-merge/git-rebase-todo.backup \
1528 .git/rebase-merge/git-rebase-todo &&
1529 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1530 ) &&
1531 git rebase --continue &&
1532 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1533 test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1534 '
1535
1536 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1537 test_config rebase.missingCommitsCheck ignore &&
1538 rebase_setup_and_clean missing-commit &&
1539 (
1540 set_fake_editor &&
1541 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1542 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1543 git rebase --continue 2>actual
1544 ) &&
1545 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1546 test_grep \
1547 "Successfully rebased and updated refs/heads/missing-commit" \
1548 actual
1549 '
1550
1551 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1552 cat >expect <<-EOF &&
1553 error: invalid command '\''pickled'\''
1554 error: invalid line 1: pickled $(git log --format="%h # %s" -1 primary~4)
1555 Warning: some commits may have been dropped accidentally.
1556 Dropped commits (newer to older):
1557 - $(git log --format="%h # %s" -1 primary)
1558 - $(git log --format="%h # %s" -1 primary~4)
1559 To avoid this message, use "drop" to explicitly remove a commit.
1560 EOF
1561 head -n5 expect >expect.2 &&
1562 tail -n1 expect >>expect.2 &&
1563 tail -n4 expect.2 >expect.3 &&
1564 test_config rebase.missingCommitsCheck warn &&
1565 rebase_setup_and_clean missing-commit &&
1566 (
1567 set_fake_editor &&
1568 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1569 git rebase -i --root &&
1570 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1571 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1572 head -n7 actual.2 >actual &&
1573 test_cmp expect actual &&
1574 cp orig .git/rebase-merge/git-rebase-todo &&
1575 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1576 head -n4 actual.2 >actual &&
1577 test_cmp expect.3 actual &&
1578 git rebase --continue 2>actual
1579 ) &&
1580 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1581 test_grep \
1582 "Successfully rebased and updated refs/heads/missing-commit" \
1583 actual
1584 '
1585
1586 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1587 cat >expect <<-EOF &&
1588 error: invalid command '\''pickled'\''
1589 error: invalid line 1: pickled $(git log --format="%h # %s" -1 primary~4)
1590 Warning: some commits may have been dropped accidentally.
1591 Dropped commits (newer to older):
1592 - $(git log --format="%h # %s" -1 primary)
1593 - $(git log --format="%h # %s" -1 primary~4)
1594 To avoid this message, use "drop" to explicitly remove a commit.
1595
1596 Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1597 The possible behaviours are: ignore, warn, error.
1598
1599 You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1600 Or you can abort the rebase with '\''git rebase --abort'\''.
1601 EOF
1602 tail -n11 expect >expect.2 &&
1603 head -n3 expect.2 >expect.3 &&
1604 tail -n7 expect.2 >>expect.3 &&
1605 test_config rebase.missingCommitsCheck error &&
1606 rebase_setup_and_clean missing-commit &&
1607 (
1608 set_fake_editor &&
1609 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1610 git rebase -i --root &&
1611 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1612 test_must_fail env FAKE_LINES="2 3 4" \
1613 git rebase --edit-todo 2>actual &&
1614 test_cmp expect actual &&
1615 test_must_fail git rebase --continue 2>actual &&
1616 test_cmp expect.2 actual &&
1617 test_must_fail git rebase --edit-todo &&
1618 cp orig .git/rebase-merge/git-rebase-todo &&
1619 test_must_fail env FAKE_LINES="1 2 3 4" \
1620 git rebase --edit-todo 2>actual &&
1621 test_cmp expect.3 actual &&
1622 test_must_fail git rebase --continue 2>actual &&
1623 test_cmp expect.3 actual &&
1624 cp orig .git/rebase-merge/git-rebase-todo &&
1625 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1626 git rebase --continue 2>actual
1627 ) &&
1628 test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1629 test_grep \
1630 "Successfully rebased and updated refs/heads/missing-commit" \
1631 actual
1632 '
1633
1634 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1635 test_config rebase.missingCommitsCheck error &&
1636 (
1637 set_fake_editor &&
1638 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1639 ) &&
1640 git rebase --edit-todo &&
1641 test_must_fail git rebase --continue &&
1642 echo x >file1 &&
1643 git add file1 &&
1644 git rebase --continue
1645 '
1646
1647 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1648 test_config rebase.missingCommitsCheck error &&
1649 (
1650 set_fake_editor &&
1651 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1652 cp .git/rebase-merge/git-rebase-todo todo &&
1653 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1654 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1655 git rebase --continue
1656 )
1657 '
1658
1659 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1660 rebase_setup_and_clean abbrevcmd &&
1661 test_commit "first" file1.txt "first line" first &&
1662 test_commit "second" file1.txt "another line" second &&
1663 test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1664 test_commit "squash! second" file1.txt "another line here" second_squash &&
1665 cat >expected <<-EOF &&
1666 p $(git rev-list --abbrev-commit -1 first) # first
1667 f $(git rev-list --abbrev-commit -1 first_fixup) # fixup! first
1668 x git show HEAD
1669 p $(git rev-list --abbrev-commit -1 second) # second
1670 s $(git rev-list --abbrev-commit -1 second_squash) # squash! second
1671 x git show HEAD
1672 EOF
1673 git checkout abbrevcmd &&
1674 test_config rebase.abbreviateCommands true &&
1675 (
1676 set_cat_todo_editor &&
1677 test_must_fail git rebase -i --exec "git show HEAD" \
1678 --autosquash primary >actual
1679 ) &&
1680 test_cmp expected actual
1681 '
1682
1683 test_expect_success 'static check of bad command' '
1684 rebase_setup_and_clean bad-cmd &&
1685 (
1686 set_fake_editor &&
1687 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1688 git rebase -i --root 2>actual &&
1689 test_grep "pickled $(git log --format="%h # %s" -1 primary~1)" \
1690 actual &&
1691 test_grep "You can fix this with .git rebase --edit-todo.." \
1692 actual &&
1693 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1694 ) &&
1695 git rebase --continue &&
1696 test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1697 test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1698 '
1699
1700 test_expect_success 'the first command cannot be a fixup' '
1701 rebase_setup_and_clean fixup-first &&
1702
1703 cat >orig <<-EOF &&
1704 fixup $(git log -1 --format="%h %s" B)
1705 pick $(git log -1 --format="%h %s" C)
1706 EOF
1707
1708 (
1709 set_replace_editor orig &&
1710 test_must_fail git rebase -i A 2>actual
1711 ) &&
1712 grep "cannot .fixup. without a previous commit" actual &&
1713 grep "You can fix this with .git rebase --edit-todo.." actual &&
1714 # verify that the todo list has not been truncated
1715 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1716 test_cmp orig actual &&
1717
1718 test_must_fail git rebase --edit-todo 2>actual &&
1719 grep "cannot .fixup. without a previous commit" actual &&
1720 grep "You can fix this with .git rebase --edit-todo.." actual &&
1721 # verify that the todo list has not been truncated
1722 grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
1723 test_cmp orig actual
1724 '
1725
1726 test_expect_success 'tabs and spaces are accepted in the todolist' '
1727 rebase_setup_and_clean indented-comment &&
1728 write_script add-indent.sh <<-\EOF &&
1729 (
1730 # Turn single spaces into space/tab mix
1731 sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
1732 printf "\n\t# comment\n #more\n\t # comment\n"
1733 ) >"$1.new"
1734 mv "$1.new" "$1"
1735 EOF
1736 (
1737 test_set_editor "$(pwd)/add-indent.sh" &&
1738 git rebase -i HEAD^^^
1739 ) &&
1740 test E = $(git cat-file commit HEAD | sed -ne \$p)
1741 '
1742
1743 test_expect_success 'static check of bad SHA-1' '
1744 rebase_setup_and_clean bad-sha &&
1745 (
1746 set_fake_editor &&
1747 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1748 git rebase -i --root 2>actual &&
1749 test_grep "edit XXXXXXX False commit" actual &&
1750 test_grep "You can fix this with .git rebase --edit-todo.." \
1751 actual &&
1752 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1753 ) &&
1754 git rebase --continue &&
1755 test E = $(git cat-file commit HEAD | sed -ne \$p)
1756 '
1757
1758 test_expect_success 'editor saves as CR/LF' '
1759 git checkout -b with-crlf &&
1760 write_script add-crs.sh <<-\EOF &&
1761 sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1762 mv -f "$1".new "$1"
1763 EOF
1764 (
1765 test_set_editor "$(pwd)/add-crs.sh" &&
1766 git rebase -i HEAD^
1767 )
1768 '
1769
1770 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1771 test_when_finished "test_might_fail git rebase --abort" &&
1772 (
1773 set_fake_editor &&
1774 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1775 HEAD^ >out 2>err
1776 ) &&
1777 test_grep "$SQ-S\"S I Gner\"$SQ" err
1778 '
1779
1780 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1781 test_when_finished "test_might_fail git rebase --abort" &&
1782 test_config commit.gpgsign true &&
1783 (
1784 set_fake_editor &&
1785 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1786 HEAD^ >out 2>err
1787 ) &&
1788 test_grep "$SQ-S\"S I Gner\"$SQ" err
1789 '
1790
1791 test_expect_success 'valid author header after --root swap' '
1792 rebase_setup_and_clean author-header no-conflict-branch &&
1793 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1794 git cat-file commit HEAD | grep ^author >expected &&
1795 (
1796 set_fake_editor &&
1797 FAKE_LINES="5 1" git rebase -i --root
1798 ) &&
1799 git cat-file commit HEAD^ | grep ^author >actual &&
1800 test_cmp expected actual
1801 '
1802
1803 test_expect_success 'valid author header when author contains single quote' '
1804 rebase_setup_and_clean author-header no-conflict-branch &&
1805 git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1806 git cat-file commit HEAD | grep ^author >expected &&
1807 (
1808 set_fake_editor &&
1809 FAKE_LINES="2" git rebase -i HEAD~2
1810 ) &&
1811 git cat-file commit HEAD | grep ^author >actual &&
1812 test_cmp expected actual
1813 '
1814
1815 test_expect_success 'post-commit hook is called' '
1816 >actual &&
1817 test_hook post-commit <<-\EOS &&
1818 git rev-parse HEAD >>actual
1819 EOS
1820 (
1821 set_fake_editor &&
1822 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1823 echo x>file3 &&
1824 git add file3 &&
1825 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1826 ) &&
1827 git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1828 >expect &&
1829 test_cmp expect actual
1830 '
1831
1832 test_expect_success 'correct error message for partial commit after empty pick' '
1833 test_when_finished "git rebase --abort" &&
1834 (
1835 set_fake_editor &&
1836 FAKE_LINES="2 1 1" &&
1837 export FAKE_LINES &&
1838 test_must_fail git rebase -i A D
1839 ) &&
1840 echo x >file1 &&
1841 test_must_fail git commit file1 2>err &&
1842 test_grep "cannot do a partial commit during a rebase." err
1843 '
1844
1845 test_expect_success 'correct error message for commit --amend after empty pick' '
1846 test_when_finished "git rebase --abort" &&
1847 (
1848 set_fake_editor &&
1849 FAKE_LINES="1 1" &&
1850 export FAKE_LINES &&
1851 test_must_fail git rebase -i A D
1852 ) &&
1853 echo x>file1 &&
1854 test_must_fail git commit -a --amend 2>err &&
1855 test_grep "middle of a rebase -- cannot amend." err
1856 '
1857
1858 test_expect_success 'todo has correct onto hash' '
1859 GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
1860 onto=$(git rev-parse --short HEAD~4) &&
1861 test_grep "^# Rebase ..* onto $onto" actual
1862 '
1863
1864 test_expect_success 'ORIG_HEAD is updated correctly' '
1865 test_when_finished "git checkout primary && git branch -D test-orig-head" &&
1866 git checkout -b test-orig-head A &&
1867 git commit --allow-empty -m A1 &&
1868 git commit --allow-empty -m A2 &&
1869 git commit --allow-empty -m A3 &&
1870 git commit --allow-empty -m A4 &&
1871 git rebase primary &&
1872 test_cmp_rev ORIG_HEAD test-orig-head@{1}
1873 '
1874
1875 test_expect_success '--update-refs adds label and update-ref commands' '
1876 git checkout -b update-refs no-conflict-branch &&
1877 git branch -f base HEAD~4 &&
1878 git branch -f first HEAD~3 &&
1879 git branch -f second HEAD~3 &&
1880 git branch -f third HEAD~1 &&
1881 git commit --allow-empty --fixup=third &&
1882 git branch -f is-not-reordered &&
1883 git commit --allow-empty --fixup=HEAD~4 &&
1884 git branch -f shared-tip &&
1885 (
1886 set_cat_todo_editor &&
1887
1888 cat >expect <<-EOF &&
1889 pick $(git log -1 --format=%h J) # J
1890 fixup $(git log -1 --format=%h update-refs) # fixup! J # empty
1891 update-ref refs/heads/second
1892 update-ref refs/heads/first
1893 pick $(git log -1 --format=%h K) # K
1894 pick $(git log -1 --format=%h L) # L
1895 fixup $(git log -1 --format=%h is-not-reordered) # fixup! L # empty
1896 update-ref refs/heads/third
1897 pick $(git log -1 --format=%h M) # M
1898 update-ref refs/heads/no-conflict-branch
1899 update-ref refs/heads/is-not-reordered
1900 update-ref refs/heads/shared-tip
1901 EOF
1902
1903 test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
1904 test_cmp expect todo &&
1905
1906 test_must_fail git -c rebase.autosquash=true \
1907 -c rebase.updaterefs=true \
1908 rebase -i primary >todo &&
1909
1910 test_cmp expect todo
1911 )
1912 '
1913
1914 test_expect_success '--update-refs adds commands with --rebase-merges' '
1915 git checkout -b update-refs-with-merge no-conflict-branch &&
1916 git branch -f base HEAD~4 &&
1917 git branch -f first HEAD~3 &&
1918 git branch -f second HEAD~3 &&
1919 git branch -f third HEAD~1 &&
1920 git merge -m merge branch2 &&
1921 git branch -f merge-branch &&
1922 git commit --fixup=third --allow-empty &&
1923 (
1924 set_cat_todo_editor &&
1925
1926 cat >expect <<-EOF &&
1927 label onto
1928 reset onto
1929 pick $(git log -1 --format=%h branch2~1) # F
1930 pick $(git log -1 --format=%h branch2) # I
1931 update-ref refs/heads/branch2
1932 label branch2
1933 reset onto
1934 pick $(git log -1 --format=%h refs/heads/second) # J
1935 update-ref refs/heads/second
1936 update-ref refs/heads/first
1937 pick $(git log -1 --format=%h refs/heads/third~1) # K
1938 pick $(git log -1 --format=%h refs/heads/third) # L
1939 fixup $(git log -1 --format=%h update-refs-with-merge) # fixup! L # empty
1940 update-ref refs/heads/third
1941 pick $(git log -1 --format=%h HEAD~2) # M
1942 update-ref refs/heads/no-conflict-branch
1943 merge -C $(git log -1 --format=%h HEAD~1) branch2 # merge
1944 update-ref refs/heads/merge-branch
1945 EOF
1946
1947 test_must_fail git rebase -i --autosquash \
1948 --rebase-merges=rebase-cousins \
1949 --update-refs primary >todo &&
1950
1951 test_cmp expect todo &&
1952
1953 test_must_fail git -c rebase.autosquash=true \
1954 -c rebase.updaterefs=true \
1955 rebase -i \
1956 --rebase-merges=rebase-cousins \
1957 primary >todo &&
1958
1959 test_cmp expect todo
1960 )
1961 '
1962
1963 test_expect_success '--update-refs ignores non-branch decorations' '
1964 test_when_finished "git branch -D update-refs" &&
1965 test_when_finished "git checkout primary" &&
1966 git checkout -B update-refs no-conflict-branch &&
1967 (
1968 set_cat_todo_editor &&
1969
1970 # rebase.instructionFormat=%d loads normal log decorations before
1971 # --update-refs adds its branch placeholders so we must ignore
1972 # all non-local decorations.
1973 test_must_fail git -c rebase.instructionFormat="%s%d" \
1974 rebase -i --update-refs HEAD^ >todo
1975 ) &&
1976 grep ^update-ref todo >actual &&
1977 test_write_lines "update-ref refs/heads/no-conflict-branch" >expect &&
1978 test_cmp expect actual
1979 '
1980
1981 test_expect_success '--update-refs updates refs correctly' '
1982 git checkout -B update-refs no-conflict-branch &&
1983 git branch -f base HEAD~4 &&
1984 git branch -f first HEAD~3 &&
1985 git branch -f second HEAD~3 &&
1986 git branch -f third HEAD~1 &&
1987 test_commit extra2 fileX &&
1988 git commit --amend --fixup=L &&
1989
1990 git rebase -i --autosquash --update-refs primary 2>err &&
1991
1992 test_cmp_rev HEAD~3 refs/heads/first &&
1993 test_cmp_rev HEAD~3 refs/heads/second &&
1994 test_cmp_rev HEAD~1 refs/heads/third &&
1995 test_cmp_rev HEAD refs/heads/no-conflict-branch &&
1996
1997 q_to_tab >expect <<-\EOF &&
1998 Successfully rebased and updated refs/heads/update-refs.
1999 Updated the following refs with --update-refs:
2000 Qrefs/heads/first
2001 Qrefs/heads/no-conflict-branch
2002 Qrefs/heads/second
2003 Qrefs/heads/third
2004 EOF
2005
2006 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
2007 sed "s/Rebasing.*Successfully/Successfully/g" <err >err.trimmed &&
2008 test_cmp expect err.trimmed
2009 '
2010
2011 test_expect_success 'respect user edits to update-ref steps' '
2012 git checkout -B update-refs-break no-conflict-branch &&
2013 git branch -f base HEAD~4 &&
2014 git branch -f first HEAD~3 &&
2015 git branch -f second HEAD~3 &&
2016 git branch -f third HEAD~1 &&
2017 git branch -f unseen base &&
2018
2019 # First, we will add breaks to the expected todo file
2020 cat >fake-todo-1 <<-EOF &&
2021 pick $(git rev-parse HEAD~3)
2022 break
2023 update-ref refs/heads/second
2024 update-ref refs/heads/first
2025
2026 pick $(git rev-parse HEAD~2)
2027 pick $(git rev-parse HEAD~1)
2028 update-ref refs/heads/third
2029
2030 pick $(git rev-parse HEAD)
2031 update-ref refs/heads/no-conflict-branch
2032 EOF
2033
2034 # Second, we will drop some update-refs commands (and move one)
2035 cat >fake-todo-2 <<-EOF &&
2036 update-ref refs/heads/second
2037
2038 pick $(git rev-parse HEAD~2)
2039 update-ref refs/heads/third
2040 pick $(git rev-parse HEAD~1)
2041 break
2042
2043 pick $(git rev-parse HEAD)
2044 EOF
2045
2046 # Third, we will:
2047 # * insert a new one (new-branch),
2048 # * re-add an old one (first), and
2049 # * add a second instance of a previously-stored one (second)
2050 cat >fake-todo-3 <<-EOF &&
2051 update-ref refs/heads/unseen
2052 update-ref refs/heads/new-branch
2053 pick $(git rev-parse HEAD)
2054 update-ref refs/heads/first
2055 update-ref refs/heads/second
2056 EOF
2057
2058 (
2059 set_replace_editor fake-todo-1 &&
2060 git rebase -i --update-refs primary &&
2061
2062 # These branches are currently locked.
2063 for b in first second third no-conflict-branch
2064 do
2065 test_must_fail git branch -f $b base || return 1
2066 done &&
2067
2068 set_replace_editor fake-todo-2 &&
2069 git rebase --edit-todo &&
2070
2071 # These branches are currently locked.
2072 for b in second third
2073 do
2074 test_must_fail git branch -f $b base || return 1
2075 done &&
2076
2077 # These branches are currently unlocked for checkout.
2078 for b in first no-conflict-branch
2079 do
2080 git worktree add wt-$b $b &&
2081 git worktree remove wt-$b || return 1
2082 done &&
2083
2084 git rebase --continue &&
2085
2086 set_replace_editor fake-todo-3 &&
2087 git rebase --edit-todo &&
2088
2089 # These branches are currently locked.
2090 for b in second third first unseen
2091 do
2092 test_must_fail git branch -f $b base || return 1
2093 done &&
2094
2095 # These branches are currently unlocked for checkout.
2096 for b in no-conflict-branch
2097 do
2098 git worktree add wt-$b $b &&
2099 git worktree remove wt-$b || return 1
2100 done &&
2101
2102 git rebase --continue
2103 ) &&
2104
2105 test_cmp_rev HEAD~2 refs/heads/third &&
2106 test_cmp_rev HEAD~1 refs/heads/unseen &&
2107 test_cmp_rev HEAD~1 refs/heads/new-branch &&
2108 test_cmp_rev HEAD refs/heads/first &&
2109 test_cmp_rev HEAD refs/heads/second &&
2110 test_cmp_rev HEAD refs/heads/no-conflict-branch
2111 '
2112
2113 test_expect_success '--update-refs: all update-ref lines removed' '
2114 git checkout -b test-refs-not-removed no-conflict-branch &&
2115 git branch -f base HEAD~4 &&
2116 git branch -f first HEAD~3 &&
2117 git branch -f second HEAD~3 &&
2118 git branch -f third HEAD~1 &&
2119 git branch -f tip &&
2120
2121 test_commit test-refs-not-removed &&
2122 git commit --amend --fixup first &&
2123
2124 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2125
2126 (
2127 set_cat_todo_editor &&
2128 test_must_fail git rebase -i --update-refs base >todo.raw &&
2129 sed -e "/^update-ref/d" <todo.raw >todo
2130 ) &&
2131 (
2132 set_replace_editor todo &&
2133 git rebase -i --update-refs base
2134 ) &&
2135
2136 # Ensure refs are not deleted and their OIDs have not changed
2137 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2138 test_cmp expect-oids actual-oids
2139 '
2140
2141 test_expect_success '--update-refs: all update-ref lines removed, then some re-added' '
2142 git checkout -b test-refs-not-removed2 no-conflict-branch &&
2143 git branch -f base HEAD~4 &&
2144 git branch -f first HEAD~3 &&
2145 git branch -f second HEAD~3 &&
2146 git branch -f third HEAD~1 &&
2147 git branch -f tip &&
2148
2149 test_commit test-refs-not-removed2 &&
2150 git commit --amend --fixup first &&
2151
2152 git rev-parse first second third >expect-oids &&
2153
2154 (
2155 set_cat_todo_editor &&
2156 test_must_fail git rebase -i \
2157 --autosquash --update-refs \
2158 base >todo.raw &&
2159 sed -e "/^update-ref/d" <todo.raw >todo
2160 ) &&
2161
2162 # Add a break to the end of the todo so we can edit later
2163 echo "break" >>todo &&
2164
2165 (
2166 set_replace_editor todo &&
2167 git rebase -i --autosquash --update-refs base &&
2168 echo "update-ref refs/heads/tip" >todo &&
2169 git rebase --edit-todo &&
2170 git rebase --continue
2171 ) &&
2172
2173 # Ensure first/second/third are unchanged, but tip is updated
2174 git rev-parse first second third >actual-oids &&
2175 test_cmp expect-oids actual-oids &&
2176 test_cmp_rev HEAD tip
2177 '
2178
2179 test_expect_success '--update-refs: --edit-todo with no update-ref lines' '
2180 git checkout -b test-refs-not-removed3 no-conflict-branch &&
2181 git branch -f base HEAD~4 &&
2182 git branch -f first HEAD~3 &&
2183 git branch -f second HEAD~3 &&
2184 git branch -f third HEAD~1 &&
2185 git branch -f tip &&
2186
2187 test_commit test-refs-not-removed3 &&
2188 git commit --amend --fixup first &&
2189
2190 git rev-parse first second third tip no-conflict-branch >expect-oids &&
2191
2192 (
2193 set_cat_todo_editor &&
2194 test_must_fail git rebase -i \
2195 --autosquash --update-refs \
2196 base >todo.raw &&
2197 sed -e "/^update-ref/d" <todo.raw >todo
2198 ) &&
2199
2200 # Add a break to the beginning of the todo so we can resume with no
2201 # update-ref lines
2202 echo "break" >todo.new &&
2203 cat todo >>todo.new &&
2204
2205 (
2206 set_replace_editor todo.new &&
2207 git rebase -i --autosquash --update-refs base &&
2208
2209 # Make no changes when editing so update-refs is still empty
2210 cat todo >todo.new &&
2211 git rebase --edit-todo &&
2212 git rebase --continue
2213 ) &&
2214
2215 # Ensure refs are not deleted and their OIDs have not changed
2216 git rev-parse first second third tip no-conflict-branch >actual-oids &&
2217 test_cmp expect-oids actual-oids
2218 '
2219
2220 test_expect_success '--update-refs: check failed ref update' '
2221 test_when_finished "test_might_fail git rebase --abort" &&
2222 git checkout -B update-refs-error no-conflict-branch &&
2223 git branch -f base HEAD~4 &&
2224 git branch -f first HEAD~3 &&
2225 git branch -f second HEAD~2 &&
2226 git branch -f third HEAD~1 &&
2227
2228 cat >fake-todo <<-EOF &&
2229 pick $(git rev-parse HEAD~3)
2230 break
2231 update-ref refs/heads/first
2232
2233 pick $(git rev-parse HEAD~2)
2234 update-ref refs/heads/second
2235
2236 pick $(git rev-parse HEAD~1)
2237 update-ref refs/heads/third
2238
2239 pick $(git rev-parse HEAD)
2240 update-ref refs/heads/no-conflict-branch
2241 EOF
2242
2243 (
2244 set_replace_editor fake-todo &&
2245 git rebase -i --update-refs base
2246 ) &&
2247
2248 # At this point, the values of first, second, and third are
2249 # recorded in the update-refs file. We will force-update the
2250 # "second" ref, but "git branch -f" will not work because of
2251 # the lock in the update-refs file.
2252 git update-ref refs/heads/second third &&
2253
2254 test_must_fail git rebase --continue 2>err &&
2255 grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
2256
2257 q_to_tab >expect <<-\EOF &&
2258 Updated the following refs with --update-refs:
2259 Qrefs/heads/first
2260 Qrefs/heads/no-conflict-branch
2261 Qrefs/heads/third
2262 Failed to update the following refs with --update-refs:
2263 Qrefs/heads/second
2264 EOF
2265
2266 # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
2267 tail -n 6 err >err.last &&
2268 sed "s/Rebasing.*Successfully/Successfully/g" <err.last >err.trimmed &&
2269 test_cmp expect err.trimmed
2270 '
2271
2272 test_expect_success 'bad labels and refs rejected when parsing todo list' '
2273 test_when_finished "test_might_fail git rebase --abort" &&
2274 cat >todo <<-\EOF &&
2275 exec >execed
2276 label #
2277 label :invalid
2278 update-ref :bad
2279 update-ref topic
2280 EOF
2281 rm -f execed &&
2282 (
2283 set_replace_editor todo &&
2284 test_must_fail git rebase -i HEAD 2>err
2285 ) &&
2286 grep "'\''#'\'' is not a valid label" err &&
2287 grep "'\'':invalid'\'' is not a valid label" err &&
2288 grep "'\'':bad'\'' is not a valid refname" err &&
2289 grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \
2290 err &&
2291 test_path_is_missing execed
2292 '
2293
2294 test_expect_success 'non-merge commands reject merge commits' '
2295 test_when_finished "test_might_fail git rebase --abort" &&
2296 git checkout E &&
2297 git merge I &&
2298 oid=$(git rev-parse HEAD) &&
2299 cat >todo <<-EOF &&
2300 pick $oid
2301 reword $oid
2302 edit $oid
2303 fixup $oid
2304 squash $oid
2305 drop $oid # acceptable, no advice
2306 EOF
2307 (
2308 set_replace_editor todo &&
2309 test_must_fail git rebase -i HEAD 2>actual
2310 ) &&
2311 cat >expect <<-EOF &&
2312 error: ${SQ}pick${SQ} does not accept merge commits
2313 hint: ${SQ}pick${SQ} does not take a merge commit. If you wanted to
2314 hint: replay the merge, use ${SQ}merge -C${SQ} on the commit.
2315 hint: Disable this message with "git config set advice.rebaseTodoError false"
2316 error: invalid line 1: pick $oid
2317 error: ${SQ}reword${SQ} does not accept merge commits
2318 hint: ${SQ}reword${SQ} does not take a merge commit. If you wanted to
2319 hint: replay the merge and reword the commit message, use
2320 hint: ${SQ}merge -c${SQ} on the commit
2321 hint: Disable this message with "git config set advice.rebaseTodoError false"
2322 error: invalid line 2: reword $oid
2323 error: ${SQ}edit${SQ} does not accept merge commits
2324 hint: ${SQ}edit${SQ} does not take a merge commit. If you wanted to
2325 hint: replay the merge, use ${SQ}merge -C${SQ} on the commit, and then
2326 hint: ${SQ}break${SQ} to give the control back to you so that you can
2327 hint: do ${SQ}git commit --amend && git rebase --continue${SQ}.
2328 hint: Disable this message with "git config set advice.rebaseTodoError false"
2329 error: invalid line 3: edit $oid
2330 error: cannot squash merge commit into another commit
2331 error: invalid line 4: fixup $oid
2332 error: cannot squash merge commit into another commit
2333 error: invalid line 5: squash $oid
2334 You can fix this with ${SQ}git rebase --edit-todo${SQ} and then run ${SQ}git rebase --continue${SQ}.
2335 Or you can abort the rebase with ${SQ}git rebase --abort${SQ}.
2336 EOF
2337 test_cmp expect actual
2338 '
2339
2340 # This must be the last test in this file
2341 test_expect_success '$EDITOR and friends are unchanged' '
2342 test_editor_unchanged
2343 '
2344
2345 test_done