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