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 two branches like this:
25 # where B, D and G touch the same file.
27 test_expect_success 'setup' '
35 git commit -m B file1 &&
42 git commit -m D file1 &&
47 git checkout -b branch1 A &&
55 git commit -m G file1 &&
60 git checkout -b branch2 F &&
68 test_expect_success 'no changes are a nop' '
70 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
71 test $(git rev-parse I) = $(git rev-parse HEAD)
74 test_expect_success 'test the [branch] option' '
75 git checkout -b dead-end &&
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)
84 test_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)
92 test_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)
102 test_expect_success 'reflog for the branch shows state before rebase' '
103 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
106 test_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)
113 diff --git a/file1 b/file1
114 index e69de29..00750ed 100644
129 test_expect_success 'stop on conflicting pick' '
130 git tag new-branch1 &&
131 test_must_fail git rebase -i master &&
132 test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
133 test_cmp expect .git/rebase-merge/patch &&
134 test_cmp expect2 file1 &&
135 test "$(git diff --name-status |
136 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
137 test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
138 test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
141 test_expect_success 'abort' '
142 git rebase --abort &&
143 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
144 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
145 ! test -d .git/rebase-merge
148 test_expect_success 'retain authorship' '
152 GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
154 git rebase -i --onto master HEAD^ &&
155 git show HEAD | grep "^Author: Twerp Snog"
158 test_expect_success 'squash' '
159 git reset --hard twerp &&
162 GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
163 echo "******************************" &&
164 FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
165 test B = $(cat file7) &&
166 test $(git rev-parse HEAD^) = $(git rev-parse master)
169 test_expect_success 'retain authorship when squashing' '
170 git show HEAD | grep "^Author: Twerp Snog"
173 test_expect_success '-p handles "no changes" gracefully' '
174 HEAD=$(git rev-parse HEAD) &&
175 git rebase -i -p HEAD^ &&
176 git update-index --refresh &&
177 git diff-files --quiet &&
178 git diff-index --quiet --cached HEAD -- &&
179 test $HEAD = $(git rev-parse HEAD)
182 test_expect_success 'preserve merges with -p' '
183 git checkout -b to-be-preserved master^ &&
184 : > unrelated-file &&
185 git add unrelated-file &&
187 git commit -m "unrelated" &&
188 git checkout -b another-branch master &&
191 git commit -m J file1 &&
193 git merge to-be-preserved &&
196 git commit -m K file1 &&
199 git commit -m L1 file1 &&
200 git checkout HEAD^ &&
201 echo 1 > unrelated-file &&
203 git commit -m L2 unrelated-file &&
205 git merge another-branch &&
208 git commit -m M file1 &&
209 git checkout -b to-be-rebased &&
211 git rebase -i -p --onto branch1 master &&
212 git update-index --refresh &&
213 git diff-files --quiet &&
214 git diff-index --quiet --cached HEAD -- &&
215 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
216 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
217 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
218 test $(git show HEAD~5:file1) = B &&
219 test $(git show HEAD~3:file1) = C &&
220 test $(git show HEAD:file1) = E &&
221 test $(git show HEAD:unrelated-file) = 1
224 test_expect_success 'edit ancestor with -p' '
225 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
226 echo 2 > unrelated-file &&
228 git commit -m L2-modified --amend unrelated-file &&
229 git rebase --continue &&
230 git update-index --refresh &&
231 git diff-files --quiet &&
232 git diff-index --quiet --cached HEAD -- &&
233 test $(git show HEAD:unrelated-file) = 2
236 test_expect_success '--continue tries to commit' '
238 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
239 echo resolved > file1 &&
241 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
242 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
243 git show HEAD | grep chouette
246 test_expect_success 'verbose flag is heeded, even after --continue' '
247 git reset --hard HEAD@{1} &&
249 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
250 echo resolved > file1 &&
252 git rebase --continue > output &&
253 grep "^ file1 | 2 +-$" output
256 test_expect_success 'multi-squash only fires up editor once' '
257 base=$(git rev-parse HEAD~4) &&
258 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
259 git rebase -i $base &&
260 test $base = $(git rev-parse HEAD^) &&
261 test 1 = $(git show | grep ONCE | wc -l)
264 test_expect_success 'squash works as expected' '
265 for n in one two three four
271 one=$(git rev-parse HEAD~3) &&
272 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
273 test $one = $(git rev-parse HEAD~2)
276 test_expect_success 'interrupted squash works as expected' '
277 for n in one two three four
279 echo $n >> conflict &&
283 one=$(git rev-parse HEAD~3) &&
285 FAKE_LINES="1 squash 3 2" &&
287 test_must_fail git rebase -i HEAD~3
289 (echo one; echo two; echo four) > conflict &&
291 test_must_fail git rebase --continue &&
292 echo resolved > conflict &&
294 git rebase --continue &&
295 test $one = $(git rev-parse HEAD~2)
298 test_expect_success 'interrupted squash works as expected (case 2)' '
299 for n in one two three four
301 echo $n >> conflict &&
305 one=$(git rev-parse HEAD~3) &&
307 FAKE_LINES="3 squash 1 2" &&
309 test_must_fail git rebase -i HEAD~3
311 (echo one; echo four) > conflict &&
313 test_must_fail git rebase --continue &&
314 (echo one; echo two; echo four) > conflict &&
316 test_must_fail git rebase --continue &&
317 echo resolved > conflict &&
319 git rebase --continue &&
320 test $one = $(git rev-parse HEAD~2)
323 test_expect_success 'ignore patch if in upstream' '
324 HEAD=$(git rev-parse HEAD) &&
325 git checkout -b has-cherry-picked HEAD^ &&
326 echo unrelated > file7 &&
329 git commit -m "unrelated change" &&
330 git cherry-pick $HEAD &&
331 EXPECT_COUNT=1 git rebase -i $HEAD &&
332 test $HEAD = $(git rev-parse HEAD^)
335 test_expect_success '--continue tries to commit, even for "edit"' '
336 parent=$(git rev-parse HEAD^) &&
338 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
339 echo edited > file7 &&
341 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
342 test edited = $(git show HEAD:file7) &&
343 git show HEAD | grep chouette &&
344 test $parent = $(git rev-parse HEAD^)
347 test_expect_success 'aborted --continue does not squash commits after "edit"' '
348 old=$(git rev-parse HEAD) &&
350 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
351 echo "edited again" > file7 &&
354 FAKE_COMMIT_MESSAGE=" " &&
355 export FAKE_COMMIT_MESSAGE &&
356 test_must_fail git rebase --continue
358 test $old = $(git rev-parse HEAD) &&
362 test_expect_success 'auto-amend only edited commits after "edit"' '
364 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
365 echo "edited again" > file7 &&
367 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
368 echo "and again" > file7 &&
372 FAKE_COMMIT_MESSAGE="and again" &&
373 export FAKE_COMMIT_MESSAGE &&
374 test_must_fail git rebase --continue
379 test_expect_success 'rebase a detached HEAD' '
380 grandparent=$(git rev-parse HEAD~2) &&
381 git checkout $(git rev-parse HEAD) &&
383 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
384 test $grandparent = $(git rev-parse HEAD~2)
387 test_expect_success 'rebase a commit violating pre-commit' '
389 mkdir -p .git/hooks &&
390 PRE_COMMIT=.git/hooks/pre-commit &&
391 echo "#!/bin/sh" > $PRE_COMMIT &&
392 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
393 chmod a+x $PRE_COMMIT &&
394 echo "monde! " >> file1 &&
396 test_must_fail git commit -m doesnt-verify file1 &&
397 git commit -m doesnt-verify --no-verify file1 &&
399 FAKE_LINES=2 git rebase -i HEAD~2
403 test_expect_success 'rebase with a file named HEAD in worktree' '
407 git checkout -b branch3 A &&
410 GIT_AUTHOR_NAME="Squashed Away" &&
411 export GIT_AUTHOR_NAME &&
414 git commit -m "Add head" &&
417 git commit -m "Add body"
420 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
421 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
425 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
427 git checkout -b branch4 HEAD &&
428 GIT_EDITOR=: git commit --amend \
429 --author="Somebody else <somebody@else.com>"
430 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
431 git rebase -i branch3 &&
432 test $(git rev-parse branch3) = $(git rev-parse branch4)
436 test_expect_success 'submodule rebase setup' '
440 cd sub && git init && >elif &&
441 git add elif && git commit -m "submodule initial"
446 git commit -m "One" &&
449 git commit -a -m "Two" &&
451 cd sub && echo 3 >elif &&
452 git commit -a -m "submodule second"
455 git commit -a -m "Three changes submodule"
458 test_expect_success 'submodule rebase -i' '
459 FAKE_LINES="1 squash 2 3" git rebase -i A
462 test_expect_success 'avoid unnecessary reset' '
463 git checkout master &&
464 test-chmtime =123456789 file3 &&
465 git update-index --refresh &&
466 HEAD=$(git rev-parse HEAD) &&
467 git rebase -i HEAD~4 &&
468 test $HEAD = $(git rev-parse HEAD) &&
469 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
470 test 123456789 = $MTIME