WIP: grep --substitute <substitution> <pattern>
[git/dscho.git] / t / t3404-rebase-interactive.sh
blob99bae979bce0bd565565c7ef03f07fe7484b3347
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 the repository like this:
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)
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 &&
43 test_commit I file6
44 git checkout -b conflict-branch A &&
45 for n in one two three four
47 test_commit $n conflict
48 done &&
49 git checkout -b no-conflict-branch A &&
50 for n in J K L M
52 test_commit $n file$n
53 done
56 test_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)
63 test_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)
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)
101 cat > expect << EOF
102 diff --git a/file1 b/file1
103 index f70f10e..fd79235 100644
104 --- a/file1
105 +++ b/file1
106 @@ -1 +1 @@
111 cat > expect2 << EOF
112 <<<<<<< HEAD
114 =======
116 >>>>>>> 51047de... G
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' '
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"
148 test_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)
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 test_commit unrelated unrelated-file &&
176 git checkout -b another-branch master &&
177 test_commit J file1 B &&
178 test_merge merge1 to-be-preserved &&
179 test_commit K file1 C &&
180 test_commit L1 file1 D &&
181 git checkout HEAD^ &&
182 test_commit L2 unrelated-file 1 &&
183 test_merge merge2 another-branch &&
184 test_commit M file1 E &&
185 git checkout -b to-be-rebased &&
186 test_tick &&
187 git rebase -i -p --onto branch1 master &&
188 git update-index --refresh &&
189 git diff-files --quiet &&
190 git diff-index --quiet --cached HEAD -- &&
191 test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
192 test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
193 test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
194 test $(git show HEAD~5:file1) = B &&
195 test $(git show HEAD~3:file1) = C &&
196 test $(git show HEAD:file1) = E &&
197 test $(git show HEAD:unrelated-file) = 1
200 test_expect_success 'edit ancestor with -p' '
201 FAKE_LINES="edit 1 2 3 4 5 6 7 9" git rebase -i -p HEAD~3 &&
202 echo 2 > unrelated-file &&
203 test_tick &&
204 git commit -m L2-modified --amend unrelated-file &&
205 git rebase --continue &&
206 git update-index --refresh &&
207 git diff-files --quiet &&
208 git diff-index --quiet --cached HEAD -- &&
209 test $(git show HEAD:unrelated-file) = 2
212 test_expect_success '--continue tries to commit' '
213 test_tick &&
214 test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
215 echo resolved > file1 &&
216 git add file1 &&
217 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
218 test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
219 git show HEAD | grep chouette
222 test_expect_success 'verbose flag is heeded, even after --continue' '
223 git reset --hard HEAD@{1} &&
224 test_tick &&
225 test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
226 echo resolved > file1 &&
227 git add file1 &&
228 git rebase --continue > output &&
229 grep "^ file1 | 2 +-$" output
232 test_expect_success 'multi-squash only fires up editor once' '
233 base=$(git rev-parse HEAD~4) &&
234 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
235 EXPECT_HEADER_COUNT=4 \
236 git rebase -i $base &&
237 test $base = $(git rev-parse HEAD^) &&
238 test 1 = $(git show | grep ONCE | wc -l)
241 test_expect_success 'multi-fixup does not fire up editor' '
242 git checkout -b multi-fixup E &&
243 base=$(git rev-parse HEAD~4) &&
244 FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
245 git rebase -i $base &&
246 test $base = $(git rev-parse HEAD^) &&
247 test 0 = $(git show | grep NEVER | wc -l) &&
248 git checkout to-be-rebased &&
249 git branch -D multi-fixup
252 test_expect_success 'commit message used after conflict' '
253 git checkout -b conflict-fixup conflict-branch &&
254 base=$(git rev-parse HEAD~4) &&
256 FAKE_LINES="1 fixup 3 fixup 4" &&
257 export FAKE_LINES &&
258 test_must_fail git rebase -i $base
259 ) &&
260 echo three > conflict &&
261 git add conflict &&
262 FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
263 git rebase --continue &&
264 test $base = $(git rev-parse HEAD^) &&
265 test 1 = $(git show | grep ONCE | wc -l) &&
266 git checkout to-be-rebased &&
267 git branch -D conflict-fixup
270 test_expect_success 'commit message retained after conflict' '
271 git checkout -b conflict-squash conflict-branch &&
272 base=$(git rev-parse HEAD~4) &&
274 FAKE_LINES="1 fixup 3 squash 4" &&
275 export FAKE_LINES &&
276 test_must_fail git rebase -i $base
277 ) &&
278 echo three > conflict &&
279 git add conflict &&
280 FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
281 git rebase --continue &&
282 test $base = $(git rev-parse HEAD^) &&
283 test 2 = $(git show | grep TWICE | wc -l) &&
284 git checkout to-be-rebased &&
285 git branch -D conflict-squash
288 cat > expect-squash-fixup << EOF
293 ONCE
296 test_expect_success 'squash and fixup generate correct log messages' '
297 git checkout -b squash-fixup E &&
298 base=$(git rev-parse HEAD~4) &&
299 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
300 EXPECT_HEADER_COUNT=4 \
301 git rebase -i $base &&
302 git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
303 test_cmp expect-squash-fixup actual-squash-fixup &&
304 git checkout to-be-rebased &&
305 git branch -D squash-fixup
308 test_expect_success 'squash ignores comments' '
309 git checkout -b skip-comments E &&
310 base=$(git rev-parse HEAD~4) &&
311 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
312 EXPECT_HEADER_COUNT=4 \
313 git rebase -i $base &&
314 test $base = $(git rev-parse HEAD^) &&
315 test 1 = $(git show | grep ONCE | wc -l) &&
316 git checkout to-be-rebased &&
317 git branch -D skip-comments
320 test_expect_success 'squash ignores blank lines' '
321 git checkout -b skip-blank-lines E &&
322 base=$(git rev-parse HEAD~4) &&
323 FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
324 EXPECT_HEADER_COUNT=4 \
325 git rebase -i $base &&
326 test $base = $(git rev-parse HEAD^) &&
327 test 1 = $(git show | grep ONCE | wc -l) &&
328 git checkout to-be-rebased &&
329 git branch -D skip-blank-lines
332 test_expect_success 'squash works as expected' '
333 git checkout -b squash-works no-conflict-branch &&
334 one=$(git rev-parse HEAD~3) &&
335 FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
336 git rebase -i HEAD~3 &&
337 test $one = $(git rev-parse HEAD~2)
340 test_expect_success 'interrupted squash works as expected' '
341 git checkout -b interrupted-squash conflict-branch &&
342 one=$(git rev-parse HEAD~3) &&
344 FAKE_LINES="1 squash 3 2" &&
345 export FAKE_LINES &&
346 test_must_fail git rebase -i HEAD~3
347 ) &&
348 (echo one; echo two; echo four) > conflict &&
349 git add conflict &&
350 test_must_fail git rebase --continue &&
351 echo resolved > conflict &&
352 git add conflict &&
353 git rebase --continue &&
354 test $one = $(git rev-parse HEAD~2)
357 test_expect_success 'interrupted squash works as expected (case 2)' '
358 git checkout -b interrupted-squash2 conflict-branch &&
359 one=$(git rev-parse HEAD~3) &&
361 FAKE_LINES="3 squash 1 2" &&
362 export FAKE_LINES &&
363 test_must_fail git rebase -i HEAD~3
364 ) &&
365 (echo one; echo four) > conflict &&
366 git add conflict &&
367 test_must_fail git rebase --continue &&
368 (echo one; echo two; echo four) > conflict &&
369 git add conflict &&
370 test_must_fail git rebase --continue &&
371 echo resolved > conflict &&
372 git add conflict &&
373 git rebase --continue &&
374 test $one = $(git rev-parse HEAD~2)
377 test_expect_success 'ignore patch if in upstream' '
378 HEAD=$(git rev-parse HEAD) &&
379 git checkout -b has-cherry-picked HEAD^ &&
380 echo unrelated > file7 &&
381 git add file7 &&
382 test_tick &&
383 git commit -m "unrelated change" &&
384 git cherry-pick $HEAD &&
385 EXPECT_COUNT=1 git rebase -i $HEAD &&
386 test $HEAD = $(git rev-parse HEAD^)
389 test_expect_success '--continue tries to commit, even for "edit"' '
390 parent=$(git rev-parse HEAD^) &&
391 test_tick &&
392 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
393 echo edited > file7 &&
394 git add file7 &&
395 FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
396 test edited = $(git show HEAD:file7) &&
397 git show HEAD | grep chouette &&
398 test $parent = $(git rev-parse HEAD^)
401 test_expect_success 'aborted --continue does not squash commits after "edit"' '
402 old=$(git rev-parse HEAD) &&
403 test_tick &&
404 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
405 echo "edited again" > file7 &&
406 git add file7 &&
408 FAKE_COMMIT_MESSAGE=" " &&
409 export FAKE_COMMIT_MESSAGE &&
410 test_must_fail git rebase --continue
411 ) &&
412 test $old = $(git rev-parse HEAD) &&
413 git rebase --abort
416 test_expect_success 'auto-amend only edited commits after "edit"' '
417 test_tick &&
418 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
419 echo "edited again" > file7 &&
420 git add file7 &&
421 FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
422 echo "and again" > file7 &&
423 git add file7 &&
424 test_tick &&
426 FAKE_COMMIT_MESSAGE="and again" &&
427 export FAKE_COMMIT_MESSAGE &&
428 test_must_fail git rebase --continue
429 ) &&
430 git rebase --abort
433 test_expect_success 'rebase a detached HEAD' '
434 grandparent=$(git rev-parse HEAD~2) &&
435 git checkout $(git rev-parse HEAD) &&
436 test_tick &&
437 FAKE_LINES="2 1" git rebase -i HEAD~2 &&
438 test $grandparent = $(git rev-parse HEAD~2)
441 test_expect_success 'rebase a commit violating pre-commit' '
443 mkdir -p .git/hooks &&
444 PRE_COMMIT=.git/hooks/pre-commit &&
445 echo "#!/bin/sh" > $PRE_COMMIT &&
446 echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
447 chmod a+x $PRE_COMMIT &&
448 echo "monde! " >> file1 &&
449 test_tick &&
450 test_must_fail git commit -m doesnt-verify file1 &&
451 git commit -m doesnt-verify --no-verify file1 &&
452 test_tick &&
453 FAKE_LINES=2 git rebase -i HEAD~2
457 test_expect_success 'rebase with a file named HEAD in worktree' '
459 rm -fr .git/hooks &&
460 git reset --hard &&
461 git checkout -b branch3 A &&
464 GIT_AUTHOR_NAME="Squashed Away" &&
465 export GIT_AUTHOR_NAME &&
466 >HEAD &&
467 git add HEAD &&
468 git commit -m "Add head" &&
469 >BODY &&
470 git add BODY &&
471 git commit -m "Add body"
472 ) &&
474 FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
475 test "$(git show -s --pretty=format:%an)" = "Squashed Away"
479 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
481 git checkout -b branch4 HEAD &&
482 GIT_EDITOR=: git commit --amend \
483 --author="Somebody else <somebody@else.com>"
484 test $(git rev-parse branch3) != $(git rev-parse branch4) &&
485 git rebase -i branch3 &&
486 test $(git rev-parse branch3) = $(git rev-parse branch4)
490 test_expect_success 'submodule rebase setup' '
491 git checkout A &&
492 mkdir sub &&
494 cd sub && git init && >elif &&
495 git add elif && git commit -m "submodule initial"
496 ) &&
497 echo 1 >file1 &&
498 git add file1 sub
499 test_tick &&
500 git commit -m "One" &&
501 echo 2 >file1 &&
502 test_tick &&
503 git commit -a -m "Two" &&
505 cd sub && echo 3 >elif &&
506 git commit -a -m "submodule second"
507 ) &&
508 test_tick &&
509 git commit -a -m "Three changes submodule"
512 test_expect_success 'submodule rebase -i' '
513 FAKE_LINES="1 squash 2 3" git rebase -i A
516 test_expect_success 'avoid unnecessary reset' '
517 git checkout master &&
518 test-chmtime =123456789 file3 &&
519 git update-index --refresh &&
520 HEAD=$(git rev-parse HEAD) &&
521 git rebase -i HEAD~4 &&
522 test $HEAD = $(git rev-parse HEAD) &&
523 MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
524 test 123456789 = $MTIME
527 test_expect_success 'reword' '
528 git checkout -b reword-branch master &&
529 FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
530 git show HEAD | grep "E changed" &&
531 test $(git rev-parse master) != $(git rev-parse HEAD) &&
532 test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
533 FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
534 git show HEAD^ | grep "D changed" &&
535 FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
536 git show HEAD~3 | grep "B changed" &&
537 FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
538 git show HEAD~2 | grep "C changed"
541 test_done