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