3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
6 # FIXME: Test the various index usages, test reflog
8 test_description
='git commit'
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13 .
"$TEST_DIRECTORY/lib-diff.sh"
15 author
='The Real Author <someguy@his.email.org>'
19 test_expect_success
'initial status' '
20 echo bongo bongo >file &&
23 test_grep "No commits yet" actual
26 test_expect_success
'fail initial amend' '
27 test_must_fail git commit --amend
30 test_expect_success
'setup: initial commit' '
34 test_expect_success
'-m and -F do not mix' '
35 git checkout HEAD file && echo >>file && git add file &&
36 test_must_fail git commit -m foo -m bar -F file
39 test_expect_success
'-m and -C do not mix' '
40 git checkout HEAD file && echo >>file && git add file &&
41 test_must_fail git commit -C HEAD -m illegal
44 test_expect_success
'paths and -a do not mix' '
45 echo King of the bongo >file &&
46 test_must_fail git commit -m foo -a file
49 test_expect_success PERL
'can use paths with --interactive' '
50 echo bong-o-bong >file &&
51 # 2: update, 1:st path, that is all, 7: quit
52 test_write_lines 2 1 "" 7 |
53 git commit -m foo --interactive file &&
54 git reset --hard HEAD^
57 test_expect_success
'removed files and relative paths' '
58 test_when_finished "rm -rf foo" &&
61 git -C foo add foo.txt &&
62 git -C foo commit -m first &&
63 git -C foo rm foo.txt &&
66 git -C foo/bar commit -m second ../foo.txt
69 test_expect_success
'using invalid commit with -C' '
70 test_must_fail git commit --allow-empty -C bogus
73 test_expect_success
'nothing to commit' '
75 test_must_fail git commit -m initial
78 test_expect_success
'--dry-run fails with nothing to commit' '
79 test_must_fail git commit -m initial --dry-run
82 test_expect_success
'--short fails with nothing to commit' '
83 test_must_fail git commit -m initial --short
86 test_expect_success
'--porcelain fails with nothing to commit' '
87 test_must_fail git commit -m initial --porcelain
90 test_expect_success
'--long fails with nothing to commit' '
91 test_must_fail git commit -m initial --long
94 test_expect_success
'fail to commit untracked file (even with --include/--only)' '
96 error="error: pathspec .baz. did not match any file(s) known to git" &&
98 test_must_fail git commit -m "baz" baz 2>err &&
99 test_grep -e "$error" err &&
101 test_must_fail git commit --only -m "baz" baz 2>err &&
102 test_grep -e "$error" err &&
104 test_must_fail git commit --include -m "baz" baz 2>err &&
105 test_grep -e "$error" err
108 test_expect_success
'setup: non-initial commit' '
109 echo bongo bongo bongo >file &&
110 git commit -m next -a
113 test_expect_success
'--dry-run with stuff to commit returns ok' '
114 echo bongo bongo bongo >>file &&
115 git commit -m next -a --dry-run
118 test_expect_success
'--short with stuff to commit returns ok' '
119 echo bongo bongo bongo >>file &&
120 git commit -m next -a --short
123 test_expect_success
'--porcelain with stuff to commit returns ok' '
124 echo bongo bongo bongo >>file &&
125 git commit -m next -a --porcelain
128 test_expect_success
'--long with stuff to commit returns ok' '
129 echo bongo bongo bongo >>file &&
130 git commit -m next -a --long
133 for opt
in "" "-o" "--only"
135 test_expect_success
'exclude additional staged changes when given pathspec' '
136 echo content >>file &&
137 echo content >>baz &&
139 git commit $opt -m "file" file &&
141 git diff --name-only >actual &&
142 test_must_be_empty actual &&
144 test_write_lines baz >expect &&
145 git diff --name-only --cached >actual &&
146 test_cmp expect actual &&
148 test_write_lines file >expect &&
149 git diff --name-only HEAD^ HEAD >actual &&
150 test_cmp expect actual
154 test_expect_success
'-i/--include includes staged changes' '
155 echo content >>file &&
156 echo content >>baz &&
159 # baz is in the index, therefore, it will be committed
160 git commit --include -m "file and baz" baz &&
162 git diff --name-only HEAD >remaining &&
163 test_must_be_empty remaining &&
165 test_write_lines baz file >expect &&
166 git diff --name-only HEAD^ HEAD >actual &&
167 test_cmp expect actual
170 test_expect_success
'--include and --only do not mix' '
171 test_when_finished "git reset --hard" &&
172 echo content >>file &&
173 echo content >>baz &&
174 test_must_fail git commit --include --only -m "file baz" file baz 2>actual &&
175 test_grep -e "fatal: options .-i/--include. and .-o/--only. cannot be used together" actual
178 test_expect_success
'commit message from non-existing file' '
179 echo more bongo: bongo bongo bongo bongo >file &&
180 test_must_fail git commit -F gah -a
183 test_expect_success
'empty commit message' '
184 # Empty except stray tabs and spaces on a few lines.
185 sed -e "s/@//g" >msg <<-\EOF &&
189 @Signed-off-by: hula@
191 test_must_fail git commit -F msg -a
194 test_expect_success
'template "emptyness" check does not kick in with -F' '
195 git checkout HEAD file && echo >>file && git add file &&
196 git commit -t file -F file
199 test_expect_success
'template "emptyness" check' '
200 git checkout HEAD file && echo >>file && git add file &&
201 test_must_fail git commit -t file 2>err &&
202 test_grep "did not edit" err
205 test_expect_success
'setup: commit message from file' '
206 git checkout HEAD file && echo >>file && git add file &&
207 echo this is the commit message, coming from a file >msg &&
211 test_expect_success
'amend commit' '
212 cat >editor <<-\EOF &&
214 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
218 EDITOR=./editor git commit --amend
221 test_expect_success
'amend --only ignores staged contents' '
222 cp file file.expect &&
223 echo changed >file &&
225 git commit --no-edit --amend --only &&
226 git cat-file blob HEAD:file >file.actual &&
227 test_cmp file.expect file.actual &&
231 test_expect_success
'allow-empty --only ignores staged contents' '
232 echo changed-again >file &&
234 git commit --allow-empty --only -m "empty" &&
235 git cat-file blob HEAD:file >file.actual &&
236 test_cmp file.expect file.actual &&
240 test_expect_success
'set up editor' '
241 cat >editor <<-\EOF &&
243 sed -e "s/unamended/amended/g" <"$1" >"$1-"
249 test_expect_success
'amend without launching editor' '
250 echo unamended >expect &&
251 git commit --allow-empty -m "unamended" &&
252 echo needs more bongo >file &&
254 EDITOR=./editor git commit --no-edit --amend &&
255 git diff --exit-code HEAD -- file &&
256 git diff-tree -s --format=%s HEAD >msg &&
260 test_expect_success
'--amend --edit' '
261 echo amended >expect &&
262 git commit --allow-empty -m "unamended" &&
263 echo bongo again >file &&
265 EDITOR=./editor git commit --edit --amend &&
266 git diff-tree -s --format=%s HEAD >msg &&
270 test_expect_success
'--amend --edit of empty message' '
271 cat >replace <<-\EOF &&
276 git commit --allow-empty --allow-empty-message -m "" &&
277 echo more bongo >file &&
279 EDITOR=./replace git commit --edit --amend &&
280 git diff-tree -s --format=%s HEAD >msg &&
285 test_expect_success
'--amend to set message to empty' '
288 git commit -m "unamended" &&
289 git commit --amend --allow-empty-message -m "" &&
290 git diff-tree -s --format=%s HEAD >msg &&
295 test_expect_success
'--amend to set empty message needs --allow-empty-message' '
298 git commit -m "unamended" &&
299 test_must_fail git commit --amend -m "" &&
300 git diff-tree -s --format=%s HEAD >msg &&
301 echo "unamended" >expect &&
305 test_expect_success
'-m --edit' '
306 echo amended >expect &&
307 git commit --allow-empty -m buffer &&
308 echo bongo bongo >file &&
310 EDITOR=./editor git commit -m unamended --edit &&
311 git diff-tree -s --format=%s HEAD >msg &&
315 test_expect_success
'-m and -F do not mix' '
316 echo enough with the bongos >file &&
317 test_must_fail git commit -F msg -m amending .
320 test_expect_success
'using message from other commit' '
321 git commit -C HEAD^ .
324 test_expect_success
'editing message from other commit' '
325 cat >editor <<-\EOF &&
327 sed -e "s/amend/older/g" <"$1" >"$1-"
331 echo hula hula >file &&
332 EDITOR=./editor git commit -c HEAD^ -a
335 test_expect_success
'message from stdin' '
336 echo silly new contents >file &&
337 echo commit message from stdin |
341 test_expect_success
'overriding author from command line' '
343 git commit -m author \
344 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
345 grep Rubber.Duck output
348 test_expect_success PERL
'interactive add' '
349 echo 7 | test_must_fail git commit --interactive >out &&
353 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
355 test_must_fail git diff --exit-code >diff1 &&
356 test_write_lines u "*" q |
360 test_must_fail git commit --interactive
363 compare_diff_patch diff1 diff2
366 test_expect_success
'editor not invoked if -F is given' '
367 cat >editor <<-\EOF &&
369 sed -e s/good/bad/g <"$1" >"$1-"
374 echo A good commit message. >msg &&
377 EDITOR=./editor git commit -a -F msg &&
378 git show -s --pretty=format:%s >subject &&
379 grep -q good subject &&
382 echo Another good message. |
383 EDITOR=./editor git commit -a -F - &&
384 git show -s --pretty=format:%s >subject &&
388 test_expect_success
'partial commit that involves removal (1)' '
390 git rm --cached file &&
393 git commit -m "Partial: add elif" elif &&
394 git diff-tree --name-status HEAD^ HEAD >current &&
395 echo "A elif" >expected &&
396 test_cmp expected current
400 test_expect_success
'partial commit that involves removal (2)' '
402 git commit -m "Partial: remove file" file &&
403 git diff-tree --name-status HEAD^ HEAD >current &&
404 echo "D file" >expected &&
405 test_cmp expected current
409 test_expect_success
'partial commit that involves removal (3)' '
411 git rm --cached elif &&
413 git commit -m "Partial: modify elif" elif &&
414 git diff-tree --name-status HEAD^ HEAD >current &&
415 echo "M elif" >expected &&
416 test_cmp expected current
420 test_expect_success
'amend commit to fix author' '
422 oldtick=$GIT_AUTHOR_DATE &&
425 git cat-file -p HEAD >commit &&
426 sed -e "s/author.*/author $author $oldtick/" \
427 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
429 git commit --amend --author="$author" &&
430 git cat-file -p HEAD >current &&
431 test_cmp expected current
435 test_expect_success
'amend commit to fix date' '
438 newtick=$GIT_AUTHOR_DATE &&
440 git cat-file -p HEAD >commit &&
441 sed -e "s/author.*/author $author $newtick/" \
442 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
444 git commit --amend --date="$newtick" &&
445 git cat-file -p HEAD >current &&
446 test_cmp expected current
450 test_expect_success
'amend commit to add signoff' '
452 test_commit "msg" file content &&
453 git commit --amend --signoff &&
454 test_commit_message HEAD <<-EOF
457 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
461 test_expect_success
'amend does not add signoff if it already exists' '
463 test_commit --signoff "tenor" file newcontent &&
464 git commit --amend --signoff &&
465 test_commit_message HEAD <<-EOF
468 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
472 test_expect_success
'commit mentions forced date in output' '
473 git commit --amend --date=2010-01-02T03:04:05 >output &&
474 grep "Date: *Sat Jan 2 03:04:05 2010" output
477 test_expect_success
'commit complains about completely bogus dates' '
478 test_must_fail git commit --amend --date=seventeen
481 test_expect_success
'commit --date allows approxidate' '
483 --date="midnight the 12th of october, anno domini 1979" &&
484 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
485 git log -1 --format=%ad >actual &&
486 test_cmp expect actual
489 test_expect_success
'sign off (1)' '
493 git commit -s -m "thank you" &&
494 git cat-file commit HEAD >commit &&
495 sed -e "1,/^\$/d" commit >actual &&
499 git var GIT_COMMITTER_IDENT >ident &&
500 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
502 test_cmp expected actual
506 test_expect_success
'sign off (2)' '
510 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
511 git commit -s -m "thank you
514 git cat-file commit HEAD >commit &&
515 sed -e "1,/^\$/d" commit >actual &&
520 git var GIT_COMMITTER_IDENT >ident &&
521 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
523 test_cmp expected actual
527 test_expect_success
'signoff gap' '
531 alt="Alt-RFC-822-Header: Value" &&
532 git commit -s -m "welcome
535 git cat-file commit HEAD >commit &&
536 sed -e "1,/^\$/d" commit >actual &&
541 git var GIT_COMMITTER_IDENT >ident &&
542 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
544 test_cmp expected actual
547 test_expect_success
'signoff gap 2' '
552 git commit -s -m "welcome
556 git cat-file commit HEAD >commit &&
557 sed -e "1,/^\$/d" commit >actual &&
564 git var GIT_COMMITTER_IDENT >ident &&
565 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
567 test_cmp expected actual
570 test_expect_success
'signoff respects trailer config' '
574 git commit -s -m "subject
578 git cat-file commit HEAD >commit &&
579 sed -e "1,/^\$/d" commit >actual &&
583 echo non-trailer line &&
586 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
588 test_cmp expected actual &&
592 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
596 git cat-file commit HEAD >commit &&
597 sed -e "1,/^\$/d" commit >actual &&
601 echo non-trailer line &&
603 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
605 test_cmp expected actual
608 test_expect_success
'signoff not confused by ---' '
609 cat >expected <<-EOF &&
614 these dashes confuse the parser!
616 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
618 # should be a noop, since we already signed
619 git commit --allow-empty --signoff -F expected &&
620 git log -1 --pretty=format:%B >actual &&
621 test_cmp expected actual
624 test_expect_success
'multiple -m' '
628 git commit -m "one" -m "two" -m "three" &&
629 git cat-file commit HEAD >commit &&
630 sed -e "1,/^\$/d" commit >actual &&
638 test_cmp expected actual
642 test_expect_success
'amend commit to fix author' '
644 oldtick=$GIT_AUTHOR_DATE &&
647 git cat-file -p HEAD >commit &&
648 sed -e "s/author.*/author $author $oldtick/" \
649 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
651 git commit --amend --author="$author" &&
652 git cat-file -p HEAD >current &&
653 test_cmp expected current
657 test_expect_success
'git commit <file> with dirty index' '
658 echo tacocat >elif &&
661 git commit elif -m "tacocat is a palindrome" &&
662 git show --stat >stat &&
664 git diff --cached >diff &&
668 test_expect_success
'same tree (single parent)' '
671 test_must_fail git commit -m empty
675 test_expect_success
'same tree (single parent) --allow-empty' '
677 git commit --allow-empty -m "forced empty" &&
678 git cat-file commit HEAD >commit &&
683 test_expect_success
'same tree (merge and amend merge)' '
685 git checkout -b side HEAD^ &&
688 git commit -m "add zero" &&
691 git merge -s ours side -m "empty ok" &&
692 git diff HEAD^ HEAD >actual &&
693 test_must_be_empty actual &&
695 git commit --amend -m "empty really ok" &&
696 git diff HEAD^ HEAD >actual &&
697 test_must_be_empty actual
701 test_expect_success
'amend using the message from another commit' '
705 git commit --allow-empty -m "old commit" &&
706 old=$(git rev-parse --verify HEAD) &&
708 git commit --allow-empty -m "new commit" &&
709 new=$(git rev-parse --verify HEAD) &&
711 git commit --allow-empty --amend -C "$old" &&
712 git show --pretty="format:%ad %s" "$old" >expected &&
713 git show --pretty="format:%ad %s" HEAD >actual &&
714 test_cmp expected actual
718 test_expect_success
'amend using the message from a commit named with tag' '
722 git commit --allow-empty -m "old commit" &&
723 old=$(git rev-parse --verify HEAD) &&
724 git tag -a -m "tag on old" tagged-old HEAD &&
726 git commit --allow-empty -m "new commit" &&
727 new=$(git rev-parse --verify HEAD) &&
729 git commit --allow-empty --amend -C tagged-old &&
730 git show --pretty="format:%ad %s" "$old" >expected &&
731 git show --pretty="format:%ad %s" HEAD >actual &&
732 test_cmp expected actual
736 test_expect_success
'amend can copy notes' '
738 git config notes.rewrite.amend true &&
739 git config notes.rewriteRef "refs/notes/*" &&
741 git notes add -m"a note" &&
743 git commit --amend -m"new foo" &&
744 test "$(git notes show)" = "a note"
748 test_expect_success
'commit a file whose name is a dash' '
750 test_write_lines 1 2 3 4 5 >./- &&
753 git commit -m "add dash" >output </dev/null &&
754 test_grep " changed, 5 insertions" output
757 test_expect_success
'--only works on to-be-born branch' '
758 # This test relies on having something in the index, as it
759 # would not otherwise actually prove much. So check this.
760 test -n "$(git ls-files)" &&
761 git checkout --orphan orphan &&
764 git commit --only newfile -m"--only on unborn branch" &&
765 echo newfile >expected &&
766 git ls-tree -r --name-only HEAD >actual &&
767 test_cmp expected actual
770 test_expect_success
'--dry-run with conflicts fixed from a merge' '
771 # setup two branches with conflicting information
772 # in the same file, resolve the conflict,
773 # call commit with --dry-run
774 echo "Initial contents, unimportant" >test-file &&
776 git commit -m "Initial commit" &&
777 echo "commit-1-state" >test-file &&
778 git commit -m "commit 1" -i test-file &&
780 git checkout -b branch-2 HEAD^1 &&
781 echo "commit-2-state" >test-file &&
782 git commit -m "commit 2" -i test-file &&
783 test_must_fail git merge --no-commit commit-1 &&
784 echo "commit-2-state" >test-file &&
786 git commit --dry-run &&
787 git commit -m "conflicts fixed from merge."
790 test_expect_success
'--dry-run --short' '
793 git commit --dry-run --short