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