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'
11. ./test-lib.sh
12
13. "$TEST_DIRECTORY"/lib-rebase.sh
14
15set_fake_editor
16
17# Set up the repository like this:
18#
19# one - two - three - four (conflict-branch)
20# /
21# A - B - C - D - E (master)
22# | \
23# | F - G - H (branch1)
24# | \
25# |\ I (branch2)
26# | \
27# | J - K - L - M (no-conflict-branch)
28# \
29# N - O - P (no-ff-branch)
30#
31# where A, B, D and G all touch file1, and one, two, three, four all
32# touch file "conflict".
33#
34# WARNING: Modifications to the initial repository can change the SHA ID used
35# in the expect2 file for the 'stop on conflicting pick' test.
36
37
38test_expect_success 'setup' '
39 test_commit A file1 &&
40 test_commit B file1 &&
41 test_commit C file2 &&
42 test_commit D file1 &&
43 test_commit E file3 &&
44 git checkout -b branch1 A &&
45 test_commit F file4 &&
46 test_commit G file1 &&
47 test_commit H file5 &&
48 git checkout -b branch2 F &&
49 test_commit I file6
50 git checkout -b conflict-branch A &&
51 for n in one two three four
52 do
53 test_commit $n conflict
54 done &&
55 git checkout -b no-conflict-branch A &&
56 for n in J K L M
57 do
58 test_commit $n file$n
59 done &&
60 git checkout -b no-ff-branch A &&
61 for n in N O P
62 do
63 test_commit $n file$n
64 done
65'
66
67test_expect_success 'no changes are a nop' '
68 git checkout branch2 &&
69 git rebase -i F &&
70 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
71 test $(git rev-parse I) = $(git rev-parse HEAD)
72'
73
74test_expect_success 'test the [branch] option' '
75 git checkout -b dead-end &&
76 git rm file6 &&
77 git commit -m "stop here" &&
78 git rebase -i F branch2 &&
79 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
80 test $(git rev-parse I) = $(git rev-parse branch2) &&
81 test $(git rev-parse I) = $(git rev-parse HEAD)
82'
83
84test_expect_success 'test --onto <branch>' '
85 git checkout -b test-onto branch2 &&
86 git rebase -i --onto branch1 F &&
87 test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
88 test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
89 test $(git rev-parse I) = $(git rev-parse branch2)
90'
91
92test_expect_success 'rebase on top of a non-conflicting commit' '
93 git checkout branch1 &&
94 git tag original-branch1 &&
95 git rebase -i branch2 &&
96 test file6 = $(git diff --name-only original-branch1) &&
97 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
98 test $(git rev-parse I) = $(git rev-parse branch2) &&
99 test $(git rev-parse I) = $(git rev-parse HEAD~2)
100'
101
102test_expect_success 'reflog for the branch shows state before rebase' '
103 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
104'
105
106test_expect_success 'exchange two commits' '
107 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
108 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
109 test G = $(git cat-file commit HEAD | sed -ne \$p)
110'
111
112cat > expect << EOF
113diff --git a/file1 b/file1
114index f70f10e..fd79235 100644
115--- a/file1
116+++ b/file1
117@@ -1 +1 @@
118-A
119+G
120EOF
121
122cat > expect2 << EOF
123<<<<<<< HEAD
124D
125=======
126G
127>>>>>>> 5d18e54... G
128EOF
129
130test_expect_success 'stop on conflicting pick' '
131 git tag new-branch1 &&
132 test_must_fail git rebase -i master &&
133 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
134 test_cmp expect .git/rebase-merge/patch &&
135 test_cmp expect2 file1 &&
136 test "$(git diff --name-status |
137 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
138 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
139 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
140'
141
142test_expect_success 'abort' '
143 git rebase --abort &&
144 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
145 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
146 ! test -d .git/rebase-merge
147'
148
149test_expect_success 'retain authorship' '
150 echo A > file7 &&
151 git add file7 &&
152 test_tick &&
153 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
154 git tag twerp &&
155 git rebase -i --onto master HEAD^ &&
156 git show HEAD | grep "^Author: Twerp Snog"
157'
158
159test_expect_success 'squash' '
160 git reset --hard twerp &&
161 echo B > file7 &&
162 test_tick &&
163 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
164 echo "******************************" &&
165 FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
166 git rebase -i --onto master HEAD~2 &&
167 test B = $(cat file7) &&
168 test $(git rev-parse HEAD^) = $(git rev-parse master)
169'
170
171test_expect_success 'retain authorship when squashing' '
172 git show HEAD | grep "^Author: Twerp Snog"
173'
174
175test_expect_success '-p handles "no changes" gracefully' '
176 HEAD=$(git rev-parse HEAD) &&
177 git rebase -i -p HEAD^ &&
178 git update-index --refresh &&
179 git diff-files --quiet &&
180 git diff-index --quiet --cached HEAD -- &&
181 test $HEAD = $(git rev-parse HEAD)
182'
183
184test_expect_success 'preserve merges with -p' '
185 git checkout -b to-be-preserved master^ &&
186 : > unrelated-file &&
187 git add unrelated-file &&
188 test_tick &&
189 git commit -m "unrelated" &&
190 git checkout -b another-branch master &&
191 echo B > file1 &&
192 test_tick &&
193 git commit -m J file1 &&
194 test_tick &&
195 git merge to-be-preserved &&
196 echo C > file1 &&
197 test_tick &&
198 git commit -m K file1 &&
199 echo D > file1 &&
200 test_tick &&
201 git commit -m L1 file1 &&
202 git checkout HEAD^ &&
203 echo 1 > unrelated-file &&
204 test_tick &&
205 git commit -m L2 unrelated-file &&
206 test_tick &&
207 git merge another-branch &&
208 echo E > file1 &&
209 test_tick &&
210 git commit -m M file1 &&
211 git checkout -b to-be-rebased &&
212 test_tick &&
213 git rebase -i -p --onto branch1 master &&
214 git update-index --refresh &&
215 git diff-files --quiet &&
216 git diff-index --quiet --cached HEAD -- &&
217 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
218 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
219 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
220 test $(git show HEAD~5:file1) = B &&
221 test $(git show HEAD~3:file1) = C &&
222 test $(git show HEAD:file1) = E &&
223 test $(git show HEAD:unrelated-file) = 1
224'
225
226test_expect_success 'edit ancestor with -p' '
227 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
228 echo 2 > unrelated-file &&
229 test_tick &&
230 git commit -m L2-modified --amend unrelated-file &&
231 git rebase --continue &&
232 git update-index --refresh &&
233 git diff-files --quiet &&
234 git diff-index --quiet --cached HEAD -- &&
235 test $(git show HEAD:unrelated-file) = 2
236'
237
238test_expect_success '--continue tries to commit' '
239 test_tick &&
240 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
241 echo resolved > file1 &&
242 git add file1 &&
243 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
244 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
245 git show HEAD | grep chouette
246'
247
248test_expect_success 'verbose flag is heeded, even after --continue' '
249 git reset --hard HEAD@{1} &&
250 test_tick &&
251 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
252 echo resolved > file1 &&
253 git add file1 &&
254 git rebase --continue > output &&
255 grep "^ file1 | 2 +-$" output
256'
257
258test_expect_success 'multi-squash only fires up editor once' '
259 base=$(git rev-parse HEAD~4) &&
260 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
261 EXPECT_HEADER_COUNT=4 \
262 git rebase -i $base &&
263 test $base = $(git rev-parse HEAD^) &&
264 test 1 = $(git show | grep ONCE | wc -l)
265'
266
267test_expect_success 'multi-fixup does not fire up editor' '
268 git checkout -b multi-fixup E &&
269 base=$(git rev-parse HEAD~4) &&
270 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
271 git rebase -i $base &&
272 test $base = $(git rev-parse HEAD^) &&
273 test 0 = $(git show | grep NEVER | wc -l) &&
274 git checkout to-be-rebased &&
275 git branch -D multi-fixup
276'
277
278test_expect_success 'commit message used after conflict' '
279 git checkout -b conflict-fixup conflict-branch &&
280 base=$(git rev-parse HEAD~4) &&
281 (
282 FAKE_LINES="1 fixup 3 fixup 4" &&
283 export FAKE_LINES &&
284 test_must_fail git rebase -i $base
285 ) &&
286 echo three > conflict &&
287 git add conflict &&
288 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
289 git rebase --continue &&
290 test $base = $(git rev-parse HEAD^) &&
291 test 1 = $(git show | grep ONCE | wc -l) &&
292 git checkout to-be-rebased &&
293 git branch -D conflict-fixup
294'
295
296test_expect_success 'commit message retained after conflict' '
297 git checkout -b conflict-squash conflict-branch &&
298 base=$(git rev-parse HEAD~4) &&
299 (
300 FAKE_LINES="1 fixup 3 squash 4" &&
301 export FAKE_LINES &&
302 test_must_fail git rebase -i $base
303 ) &&
304 echo three > conflict &&
305 git add conflict &&
306 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
307 git rebase --continue &&
308 test $base = $(git rev-parse HEAD^) &&
309 test 2 = $(git show | grep TWICE | wc -l) &&
310 git checkout to-be-rebased &&
311 git branch -D conflict-squash
312'
313
314cat > expect-squash-fixup << EOF
315B
316
317D
318
319ONCE
320EOF
321
322test_expect_success 'squash and fixup generate correct log messages' '
323 git checkout -b squash-fixup E &&
324 base=$(git rev-parse HEAD~4) &&
325 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
326 EXPECT_HEADER_COUNT=4 \
327 git rebase -i $base &&
328 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
329 test_cmp expect-squash-fixup actual-squash-fixup &&
330 git checkout to-be-rebased &&
331 git branch -D squash-fixup
332'
333
334test_expect_success 'squash ignores comments' '
335 git checkout -b skip-comments E &&
336 base=$(git rev-parse HEAD~4) &&
337 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
338 EXPECT_HEADER_COUNT=4 \
339 git rebase -i $base &&
340 test $base = $(git rev-parse HEAD^) &&
341 test 1 = $(git show | grep ONCE | wc -l) &&
342 git checkout to-be-rebased &&
343 git branch -D skip-comments
344'
345
346test_expect_success 'squash ignores blank lines' '
347 git checkout -b skip-blank-lines E &&
348 base=$(git rev-parse HEAD~4) &&
349 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
350 EXPECT_HEADER_COUNT=4 \
351 git rebase -i $base &&
352 test $base = $(git rev-parse HEAD^) &&
353 test 1 = $(git show | grep ONCE | wc -l) &&
354 git checkout to-be-rebased &&
355 git branch -D skip-blank-lines
356'
357
358test_expect_success 'squash works as expected' '
359 git checkout -b squash-works no-conflict-branch &&
360 one=$(git rev-parse HEAD~3) &&
361 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
362 git rebase -i HEAD~3 &&
363 test $one = $(git rev-parse HEAD~2)
364'
365
366test_expect_success 'interrupted squash works as expected' '
367 git checkout -b interrupted-squash conflict-branch &&
368 one=$(git rev-parse HEAD~3) &&
369 (
370 FAKE_LINES="1 squash 3 2" &&
371 export FAKE_LINES &&
372 test_must_fail git rebase -i HEAD~3
373 ) &&
374 (echo one; echo two; echo four) > conflict &&
375 git add conflict &&
376 test_must_fail git rebase --continue &&
377 echo resolved > conflict &&
378 git add conflict &&
379 git rebase --continue &&
380 test $one = $(git rev-parse HEAD~2)
381'
382
383test_expect_success 'interrupted squash works as expected (case 2)' '
384 git checkout -b interrupted-squash2 conflict-branch &&
385 one=$(git rev-parse HEAD~3) &&
386 (
387 FAKE_LINES="3 squash 1 2" &&
388 export FAKE_LINES &&
389 test_must_fail git rebase -i HEAD~3
390 ) &&
391 (echo one; echo four) > conflict &&
392 git add conflict &&
393 test_must_fail git rebase --continue &&
394 (echo one; echo two; echo four) > conflict &&
395 git add conflict &&
396 test_must_fail git rebase --continue &&
397 echo resolved > conflict &&
398 git add conflict &&
399 git rebase --continue &&
400 test $one = $(git rev-parse HEAD~2)
401'
402
403test_expect_success 'ignore patch if in upstream' '
404 HEAD=$(git rev-parse HEAD) &&
405 git checkout -b has-cherry-picked HEAD^ &&
406 echo unrelated > file7 &&
407 git add file7 &&
408 test_tick &&
409 git commit -m "unrelated change" &&
410 git cherry-pick $HEAD &&
411 EXPECT_COUNT=1 git rebase -i $HEAD &&
412 test $HEAD = $(git rev-parse HEAD^)
413'
414
415test_expect_success '--continue tries to commit, even for "edit"' '
416 parent=$(git rev-parse HEAD^) &&
417 test_tick &&
418 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
419 echo edited > file7 &&
420 git add file7 &&
421 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
422 test edited = $(git show HEAD:file7) &&
423 git show HEAD | grep chouette &&
424 test $parent = $(git rev-parse HEAD^)
425'
426
427test_expect_success 'aborted --continue does not squash commits after "edit"' '
428 old=$(git rev-parse HEAD) &&
429 test_tick &&
430 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
431 echo "edited again" > file7 &&
432 git add file7 &&
433 (
434 FAKE_COMMIT_MESSAGE=" " &&
435 export FAKE_COMMIT_MESSAGE &&
436 test_must_fail git rebase --continue
437 ) &&
438 test $old = $(git rev-parse HEAD) &&
439 git rebase --abort
440'
441
442test_expect_success 'auto-amend only edited commits after "edit"' '
443 test_tick &&
444 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
445 echo "edited again" > file7 &&
446 git add file7 &&
447 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
448 echo "and again" > file7 &&
449 git add file7 &&
450 test_tick &&
451 (
452 FAKE_COMMIT_MESSAGE="and again" &&
453 export FAKE_COMMIT_MESSAGE &&
454 test_must_fail git rebase --continue
455 ) &&
456 git rebase --abort
457'
458
459test_expect_success 'rebase a detached HEAD' '
460 grandparent=$(git rev-parse HEAD~2) &&
461 git checkout $(git rev-parse HEAD) &&
462 test_tick &&
463 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
464 test $grandparent = $(git rev-parse HEAD~2)
465'
466
467test_expect_success 'rebase a commit violating pre-commit' '
468
469 mkdir -p .git/hooks &&
470 PRE_COMMIT=.git/hooks/pre-commit &&
471 echo "#!/bin/sh" > $PRE_COMMIT &&
472 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
473 chmod a+x $PRE_COMMIT &&
474 echo "monde! " >> file1 &&
475 test_tick &&
476 test_must_fail git commit -m doesnt-verify file1 &&
477 git commit -m doesnt-verify --no-verify file1 &&
478 test_tick &&
479 FAKE_LINES=2 git rebase -i HEAD~2
480
481'
482
483test_expect_success 'rebase with a file named HEAD in worktree' '
484
485 rm -fr .git/hooks &&
486 git reset --hard &&
487 git checkout -b branch3 A &&
488
489 (
490 GIT_AUTHOR_NAME="Squashed Away" &&
491 export GIT_AUTHOR_NAME &&
492 >HEAD &&
493 git add HEAD &&
494 git commit -m "Add head" &&
495 >BODY &&
496 git add BODY &&
497 git commit -m "Add body"
498 ) &&
499
500 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
501 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
502
503'
504
505test_expect_success 'do "noop" when there is nothing to cherry-pick' '
506
507 git checkout -b branch4 HEAD &&
508 GIT_EDITOR=: git commit --amend \
509 --author="Somebody else <somebody@else.com>"
510 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
511 git rebase -i branch3 &&
512 test $(git rev-parse branch3) = $(git rev-parse branch4)
513
514'
515
516test_expect_success 'submodule rebase setup' '
517 git checkout A &&
518 mkdir sub &&
519 (
520 cd sub && git init && >elif &&
521 git add elif && git commit -m "submodule initial"
522 ) &&
523 echo 1 >file1 &&
524 git add file1 sub
525 test_tick &&
526 git commit -m "One" &&
527 echo 2 >file1 &&
528 test_tick &&
529 git commit -a -m "Two" &&
530 (
531 cd sub && echo 3 >elif &&
532 git commit -a -m "submodule second"
533 ) &&
534 test_tick &&
535 git commit -a -m "Three changes submodule"
536'
537
538test_expect_success 'submodule rebase -i' '
539 FAKE_LINES="1 squash 2 3" git rebase -i A
540'
541
542test_expect_success 'avoid unnecessary reset' '
543 git checkout master &&
544 test-chmtime =123456789 file3 &&
545 git update-index --refresh &&
546 HEAD=$(git rev-parse HEAD) &&
547 git rebase -i HEAD~4 &&
548 test $HEAD = $(git rev-parse HEAD) &&
549 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
550 test 123456789 = $MTIME
551'
552
553test_expect_success 'reword' '
554 git checkout -b reword-branch master &&
555 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
556 git show HEAD | grep "E changed" &&
557 test $(git rev-parse master) != $(git rev-parse HEAD) &&
558 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
559 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
560 git show HEAD^ | grep "D changed" &&
561 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
562 git show HEAD~3 | grep "B changed" &&
563 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
564 git show HEAD~2 | grep "C changed"
565'
566
567test_expect_success 'rebase -i can copy notes' '
568 git config notes.rewrite.rebase true &&
569 git config notes.rewriteRef "refs/notes/*" &&
570 test_commit n1 &&
571 test_commit n2 &&
572 test_commit n3 &&
573 git notes add -m"a note" n3 &&
574 git rebase --onto n1 n2 &&
575 test "a note" = "$(git notes show HEAD)"
576'
577
578cat >expect <<EOF
579an earlier note
580a note
581EOF
582
583test_expect_success 'rebase -i can copy notes over a fixup' '
584 git reset --hard n3 &&
585 git notes add -m"an earlier note" n2 &&
586 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
587 git notes show > output &&
588 test_cmp expect output
589'
590
591test_expect_success 'rebase while detaching HEAD' '
592 git symbolic-ref HEAD &&
593 grandparent=$(git rev-parse HEAD~2) &&
594 test_tick &&
595 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
596 test $grandparent = $(git rev-parse HEAD~2) &&
597 test_must_fail git symbolic-ref HEAD
598'
599
600test_tick # Ensure that the rebased commits get a different timestamp.
601test_expect_success 'always cherry-pick with --no-ff' '
602 git checkout no-ff-branch &&
603 git tag original-no-ff-branch &&
604 git rebase -i --no-ff A &&
605 touch empty &&
606 for p in 0 1 2
607 do
608 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
609 git diff HEAD~$p original-no-ff-branch~$p > out &&
610 test_cmp empty out
611 done &&
612 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
613 git diff HEAD~3 original-no-ff-branch~3 > out &&
614 test_cmp empty out
615'
616
617test_done