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