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