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