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