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