3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
6 # FIXME: Test the various index usages, -i and -o, test reflog,
9 test_description='git commit'
11 . "$TEST_DIRECTORY/diff-lib.sh"
13 author='The Real Author <someguy@his.email.org>'
17 test_expect_success 'initial status' '
18 echo bongo bongo >file &&
21 test_i18ngrep "No commits yet" actual
24 test_expect_success 'fail initial amend' '
25 test_must_fail git commit --amend
28 test_expect_success 'setup: initial commit' '
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
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
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
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 test_write_lines 2 1 "" 7 |
51 git commit -m foo --interactive file &&
52 git reset --hard HEAD^
55 test_expect_success 'removed files and relative paths' '
56 test_when_finished "rm -rf foo" &&
59 git -C foo add foo.txt &&
60 git -C foo commit -m first &&
61 git -C foo rm foo.txt &&
64 git -C foo/bar commit -m second ../foo.txt
67 test_expect_success 'using invalid commit with -C' '
68 test_must_fail git commit --allow-empty -C bogus
71 test_expect_success 'nothing to commit' '
73 test_must_fail git commit -m initial
76 test_expect_success '--dry-run fails with nothing to commit' '
77 test_must_fail git commit -m initial --dry-run
80 test_expect_success '--short fails with nothing to commit' '
81 test_must_fail git commit -m initial --short
84 test_expect_success '--porcelain fails with nothing to commit' '
85 test_must_fail git commit -m initial --porcelain
88 test_expect_success '--long fails with nothing to commit' '
89 test_must_fail git commit -m initial --long
92 test_expect_success 'setup: non-initial commit' '
93 echo bongo bongo bongo >file &&
97 test_expect_success '--dry-run with stuff to commit returns ok' '
98 echo bongo bongo bongo >>file &&
99 git commit -m next -a --dry-run
102 test_expect_success '--short with stuff to commit returns ok' '
103 echo bongo bongo bongo >>file &&
104 git commit -m next -a --short
107 test_expect_success '--porcelain with stuff to commit returns ok' '
108 echo bongo bongo bongo >>file &&
109 git commit -m next -a --porcelain
112 test_expect_success '--long with stuff to commit returns ok' '
113 echo bongo bongo bongo >>file &&
114 git commit -m next -a --long
117 test_expect_success 'commit message from non-existing file' '
118 echo more bongo: bongo bongo bongo bongo >file &&
119 test_must_fail git commit -F gah -a
122 test_expect_success 'empty commit message' '
123 # Empty except stray tabs and spaces on a few lines.
124 sed -e "s/@//g" >msg <<-\EOF &&
128 @Signed-off-by: hula@
130 test_must_fail git commit -F msg -a
133 test_expect_success 'template "emptyness" check does not kick in with -F' '
134 git checkout HEAD file && echo >>file && git add file &&
135 git commit -t file -F file
138 test_expect_success 'template "emptyness" check' '
139 git checkout HEAD file && echo >>file && git add file &&
140 test_must_fail git commit -t file 2>err &&
141 test_i18ngrep "did not edit" err
144 test_expect_success 'setup: commit message from file' '
145 git checkout HEAD file && echo >>file && git add file &&
146 echo this is the commit message, coming from a file >msg &&
150 test_expect_success 'amend commit' '
151 cat >editor <<-\EOF &&
153 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
157 EDITOR=./editor git commit --amend
160 test_expect_success 'amend --only ignores staged contents' '
161 cp file file.expect &&
162 echo changed >file &&
164 git commit --no-edit --amend --only &&
165 git cat-file blob HEAD:file >file.actual &&
166 test_cmp file.expect file.actual &&
170 test_expect_success 'allow-empty --only ignores staged contents' '
171 echo changed-again >file &&
173 git commit --allow-empty --only -m "empty" &&
174 git cat-file blob HEAD:file >file.actual &&
175 test_cmp file.expect file.actual &&
179 test_expect_success 'set up editor' '
180 cat >editor <<-\EOF &&
182 sed -e "s/unamended/amended/g" <"$1" >"$1-"
188 test_expect_success 'amend without launching editor' '
189 echo unamended >expect &&
190 git commit --allow-empty -m "unamended" &&
191 echo needs more bongo >file &&
193 EDITOR=./editor git commit --no-edit --amend &&
194 git diff --exit-code HEAD -- file &&
195 git diff-tree -s --format=%s HEAD >msg &&
199 test_expect_success '--amend --edit' '
200 echo amended >expect &&
201 git commit --allow-empty -m "unamended" &&
202 echo bongo again >file &&
204 EDITOR=./editor git commit --edit --amend &&
205 git diff-tree -s --format=%s HEAD >msg &&
209 test_expect_success '--amend --edit of empty message' '
210 cat >replace <<-\EOF &&
215 git commit --allow-empty --allow-empty-message -m "" &&
216 echo more bongo >file &&
218 EDITOR=./replace git commit --edit --amend &&
219 git diff-tree -s --format=%s HEAD >msg &&
224 test_expect_success '--amend to set message to empty' '
227 git commit -m "unamended" &&
228 git commit --amend --allow-empty-message -m "" &&
229 git diff-tree -s --format=%s HEAD >msg &&
234 test_expect_success '--amend to set empty message needs --allow-empty-message' '
237 git commit -m "unamended" &&
238 test_must_fail git commit --amend -m "" &&
239 git diff-tree -s --format=%s HEAD >msg &&
240 echo "unamended" >expect &&
244 test_expect_success '-m --edit' '
245 echo amended >expect &&
246 git commit --allow-empty -m buffer &&
247 echo bongo bongo >file &&
249 EDITOR=./editor git commit -m unamended --edit &&
250 git diff-tree -s --format=%s HEAD >msg &&
254 test_expect_success '-m and -F do not mix' '
255 echo enough with the bongos >file &&
256 test_must_fail git commit -F msg -m amending .
259 test_expect_success 'using message from other commit' '
260 git commit -C HEAD^ .
263 test_expect_success 'editing message from other commit' '
264 cat >editor <<-\EOF &&
266 sed -e "s/amend/older/g" <"$1" >"$1-"
270 echo hula hula >file &&
271 EDITOR=./editor git commit -c HEAD^ -a
274 test_expect_success 'message from stdin' '
275 echo silly new contents >file &&
276 echo commit message from stdin |
280 test_expect_success 'overriding author from command line' '
282 git commit -m author \
283 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
284 grep Rubber.Duck output
287 test_expect_success PERL 'interactive add' '
288 echo 7 | test_must_fail git commit --interactive >out &&
292 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
294 test_must_fail git diff --exit-code >diff1 &&
295 test_write_lines u "*" q |
299 test_must_fail git commit --interactive
302 compare_diff_patch diff1 diff2
305 test_expect_success 'editor not invoked if -F is given' '
306 cat >editor <<-\EOF &&
308 sed -e s/good/bad/g <"$1" >"$1-"
313 echo A good commit message. >msg &&
316 EDITOR=./editor git commit -a -F msg &&
317 git show -s --pretty=format:%s >subject &&
318 grep -q good subject &&
321 echo Another good message. |
322 EDITOR=./editor git commit -a -F - &&
323 git show -s --pretty=format:%s >subject &&
327 test_expect_success 'partial commit that involves removal (1)' '
329 git rm --cached file &&
332 git commit -m "Partial: add elif" elif &&
333 git diff-tree --name-status HEAD^ HEAD >current &&
334 echo "A elif" >expected &&
335 test_cmp expected current
339 test_expect_success 'partial commit that involves removal (2)' '
341 git commit -m "Partial: remove file" file &&
342 git diff-tree --name-status HEAD^ HEAD >current &&
343 echo "D file" >expected &&
344 test_cmp expected current
348 test_expect_success 'partial commit that involves removal (3)' '
350 git rm --cached elif &&
352 git commit -m "Partial: modify elif" elif &&
353 git diff-tree --name-status HEAD^ HEAD >current &&
354 echo "M elif" >expected &&
355 test_cmp expected current
359 test_expect_success 'amend commit to fix author' '
361 oldtick=$GIT_AUTHOR_DATE &&
364 git cat-file -p HEAD >commit &&
365 sed -e "s/author.*/author $author $oldtick/" \
366 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
368 git commit --amend --author="$author" &&
369 git cat-file -p HEAD >current &&
370 test_cmp expected current
374 test_expect_success 'amend commit to fix date' '
377 newtick=$GIT_AUTHOR_DATE &&
379 git cat-file -p HEAD >commit &&
380 sed -e "s/author.*/author $author $newtick/" \
381 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
383 git commit --amend --date="$newtick" &&
384 git cat-file -p HEAD >current &&
385 test_cmp expected current
389 test_expect_success 'commit mentions forced date in output' '
390 git commit --amend --date=2010-01-02T03:04:05 >output &&
391 grep "Date: *Sat Jan 2 03:04:05 2010" output
394 test_expect_success 'commit complains about completely bogus dates' '
395 test_must_fail git commit --amend --date=seventeen
398 test_expect_success 'commit --date allows approxidate' '
400 --date="midnight the 12th of october, anno domini 1979" &&
401 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
402 git log -1 --format=%ad >actual &&
403 test_cmp expect actual
406 test_expect_success 'sign off (1)' '
410 git commit -s -m "thank you" &&
411 git cat-file commit HEAD >commit &&
412 sed -e "1,/^\$/d" commit >actual &&
416 git var GIT_COMMITTER_IDENT >ident &&
417 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
419 test_cmp expected actual
423 test_expect_success 'sign off (2)' '
427 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
428 git commit -s -m "thank you
431 git cat-file commit HEAD >commit &&
432 sed -e "1,/^\$/d" commit >actual &&
437 git var GIT_COMMITTER_IDENT >ident &&
438 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
440 test_cmp expected actual
444 test_expect_success 'signoff gap' '
448 alt="Alt-RFC-822-Header: Value" &&
449 git commit -s -m "welcome
452 git cat-file commit HEAD >commit &&
453 sed -e "1,/^\$/d" commit >actual &&
458 git var GIT_COMMITTER_IDENT >ident &&
459 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
461 test_cmp expected actual
464 test_expect_success 'signoff gap 2' '
469 git commit -s -m "welcome
473 git cat-file commit HEAD >commit &&
474 sed -e "1,/^\$/d" commit >actual &&
481 git var GIT_COMMITTER_IDENT >ident &&
482 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
484 test_cmp expected actual
487 test_expect_success 'signoff respects trailer config' '
491 git commit -s -m "subject
495 git cat-file commit HEAD >commit &&
496 sed -e "1,/^\$/d" commit >actual &&
500 echo non-trailer line &&
503 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
505 test_cmp expected actual &&
509 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
513 git cat-file commit HEAD >commit &&
514 sed -e "1,/^\$/d" commit >actual &&
518 echo non-trailer line &&
520 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
522 test_cmp expected actual
525 test_expect_success 'signoff not confused by ---' '
526 cat >expected <<-EOF &&
531 these dashes confuse the parser!
533 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
535 # should be a noop, since we already signed
536 git commit --allow-empty --signoff -F expected &&
537 git log -1 --pretty=format:%B >actual &&
538 test_cmp expected actual
541 test_expect_success 'multiple -m' '
545 git commit -m "one" -m "two" -m "three" &&
546 git cat-file commit HEAD >commit &&
547 sed -e "1,/^\$/d" commit >actual &&
555 test_cmp expected actual
559 test_expect_success 'amend commit to fix author' '
561 oldtick=$GIT_AUTHOR_DATE &&
564 git cat-file -p HEAD >commit &&
565 sed -e "s/author.*/author $author $oldtick/" \
566 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
568 git commit --amend --author="$author" &&
569 git cat-file -p HEAD >current &&
570 test_cmp expected current
574 test_expect_success 'git commit <file> with dirty index' '
575 echo tacocat >elif &&
578 git commit elif -m "tacocat is a palindrome" &&
579 git show --stat >stat &&
581 git diff --cached >diff &&
585 test_expect_success 'same tree (single parent)' '
588 test_must_fail git commit -m empty
592 test_expect_success 'same tree (single parent) --allow-empty' '
594 git commit --allow-empty -m "forced empty" &&
595 git cat-file commit HEAD >commit &&
600 test_expect_success 'same tree (merge and amend merge)' '
602 git checkout -b side HEAD^ &&
605 git commit -m "add zero" &&
606 git checkout master &&
608 git merge -s ours side -m "empty ok" &&
609 git diff HEAD^ HEAD >actual &&
610 test_must_be_empty actual &&
612 git commit --amend -m "empty really ok" &&
613 git diff HEAD^ HEAD >actual &&
614 test_must_be_empty actual
618 test_expect_success 'amend using the message from another commit' '
622 git commit --allow-empty -m "old commit" &&
623 old=$(git rev-parse --verify HEAD) &&
625 git commit --allow-empty -m "new commit" &&
626 new=$(git rev-parse --verify HEAD) &&
628 git commit --allow-empty --amend -C "$old" &&
629 git show --pretty="format:%ad %s" "$old" >expected &&
630 git show --pretty="format:%ad %s" HEAD >actual &&
631 test_cmp expected actual
635 test_expect_success 'amend using the message from a commit named with tag' '
639 git commit --allow-empty -m "old commit" &&
640 old=$(git rev-parse --verify HEAD) &&
641 git tag -a -m "tag on old" tagged-old HEAD &&
643 git commit --allow-empty -m "new commit" &&
644 new=$(git rev-parse --verify HEAD) &&
646 git commit --allow-empty --amend -C tagged-old &&
647 git show --pretty="format:%ad %s" "$old" >expected &&
648 git show --pretty="format:%ad %s" HEAD >actual &&
649 test_cmp expected actual
653 test_expect_success 'amend can copy notes' '
655 git config notes.rewrite.amend true &&
656 git config notes.rewriteRef "refs/notes/*" &&
658 git notes add -m"a note" &&
660 git commit --amend -m"new foo" &&
661 test "$(git notes show)" = "a note"
665 test_expect_success 'commit a file whose name is a dash' '
673 git commit -m "add dash" >output </dev/null &&
674 test_i18ngrep " changed, 5 insertions" output
677 test_expect_success '--only works on to-be-born branch' '
678 # This test relies on having something in the index, as it
679 # would not otherwise actually prove much. So check this.
680 test -n "$(git ls-files)" &&
681 git checkout --orphan orphan &&
684 git commit --only newfile -m"--only on unborn branch" &&
685 echo newfile >expected &&
686 git ls-tree -r --name-only HEAD >actual &&
687 test_cmp expected actual
690 test_expect_success '--dry-run with conflicts fixed from a merge' '
691 # setup two branches with conflicting information
692 # in the same file, resolve the conflict,
693 # call commit with --dry-run
694 echo "Initial contents, unimportant" >test-file &&
696 git commit -m "Initial commit" &&
697 echo "commit-1-state" >test-file &&
698 git commit -m "commit 1" -i test-file &&
700 git checkout -b branch-2 HEAD^1 &&
701 echo "commit-2-state" >test-file &&
702 git commit -m "commit 2" -i test-file &&
703 test_must_fail git merge --no-commit commit-1 &&
704 echo "commit-2-state" >test-file &&
706 git commit --dry-run &&
707 git commit -m "conflicts fixed from merge."
710 test_expect_success '--dry-run --short' '
713 git commit --dry-run --short