rebase-i: Ignore comments and blank lines in peek_next_command
[git/dscho.git] / t / t3404-rebase-interactive.sh
blobd9382e41d31c6f469095eb35e449a46fd2cf3eb5
1 #!/bin/sh
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.
11 . ./test-lib.sh
13 . "$TEST_DIRECTORY"/lib-rebase.sh
15 set_fake_editor
17 # set up two branches like this:
19 # A - B - C - D - E (master)
20 # \
21 # F - G - H (branch1)
22 # \
23 # I (branch2)
25 # where A, B, D and G touch the same file.
27 test_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
41 test_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)
47 test_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)
57 test_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)
65 test_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)
75 test_expect_success 'reflog for the branch shows state before rebase' '
76 test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
79 test_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)
85 cat > expect << EOF
86 diff --git a/file1 b/file1
87 index f70f10e..fd79235 100644
88 --- a/file1
89 +++ b/file1
90 @@ -1 +1 @@
93 EOF
95 cat > expect2 << EOF
96 <<<<<<< HEAD
98 =======
100 >>>>>>> 91201e5... G
103 test_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)
115 test_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
122 test_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"
132 test_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" git rebase -i --onto master HEAD~2 &&
139 test B = $(cat file7) &&
140 test $(git rev-parse HEAD^) = $(git rev-parse master)
143 test_expect_success 'retain authorship when squashing' '
144 git show HEAD | grep "^Author: Twerp Snog"
147 test_expect_success '-p handles "no changes" gracefully' '
148 HEAD=$(git rev-parse HEAD) &&
149 git rebase -i -p HEAD^ &&
150 git update-index --refresh &&
151 git diff-files --quiet &&
152 git diff-index --quiet --cached HEAD -- &&
153 test $HEAD = $(git rev-parse HEAD)
156 test_expect_success 'preserve merges with -p' '
157 git checkout -b to-be-preserved master^ &&
158 : > unrelated-file &&
159 git add unrelated-file &&
160 test_tick &&
161 git commit -m "unrelated" &&
162 git checkout -b another-branch master &&
163 echo B > file1 &&
164 test_tick &&
165 git commit -m J file1 &&
166 test_tick &&
167 git merge to-be-preserved &&
168 echo C > file1 &&
169 test_tick &&
170 git commit -m K file1 &&
171 echo D > file1 &&
172 test_tick &&
173 git commit -m L1 file1 &&
174 git checkout HEAD^ &&
175 echo 1 > unrelated-file &&
176 test_tick &&
177 git commit -m L2 unrelated-file &&
178 test_tick &&
179 git merge another-branch &&
180 echo E > file1 &&
181 test_tick &&
182 git commit -m M file1 &&
183 git checkout -b to-be-rebased &&
184 test_tick &&
185 git rebase -i -p --onto branch1 master &&
186 git update-index --refresh &&
187 git diff-files --quiet &&
188 git diff-index --quiet --cached HEAD -- &&
189 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
190 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
191 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
192 test $(git show HEAD~5:file1) = B &&
193 test $(git show HEAD~3:file1) = C &&
194 test $(git show HEAD:file1) = E &&
195 test $(git show HEAD:unrelated-file) = 1
198 test_expect_success 'edit ancestor with -p' '
199 FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
200 echo 2 > unrelated-file &&
201 test_tick &&
202 git commit -m L2-modified --amend unrelated-file &&
203 git rebase --continue &&
204 git update-index --refresh &&
205 git diff-files --quiet &&
206 git diff-index --quiet --cached HEAD -- &&
207 test $(git show HEAD:unrelated-file) = 2
210 test_expect_success '--continue tries to commit' '
211 test_tick &&
212 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
213 echo resolved > file1 &&
214 git add file1 &&
215 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
216 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
217 git show HEAD | grep chouette
220 test_expect_success 'verbose flag is heeded, even after --continue' '
221 git reset --hard HEAD@{1} &&
222 test_tick &&
223 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
224 echo resolved > file1 &&
225 git add file1 &&
226 git rebase --continue > output &&
227 grep "^ file1 | 2 +-$" output
230 test_expect_success 'multi-squash only fires up editor once' '
231 base=$(git rev-parse HEAD~4) &&
232 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
233 git rebase -i $base &&
234 test $base = $(git rev-parse HEAD^) &&
235 test 1 = $(git show | grep ONCE | wc -l)
238 test_expect_success 'multi-fixup only fires up editor once' '
239 git checkout -b multi-fixup E &&
240 base=$(git rev-parse HEAD~4) &&
241 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
242 git rebase -i $base &&
243 test $base = $(git rev-parse HEAD^) &&
244 test 1 = $(git show | grep ONCE | wc -l) &&
245 git checkout to-be-rebased &&
246 git branch -D multi-fixup
249 cat > expect-squash-fixup << EOF
254 ONCE
257 test_expect_success 'squash and fixup generate correct log messages' '
258 git checkout -b squash-fixup E &&
259 base=$(git rev-parse HEAD~4) &&
260 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
261 git rebase -i $base &&
262 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
263 test_cmp expect-squash-fixup actual-squash-fixup &&
264 git checkout to-be-rebased &&
265 git branch -D squash-fixup
268 test_expect_success 'squash ignores comments' '
269 git checkout -b skip-comments E &&
270 base=$(git rev-parse HEAD~4) &&
271 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
272 EXPECT_HEADER_COUNT=4 \
273 git rebase -i $base &&
274 test $base = $(git rev-parse HEAD^) &&
275 test 1 = $(git show | grep ONCE | wc -l) &&
276 git checkout to-be-rebased &&
277 git branch -D skip-comments
280 test_expect_success 'squash ignores blank lines' '
281 git checkout -b skip-blank-lines E &&
282 base=$(git rev-parse HEAD~4) &&
283 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
284 EXPECT_HEADER_COUNT=4 \
285 git rebase -i $base &&
286 test $base = $(git rev-parse HEAD^) &&
287 test 1 = $(git show | grep ONCE | wc -l) &&
288 git checkout to-be-rebased &&
289 git branch -D skip-blank-lines
292 test_expect_success 'squash works as expected' '
293 for n in one two three four
295 echo $n >> file$n &&
296 git add file$n &&
297 git commit -m $n
298 done &&
299 one=$(git rev-parse HEAD~3) &&
300 FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
301 test $one = $(git rev-parse HEAD~2)
304 test_expect_success 'interrupted squash works as expected' '
305 for n in one two three four
307 echo $n >> conflict &&
308 git add conflict &&
309 git commit -m $n
310 done &&
311 one=$(git rev-parse HEAD~3) &&
313 FAKE_LINES="1 squash 3 2" &&
314 export FAKE_LINES &&
315 test_must_fail git rebase -i HEAD~3
316 ) &&
317 (echo one; echo two; echo four) > conflict &&
318 git add conflict &&
319 test_must_fail git rebase --continue &&
320 echo resolved > conflict &&
321 git add conflict &&
322 git rebase --continue &&
323 test $one = $(git rev-parse HEAD~2)
326 test_expect_success 'interrupted squash works as expected (case 2)' '
327 for n in one two three four
329 echo $n >> conflict &&
330 git add conflict &&
331 git commit -m $n
332 done &&
333 one=$(git rev-parse HEAD~3) &&
335 FAKE_LINES="3 squash 1 2" &&
336 export FAKE_LINES &&
337 test_must_fail git rebase -i HEAD~3
338 ) &&
339 (echo one; echo four) > conflict &&
340 git add conflict &&
341 test_must_fail git rebase --continue &&
342 (echo one; echo two; echo four) > conflict &&
343 git add conflict &&
344 test_must_fail git rebase --continue &&
345 echo resolved > conflict &&
346 git add conflict &&
347 git rebase --continue &&
348 test $one = $(git rev-parse HEAD~2)
351 test_expect_success 'ignore patch if in upstream' '
352 HEAD=$(git rev-parse HEAD) &&
353 git checkout -b has-cherry-picked HEAD^ &&
354 echo unrelated > file7 &&
355 git add file7 &&
356 test_tick &&
357 git commit -m "unrelated change" &&
358 git cherry-pick $HEAD &&
359 EXPECT_COUNT=1 git rebase -i $HEAD &&
360 test $HEAD = $(git rev-parse HEAD^)
363 test_expect_success '--continue tries to commit, even for "edit"' '
364 parent=$(git rev-parse HEAD^) &&
365 test_tick &&
366 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
367 echo edited > file7 &&
368 git add file7 &&
369 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
370 test edited = $(git show HEAD:file7) &&
371 git show HEAD | grep chouette &&
372 test $parent = $(git rev-parse HEAD^)
375 test_expect_success 'aborted --continue does not squash commits after "edit"' '
376 old=$(git rev-parse HEAD) &&
377 test_tick &&
378 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
379 echo "edited again" > file7 &&
380 git add file7 &&
382 FAKE_COMMIT_MESSAGE=" " &&
383 export FAKE_COMMIT_MESSAGE &&
384 test_must_fail git rebase --continue
385 ) &&
386 test $old = $(git rev-parse HEAD) &&
387 git rebase --abort
390 test_expect_success 'auto-amend only edited commits after "edit"' '
391 test_tick &&
392 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
393 echo "edited again" > file7 &&
394 git add file7 &&
395 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
396 echo "and again" > file7 &&
397 git add file7 &&
398 test_tick &&
400 FAKE_COMMIT_MESSAGE="and again" &&
401 export FAKE_COMMIT_MESSAGE &&
402 test_must_fail git rebase --continue
403 ) &&
404 git rebase --abort
407 test_expect_success 'rebase a detached HEAD' '
408 grandparent=$(git rev-parse HEAD~2) &&
409 git checkout $(git rev-parse HEAD) &&
410 test_tick &&
411 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
412 test $grandparent = $(git rev-parse HEAD~2)
415 test_expect_success 'rebase a commit violating pre-commit' '
417 mkdir -p .git/hooks &&
418 PRE_COMMIT=.git/hooks/pre-commit &&
419 echo "#!/bin/sh" > $PRE_COMMIT &&
420 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
421 chmod a+x $PRE_COMMIT &&
422 echo "monde! " >> file1 &&
423 test_tick &&
424 test_must_fail git commit -m doesnt-verify file1 &&
425 git commit -m doesnt-verify --no-verify file1 &&
426 test_tick &&
427 FAKE_LINES=2 git rebase -i HEAD~2
431 test_expect_success 'rebase with a file named HEAD in worktree' '
433 rm -fr .git/hooks &&
434 git reset --hard &&
435 git checkout -b branch3 A &&
438 GIT_AUTHOR_NAME="Squashed Away" &&
439 export GIT_AUTHOR_NAME &&
440 >HEAD &&
441 git add HEAD &&
442 git commit -m "Add head" &&
443 >BODY &&
444 git add BODY &&
445 git commit -m "Add body"
446 ) &&
448 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
449 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
453 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
455 git checkout -b branch4 HEAD &&
456 GIT_EDITOR=: git commit --amend \
457 --author="Somebody else <somebody@else.com>"
458 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
459 git rebase -i branch3 &&
460 test $(git rev-parse branch3) = $(git rev-parse branch4)
464 test_expect_success 'submodule rebase setup' '
465 git checkout A &&
466 mkdir sub &&
468 cd sub && git init && >elif &&
469 git add elif && git commit -m "submodule initial"
470 ) &&
471 echo 1 >file1 &&
472 git add file1 sub
473 test_tick &&
474 git commit -m "One" &&
475 echo 2 >file1 &&
476 test_tick &&
477 git commit -a -m "Two" &&
479 cd sub && echo 3 >elif &&
480 git commit -a -m "submodule second"
481 ) &&
482 test_tick &&
483 git commit -a -m "Three changes submodule"
486 test_expect_success 'submodule rebase -i' '
487 FAKE_LINES="1 squash 2 3" git rebase -i A
490 test_expect_success 'avoid unnecessary reset' '
491 git checkout master &&
492 test-chmtime =123456789 file3 &&
493 git update-index --refresh &&
494 HEAD=$(git rev-parse HEAD) &&
495 git rebase -i HEAD~4 &&
496 test $HEAD = $(git rev-parse HEAD) &&
497 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
498 test 123456789 = $MTIME
501 test_expect_success 'reword' '
502 git checkout -b reword-branch master &&
503 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
504 git show HEAD | grep "E changed" &&
505 test $(git rev-parse master) != $(git rev-parse HEAD) &&
506 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
507 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
508 git show HEAD^ | grep "D changed" &&
509 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
510 git show HEAD~3 | grep "B changed" &&
511 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
512 git show HEAD~2 | grep "C changed"
515 test_done