Merge branch 'cw/amend-commit-without-message'
[git] / t / t7501-commit.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
4 #
5
6 # FIXME: Test the various index usages, -i and -o, test reflog,
7 # signoff
8
9 test_description='git commit'
10 . ./test-lib.sh
11 . "$TEST_DIRECTORY/diff-lib.sh"
12
13 author='The Real Author <someguy@his.email.org>'
14
15 test_tick
16
17 test_expect_success 'initial status' '
18         echo bongo bongo >file &&
19         git add file &&
20         git status >actual &&
21         test_i18ngrep "Initial commit" actual
22 '
23
24 test_expect_success 'fail initial amend' '
25         test_must_fail git commit --amend
26 '
27
28 test_expect_success 'setup: initial commit' '
29         git commit -m initial
30 '
31
32 test_expect_success '-m and -F do not mix' '
33         git checkout HEAD file && echo >>file && git add file &&
34         test_must_fail git commit -m foo -m bar -F file
35 '
36
37 test_expect_success '-m and -C do not mix' '
38         git checkout HEAD file && echo >>file && git add file &&
39         test_must_fail git commit -C HEAD -m illegal
40 '
41
42 test_expect_success 'paths and -a do not mix' '
43         echo King of the bongo >file &&
44         test_must_fail git commit -m foo -a file
45 '
46
47 test_expect_success PERL 'can use paths with --interactive' '
48         echo bong-o-bong >file &&
49         # 2: update, 1:st path, that is all, 7: quit
50         ( echo 2; echo 1; echo; echo 7 ) |
51         git commit -m foo --interactive file &&
52         git reset --hard HEAD^
53 '
54
55 test_expect_success 'using invalid commit with -C' '
56         test_must_fail git commit -C bogus
57 '
58
59 test_expect_success 'nothing to commit' '
60         test_must_fail git commit -m initial
61 '
62
63 test_expect_success 'setup: non-initial commit' '
64         echo bongo bongo bongo >file &&
65         git commit -m next -a
66 '
67
68 test_expect_success 'commit message from non-existing file' '
69         echo more bongo: bongo bongo bongo bongo >file &&
70         test_must_fail git commit -F gah -a
71 '
72
73 test_expect_success 'empty commit message' '
74         # Empty except stray tabs and spaces on a few lines.
75         sed -e "s/@//g" >msg <<-\EOF &&
76                 @               @
77                 @@
78                 @  @
79                 @Signed-off-by: hula@
80         EOF
81         test_must_fail git commit -F msg -a
82 '
83
84 test_expect_success 'template "emptyness" check does not kick in with -F' '
85         git checkout HEAD file && echo >>file && git add file &&
86         git commit -t file -F file
87 '
88
89 test_expect_success 'template "emptyness" check' '
90         git checkout HEAD file && echo >>file && git add file &&
91         test_must_fail git commit -t file 2>err &&
92         test_i18ngrep "did not edit" err
93 '
94
95 test_expect_success 'setup: commit message from file' '
96         git checkout HEAD file && echo >>file && git add file &&
97         echo this is the commit message, coming from a file >msg &&
98         git commit -F msg -a
99 '
100
101 test_expect_success 'amend commit' '
102         cat >editor <<-\EOF &&
103         #!/bin/sh
104         sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
105         mv "$1-" "$1"
106         EOF
107         chmod 755 editor &&
108         EDITOR=./editor git commit --amend
109 '
110
111 test_expect_success 'set up editor' '
112         cat >editor <<-\EOF &&
113         #!/bin/sh
114         sed -e "s/unamended/amended/g" <"$1" >"$1-"
115         mv "$1-" "$1"
116         EOF
117         chmod 755 editor
118 '
119
120 test_expect_success 'amend without launching editor' '
121         echo unamended >expect &&
122         git commit --allow-empty -m "unamended" &&
123         echo needs more bongo >file &&
124         git add file &&
125         EDITOR=./editor git commit --no-edit --amend &&
126         git diff --exit-code HEAD -- file &&
127         git diff-tree -s --format=%s HEAD >msg &&
128         test_cmp expect msg
129 '
130
131 test_expect_success '--amend --edit' '
132         echo amended >expect &&
133         git commit --allow-empty -m "unamended" &&
134         echo bongo again >file &&
135         git add file &&
136         EDITOR=./editor git commit --edit --amend &&
137         git diff-tree -s --format=%s HEAD >msg &&
138         test_cmp expect msg
139 '
140
141 test_expect_success '--amend --edit of empty message' '
142         cat >replace <<-\EOF &&
143         #!/bin/sh
144         echo "amended" >"$1"
145         EOF
146         chmod 755 replace &&
147         git commit --allow-empty --allow-empty-message -m "" &&
148         echo more bongo >file &&
149         git add file &&
150         EDITOR=./replace git commit --edit --amend &&
151         git diff-tree -s --format=%s HEAD >msg &&
152         ./replace expect &&
153         test_cmp expect msg
154 '
155
156 test_expect_success '-m --edit' '
157         echo amended >expect &&
158         git commit --allow-empty -m buffer &&
159         echo bongo bongo >file &&
160         git add file &&
161         EDITOR=./editor git commit -m unamended --edit &&
162         git diff-tree -s  --format=%s HEAD >msg &&
163         test_cmp expect msg
164 '
165
166 test_expect_success '-m and -F do not mix' '
167         echo enough with the bongos >file &&
168         test_must_fail git commit -F msg -m amending .
169 '
170
171 test_expect_success 'using message from other commit' '
172         git commit -C HEAD^ .
173 '
174
175 test_expect_success 'editing message from other commit' '
176         cat >editor <<-\EOF &&
177         #!/bin/sh
178         sed -e "s/amend/older/g"  < "$1" > "$1-"
179         mv "$1-" "$1"
180         EOF
181         chmod 755 editor &&
182         echo hula hula >file &&
183         EDITOR=./editor git commit -c HEAD^ -a
184 '
185
186 test_expect_success 'message from stdin' '
187         echo silly new contents >file &&
188         echo commit message from stdin |
189         git commit -F - -a
190 '
191
192 test_expect_success 'overriding author from command line' '
193         echo gak >file &&
194         git commit -m author \
195                 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
196         grep Rubber.Duck output
197 '
198
199 test_expect_success PERL 'interactive add' '
200         echo 7 |
201         git commit --interactive |
202         grep "What now"
203 '
204
205 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
206         echo zoo >file &&
207         test_must_fail git diff --exit-code >diff1 &&
208         (echo u ; echo "*" ; echo q) |
209         (
210                 EDITOR=: &&
211                 export EDITOR &&
212                 test_must_fail git commit --interactive
213         ) &&
214         git diff >diff2 &&
215         compare_diff_patch diff1 diff2
216 '
217
218 test_expect_success 'editor not invoked if -F is given' '
219         cat >editor <<-\EOF &&
220         #!/bin/sh
221         sed -e s/good/bad/g <"$1" >"$1-"
222         mv "$1-" "$1"
223         EOF
224         chmod 755 editor &&
225
226         echo A good commit message. >msg &&
227         echo moo >file &&
228
229         EDITOR=./editor git commit -a -F msg &&
230         git show -s --pretty=format:%s >subject &&
231         grep -q good subject &&
232
233         echo quack >file &&
234         echo Another good message. |
235         EDITOR=./editor git commit -a -F - &&
236         git show -s --pretty=format:%s >subject &&
237         grep -q good subject
238 '
239
240 test_expect_success 'partial commit that involves removal (1)' '
241
242         git rm --cached file &&
243         mv file elif &&
244         git add elif &&
245         git commit -m "Partial: add elif" elif &&
246         git diff-tree --name-status HEAD^ HEAD >current &&
247         echo "A elif" >expected &&
248         test_cmp expected current
249
250 '
251
252 test_expect_success 'partial commit that involves removal (2)' '
253
254         git commit -m "Partial: remove file" file &&
255         git diff-tree --name-status HEAD^ HEAD >current &&
256         echo "D file" >expected &&
257         test_cmp expected current
258
259 '
260
261 test_expect_success 'partial commit that involves removal (3)' '
262
263         git rm --cached elif &&
264         echo elif >elif &&
265         git commit -m "Partial: modify elif" elif &&
266         git diff-tree --name-status HEAD^ HEAD >current &&
267         echo "M elif" >expected &&
268         test_cmp expected current
269
270 '
271
272 test_expect_success 'amend commit to fix author' '
273
274         oldtick=$GIT_AUTHOR_DATE &&
275         test_tick &&
276         git reset --hard &&
277         git cat-file -p HEAD |
278         sed -e "s/author.*/author $author $oldtick/" \
279                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
280                 expected &&
281         git commit --amend --author="$author" &&
282         git cat-file -p HEAD > current &&
283         test_cmp expected current
284
285 '
286
287 test_expect_success 'amend commit to fix date' '
288
289         test_tick &&
290         newtick=$GIT_AUTHOR_DATE &&
291         git reset --hard &&
292         git cat-file -p HEAD |
293         sed -e "s/author.*/author $author $newtick/" \
294                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
295                 expected &&
296         git commit --amend --date="$newtick" &&
297         git cat-file -p HEAD > current &&
298         test_cmp expected current
299
300 '
301
302 test_expect_success 'commit complains about bogus date' '
303         test_must_fail git commit --amend --date=10.11.2010
304 '
305
306 test_expect_success 'sign off (1)' '
307
308         echo 1 >positive &&
309         git add positive &&
310         git commit -s -m "thank you" &&
311         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
312         (
313                 echo thank you
314                 echo
315                 git var GIT_COMMITTER_IDENT |
316                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
317         ) >expected &&
318         test_cmp expected actual
319
320 '
321
322 test_expect_success 'sign off (2)' '
323
324         echo 2 >positive &&
325         git add positive &&
326         existing="Signed-off-by: Watch This <watchthis@example.com>" &&
327         git commit -s -m "thank you
328
329 $existing" &&
330         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
331         (
332                 echo thank you
333                 echo
334                 echo $existing
335                 git var GIT_COMMITTER_IDENT |
336                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
337         ) >expected &&
338         test_cmp expected actual
339
340 '
341
342 test_expect_success 'signoff gap' '
343
344         echo 3 >positive &&
345         git add positive &&
346         alt="Alt-RFC-822-Header: Value" &&
347         git commit -s -m "welcome
348
349 $alt" &&
350         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
351         (
352                 echo welcome
353                 echo
354                 echo $alt
355                 git var GIT_COMMITTER_IDENT |
356                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
357         ) >expected &&
358         test_cmp expected actual
359 '
360
361 test_expect_success 'signoff gap 2' '
362
363         echo 4 >positive &&
364         git add positive &&
365         alt="fixed: 34" &&
366         git commit -s -m "welcome
367
368 We have now
369 $alt" &&
370         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
371         (
372                 echo welcome
373                 echo
374                 echo We have now
375                 echo $alt
376                 echo
377                 git var GIT_COMMITTER_IDENT |
378                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
379         ) >expected &&
380         test_cmp expected actual
381 '
382
383 test_expect_success 'multiple -m' '
384
385         >negative &&
386         git add negative &&
387         git commit -m "one" -m "two" -m "three" &&
388         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
389         (
390                 echo one
391                 echo
392                 echo two
393                 echo
394                 echo three
395         ) >expected &&
396         test_cmp expected actual
397
398 '
399
400 test_expect_success 'amend commit to fix author' '
401
402         oldtick=$GIT_AUTHOR_DATE &&
403         test_tick &&
404         git reset --hard &&
405         git cat-file -p HEAD |
406         sed -e "s/author.*/author $author $oldtick/" \
407                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
408                 expected &&
409         git commit --amend --author="$author" &&
410         git cat-file -p HEAD > current &&
411         test_cmp expected current
412
413 '
414
415 test_expect_success 'git commit <file> with dirty index' '
416         echo tacocat > elif &&
417         echo tehlulz > chz &&
418         git add chz &&
419         git commit elif -m "tacocat is a palindrome" &&
420         git show --stat | grep elif &&
421         git diff --cached | grep chz
422 '
423
424 test_expect_success 'same tree (single parent)' '
425
426         git reset --hard &&
427         test_must_fail git commit -m empty
428
429 '
430
431 test_expect_success 'same tree (single parent) --allow-empty' '
432
433         git commit --allow-empty -m "forced empty" &&
434         git cat-file commit HEAD | grep forced
435
436 '
437
438 test_expect_success 'same tree (merge and amend merge)' '
439
440         git checkout -b side HEAD^ &&
441         echo zero >zero &&
442         git add zero &&
443         git commit -m "add zero" &&
444         git checkout master &&
445
446         git merge -s ours side -m "empty ok" &&
447         git diff HEAD^ HEAD >actual &&
448         : >expected &&
449         test_cmp expected actual &&
450
451         git commit --amend -m "empty really ok" &&
452         git diff HEAD^ HEAD >actual &&
453         : >expected &&
454         test_cmp expected actual
455
456 '
457
458 test_expect_success 'amend using the message from another commit' '
459
460         git reset --hard &&
461         test_tick &&
462         git commit --allow-empty -m "old commit" &&
463         old=$(git rev-parse --verify HEAD) &&
464         test_tick &&
465         git commit --allow-empty -m "new commit" &&
466         new=$(git rev-parse --verify HEAD) &&
467         test_tick &&
468         git commit --allow-empty --amend -C "$old" &&
469         git show --pretty="format:%ad %s" "$old" >expected &&
470         git show --pretty="format:%ad %s" HEAD >actual &&
471         test_cmp expected actual
472
473 '
474
475 test_expect_success 'amend using the message from a commit named with tag' '
476
477         git reset --hard &&
478         test_tick &&
479         git commit --allow-empty -m "old commit" &&
480         old=$(git rev-parse --verify HEAD) &&
481         git tag -a -m "tag on old" tagged-old HEAD &&
482         test_tick &&
483         git commit --allow-empty -m "new commit" &&
484         new=$(git rev-parse --verify HEAD) &&
485         test_tick &&
486         git commit --allow-empty --amend -C tagged-old &&
487         git show --pretty="format:%ad %s" "$old" >expected &&
488         git show --pretty="format:%ad %s" HEAD >actual &&
489         test_cmp expected actual
490
491 '
492
493 test_expect_success 'amend can copy notes' '
494
495         git config notes.rewrite.amend true &&
496         git config notes.rewriteRef "refs/notes/*" &&
497         test_commit foo &&
498         git notes add -m"a note" &&
499         test_tick &&
500         git commit --amend -m"new foo" &&
501         test "$(git notes show)" = "a note"
502
503 '
504
505 test_expect_success 'commit a file whose name is a dash' '
506         git reset --hard &&
507         for i in 1 2 3 4 5
508         do
509                 echo $i
510         done >./- &&
511         git add ./- &&
512         test_tick &&
513         git commit -m "add dash" >output </dev/null &&
514         test_i18ngrep " changed, 5 insertions" output
515 '
516
517 test_done