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
9test_description='git commit'
10. ./test-lib.sh
11. "$TEST_DIRECTORY/diff-lib.sh"
12
13author='The Real Author <someguy@his.email.org>'
14
15test_tick
16
17test_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
24test_expect_success 'fail initial amend' '
25 test_must_fail git commit --amend
26'
27
28test_expect_success 'setup: initial commit' '
29 git commit -m initial
30'
31
32test_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
37test_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
42test_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
47test_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
55test_expect_success 'using invalid commit with -C' '
56 test_must_fail git commit --allow-empty -C bogus
57'
58
59test_expect_success 'nothing to commit' '
60 git reset --hard &&
61 test_must_fail git commit -m initial
62'
63
64test_expect_success '--dry-run fails with nothing to commit' '
65 test_must_fail git commit -m initial --dry-run
66'
67
68test_expect_success '--short fails with nothing to commit' '
69 test_must_fail git commit -m initial --short
70'
71
72test_expect_success '--porcelain fails with nothing to commit' '
73 test_must_fail git commit -m initial --porcelain
74'
75
76test_expect_success '--long fails with nothing to commit' '
77 test_must_fail git commit -m initial --long
78'
79
80test_expect_success 'setup: non-initial commit' '
81 echo bongo bongo bongo >file &&
82 git commit -m next -a
83'
84
85test_expect_success '--dry-run with stuff to commit returns ok' '
86 echo bongo bongo bongo >>file &&
87 git commit -m next -a --dry-run
88'
89
90test_expect_failure '--short with stuff to commit returns ok' '
91 echo bongo bongo bongo >>file &&
92 git commit -m next -a --short
93'
94
95test_expect_failure '--porcelain with stuff to commit returns ok' '
96 echo bongo bongo bongo >>file &&
97 git commit -m next -a --porcelain
98'
99
100test_expect_success '--long with stuff to commit returns ok' '
101 echo bongo bongo bongo >>file &&
102 git commit -m next -a --long
103'
104
105test_expect_success 'commit message from non-existing file' '
106 echo more bongo: bongo bongo bongo bongo >file &&
107 test_must_fail git commit -F gah -a
108'
109
110test_expect_success 'empty commit message' '
111 # Empty except stray tabs and spaces on a few lines.
112 sed -e "s/@//g" >msg <<-\EOF &&
113 @ @
114 @@
115 @ @
116 @Signed-off-by: hula@
117 EOF
118 test_must_fail git commit -F msg -a
119'
120
121test_expect_success 'template "emptyness" check does not kick in with -F' '
122 git checkout HEAD file && echo >>file && git add file &&
123 git commit -t file -F file
124'
125
126test_expect_success 'template "emptyness" check' '
127 git checkout HEAD file && echo >>file && git add file &&
128 test_must_fail git commit -t file 2>err &&
129 test_i18ngrep "did not edit" err
130'
131
132test_expect_success 'setup: commit message from file' '
133 git checkout HEAD file && echo >>file && git add file &&
134 echo this is the commit message, coming from a file >msg &&
135 git commit -F msg -a
136'
137
138test_expect_success 'amend commit' '
139 cat >editor <<-\EOF &&
140 #!/bin/sh
141 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
142 mv "$1-" "$1"
143 EOF
144 chmod 755 editor &&
145 EDITOR=./editor git commit --amend
146'
147
148test_expect_success 'amend --only ignores staged contents' '
149 cp file file.expect &&
150 echo changed >file &&
151 git add file &&
152 git commit --no-edit --amend --only &&
153 git cat-file blob HEAD:file >file.actual &&
154 test_cmp file.expect file.actual &&
155 git diff --exit-code
156'
157
158test_expect_success 'set up editor' '
159 cat >editor <<-\EOF &&
160 #!/bin/sh
161 sed -e "s/unamended/amended/g" <"$1" >"$1-"
162 mv "$1-" "$1"
163 EOF
164 chmod 755 editor
165'
166
167test_expect_success 'amend without launching editor' '
168 echo unamended >expect &&
169 git commit --allow-empty -m "unamended" &&
170 echo needs more bongo >file &&
171 git add file &&
172 EDITOR=./editor git commit --no-edit --amend &&
173 git diff --exit-code HEAD -- file &&
174 git diff-tree -s --format=%s HEAD >msg &&
175 test_cmp expect msg
176'
177
178test_expect_success '--amend --edit' '
179 echo amended >expect &&
180 git commit --allow-empty -m "unamended" &&
181 echo bongo again >file &&
182 git add file &&
183 EDITOR=./editor git commit --edit --amend &&
184 git diff-tree -s --format=%s HEAD >msg &&
185 test_cmp expect msg
186'
187
188test_expect_success '--amend --edit of empty message' '
189 cat >replace <<-\EOF &&
190 #!/bin/sh
191 echo "amended" >"$1"
192 EOF
193 chmod 755 replace &&
194 git commit --allow-empty --allow-empty-message -m "" &&
195 echo more bongo >file &&
196 git add file &&
197 EDITOR=./replace git commit --edit --amend &&
198 git diff-tree -s --format=%s HEAD >msg &&
199 ./replace expect &&
200 test_cmp expect msg
201'
202
203test_expect_success '--amend to set message to empty' '
204 echo bata >file &&
205 git add file &&
206 git commit -m "unamended" &&
207 git commit --amend --allow-empty-message -m "" &&
208 git diff-tree -s --format=%s HEAD >msg &&
209 echo "" >expect &&
210 test_cmp expect msg
211'
212
213test_expect_success '--amend to set empty message needs --allow-empty-message' '
214 echo conga >file &&
215 git add file &&
216 git commit -m "unamended" &&
217 test_must_fail git commit --amend -m "" &&
218 git diff-tree -s --format=%s HEAD >msg &&
219 echo "unamended" >expect &&
220 test_cmp expect msg
221'
222
223test_expect_success '-m --edit' '
224 echo amended >expect &&
225 git commit --allow-empty -m buffer &&
226 echo bongo bongo >file &&
227 git add file &&
228 EDITOR=./editor git commit -m unamended --edit &&
229 git diff-tree -s --format=%s HEAD >msg &&
230 test_cmp expect msg
231'
232
233test_expect_success '-m and -F do not mix' '
234 echo enough with the bongos >file &&
235 test_must_fail git commit -F msg -m amending .
236'
237
238test_expect_success 'using message from other commit' '
239 git commit -C HEAD^ .
240'
241
242test_expect_success 'editing message from other commit' '
243 cat >editor <<-\EOF &&
244 #!/bin/sh
245 sed -e "s/amend/older/g" < "$1" > "$1-"
246 mv "$1-" "$1"
247 EOF
248 chmod 755 editor &&
249 echo hula hula >file &&
250 EDITOR=./editor git commit -c HEAD^ -a
251'
252
253test_expect_success 'message from stdin' '
254 echo silly new contents >file &&
255 echo commit message from stdin |
256 git commit -F - -a
257'
258
259test_expect_success 'overriding author from command line' '
260 echo gak >file &&
261 git commit -m author \
262 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
263 grep Rubber.Duck output
264'
265
266test_expect_success PERL 'interactive add' '
267 echo 7 |
268 git commit --interactive |
269 grep "What now"
270'
271
272test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
273 echo zoo >file &&
274 test_must_fail git diff --exit-code >diff1 &&
275 (echo u ; echo "*" ; echo q) |
276 (
277 EDITOR=: &&
278 export EDITOR &&
279 test_must_fail git commit --interactive
280 ) &&
281 git diff >diff2 &&
282 compare_diff_patch diff1 diff2
283'
284
285test_expect_success 'editor not invoked if -F is given' '
286 cat >editor <<-\EOF &&
287 #!/bin/sh
288 sed -e s/good/bad/g <"$1" >"$1-"
289 mv "$1-" "$1"
290 EOF
291 chmod 755 editor &&
292
293 echo A good commit message. >msg &&
294 echo moo >file &&
295
296 EDITOR=./editor git commit -a -F msg &&
297 git show -s --pretty=format:%s >subject &&
298 grep -q good subject &&
299
300 echo quack >file &&
301 echo Another good message. |
302 EDITOR=./editor git commit -a -F - &&
303 git show -s --pretty=format:%s >subject &&
304 grep -q good subject
305'
306
307test_expect_success 'partial commit that involves removal (1)' '
308
309 git rm --cached file &&
310 mv file elif &&
311 git add elif &&
312 git commit -m "Partial: add elif" elif &&
313 git diff-tree --name-status HEAD^ HEAD >current &&
314 echo "A elif" >expected &&
315 test_cmp expected current
316
317'
318
319test_expect_success 'partial commit that involves removal (2)' '
320
321 git commit -m "Partial: remove file" file &&
322 git diff-tree --name-status HEAD^ HEAD >current &&
323 echo "D file" >expected &&
324 test_cmp expected current
325
326'
327
328test_expect_success 'partial commit that involves removal (3)' '
329
330 git rm --cached elif &&
331 echo elif >elif &&
332 git commit -m "Partial: modify elif" elif &&
333 git diff-tree --name-status HEAD^ HEAD >current &&
334 echo "M elif" >expected &&
335 test_cmp expected current
336
337'
338
339test_expect_success 'amend commit to fix author' '
340
341 oldtick=$GIT_AUTHOR_DATE &&
342 test_tick &&
343 git reset --hard &&
344 git cat-file -p HEAD |
345 sed -e "s/author.*/author $author $oldtick/" \
346 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
347 expected &&
348 git commit --amend --author="$author" &&
349 git cat-file -p HEAD > current &&
350 test_cmp expected current
351
352'
353
354test_expect_success 'amend commit to fix date' '
355
356 test_tick &&
357 newtick=$GIT_AUTHOR_DATE &&
358 git reset --hard &&
359 git cat-file -p HEAD |
360 sed -e "s/author.*/author $author $newtick/" \
361 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
362 expected &&
363 git commit --amend --date="$newtick" &&
364 git cat-file -p HEAD > current &&
365 test_cmp expected current
366
367'
368
369test_expect_success 'commit mentions forced date in output' '
370 git commit --amend --date=2010-01-02T03:04:05 >output &&
371 grep "Date: *Sat Jan 2 03:04:05 2010" output
372'
373
374test_expect_success 'commit complains about completely bogus dates' '
375 test_must_fail git commit --amend --date=seventeen
376'
377
378test_expect_success 'commit --date allows approxidate' '
379 git commit --amend \
380 --date="midnight the 12th of october, anno domini 1979" &&
381 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
382 git log -1 --format=%ad >actual &&
383 test_cmp expect actual
384'
385
386test_expect_success 'sign off (1)' '
387
388 echo 1 >positive &&
389 git add positive &&
390 git commit -s -m "thank you" &&
391 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
392 (
393 echo thank you
394 echo
395 git var GIT_COMMITTER_IDENT |
396 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
397 ) >expected &&
398 test_cmp expected actual
399
400'
401
402test_expect_success 'sign off (2)' '
403
404 echo 2 >positive &&
405 git add positive &&
406 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
407 git commit -s -m "thank you
408
409$existing" &&
410 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
411 (
412 echo thank you
413 echo
414 echo $existing
415 git var GIT_COMMITTER_IDENT |
416 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
417 ) >expected &&
418 test_cmp expected actual
419
420'
421
422test_expect_success 'signoff gap' '
423
424 echo 3 >positive &&
425 git add positive &&
426 alt="Alt-RFC-822-Header: Value" &&
427 git commit -s -m "welcome
428
429$alt" &&
430 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
431 (
432 echo welcome
433 echo
434 echo $alt
435 git var GIT_COMMITTER_IDENT |
436 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
437 ) >expected &&
438 test_cmp expected actual
439'
440
441test_expect_success 'signoff gap 2' '
442
443 echo 4 >positive &&
444 git add positive &&
445 alt="fixed: 34" &&
446 git commit -s -m "welcome
447
448We have now
449$alt" &&
450 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
451 (
452 echo welcome
453 echo
454 echo We have now
455 echo $alt
456 echo
457 git var GIT_COMMITTER_IDENT |
458 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
459 ) >expected &&
460 test_cmp expected actual
461'
462
463test_expect_success 'signoff respects trailer config' '
464
465 echo 5 >positive &&
466 git add positive &&
467 git commit -s -m "subject
468
469non-trailer line
470Myfooter: x" &&
471 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
472 (
473 echo subject
474 echo
475 echo non-trailer line
476 echo Myfooter: x
477 echo
478 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
479 ) >expected &&
480 test_cmp expected actual &&
481
482 echo 6 >positive &&
483 git add positive &&
484 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
485
486non-trailer line
487Myfooter: x" &&
488 git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
489 (
490 echo subject
491 echo
492 echo non-trailer line
493 echo Myfooter: x
494 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
495 ) >expected &&
496 test_cmp expected actual
497'
498
499test_expect_success 'multiple -m' '
500
501 >negative &&
502 git add negative &&
503 git commit -m "one" -m "two" -m "three" &&
504 git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
505 (
506 echo one
507 echo
508 echo two
509 echo
510 echo three
511 ) >expected &&
512 test_cmp expected actual
513
514'
515
516test_expect_success 'amend commit to fix author' '
517
518 oldtick=$GIT_AUTHOR_DATE &&
519 test_tick &&
520 git reset --hard &&
521 git cat-file -p HEAD |
522 sed -e "s/author.*/author $author $oldtick/" \
523 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
524 expected &&
525 git commit --amend --author="$author" &&
526 git cat-file -p HEAD > current &&
527 test_cmp expected current
528
529'
530
531test_expect_success 'git commit <file> with dirty index' '
532 echo tacocat > elif &&
533 echo tehlulz > chz &&
534 git add chz &&
535 git commit elif -m "tacocat is a palindrome" &&
536 git show --stat | grep elif &&
537 git diff --cached | grep chz
538'
539
540test_expect_success 'same tree (single parent)' '
541
542 git reset --hard &&
543 test_must_fail git commit -m empty
544
545'
546
547test_expect_success 'same tree (single parent) --allow-empty' '
548
549 git commit --allow-empty -m "forced empty" &&
550 git cat-file commit HEAD | grep forced
551
552'
553
554test_expect_success 'same tree (merge and amend merge)' '
555
556 git checkout -b side HEAD^ &&
557 echo zero >zero &&
558 git add zero &&
559 git commit -m "add zero" &&
560 git checkout master &&
561
562 git merge -s ours side -m "empty ok" &&
563 git diff HEAD^ HEAD >actual &&
564 : >expected &&
565 test_cmp expected actual &&
566
567 git commit --amend -m "empty really ok" &&
568 git diff HEAD^ HEAD >actual &&
569 : >expected &&
570 test_cmp expected actual
571
572'
573
574test_expect_success 'amend using the message from another commit' '
575
576 git reset --hard &&
577 test_tick &&
578 git commit --allow-empty -m "old commit" &&
579 old=$(git rev-parse --verify HEAD) &&
580 test_tick &&
581 git commit --allow-empty -m "new commit" &&
582 new=$(git rev-parse --verify HEAD) &&
583 test_tick &&
584 git commit --allow-empty --amend -C "$old" &&
585 git show --pretty="format:%ad %s" "$old" >expected &&
586 git show --pretty="format:%ad %s" HEAD >actual &&
587 test_cmp expected actual
588
589'
590
591test_expect_success 'amend using the message from a commit named with tag' '
592
593 git reset --hard &&
594 test_tick &&
595 git commit --allow-empty -m "old commit" &&
596 old=$(git rev-parse --verify HEAD) &&
597 git tag -a -m "tag on old" tagged-old HEAD &&
598 test_tick &&
599 git commit --allow-empty -m "new commit" &&
600 new=$(git rev-parse --verify HEAD) &&
601 test_tick &&
602 git commit --allow-empty --amend -C tagged-old &&
603 git show --pretty="format:%ad %s" "$old" >expected &&
604 git show --pretty="format:%ad %s" HEAD >actual &&
605 test_cmp expected actual
606
607'
608
609test_expect_success 'amend can copy notes' '
610
611 git config notes.rewrite.amend true &&
612 git config notes.rewriteRef "refs/notes/*" &&
613 test_commit foo &&
614 git notes add -m"a note" &&
615 test_tick &&
616 git commit --amend -m"new foo" &&
617 test "$(git notes show)" = "a note"
618
619'
620
621test_expect_success 'commit a file whose name is a dash' '
622 git reset --hard &&
623 for i in 1 2 3 4 5
624 do
625 echo $i
626 done >./- &&
627 git add ./- &&
628 test_tick &&
629 git commit -m "add dash" >output </dev/null &&
630 test_i18ngrep " changed, 5 insertions" output
631'
632
633test_expect_success '--only works on to-be-born branch' '
634 # This test relies on having something in the index, as it
635 # would not otherwise actually prove much. So check this.
636 test -n "$(git ls-files)" &&
637 git checkout --orphan orphan &&
638 echo foo >newfile &&
639 git add newfile &&
640 git commit --only newfile -m"--only on unborn branch" &&
641 echo newfile >expected &&
642 git ls-tree -r --name-only HEAD >actual &&
643 test_cmp expected actual
644'
645
646test_expect_success '--dry-run with conflicts fixed from a merge' '
647 # setup two branches with conflicting information
648 # in the same file, resolve the conflict,
649 # call commit with --dry-run
650 echo "Initial contents, unimportant" >test-file &&
651 git add test-file &&
652 git commit -m "Initial commit" &&
653 echo "commit-1-state" >test-file &&
654 git commit -m "commit 1" -i test-file &&
655 git tag commit-1 &&
656 git checkout -b branch-2 HEAD^1 &&
657 echo "commit-2-state" >test-file &&
658 git commit -m "commit 2" -i test-file &&
659 ! $(git merge --no-commit commit-1) &&
660 echo "commit-2-state" >test-file &&
661 git add test-file &&
662 git commit --dry-run &&
663 git commit -m "conflicts fixed from merge."
664'
665
666test_done