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