Imported Upstream version 2.27.0
[platform/upstream/git.git] / t / t3404-rebase-interactive.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes E. Schindelin
4 #
5
6 test_description='git rebase interactive
7
8 This test runs git rebase "interactively", by faking an edit, and verifies
9 that the result still makes sense.
10
11 Initial setup:
12
13      one - two - three - four (conflict-branch)
14    /
15  A - B - C - D - E            (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 SHA1 'short SHA-1 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 test_expect_success SHA1 'short SHA-1 collide' '
1266         test_when_finished "reset_rebase && git checkout master" &&
1267         git checkout collide &&
1268         colliding_sha1=6bcda37 &&
1269         test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
1270         (
1271                 unset test_tick &&
1272                 test_tick &&
1273                 set_fake_editor &&
1274                 FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1275                 FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
1276                 test $colliding_sha1 = "$(git rev-parse HEAD | cut -c 1-7)" &&
1277                 grep "^pick $colliding_sha1 " \
1278                         .git/rebase-merge/git-rebase-todo.tmp &&
1279                 grep "^pick [0-9a-f]\{40\}" \
1280                         .git/rebase-merge/git-rebase-todo &&
1281                 grep "^pick [0-9a-f]\{40\}" \
1282                         .git/rebase-merge/git-rebase-todo.backup &&
1283                 git rebase --continue
1284         ) &&
1285         collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
1286         collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
1287         test "$collide2" = "$collide3"
1288 '
1289
1290 test_expect_success 'respect core.abbrev' '
1291         git config core.abbrev 12 &&
1292         (
1293                 set_cat_todo_editor &&
1294                 test_must_fail git rebase -i HEAD~4 >todo-list
1295         ) &&
1296         test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1297 '
1298
1299 test_expect_success 'todo count' '
1300         write_script dump-raw.sh <<-\EOF &&
1301                 cat "$1"
1302         EOF
1303         (
1304                 test_set_editor "$(pwd)/dump-raw.sh" &&
1305                 git rebase -i HEAD~4 >actual
1306         ) &&
1307         test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1308 '
1309
1310 test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1311         git checkout --force branch2 &&
1312         git clean -f &&
1313         (
1314                 set_fake_editor &&
1315                 FAKE_LINES="edit 1 2" git rebase -i A
1316         ) &&
1317         test_cmp_rev HEAD F &&
1318         test_path_is_missing file6 &&
1319         >file6 &&
1320         test_must_fail git rebase --continue &&
1321         test_cmp_rev HEAD F &&
1322         rm file6 &&
1323         git rebase --continue &&
1324         test_cmp_rev HEAD I
1325 '
1326
1327 test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1328         git checkout --force branch2 &&
1329         git clean -f &&
1330         git tag original-branch2 &&
1331         (
1332                 set_fake_editor &&
1333                 FAKE_LINES="edit 1 squash 2" git rebase -i A
1334         ) &&
1335         test_cmp_rev HEAD F &&
1336         test_path_is_missing file6 &&
1337         >file6 &&
1338         test_must_fail git rebase --continue &&
1339         test_cmp_rev HEAD F &&
1340         rm file6 &&
1341         git rebase --continue &&
1342         test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1343         git reset --hard original-branch2
1344 '
1345
1346 test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1347         git checkout --force branch2 &&
1348         git clean -f &&
1349         (
1350                 set_fake_editor &&
1351                 FAKE_LINES="edit 1 2" git rebase -i --no-ff A
1352         ) &&
1353         test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1354         test_path_is_missing file6 &&
1355         >file6 &&
1356         test_must_fail git rebase --continue &&
1357         test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1358         rm file6 &&
1359         git rebase --continue &&
1360         test $(git cat-file commit HEAD | sed -ne \$p) = I
1361 '
1362
1363 test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1364         git checkout -b commit-to-skip &&
1365         for double in X 3 1
1366         do
1367                 test_seq 5 | sed "s/$double/&&/" >seq &&
1368                 git add seq &&
1369                 test_tick &&
1370                 git commit -m seq-$double
1371         done &&
1372         git tag seq-onto &&
1373         git reset --hard HEAD~2 &&
1374         git cherry-pick seq-onto &&
1375         (
1376                 set_fake_editor &&
1377                 test_must_fail env FAKE_LINES= git rebase -i seq-onto
1378         ) &&
1379         test -d .git/rebase-merge &&
1380         git rebase --continue &&
1381         git diff --exit-code seq-onto &&
1382         test ! -d .git/rebase-merge &&
1383         test ! -f .git/CHERRY_PICK_HEAD
1384 '
1385
1386 rebase_setup_and_clean () {
1387         test_when_finished "
1388                 git checkout master &&
1389                 test_might_fail git branch -D $1 &&
1390                 test_might_fail git rebase --abort
1391         " &&
1392         git checkout -b $1 ${2:-master}
1393 }
1394
1395 test_expect_success 'drop' '
1396         rebase_setup_and_clean drop-test &&
1397         (
1398                 set_fake_editor &&
1399                 FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
1400         ) &&
1401         test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1402         test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1403         test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1404 '
1405
1406 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1407         test_config rebase.missingCommitsCheck ignore &&
1408         rebase_setup_and_clean missing-commit &&
1409         (
1410                 set_fake_editor &&
1411                 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
1412         ) &&
1413         test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1414         test_i18ngrep \
1415                 "Successfully rebased and updated refs/heads/missing-commit" \
1416                 actual
1417 '
1418
1419 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1420         cat >expect <<-EOF &&
1421         Warning: some commits may have been dropped accidentally.
1422         Dropped commits (newer to older):
1423          - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1424         To avoid this message, use "drop" to explicitly remove a commit.
1425         EOF
1426         test_config rebase.missingCommitsCheck warn &&
1427         rebase_setup_and_clean missing-commit &&
1428         (
1429                 set_fake_editor &&
1430                 FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
1431         ) &&
1432         head -n4 actual.2 >actual &&
1433         test_i18ncmp expect actual &&
1434         test D = $(git cat-file commit HEAD | sed -ne \$p)
1435 '
1436
1437 test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1438         cat >expect <<-EOF &&
1439         Warning: some commits may have been dropped accidentally.
1440         Dropped commits (newer to older):
1441          - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1442          - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1443         To avoid this message, use "drop" to explicitly remove a commit.
1444
1445         Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1446         The possible behaviours are: ignore, warn, error.
1447
1448         You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1449         Or you can abort the rebase with '\''git rebase --abort'\''.
1450         EOF
1451         test_config rebase.missingCommitsCheck error &&
1452         rebase_setup_and_clean missing-commit &&
1453         (
1454                 set_fake_editor &&
1455                 test_must_fail env FAKE_LINES="1 2 4" \
1456                         git rebase -i --root 2>actual &&
1457                 test_i18ncmp expect actual &&
1458                 cp .git/rebase-merge/git-rebase-todo.backup \
1459                         .git/rebase-merge/git-rebase-todo &&
1460                 FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
1461         ) &&
1462         git rebase --continue &&
1463         test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1464         test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1465 '
1466
1467 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
1468         test_config rebase.missingCommitsCheck ignore &&
1469         rebase_setup_and_clean missing-commit &&
1470         (
1471                 set_fake_editor &&
1472                 FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
1473                 FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
1474                 git rebase --continue 2>actual
1475         ) &&
1476         test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1477         test_i18ngrep \
1478                 "Successfully rebased and updated refs/heads/missing-commit" \
1479                 actual
1480 '
1481
1482 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
1483         cat >expect <<-EOF &&
1484         error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 master~4)
1485         Warning: some commits may have been dropped accidentally.
1486         Dropped commits (newer to older):
1487          - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1488          - $(git rev-list --pretty=oneline --abbrev-commit -1 master~4)
1489         To avoid this message, use "drop" to explicitly remove a commit.
1490         EOF
1491         head -n4 expect >expect.2 &&
1492         tail -n1 expect >>expect.2 &&
1493         tail -n4 expect.2 >expect.3 &&
1494         test_config rebase.missingCommitsCheck warn &&
1495         rebase_setup_and_clean missing-commit &&
1496         (
1497                 set_fake_editor &&
1498                 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1499                         git rebase -i --root &&
1500                 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1501                 FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
1502                 head -n6 actual.2 >actual &&
1503                 test_i18ncmp expect actual &&
1504                 cp orig .git/rebase-merge/git-rebase-todo &&
1505                 FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
1506                 head -n4 actual.2 >actual &&
1507                 test_i18ncmp expect.3 actual &&
1508                 git rebase --continue 2>actual
1509         ) &&
1510         test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1511         test_i18ngrep \
1512                 "Successfully rebased and updated refs/heads/missing-commit" \
1513                 actual
1514 '
1515
1516 test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
1517         cat >expect <<-EOF &&
1518         error: invalid line 1: badcmd $(git rev-list --pretty=oneline --abbrev-commit -1 master~4)
1519         Warning: some commits may have been dropped accidentally.
1520         Dropped commits (newer to older):
1521          - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1522          - $(git rev-list --pretty=oneline --abbrev-commit -1 master~4)
1523         To avoid this message, use "drop" to explicitly remove a commit.
1524
1525         Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
1526         The possible behaviours are: ignore, warn, error.
1527
1528         You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
1529         Or you can abort the rebase with '\''git rebase --abort'\''.
1530         EOF
1531         tail -n11 expect >expect.2 &&
1532         head -n3 expect.2 >expect.3 &&
1533         tail -n7 expect.2 >>expect.3 &&
1534         test_config rebase.missingCommitsCheck error &&
1535         rebase_setup_and_clean missing-commit &&
1536         (
1537                 set_fake_editor &&
1538                 test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
1539                         git rebase -i --root &&
1540                 cp .git/rebase-merge/git-rebase-todo.backup orig &&
1541                 test_must_fail env FAKE_LINES="2 3 4" \
1542                         git rebase --edit-todo 2>actual &&
1543                 test_i18ncmp expect actual &&
1544                 test_must_fail git rebase --continue 2>actual &&
1545                 test_i18ncmp expect.2 actual &&
1546                 test_must_fail git rebase --edit-todo &&
1547                 cp orig .git/rebase-merge/git-rebase-todo &&
1548                 test_must_fail env FAKE_LINES="1 2 3 4" \
1549                         git rebase --edit-todo 2>actual &&
1550                 test_i18ncmp expect.3 actual &&
1551                 test_must_fail git rebase --continue 2>actual &&
1552                 test_i18ncmp expect.3 actual &&
1553                 cp orig .git/rebase-merge/git-rebase-todo &&
1554                 FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
1555                 git rebase --continue 2>actual
1556         ) &&
1557         test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1558         test_i18ngrep \
1559                 "Successfully rebased and updated refs/heads/missing-commit" \
1560                 actual
1561 '
1562
1563 test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
1564         test_config rebase.missingCommitsCheck error &&
1565         (
1566                 set_fake_editor &&
1567                 FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
1568         ) &&
1569         git rebase --edit-todo &&
1570         test_must_fail git rebase --continue &&
1571         echo x >file1 &&
1572         git add file1 &&
1573         git rebase --continue
1574 '
1575
1576 test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
1577         test_config rebase.missingCommitsCheck error &&
1578         (
1579                 set_fake_editor &&
1580                 FAKE_LINES="1 break 2 3" git rebase -i A D &&
1581                 cp .git/rebase-merge/git-rebase-todo todo &&
1582                 test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
1583                 GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
1584                 git rebase --continue
1585         )
1586 '
1587
1588 test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1589         rebase_setup_and_clean abbrevcmd &&
1590         test_commit "first" file1.txt "first line" first &&
1591         test_commit "second" file1.txt "another line" second &&
1592         test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1593         test_commit "squash! second" file1.txt "another line here" second_squash &&
1594         cat >expected <<-EOF &&
1595         p $(git rev-list --abbrev-commit -1 first) first
1596         f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1597         x git show HEAD
1598         p $(git rev-list --abbrev-commit -1 second) second
1599         s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1600         x git show HEAD
1601         EOF
1602         git checkout abbrevcmd &&
1603         test_config rebase.abbreviateCommands true &&
1604         (
1605                 set_cat_todo_editor &&
1606                 test_must_fail git rebase -i --exec "git show HEAD" \
1607                         --autosquash master >actual
1608         ) &&
1609         test_cmp expected actual
1610 '
1611
1612 test_expect_success 'static check of bad command' '
1613         rebase_setup_and_clean bad-cmd &&
1614         (
1615                 set_fake_editor &&
1616                 test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1617                 git rebase -i --root 2>actual &&
1618                 test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" \
1619                                 actual &&
1620                 test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1621                                 actual &&
1622                 FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
1623         ) &&
1624         git rebase --continue &&
1625         test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1626         test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1627 '
1628
1629 test_expect_success 'tabs and spaces are accepted in the todolist' '
1630         rebase_setup_and_clean indented-comment &&
1631         write_script add-indent.sh <<-\EOF &&
1632         (
1633                 # Turn single spaces into space/tab mix
1634                 sed "1s/ /      /g; 2s/ /  /g; 3s/ /    /g" "$1"
1635                 printf "\n\t# comment\n #more\n\t # comment\n"
1636         ) >"$1.new"
1637         mv "$1.new" "$1"
1638         EOF
1639         (
1640                 test_set_editor "$(pwd)/add-indent.sh" &&
1641                 git rebase -i HEAD^^^
1642         ) &&
1643         test E = $(git cat-file commit HEAD | sed -ne \$p)
1644 '
1645
1646 test_expect_success 'static check of bad SHA-1' '
1647         rebase_setup_and_clean bad-sha &&
1648         (
1649                 set_fake_editor &&
1650                 test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1651                         git rebase -i --root 2>actual &&
1652                         test_i18ngrep "edit XXXXXXX False commit" actual &&
1653                         test_i18ngrep "You can fix this with .git rebase --edit-todo.." \
1654                                         actual &&
1655                 FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
1656         ) &&
1657         git rebase --continue &&
1658         test E = $(git cat-file commit HEAD | sed -ne \$p)
1659 '
1660
1661 test_expect_success 'editor saves as CR/LF' '
1662         git checkout -b with-crlf &&
1663         write_script add-crs.sh <<-\EOF &&
1664         sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1665         mv -f "$1".new "$1"
1666         EOF
1667         (
1668                 test_set_editor "$(pwd)/add-crs.sh" &&
1669                 git rebase -i HEAD^
1670         )
1671 '
1672
1673 test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1674         test_when_finished "test_might_fail git rebase --abort" &&
1675         (
1676                 set_fake_editor &&
1677                 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1678                         HEAD^ >out 2>err
1679         ) &&
1680         test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1681 '
1682
1683 test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1684         test_when_finished "test_might_fail git rebase --abort" &&
1685         test_config commit.gpgsign true &&
1686         (
1687                 set_fake_editor &&
1688                 FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
1689                         HEAD^ >out 2>err
1690         ) &&
1691         test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1692 '
1693
1694 test_expect_success 'valid author header after --root swap' '
1695         rebase_setup_and_clean author-header no-conflict-branch &&
1696         git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1697         git cat-file commit HEAD | grep ^author >expected &&
1698         (
1699                 set_fake_editor &&
1700                 FAKE_LINES="5 1" git rebase -i --root
1701         ) &&
1702         git cat-file commit HEAD^ | grep ^author >actual &&
1703         test_cmp expected actual
1704 '
1705
1706 test_expect_success 'valid author header when author contains single quote' '
1707         rebase_setup_and_clean author-header no-conflict-branch &&
1708         git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
1709         git cat-file commit HEAD | grep ^author >expected &&
1710         (
1711                 set_fake_editor &&
1712                 FAKE_LINES="2" git rebase -i HEAD~2
1713         ) &&
1714         git cat-file commit HEAD | grep ^author >actual &&
1715         test_cmp expected actual
1716 '
1717
1718 test_expect_success 'post-commit hook is called' '
1719         test_when_finished "rm -f .git/hooks/post-commit" &&
1720         >actual &&
1721         mkdir -p .git/hooks &&
1722         write_script .git/hooks/post-commit <<-\EOS &&
1723         git rev-parse HEAD >>actual
1724         EOS
1725         (
1726                 set_fake_editor &&
1727                 FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
1728                 echo x>file3 &&
1729                 git add file3 &&
1730                 FAKE_COMMIT_MESSAGE=edited git rebase --continue
1731         ) &&
1732         git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
1733                 >expect &&
1734         test_cmp expect actual
1735 '
1736
1737 test_expect_success 'correct error message for partial commit after empty pick' '
1738         test_when_finished "git rebase --abort" &&
1739         (
1740                 set_fake_editor &&
1741                 FAKE_LINES="2 1 1" &&
1742                 export FAKE_LINES &&
1743                 test_must_fail git rebase -i A D
1744         ) &&
1745         echo x >file1 &&
1746         test_must_fail git commit file1 2>err &&
1747         test_i18ngrep "cannot do a partial commit during a rebase." err
1748 '
1749
1750 test_expect_success 'correct error message for commit --amend after empty pick' '
1751         test_when_finished "git rebase --abort" &&
1752         (
1753                 set_fake_editor &&
1754                 FAKE_LINES="1 1" &&
1755                 export FAKE_LINES &&
1756                 test_must_fail git rebase -i A D
1757         ) &&
1758         echo x>file1 &&
1759         test_must_fail git commit -a --amend 2>err &&
1760         test_i18ngrep "middle of a rebase -- cannot amend." err
1761 '
1762
1763 # This must be the last test in this file
1764 test_expect_success '$EDITOR and friends are unchanged' '
1765         test_editor_unchanged
1766 '
1767
1768 test_done