3 # Copyright (c) 2007 Johannes E. Schindelin
6 test_description
='git rebase interactive
8 This test runs git rebase "interactively", by faking an edit, and verifies
9 that the result still makes sense.
13 .
"$TEST_DIRECTORY"/lib-rebase.sh
17 # Set up the repository like this:
19 # one - two - three - four (conflict-branch)
21 # A - B - C - D - E (master)
23 # | F - G - H (branch1)
27 # J - K - L - M (no-conflict-branch)
29 # where A, B, D and G all touch file1, and one, two, three, four all
30 # touch file "conflict".
32 test_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 &&
44 git checkout -b conflict-branch A &&
45 for n in one two three four
47 test_commit $n conflict
49 git checkout -b no-conflict-branch A &&
56 test_expect_success
'no changes are a nop' '
57 git checkout branch2 &&
59 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
60 test $(git rev-parse I) = $(git rev-parse HEAD)
63 test_expect_success
'test the [branch] option' '
64 git checkout -b dead-end &&
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)
73 test_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)
81 test_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)
91 test_expect_success
'reflog for the branch shows state before rebase' '
92 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
95 test_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)
102 diff --git a/file1 b/file1
103 index f70f10e..fd79235 100644
119 test_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)
131 test_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
138 test_expect_success
'retain authorship' '
142 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
144 git rebase -i --onto master HEAD^ &&
145 git show HEAD | grep "^Author: Twerp Snog"
148 test_expect_success
'squash' '
149 git reset --hard twerp &&
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)
160 test_expect_success
'retain authorship when squashing' '
161 git show HEAD | grep "^Author: Twerp Snog"
164 test_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)
173 test_expect_success
'preserve merges with -p' '
174 git checkout -b to-be-preserved master^ &&
175 : > unrelated-file &&
176 git add unrelated-file &&
178 git commit -m "unrelated" &&
179 git checkout -b another-branch master &&
182 git commit -m J file1 &&
184 git merge to-be-preserved &&
187 git commit -m K file1 &&
190 git commit -m L1 file1 &&
191 git checkout HEAD^ &&
192 echo 1 > unrelated-file &&
194 git commit -m L2 unrelated-file &&
196 git merge another-branch &&
199 git commit -m M file1 &&
200 git checkout -b to-be-rebased &&
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
215 test_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 &&
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
227 test_expect_success
'--continue tries to commit' '
229 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
230 echo resolved > 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
237 test_expect_success
'verbose flag is heeded, even after --continue' '
238 git reset --hard HEAD@{1} &&
240 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
241 echo resolved > file1 &&
243 git rebase --continue > output &&
244 grep "^ file1 | 2 +-$" output
247 test_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)
256 test_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
267 cat > expect-squash-fixup
<< EOF
275 test_expect_success
'squash and fixup generate correct log messages' '
276 git checkout -b squash-fixup E &&
277 base=$(git rev-parse HEAD~4) &&
278 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
279 EXPECT_HEADER_COUNT=4 \
280 git rebase -i $base &&
281 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
282 test_cmp expect-squash-fixup actual-squash-fixup &&
283 git checkout to-be-rebased &&
284 git branch -D squash-fixup
287 test_expect_success
'squash ignores comments' '
288 git checkout -b skip-comments E &&
289 base=$(git rev-parse HEAD~4) &&
290 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
291 EXPECT_HEADER_COUNT=4 \
292 git rebase -i $base &&
293 test $base = $(git rev-parse HEAD^) &&
294 test 1 = $(git show | grep ONCE | wc -l) &&
295 git checkout to-be-rebased &&
296 git branch -D skip-comments
299 test_expect_success
'squash ignores blank lines' '
300 git checkout -b skip-blank-lines E &&
301 base=$(git rev-parse HEAD~4) &&
302 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
303 EXPECT_HEADER_COUNT=4 \
304 git rebase -i $base &&
305 test $base = $(git rev-parse HEAD^) &&
306 test 1 = $(git show | grep ONCE | wc -l) &&
307 git checkout to-be-rebased &&
308 git branch -D skip-blank-lines
311 test_expect_success
'squash works as expected' '
312 git checkout -b squash-works no-conflict-branch &&
313 one=$(git rev-parse HEAD~3) &&
314 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
315 git rebase -i HEAD~3 &&
316 test $one = $(git rev-parse HEAD~2)
319 test_expect_success
'interrupted squash works as expected' '
320 git checkout -b interrupted-squash conflict-branch &&
321 one=$(git rev-parse HEAD~3) &&
323 FAKE_LINES="1 squash 3 2" &&
325 test_must_fail git rebase -i HEAD~3
327 (echo one; echo two; echo four) > conflict &&
329 test_must_fail git rebase --continue &&
330 echo resolved > conflict &&
332 git rebase --continue &&
333 test $one = $(git rev-parse HEAD~2)
336 test_expect_success
'interrupted squash works as expected (case 2)' '
337 git checkout -b interrupted-squash2 conflict-branch &&
338 one=$(git rev-parse HEAD~3) &&
340 FAKE_LINES="3 squash 1 2" &&
342 test_must_fail git rebase -i HEAD~3
344 (echo one; echo four) > conflict &&
346 test_must_fail git rebase --continue &&
347 (echo one; echo two; echo four) > conflict &&
349 test_must_fail git rebase --continue &&
350 echo resolved > conflict &&
352 git rebase --continue &&
353 test $one = $(git rev-parse HEAD~2)
356 test_expect_success
'ignore patch if in upstream' '
357 HEAD=$(git rev-parse HEAD) &&
358 git checkout -b has-cherry-picked HEAD^ &&
359 echo unrelated > file7 &&
362 git commit -m "unrelated change" &&
363 git cherry-pick $HEAD &&
364 EXPECT_COUNT=1 git rebase -i $HEAD &&
365 test $HEAD = $(git rev-parse HEAD^)
368 test_expect_success
'--continue tries to commit, even for "edit"' '
369 parent=$(git rev-parse HEAD^) &&
371 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
372 echo edited > file7 &&
374 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
375 test edited = $(git show HEAD:file7) &&
376 git show HEAD | grep chouette &&
377 test $parent = $(git rev-parse HEAD^)
380 test_expect_success
'aborted --continue does not squash commits after "edit"' '
381 old=$(git rev-parse HEAD) &&
383 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
384 echo "edited again" > file7 &&
387 FAKE_COMMIT_MESSAGE=" " &&
388 export FAKE_COMMIT_MESSAGE &&
389 test_must_fail git rebase --continue
391 test $old = $(git rev-parse HEAD) &&
395 test_expect_success
'auto-amend only edited commits after "edit"' '
397 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
398 echo "edited again" > file7 &&
400 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
401 echo "and again" > file7 &&
405 FAKE_COMMIT_MESSAGE="and again" &&
406 export FAKE_COMMIT_MESSAGE &&
407 test_must_fail git rebase --continue
412 test_expect_success
'rebase a detached HEAD' '
413 grandparent=$(git rev-parse HEAD~2) &&
414 git checkout $(git rev-parse HEAD) &&
416 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
417 test $grandparent = $(git rev-parse HEAD~2)
420 test_expect_success
'rebase a commit violating pre-commit' '
422 mkdir -p .git/hooks &&
423 PRE_COMMIT=.git/hooks/pre-commit &&
424 echo "#!/bin/sh" > $PRE_COMMIT &&
425 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
426 chmod a+x $PRE_COMMIT &&
427 echo "monde! " >> file1 &&
429 test_must_fail git commit -m doesnt-verify file1 &&
430 git commit -m doesnt-verify --no-verify file1 &&
432 FAKE_LINES=2 git rebase -i HEAD~2
436 test_expect_success
'rebase with a file named HEAD in worktree' '
440 git checkout -b branch3 A &&
443 GIT_AUTHOR_NAME="Squashed Away" &&
444 export GIT_AUTHOR_NAME &&
447 git commit -m "Add head" &&
450 git commit -m "Add body"
453 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
454 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
458 test_expect_success
'do "noop" when there is nothing to cherry-pick' '
460 git checkout -b branch4 HEAD &&
461 GIT_EDITOR=: git commit --amend \
462 --author="Somebody else <somebody@else.com>"
463 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
464 git rebase -i branch3 &&
465 test $(git rev-parse branch3) = $(git rev-parse branch4)
469 test_expect_success
'submodule rebase setup' '
473 cd sub && git init && >elif &&
474 git add elif && git commit -m "submodule initial"
479 git commit -m "One" &&
482 git commit -a -m "Two" &&
484 cd sub && echo 3 >elif &&
485 git commit -a -m "submodule second"
488 git commit -a -m "Three changes submodule"
491 test_expect_success
'submodule rebase -i' '
492 FAKE_LINES="1 squash 2 3" git rebase -i A
495 test_expect_success
'avoid unnecessary reset' '
496 git checkout master &&
497 test-chmtime =123456789 file3 &&
498 git update-index --refresh &&
499 HEAD=$(git rev-parse HEAD) &&
500 git rebase -i HEAD~4 &&
501 test $HEAD = $(git rev-parse HEAD) &&
502 MTIME=$(test-chmtime -v +0 file3 | sed 's
/[^
0-9].
*$
//') &&
503 test 123456789 = $MTIME
506 test_expect_success
'reword' '
507 git checkout -b reword-branch master &&
508 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
509 git show HEAD | grep "E changed" &&
510 test $(git rev-parse master) != $(git rev-parse HEAD) &&
511 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
512 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
513 git show HEAD^ | grep "D changed" &&
514 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
515 git show HEAD~3 | grep "B changed" &&
516 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
517 git show HEAD~2 | grep "C changed"