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_failure 'exchange two commits with -p' '
185 FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
186 test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
187 test G = $(git cat-file commit HEAD | sed -ne \$p)
188'
189
190test_expect_success 'preserve merges with -p' '
191 git checkout -b to-be-preserved master^ &&
192 : > unrelated-file &&
193 git add unrelated-file &&
194 test_tick &&
195 git commit -m "unrelated" &&
196 git checkout -b another-branch master &&
197 echo B > file1 &&
198 test_tick &&
199 git commit -m J file1 &&
200 test_tick &&
201 git merge to-be-preserved &&
202 echo C > file1 &&
203 test_tick &&
204 git commit -m K file1 &&
205 echo D > file1 &&
206 test_tick &&
207 git commit -m L1 file1 &&
208 git checkout HEAD^ &&
209 echo 1 > unrelated-file &&
210 test_tick &&
211 git commit -m L2 unrelated-file &&
212 test_tick &&
213 git merge another-branch &&
214 echo E > file1 &&
215 test_tick &&
216 git commit -m M file1 &&
217 git checkout -b to-be-rebased &&
218 test_tick &&
219 git rebase -i -p --onto branch1 master &&
220 git update-index --refresh &&
221 git diff-files --quiet &&
222 git diff-index --quiet --cached HEAD -- &&
223 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
224 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
225 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
226 test $(git show HEAD~5:file1) = B &&
227 test $(git show HEAD~3:file1) = C &&
228 test $(git show HEAD:file1) = E &&
229 test $(git show HEAD:unrelated-file) = 1
230'
231
232test_expect_success 'edit ancestor with -p' '
233 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
234 echo 2 > unrelated-file &&
235 test_tick &&
236 git commit -m L2-modified --amend unrelated-file &&
237 git rebase --continue &&
238 git update-index --refresh &&
239 git diff-files --quiet &&
240 git diff-index --quiet --cached HEAD -- &&
241 test $(git show HEAD:unrelated-file) = 2
242'
243
244test_expect_success '--continue tries to commit' '
245 test_tick &&
246 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
247 echo resolved > file1 &&
248 git add file1 &&
249 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
250 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
251 git show HEAD | grep chouette
252'
253
254test_expect_success 'verbose flag is heeded, even after --continue' '
255 git reset --hard HEAD@{1} &&
256 test_tick &&
257 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
258 echo resolved > file1 &&
259 git add file1 &&
260 git rebase --continue > output &&
261 grep "^ file1 | 2 +-$" output
262'
263
264test_expect_success 'multi-squash only fires up editor once' '
265 base=$(git rev-parse HEAD~4) &&
266 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
267 EXPECT_HEADER_COUNT=4 \
268 git rebase -i $base &&
269 test $base = $(git rev-parse HEAD^) &&
270 test 1 = $(git show | grep ONCE | wc -l)
271'
272
273test_expect_success 'multi-fixup does not fire up editor' '
274 git checkout -b multi-fixup E &&
275 base=$(git rev-parse HEAD~4) &&
276 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
277 git rebase -i $base &&
278 test $base = $(git rev-parse HEAD^) &&
279 test 0 = $(git show | grep NEVER | wc -l) &&
280 git checkout to-be-rebased &&
281 git branch -D multi-fixup
282'
283
284test_expect_success 'commit message used after conflict' '
285 git checkout -b conflict-fixup conflict-branch &&
286 base=$(git rev-parse HEAD~4) &&
287 (
288 FAKE_LINES="1 fixup 3 fixup 4" &&
289 export FAKE_LINES &&
290 test_must_fail git rebase -i $base
291 ) &&
292 echo three > conflict &&
293 git add conflict &&
294 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
295 git rebase --continue &&
296 test $base = $(git rev-parse HEAD^) &&
297 test 1 = $(git show | grep ONCE | wc -l) &&
298 git checkout to-be-rebased &&
299 git branch -D conflict-fixup
300'
301
302test_expect_success 'commit message retained after conflict' '
303 git checkout -b conflict-squash conflict-branch &&
304 base=$(git rev-parse HEAD~4) &&
305 (
306 FAKE_LINES="1 fixup 3 squash 4" &&
307 export FAKE_LINES &&
308 test_must_fail git rebase -i $base
309 ) &&
310 echo three > conflict &&
311 git add conflict &&
312 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
313 git rebase --continue &&
314 test $base = $(git rev-parse HEAD^) &&
315 test 2 = $(git show | grep TWICE | wc -l) &&
316 git checkout to-be-rebased &&
317 git branch -D conflict-squash
318'
319
320cat > expect-squash-fixup << EOF
321B
322
323D
324
325ONCE
326EOF
327
328test_expect_success 'squash and fixup generate correct log messages' '
329 git checkout -b squash-fixup E &&
330 base=$(git rev-parse HEAD~4) &&
331 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
332 EXPECT_HEADER_COUNT=4 \
333 git rebase -i $base &&
334 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
335 test_cmp expect-squash-fixup actual-squash-fixup &&
336 git checkout to-be-rebased &&
337 git branch -D squash-fixup
338'
339
340test_expect_success 'squash ignores comments' '
341 git checkout -b skip-comments E &&
342 base=$(git rev-parse HEAD~4) &&
343 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
344 EXPECT_HEADER_COUNT=4 \
345 git rebase -i $base &&
346 test $base = $(git rev-parse HEAD^) &&
347 test 1 = $(git show | grep ONCE | wc -l) &&
348 git checkout to-be-rebased &&
349 git branch -D skip-comments
350'
351
352test_expect_success 'squash ignores blank lines' '
353 git checkout -b skip-blank-lines E &&
354 base=$(git rev-parse HEAD~4) &&
355 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
356 EXPECT_HEADER_COUNT=4 \
357 git rebase -i $base &&
358 test $base = $(git rev-parse HEAD^) &&
359 test 1 = $(git show | grep ONCE | wc -l) &&
360 git checkout to-be-rebased &&
361 git branch -D skip-blank-lines
362'
363
364test_expect_success 'squash works as expected' '
365 git checkout -b squash-works no-conflict-branch &&
366 one=$(git rev-parse HEAD~3) &&
367 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
368 git rebase -i HEAD~3 &&
369 test $one = $(git rev-parse HEAD~2)
370'
371
372test_expect_success 'interrupted squash works as expected' '
373 git checkout -b interrupted-squash conflict-branch &&
374 one=$(git rev-parse HEAD~3) &&
375 (
376 FAKE_LINES="1 squash 3 2" &&
377 export FAKE_LINES &&
378 test_must_fail git rebase -i HEAD~3
379 ) &&
380 (echo one; echo two; echo four) > conflict &&
381 git add conflict &&
382 test_must_fail git rebase --continue &&
383 echo resolved > conflict &&
384 git add conflict &&
385 git rebase --continue &&
386 test $one = $(git rev-parse HEAD~2)
387'
388
389test_expect_success 'interrupted squash works as expected (case 2)' '
390 git checkout -b interrupted-squash2 conflict-branch &&
391 one=$(git rev-parse HEAD~3) &&
392 (
393 FAKE_LINES="3 squash 1 2" &&
394 export FAKE_LINES &&
395 test_must_fail git rebase -i HEAD~3
396 ) &&
397 (echo one; echo four) > conflict &&
398 git add conflict &&
399 test_must_fail git rebase --continue &&
400 (echo one; echo two; echo four) > conflict &&
401 git add conflict &&
402 test_must_fail git rebase --continue &&
403 echo resolved > conflict &&
404 git add conflict &&
405 git rebase --continue &&
406 test $one = $(git rev-parse HEAD~2)
407'
408
409test_expect_success 'ignore patch if in upstream' '
410 HEAD=$(git rev-parse HEAD) &&
411 git checkout -b has-cherry-picked HEAD^ &&
412 echo unrelated > file7 &&
413 git add file7 &&
414 test_tick &&
415 git commit -m "unrelated change" &&
416 git cherry-pick $HEAD &&
417 EXPECT_COUNT=1 git rebase -i $HEAD &&
418 test $HEAD = $(git rev-parse HEAD^)
419'
420
421test_expect_success '--continue tries to commit, even for "edit"' '
422 parent=$(git rev-parse HEAD^) &&
423 test_tick &&
424 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
425 echo edited > file7 &&
426 git add file7 &&
427 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
428 test edited = $(git show HEAD:file7) &&
429 git show HEAD | grep chouette &&
430 test $parent = $(git rev-parse HEAD^)
431'
432
433test_expect_success 'aborted --continue does not squash commits after "edit"' '
434 old=$(git rev-parse HEAD) &&
435 test_tick &&
436 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
437 echo "edited again" > file7 &&
438 git add file7 &&
439 (
440 FAKE_COMMIT_MESSAGE=" " &&
441 export FAKE_COMMIT_MESSAGE &&
442 test_must_fail git rebase --continue
443 ) &&
444 test $old = $(git rev-parse HEAD) &&
445 git rebase --abort
446'
447
448test_expect_success 'auto-amend only edited commits after "edit"' '
449 test_tick &&
450 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
451 echo "edited again" > file7 &&
452 git add file7 &&
453 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
454 echo "and again" > file7 &&
455 git add file7 &&
456 test_tick &&
457 (
458 FAKE_COMMIT_MESSAGE="and again" &&
459 export FAKE_COMMIT_MESSAGE &&
460 test_must_fail git rebase --continue
461 ) &&
462 git rebase --abort
463'
464
465test_expect_success 'rebase a detached HEAD' '
466 grandparent=$(git rev-parse HEAD~2) &&
467 git checkout $(git rev-parse HEAD) &&
468 test_tick &&
469 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
470 test $grandparent = $(git rev-parse HEAD~2)
471'
472
473test_expect_success 'rebase a commit violating pre-commit' '
474
475 mkdir -p .git/hooks &&
476 PRE_COMMIT=.git/hooks/pre-commit &&
477 echo "#!/bin/sh" > $PRE_COMMIT &&
478 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
479 chmod a+x $PRE_COMMIT &&
480 echo "monde! " >> file1 &&
481 test_tick &&
482 test_must_fail git commit -m doesnt-verify file1 &&
483 git commit -m doesnt-verify --no-verify file1 &&
484 test_tick &&
485 FAKE_LINES=2 git rebase -i HEAD~2
486
487'
488
489test_expect_success 'rebase with a file named HEAD in worktree' '
490
491 rm -fr .git/hooks &&
492 git reset --hard &&
493 git checkout -b branch3 A &&
494
495 (
496 GIT_AUTHOR_NAME="Squashed Away" &&
497 export GIT_AUTHOR_NAME &&
498 >HEAD &&
499 git add HEAD &&
500 git commit -m "Add head" &&
501 >BODY &&
502 git add BODY &&
503 git commit -m "Add body"
504 ) &&
505
506 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
507 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
508
509'
510
511test_expect_success 'do "noop" when there is nothing to cherry-pick' '
512
513 git checkout -b branch4 HEAD &&
514 GIT_EDITOR=: git commit --amend \
515 --author="Somebody else <somebody@else.com>"
516 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
517 git rebase -i branch3 &&
518 test $(git rev-parse branch3) = $(git rev-parse branch4)
519
520'
521
522test_expect_success 'submodule rebase setup' '
523 git checkout A &&
524 mkdir sub &&
525 (
526 cd sub && git init && >elif &&
527 git add elif && git commit -m "submodule initial"
528 ) &&
529 echo 1 >file1 &&
530 git add file1 sub
531 test_tick &&
532 git commit -m "One" &&
533 echo 2 >file1 &&
534 test_tick &&
535 git commit -a -m "Two" &&
536 (
537 cd sub && echo 3 >elif &&
538 git commit -a -m "submodule second"
539 ) &&
540 test_tick &&
541 git commit -a -m "Three changes submodule"
542'
543
544test_expect_success 'submodule rebase -i' '
545 FAKE_LINES="1 squash 2 3" git rebase -i A
546'
547
548test_expect_success 'avoid unnecessary reset' '
549 git checkout master &&
550 test-chmtime =123456789 file3 &&
551 git update-index --refresh &&
552 HEAD=$(git rev-parse HEAD) &&
553 git rebase -i HEAD~4 &&
554 test $HEAD = $(git rev-parse HEAD) &&
555 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
556 test 123456789 = $MTIME
557'
558
559test_expect_success 'reword' '
560 git checkout -b reword-branch master &&
561 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
562 git show HEAD | grep "E changed" &&
563 test $(git rev-parse master) != $(git rev-parse HEAD) &&
564 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
565 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
566 git show HEAD^ | grep "D changed" &&
567 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
568 git show HEAD~3 | grep "B changed" &&
569 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
570 git show HEAD~2 | grep "C changed"
571'
572
573test_expect_success 'rebase -i can copy notes' '
574 git config notes.rewrite.rebase true &&
575 git config notes.rewriteRef "refs/notes/*" &&
576 test_commit n1 &&
577 test_commit n2 &&
578 test_commit n3 &&
579 git notes add -m"a note" n3 &&
580 git rebase --onto n1 n2 &&
581 test "a note" = "$(git notes show HEAD)"
582'
583
584cat >expect <<EOF
585an earlier note
586a note
587EOF
588
589test_expect_success 'rebase -i can copy notes over a fixup' '
590 git reset --hard n3 &&
591 git notes add -m"an earlier note" n2 &&
592 GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
593 git notes show > output &&
594 test_cmp expect output
595'
596
597test_expect_success 'rebase while detaching HEAD' '
598 git symbolic-ref HEAD &&
599 grandparent=$(git rev-parse HEAD~2) &&
600 test_tick &&
601 FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
602 test $grandparent = $(git rev-parse HEAD~2) &&
603 test_must_fail git symbolic-ref HEAD
604'
605
606test_tick # Ensure that the rebased commits get a different timestamp.
607test_expect_success 'always cherry-pick with --no-ff' '
608 git checkout no-ff-branch &&
609 git tag original-no-ff-branch &&
610 git rebase -i --no-ff A &&
611 touch empty &&
612 for p in 0 1 2
613 do
614 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
615 git diff HEAD~$p original-no-ff-branch~$p > out &&
616 test_cmp empty out
617 done &&
618 test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
619 git diff HEAD~3 original-no-ff-branch~3 > out &&
620 test_cmp empty out
621'
622
623test_done