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