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