]> asedeno.scripts.mit.edu Git - git.git/blob - t/t3404-rebase-interactive.sh
8d409156d24a11355c2fa124989527b69a683c53
[git.git] / t / t3404-rebase-interactive.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes E. Schindelin
4 #
5
6 test_description='git rebase interactive
7
8 This test runs git rebase "interactively", by faking an edit, and verifies
9 that the result still makes sense.
10 '
11 . ./test-lib.sh
12
13 . "$TEST_DIRECTORY"/lib-rebase.sh
14
15 set_fake_editor
16
17 # Set up the repository like this:
18 #
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)
28 #  \
29 #    N - O - P                 (no-ff-branch)
30 #
31 # where A, B, D and G all touch file1, and one, two, three, four all
32 # touch file "conflict".
33 #
34 # WARNING: Modifications to the initial repository can change the SHA ID used
35 # in the expect2 file for the 'stop on conflicting pick' test.
36
37
38 test_expect_success 'setup' '
39         test_commit A file1 &&
40         test_commit B file1 &&
41         test_commit C file2 &&
42         test_commit D file1 &&
43         test_commit E file3 &&
44         git checkout -b branch1 A &&
45         test_commit F file4 &&
46         test_commit G file1 &&
47         test_commit H file5 &&
48         git checkout -b branch2 F &&
49         test_commit I file6
50         git checkout -b conflict-branch A &&
51         for n in one two three four
52         do
53                 test_commit $n conflict
54         done &&
55         git checkout -b no-conflict-branch A &&
56         for n in J K L M
57         do
58                 test_commit $n file$n
59         done &&
60         git checkout -b no-ff-branch A &&
61         for n in N O P
62         do
63                 test_commit $n file$n
64         done
65 '
66
67 test_expect_success 'no changes are a nop' '
68         git checkout branch2 &&
69         git rebase -i F &&
70         test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
71         test $(git rev-parse I) = $(git rev-parse HEAD)
72 '
73
74 test_expect_success 'test the [branch] option' '
75         git checkout -b dead-end &&
76         git rm file6 &&
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)
82 '
83
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)
90 '
91
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)
100 '
101
102 test_expect_success 'reflog for the branch shows state before rebase' '
103         test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
104 '
105
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)
110 '
111
112 cat > expect << EOF
113 diff --git a/file1 b/file1
114 index f70f10e..fd79235 100644
115 --- a/file1
116 +++ b/file1
117 @@ -1 +1 @@
118 -A
119 +G
120 EOF
121
122 cat > expect2 << EOF
123 <<<<<<< HEAD
124 D
125 =======
126 G
127 >>>>>>> 5d18e54... G
128 EOF
129
130 test_expect_success 'stop on conflicting pick' '
131         git tag new-branch1 &&
132         test_must_fail git rebase -i master &&
133         test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
134         test_cmp expect .git/rebase-merge/patch &&
135         test_cmp expect2 file1 &&
136         test "$(git diff --name-status |
137                 sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
138         test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
139         test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
140 '
141
142 test_expect_success 'abort' '
143         git rebase --abort &&
144         test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
145         test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
146         ! test -d .git/rebase-merge
147 '
148
149 test_expect_success 'abort with error when new base cannot be checked out' '
150         git rm --cached file1 &&
151         git commit -m "remove file in base" &&
152         test_must_fail git rebase -i master > output 2>&1 &&
153         grep "Untracked working tree file .file1. would be overwritten" \
154                 output &&
155         ! test -d .git/rebase-merge &&
156         git reset --hard HEAD^
157 '
158
159 test_expect_success 'retain authorship' '
160         echo A > file7 &&
161         git add file7 &&
162         test_tick &&
163         GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
164         git tag twerp &&
165         git rebase -i --onto master HEAD^ &&
166         git show HEAD | grep "^Author: Twerp Snog"
167 '
168
169 test_expect_success 'squash' '
170         git reset --hard twerp &&
171         echo B > file7 &&
172         test_tick &&
173         GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
174         echo "******************************" &&
175         FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
176                 git rebase -i --onto master HEAD~2 &&
177         test B = $(cat file7) &&
178         test $(git rev-parse HEAD^) = $(git rev-parse master)
179 '
180
181 test_expect_success 'retain authorship when squashing' '
182         git show HEAD | grep "^Author: Twerp Snog"
183 '
184
185 test_expect_success '-p handles "no changes" gracefully' '
186         HEAD=$(git rev-parse HEAD) &&
187         git rebase -i -p HEAD^ &&
188         git update-index --refresh &&
189         git diff-files --quiet &&
190         git diff-index --quiet --cached HEAD -- &&
191         test $HEAD = $(git rev-parse HEAD)
192 '
193
194 test_expect_success 'preserve merges with -p' '
195         git checkout -b to-be-preserved master^ &&
196         : > unrelated-file &&
197         git add unrelated-file &&
198         test_tick &&
199         git commit -m "unrelated" &&
200         git checkout -b another-branch master &&
201         echo B > file1 &&
202         test_tick &&
203         git commit -m J file1 &&
204         test_tick &&
205         git merge to-be-preserved &&
206         echo C > file1 &&
207         test_tick &&
208         git commit -m K file1 &&
209         echo D > file1 &&
210         test_tick &&
211         git commit -m L1 file1 &&
212         git checkout HEAD^ &&
213         echo 1 > unrelated-file &&
214         test_tick &&
215         git commit -m L2 unrelated-file &&
216         test_tick &&
217         git merge another-branch &&
218         echo E > file1 &&
219         test_tick &&
220         git commit -m M file1 &&
221         git checkout -b to-be-rebased &&
222         test_tick &&
223         git rebase -i -p --onto branch1 master &&
224         git update-index --refresh &&
225         git diff-files --quiet &&
226         git diff-index --quiet --cached HEAD -- &&
227         test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
228         test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
229         test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
230         test $(git show HEAD~5:file1) = B &&
231         test $(git show HEAD~3:file1) = C &&
232         test $(git show HEAD:file1) = E &&
233         test $(git show HEAD:unrelated-file) = 1
234 '
235
236 test_expect_success 'edit ancestor with -p' '
237         FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
238         echo 2 > unrelated-file &&
239         test_tick &&
240         git commit -m L2-modified --amend unrelated-file &&
241         git rebase --continue &&
242         git update-index --refresh &&
243         git diff-files --quiet &&
244         git diff-index --quiet --cached HEAD -- &&
245         test $(git show HEAD:unrelated-file) = 2
246 '
247
248 test_expect_success '--continue tries to commit' '
249         test_tick &&
250         test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
251         echo resolved > file1 &&
252         git add file1 &&
253         FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
254         test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
255         git show HEAD | grep chouette
256 '
257
258 test_expect_success 'verbose flag is heeded, even after --continue' '
259         git reset --hard HEAD@{1} &&
260         test_tick &&
261         test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
262         echo resolved > file1 &&
263         git add file1 &&
264         git rebase --continue > output &&
265         grep "^ file1 |    2 +-$" output
266 '
267
268 test_expect_success 'multi-squash only fires up editor once' '
269         base=$(git rev-parse HEAD~4) &&
270         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
271                 EXPECT_HEADER_COUNT=4 \
272                 git rebase -i $base &&
273         test $base = $(git rev-parse HEAD^) &&
274         test 1 = $(git show | grep ONCE | wc -l)
275 '
276
277 test_expect_success 'multi-fixup does not fire up editor' '
278         git checkout -b multi-fixup E &&
279         base=$(git rev-parse HEAD~4) &&
280         FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
281                 git rebase -i $base &&
282         test $base = $(git rev-parse HEAD^) &&
283         test 0 = $(git show | grep NEVER | wc -l) &&
284         git checkout to-be-rebased &&
285         git branch -D multi-fixup
286 '
287
288 test_expect_success 'commit message used after conflict' '
289         git checkout -b conflict-fixup conflict-branch &&
290         base=$(git rev-parse HEAD~4) &&
291         (
292                 FAKE_LINES="1 fixup 3 fixup 4" &&
293                 export FAKE_LINES &&
294                 test_must_fail git rebase -i $base
295         ) &&
296         echo three > conflict &&
297         git add conflict &&
298         FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
299                 git rebase --continue &&
300         test $base = $(git rev-parse HEAD^) &&
301         test 1 = $(git show | grep ONCE | wc -l) &&
302         git checkout to-be-rebased &&
303         git branch -D conflict-fixup
304 '
305
306 test_expect_success 'commit message retained after conflict' '
307         git checkout -b conflict-squash conflict-branch &&
308         base=$(git rev-parse HEAD~4) &&
309         (
310                 FAKE_LINES="1 fixup 3 squash 4" &&
311                 export FAKE_LINES &&
312                 test_must_fail git rebase -i $base
313         ) &&
314         echo three > conflict &&
315         git add conflict &&
316         FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
317                 git rebase --continue &&
318         test $base = $(git rev-parse HEAD^) &&
319         test 2 = $(git show | grep TWICE | wc -l) &&
320         git checkout to-be-rebased &&
321         git branch -D conflict-squash
322 '
323
324 cat > expect-squash-fixup << EOF
325 B
326
327 D
328
329 ONCE
330 EOF
331
332 test_expect_success 'squash and fixup generate correct log messages' '
333         git checkout -b squash-fixup E &&
334         base=$(git rev-parse HEAD~4) &&
335         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
336                 EXPECT_HEADER_COUNT=4 \
337                 git rebase -i $base &&
338         git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
339         test_cmp expect-squash-fixup actual-squash-fixup &&
340         git checkout to-be-rebased &&
341         git branch -D squash-fixup
342 '
343
344 test_expect_success 'squash ignores comments' '
345         git checkout -b skip-comments E &&
346         base=$(git rev-parse HEAD~4) &&
347         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
348                 EXPECT_HEADER_COUNT=4 \
349                 git rebase -i $base &&
350         test $base = $(git rev-parse HEAD^) &&
351         test 1 = $(git show | grep ONCE | wc -l) &&
352         git checkout to-be-rebased &&
353         git branch -D skip-comments
354 '
355
356 test_expect_success 'squash ignores blank lines' '
357         git checkout -b skip-blank-lines E &&
358         base=$(git rev-parse HEAD~4) &&
359         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
360                 EXPECT_HEADER_COUNT=4 \
361                 git rebase -i $base &&
362         test $base = $(git rev-parse HEAD^) &&
363         test 1 = $(git show | grep ONCE | wc -l) &&
364         git checkout to-be-rebased &&
365         git branch -D skip-blank-lines
366 '
367
368 test_expect_success 'squash works as expected' '
369         git checkout -b squash-works no-conflict-branch &&
370         one=$(git rev-parse HEAD~3) &&
371         FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
372                 git rebase -i HEAD~3 &&
373         test $one = $(git rev-parse HEAD~2)
374 '
375
376 test_expect_success 'interrupted squash works as expected' '
377         git checkout -b interrupted-squash conflict-branch &&
378         one=$(git rev-parse HEAD~3) &&
379         (
380                 FAKE_LINES="1 squash 3 2" &&
381                 export FAKE_LINES &&
382                 test_must_fail git rebase -i HEAD~3
383         ) &&
384         (echo one; echo two; echo four) > conflict &&
385         git add conflict &&
386         test_must_fail git rebase --continue &&
387         echo resolved > conflict &&
388         git add conflict &&
389         git rebase --continue &&
390         test $one = $(git rev-parse HEAD~2)
391 '
392
393 test_expect_success 'interrupted squash works as expected (case 2)' '
394         git checkout -b interrupted-squash2 conflict-branch &&
395         one=$(git rev-parse HEAD~3) &&
396         (
397                 FAKE_LINES="3 squash 1 2" &&
398                 export FAKE_LINES &&
399                 test_must_fail git rebase -i HEAD~3
400         ) &&
401         (echo one; echo four) > conflict &&
402         git add conflict &&
403         test_must_fail git rebase --continue &&
404         (echo one; echo two; echo four) > conflict &&
405         git add conflict &&
406         test_must_fail git rebase --continue &&
407         echo resolved > conflict &&
408         git add conflict &&
409         git rebase --continue &&
410         test $one = $(git rev-parse HEAD~2)
411 '
412
413 test_expect_success 'ignore patch if in upstream' '
414         HEAD=$(git rev-parse HEAD) &&
415         git checkout -b has-cherry-picked HEAD^ &&
416         echo unrelated > file7 &&
417         git add file7 &&
418         test_tick &&
419         git commit -m "unrelated change" &&
420         git cherry-pick $HEAD &&
421         EXPECT_COUNT=1 git rebase -i $HEAD &&
422         test $HEAD = $(git rev-parse HEAD^)
423 '
424
425 test_expect_success '--continue tries to commit, even for "edit"' '
426         parent=$(git rev-parse HEAD^) &&
427         test_tick &&
428         FAKE_LINES="edit 1" git rebase -i HEAD^ &&
429         echo edited > file7 &&
430         git add file7 &&
431         FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
432         test edited = $(git show HEAD:file7) &&
433         git show HEAD | grep chouette &&
434         test $parent = $(git rev-parse HEAD^)
435 '
436
437 test_expect_success 'aborted --continue does not squash commits after "edit"' '
438         old=$(git rev-parse HEAD) &&
439         test_tick &&
440         FAKE_LINES="edit 1" git rebase -i HEAD^ &&
441         echo "edited again" > file7 &&
442         git add file7 &&
443         (
444                 FAKE_COMMIT_MESSAGE=" " &&
445                 export FAKE_COMMIT_MESSAGE &&
446                 test_must_fail git rebase --continue
447         ) &&
448         test $old = $(git rev-parse HEAD) &&
449         git rebase --abort
450 '
451
452 test_expect_success 'auto-amend only edited commits after "edit"' '
453         test_tick &&
454         FAKE_LINES="edit 1" git rebase -i HEAD^ &&
455         echo "edited again" > file7 &&
456         git add file7 &&
457         FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
458         echo "and again" > file7 &&
459         git add file7 &&
460         test_tick &&
461         (
462                 FAKE_COMMIT_MESSAGE="and again" &&
463                 export FAKE_COMMIT_MESSAGE &&
464                 test_must_fail git rebase --continue
465         ) &&
466         git rebase --abort
467 '
468
469 test_expect_success 'rebase a detached HEAD' '
470         grandparent=$(git rev-parse HEAD~2) &&
471         git checkout $(git rev-parse HEAD) &&
472         test_tick &&
473         FAKE_LINES="2 1" git rebase -i HEAD~2 &&
474         test $grandparent = $(git rev-parse HEAD~2)
475 '
476
477 test_expect_success 'rebase a commit violating pre-commit' '
478
479         mkdir -p .git/hooks &&
480         PRE_COMMIT=.git/hooks/pre-commit &&
481         echo "#!/bin/sh" > $PRE_COMMIT &&
482         echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
483         chmod a+x $PRE_COMMIT &&
484         echo "monde! " >> file1 &&
485         test_tick &&
486         test_must_fail git commit -m doesnt-verify file1 &&
487         git commit -m doesnt-verify --no-verify file1 &&
488         test_tick &&
489         FAKE_LINES=2 git rebase -i HEAD~2
490
491 '
492
493 test_expect_success 'rebase with a file named HEAD in worktree' '
494
495         rm -fr .git/hooks &&
496         git reset --hard &&
497         git checkout -b branch3 A &&
498
499         (
500                 GIT_AUTHOR_NAME="Squashed Away" &&
501                 export GIT_AUTHOR_NAME &&
502                 >HEAD &&
503                 git add HEAD &&
504                 git commit -m "Add head" &&
505                 >BODY &&
506                 git add BODY &&
507                 git commit -m "Add body"
508         ) &&
509
510         FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
511         test "$(git show -s --pretty=format:%an)" = "Squashed Away"
512
513 '
514
515 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
516
517         git checkout -b branch4 HEAD &&
518         GIT_EDITOR=: git commit --amend \
519                 --author="Somebody else <somebody@else.com>" 
520         test $(git rev-parse branch3) != $(git rev-parse branch4) &&
521         git rebase -i branch3 &&
522         test $(git rev-parse branch3) = $(git rev-parse branch4)
523
524 '
525
526 test_expect_success 'submodule rebase setup' '
527         git checkout A &&
528         mkdir sub &&
529         (
530                 cd sub && git init && >elif &&
531                 git add elif && git commit -m "submodule initial"
532         ) &&
533         echo 1 >file1 &&
534         git add file1 sub
535         test_tick &&
536         git commit -m "One" &&
537         echo 2 >file1 &&
538         test_tick &&
539         git commit -a -m "Two" &&
540         (
541                 cd sub && echo 3 >elif &&
542                 git commit -a -m "submodule second"
543         ) &&
544         test_tick &&
545         git commit -a -m "Three changes submodule"
546 '
547
548 test_expect_success 'submodule rebase -i' '
549         FAKE_LINES="1 squash 2 3" git rebase -i A
550 '
551
552 test_expect_success 'avoid unnecessary reset' '
553         git checkout master &&
554         test-chmtime =123456789 file3 &&
555         git update-index --refresh &&
556         HEAD=$(git rev-parse HEAD) &&
557         git rebase -i HEAD~4 &&
558         test $HEAD = $(git rev-parse HEAD) &&
559         MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
560         test 123456789 = $MTIME
561 '
562
563 test_expect_success 'reword' '
564         git checkout -b reword-branch master &&
565         FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
566         git show HEAD | grep "E changed" &&
567         test $(git rev-parse master) != $(git rev-parse HEAD) &&
568         test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
569         FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
570         git show HEAD^ | grep "D changed" &&
571         FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
572         git show HEAD~3 | grep "B changed" &&
573         FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
574         git show HEAD~2 | grep "C changed"
575 '
576
577 test_expect_success 'rebase -i can copy notes' '
578         git config notes.rewrite.rebase true &&
579         git config notes.rewriteRef "refs/notes/*" &&
580         test_commit n1 &&
581         test_commit n2 &&
582         test_commit n3 &&
583         git notes add -m"a note" n3 &&
584         git rebase --onto n1 n2 &&
585         test "a note" = "$(git notes show HEAD)"
586 '
587
588 cat >expect <<EOF
589 an earlier note
590 a note
591 EOF
592
593 test_expect_success 'rebase -i can copy notes over a fixup' '
594         git reset --hard n3 &&
595         git notes add -m"an earlier note" n2 &&
596         GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
597         git notes show > output &&
598         test_cmp expect output
599 '
600
601 test_expect_success 'rebase while detaching HEAD' '
602         git symbolic-ref HEAD &&
603         grandparent=$(git rev-parse HEAD~2) &&
604         test_tick &&
605         FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
606         test $grandparent = $(git rev-parse HEAD~2) &&
607         test_must_fail git symbolic-ref HEAD
608 '
609
610 test_tick # Ensure that the rebased commits get a different timestamp.
611 test_expect_success 'always cherry-pick with --no-ff' '
612         git checkout no-ff-branch &&
613         git tag original-no-ff-branch &&
614         git rebase -i --no-ff A &&
615         touch empty &&
616         for p in 0 1 2
617         do
618                 test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
619                 git diff HEAD~$p original-no-ff-branch~$p > out &&
620                 test_cmp empty out
621         done &&
622         test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
623         git diff HEAD~3 original-no-ff-branch~3 > out &&
624         test_cmp empty out
625 '
626
627 test_done