]> asedeno.scripts.mit.edu Git - git.git/blob - t/t3404-rebase-interactive.sh
rebase -i: Improve consistency of commit count in generated commit messages
[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 two branches like this:
18 #
19 # A - B - C - D - E     (master)
20 #   \
21 #     F - G - H         (branch1)
22 #       \
23 #         I             (branch2)
24 #
25 # where A, B, D and G touch the same file.
26
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
39 '
40
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)
45 '
46
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)
55 '
56
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)
63 '
64
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)
73 '
74
75 test_expect_success 'reflog for the branch shows state before rebase' '
76         test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
77 '
78
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)
83 '
84
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 @@
91 -A
92 +G
93 EOF
94
95 cat > expect2 << EOF
96 <<<<<<< HEAD
97 D
98 =======
99 G
100 >>>>>>> 91201e5... G
101 EOF
102
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)
113 '
114
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
120 '
121
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"
130 '
131
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" EXPECT_HEADER_COUNT=2 \
139                 git rebase -i --onto master HEAD~2 &&
140         test B = $(cat file7) &&
141         test $(git rev-parse HEAD^) = $(git rev-parse master)
142 '
143
144 test_expect_success 'retain authorship when squashing' '
145         git show HEAD | grep "^Author: Twerp Snog"
146 '
147
148 test_expect_success '-p handles "no changes" gracefully' '
149         HEAD=$(git rev-parse HEAD) &&
150         git rebase -i -p HEAD^ &&
151         git update-index --refresh &&
152         git diff-files --quiet &&
153         git diff-index --quiet --cached HEAD -- &&
154         test $HEAD = $(git rev-parse HEAD)
155 '
156
157 test_expect_success 'preserve merges with -p' '
158         git checkout -b to-be-preserved master^ &&
159         : > unrelated-file &&
160         git add unrelated-file &&
161         test_tick &&
162         git commit -m "unrelated" &&
163         git checkout -b another-branch master &&
164         echo B > file1 &&
165         test_tick &&
166         git commit -m J file1 &&
167         test_tick &&
168         git merge to-be-preserved &&
169         echo C > file1 &&
170         test_tick &&
171         git commit -m K file1 &&
172         echo D > file1 &&
173         test_tick &&
174         git commit -m L1 file1 &&
175         git checkout HEAD^ &&
176         echo 1 > unrelated-file &&
177         test_tick &&
178         git commit -m L2 unrelated-file &&
179         test_tick &&
180         git merge another-branch &&
181         echo E > file1 &&
182         test_tick &&
183         git commit -m M file1 &&
184         git checkout -b to-be-rebased &&
185         test_tick &&
186         git rebase -i -p --onto branch1 master &&
187         git update-index --refresh &&
188         git diff-files --quiet &&
189         git diff-index --quiet --cached HEAD -- &&
190         test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
191         test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
192         test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
193         test $(git show HEAD~5:file1) = B &&
194         test $(git show HEAD~3:file1) = C &&
195         test $(git show HEAD:file1) = E &&
196         test $(git show HEAD:unrelated-file) = 1
197 '
198
199 test_expect_success 'edit ancestor with -p' '
200         FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
201         echo 2 > unrelated-file &&
202         test_tick &&
203         git commit -m L2-modified --amend unrelated-file &&
204         git rebase --continue &&
205         git update-index --refresh &&
206         git diff-files --quiet &&
207         git diff-index --quiet --cached HEAD -- &&
208         test $(git show HEAD:unrelated-file) = 2
209 '
210
211 test_expect_success '--continue tries to commit' '
212         test_tick &&
213         test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
214         echo resolved > file1 &&
215         git add file1 &&
216         FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
217         test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
218         git show HEAD | grep chouette
219 '
220
221 test_expect_success 'verbose flag is heeded, even after --continue' '
222         git reset --hard HEAD@{1} &&
223         test_tick &&
224         test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
225         echo resolved > file1 &&
226         git add file1 &&
227         git rebase --continue > output &&
228         grep "^ file1 |    2 +-$" output
229 '
230
231 test_expect_success 'multi-squash only fires up editor once' '
232         base=$(git rev-parse HEAD~4) &&
233         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
234                 EXPECT_HEADER_COUNT=4 \
235                 git rebase -i $base &&
236         test $base = $(git rev-parse HEAD^) &&
237         test 1 = $(git show | grep ONCE | wc -l)
238 '
239
240 test_expect_success 'multi-fixup only fires up editor once' '
241         git checkout -b multi-fixup E &&
242         base=$(git rev-parse HEAD~4) &&
243         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
244                 EXPECT_HEADER_COUNT=4 \
245                 git rebase -i $base &&
246         test $base = $(git rev-parse HEAD^) &&
247         test 1 = $(git show | grep ONCE | wc -l) &&
248         git checkout to-be-rebased &&
249         git branch -D multi-fixup
250 '
251
252 cat > expect-squash-fixup << EOF
253 B
254
255 D
256
257 ONCE
258 EOF
259
260 test_expect_success 'squash and fixup generate correct log messages' '
261         git checkout -b squash-fixup E &&
262         base=$(git rev-parse HEAD~4) &&
263         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
264                 EXPECT_HEADER_COUNT=4 \
265                 git rebase -i $base &&
266         git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
267         test_cmp expect-squash-fixup actual-squash-fixup &&
268         git checkout to-be-rebased &&
269         git branch -D squash-fixup
270 '
271
272 test_expect_success 'squash ignores comments' '
273         git checkout -b skip-comments E &&
274         base=$(git rev-parse HEAD~4) &&
275         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
276                 EXPECT_HEADER_COUNT=4 \
277                 git rebase -i $base &&
278         test $base = $(git rev-parse HEAD^) &&
279         test 1 = $(git show | grep ONCE | wc -l) &&
280         git checkout to-be-rebased &&
281         git branch -D skip-comments
282 '
283
284 test_expect_success 'squash ignores blank lines' '
285         git checkout -b skip-blank-lines E &&
286         base=$(git rev-parse HEAD~4) &&
287         FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
288                 EXPECT_HEADER_COUNT=4 \
289                 git rebase -i $base &&
290         test $base = $(git rev-parse HEAD^) &&
291         test 1 = $(git show | grep ONCE | wc -l) &&
292         git checkout to-be-rebased &&
293         git branch -D skip-blank-lines
294 '
295
296 test_expect_success 'squash works as expected' '
297         for n in one two three four
298         do
299                 echo $n >> file$n &&
300                 git add file$n &&
301                 git commit -m $n
302         done &&
303         one=$(git rev-parse HEAD~3) &&
304         FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
305                 git rebase -i HEAD~3 &&
306         test $one = $(git rev-parse HEAD~2)
307 '
308
309 test_expect_success 'interrupted squash works as expected' '
310         for n in one two three four
311         do
312                 echo $n >> conflict &&
313                 git add conflict &&
314                 git commit -m $n
315         done &&
316         one=$(git rev-parse HEAD~3) &&
317         (
318                 FAKE_LINES="1 squash 3 2" &&
319                 export FAKE_LINES &&
320                 test_must_fail git rebase -i HEAD~3
321         ) &&
322         (echo one; echo two; echo four) > conflict &&
323         git add conflict &&
324         test_must_fail git rebase --continue &&
325         echo resolved > conflict &&
326         git add conflict &&
327         git rebase --continue &&
328         test $one = $(git rev-parse HEAD~2)
329 '
330
331 test_expect_success 'interrupted squash works as expected (case 2)' '
332         for n in one two three four
333         do
334                 echo $n >> conflict &&
335                 git add conflict &&
336                 git commit -m $n
337         done &&
338         one=$(git rev-parse HEAD~3) &&
339         (
340                 FAKE_LINES="3 squash 1 2" &&
341                 export FAKE_LINES &&
342                 test_must_fail git rebase -i HEAD~3
343         ) &&
344         (echo one; echo four) > conflict &&
345         git add conflict &&
346         test_must_fail git rebase --continue &&
347         (echo one; echo two; echo four) > conflict &&
348         git add conflict &&
349         test_must_fail git rebase --continue &&
350         echo resolved > conflict &&
351         git add conflict &&
352         git rebase --continue &&
353         test $one = $(git rev-parse HEAD~2)
354 '
355
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 &&
360         git add file7 &&
361         test_tick &&
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^)
366 '
367
368 test_expect_success '--continue tries to commit, even for "edit"' '
369         parent=$(git rev-parse HEAD^) &&
370         test_tick &&
371         FAKE_LINES="edit 1" git rebase -i HEAD^ &&
372         echo edited > file7 &&
373         git add 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^)
378 '
379
380 test_expect_success 'aborted --continue does not squash commits after "edit"' '
381         old=$(git rev-parse HEAD) &&
382         test_tick &&
383         FAKE_LINES="edit 1" git rebase -i HEAD^ &&
384         echo "edited again" > file7 &&
385         git add file7 &&
386         (
387                 FAKE_COMMIT_MESSAGE=" " &&
388                 export FAKE_COMMIT_MESSAGE &&
389                 test_must_fail git rebase --continue
390         ) &&
391         test $old = $(git rev-parse HEAD) &&
392         git rebase --abort
393 '
394
395 test_expect_success 'auto-amend only edited commits after "edit"' '
396         test_tick &&
397         FAKE_LINES="edit 1" git rebase -i HEAD^ &&
398         echo "edited again" > file7 &&
399         git add file7 &&
400         FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
401         echo "and again" > file7 &&
402         git add file7 &&
403         test_tick &&
404         (
405                 FAKE_COMMIT_MESSAGE="and again" &&
406                 export FAKE_COMMIT_MESSAGE &&
407                 test_must_fail git rebase --continue
408         ) &&
409         git rebase --abort
410 '
411
412 test_expect_success 'rebase a detached HEAD' '
413         grandparent=$(git rev-parse HEAD~2) &&
414         git checkout $(git rev-parse HEAD) &&
415         test_tick &&
416         FAKE_LINES="2 1" git rebase -i HEAD~2 &&
417         test $grandparent = $(git rev-parse HEAD~2)
418 '
419
420 test_expect_success 'rebase a commit violating pre-commit' '
421
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 &&
428         test_tick &&
429         test_must_fail git commit -m doesnt-verify file1 &&
430         git commit -m doesnt-verify --no-verify file1 &&
431         test_tick &&
432         FAKE_LINES=2 git rebase -i HEAD~2
433
434 '
435
436 test_expect_success 'rebase with a file named HEAD in worktree' '
437
438         rm -fr .git/hooks &&
439         git reset --hard &&
440         git checkout -b branch3 A &&
441
442         (
443                 GIT_AUTHOR_NAME="Squashed Away" &&
444                 export GIT_AUTHOR_NAME &&
445                 >HEAD &&
446                 git add HEAD &&
447                 git commit -m "Add head" &&
448                 >BODY &&
449                 git add BODY &&
450                 git commit -m "Add body"
451         ) &&
452
453         FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
454         test "$(git show -s --pretty=format:%an)" = "Squashed Away"
455
456 '
457
458 test_expect_success 'do "noop" when there is nothing to cherry-pick' '
459
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)
466
467 '
468
469 test_expect_success 'submodule rebase setup' '
470         git checkout A &&
471         mkdir sub &&
472         (
473                 cd sub && git init && >elif &&
474                 git add elif && git commit -m "submodule initial"
475         ) &&
476         echo 1 >file1 &&
477         git add file1 sub
478         test_tick &&
479         git commit -m "One" &&
480         echo 2 >file1 &&
481         test_tick &&
482         git commit -a -m "Two" &&
483         (
484                 cd sub && echo 3 >elif &&
485                 git commit -a -m "submodule second"
486         ) &&
487         test_tick &&
488         git commit -a -m "Three changes submodule"
489 '
490
491 test_expect_success 'submodule rebase -i' '
492         FAKE_LINES="1 squash 2 3" git rebase -i A
493 '
494
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
504 '
505
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"
518 '
519
520 test_done