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