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