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