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