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