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
12 TEST_PASSES_SANITIZE_LEAK
=true
14 .
"$TEST_DIRECTORY/lib-diff.sh"
16 author
='The Real Author <someguy@his.email.org>'
20 test_expect_success
'initial status' '
21 echo bongo bongo >file &&
24 test_grep "No commits yet" actual
27 test_expect_success
'fail initial amend' '
28 test_must_fail git commit --amend
31 test_expect_success
'setup: initial commit' '
35 test_expect_success
'-m and -F do not mix' '
36 git checkout HEAD file && echo >>file && git add file &&
37 test_must_fail git commit -m foo -m bar -F file
40 test_expect_success
'-m and -C do not mix' '
41 git checkout HEAD file && echo >>file && git add file &&
42 test_must_fail git commit -C HEAD -m illegal
45 test_expect_success
'paths and -a do not mix' '
46 echo King of the bongo >file &&
47 test_must_fail git commit -m foo -a file
50 test_expect_success PERL
'can use paths with --interactive' '
51 echo bong-o-bong >file &&
52 # 2: update, 1:st path, that is all, 7: quit
53 test_write_lines 2 1 "" 7 |
54 git commit -m foo --interactive file &&
55 git reset --hard HEAD^
58 test_expect_success
'removed files and relative paths' '
59 test_when_finished "rm -rf foo" &&
62 git -C foo add foo.txt &&
63 git -C foo commit -m first &&
64 git -C foo rm foo.txt &&
67 git -C foo/bar commit -m second ../foo.txt
70 test_expect_success
'using invalid commit with -C' '
71 test_must_fail git commit --allow-empty -C bogus
74 test_expect_success
'nothing to commit' '
76 test_must_fail git commit -m initial
79 test_expect_success
'--dry-run fails with nothing to commit' '
80 test_must_fail git commit -m initial --dry-run
83 test_expect_success
'--short fails with nothing to commit' '
84 test_must_fail git commit -m initial --short
87 test_expect_success
'--porcelain fails with nothing to commit' '
88 test_must_fail git commit -m initial --porcelain
91 test_expect_success
'--long fails with nothing to commit' '
92 test_must_fail git commit -m initial --long
95 test_expect_success
'fail to commit untracked file (even with --include/--only)' '
97 error="error: pathspec .baz. did not match any file(s) known to git" &&
99 test_must_fail git commit -m "baz" baz 2>err &&
100 test_grep -e "$error" err &&
102 test_must_fail git commit --only -m "baz" baz 2>err &&
103 test_grep -e "$error" err &&
105 test_must_fail git commit --include -m "baz" baz 2>err &&
106 test_grep -e "$error" err
109 test_expect_success
'setup: non-initial commit' '
110 echo bongo bongo bongo >file &&
111 git commit -m next -a
114 test_expect_success
'--dry-run with stuff to commit returns ok' '
115 echo bongo bongo bongo >>file &&
116 git commit -m next -a --dry-run
119 test_expect_success
'--short with stuff to commit returns ok' '
120 echo bongo bongo bongo >>file &&
121 git commit -m next -a --short
124 test_expect_success
'--porcelain with stuff to commit returns ok' '
125 echo bongo bongo bongo >>file &&
126 git commit -m next -a --porcelain
129 test_expect_success
'--long with stuff to commit returns ok' '
130 echo bongo bongo bongo >>file &&
131 git commit -m next -a --long
134 for opt
in "" "-o" "--only"
136 test_expect_success
'exclude additional staged changes when given pathspec' '
137 echo content >>file &&
138 echo content >>baz &&
140 git commit $opt -m "file" file &&
142 git diff --name-only >actual &&
143 test_must_be_empty actual &&
145 test_write_lines baz >expect &&
146 git diff --name-only --cached >actual &&
147 test_cmp expect actual &&
149 test_write_lines file >expect &&
150 git diff --name-only HEAD^ HEAD >actual &&
151 test_cmp expect actual
155 test_expect_success
'-i/--include includes staged changes' '
156 echo content >>file &&
157 echo content >>baz &&
160 # baz is in the index, therefore, it will be committed
161 git commit --include -m "file and baz" baz &&
163 git diff --name-only HEAD >remaining &&
164 test_must_be_empty remaining &&
166 test_write_lines baz file >expect &&
167 git diff --name-only HEAD^ HEAD >actual &&
168 test_cmp expect actual
171 test_expect_success
'--include and --only do not mix' '
172 test_when_finished "git reset --hard" &&
173 echo content >>file &&
174 echo content >>baz &&
175 test_must_fail git commit --include --only -m "file baz" file baz 2>actual &&
176 test_grep -e "fatal: options .-i/--include. and .-o/--only. cannot be used together" actual
179 test_expect_success
'commit message from non-existing file' '
180 echo more bongo: bongo bongo bongo bongo >file &&
181 test_must_fail git commit -F gah -a
184 test_expect_success
'empty commit message' '
185 # Empty except stray tabs and spaces on a few lines.
186 sed -e "s/@//g" >msg <<-\EOF &&
190 @Signed-off-by: hula@
192 test_must_fail git commit -F msg -a
195 test_expect_success
'template "emptyness" check does not kick in with -F' '
196 git checkout HEAD file && echo >>file && git add file &&
197 git commit -t file -F file
200 test_expect_success
'template "emptyness" check' '
201 git checkout HEAD file && echo >>file && git add file &&
202 test_must_fail git commit -t file 2>err &&
203 test_grep "did not edit" err
206 test_expect_success
'setup: commit message from file' '
207 git checkout HEAD file && echo >>file && git add file &&
208 echo this is the commit message, coming from a file >msg &&
212 test_expect_success
'amend commit' '
213 cat >editor <<-\EOF &&
215 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
219 EDITOR=./editor git commit --amend
222 test_expect_success
'amend --only ignores staged contents' '
223 cp file file.expect &&
224 echo changed >file &&
226 git commit --no-edit --amend --only &&
227 git cat-file blob HEAD:file >file.actual &&
228 test_cmp file.expect file.actual &&
232 test_expect_success
'allow-empty --only ignores staged contents' '
233 echo changed-again >file &&
235 git commit --allow-empty --only -m "empty" &&
236 git cat-file blob HEAD:file >file.actual &&
237 test_cmp file.expect file.actual &&
241 test_expect_success
'set up editor' '
242 cat >editor <<-\EOF &&
244 sed -e "s/unamended/amended/g" <"$1" >"$1-"
250 test_expect_success
'amend without launching editor' '
251 echo unamended >expect &&
252 git commit --allow-empty -m "unamended" &&
253 echo needs more bongo >file &&
255 EDITOR=./editor git commit --no-edit --amend &&
256 git diff --exit-code HEAD -- file &&
257 git diff-tree -s --format=%s HEAD >msg &&
261 test_expect_success
'--amend --edit' '
262 echo amended >expect &&
263 git commit --allow-empty -m "unamended" &&
264 echo bongo again >file &&
266 EDITOR=./editor git commit --edit --amend &&
267 git diff-tree -s --format=%s HEAD >msg &&
271 test_expect_success
'--amend --edit of empty message' '
272 cat >replace <<-\EOF &&
277 git commit --allow-empty --allow-empty-message -m "" &&
278 echo more bongo >file &&
280 EDITOR=./replace git commit --edit --amend &&
281 git diff-tree -s --format=%s HEAD >msg &&
286 test_expect_success
'--amend to set message to empty' '
289 git commit -m "unamended" &&
290 git commit --amend --allow-empty-message -m "" &&
291 git diff-tree -s --format=%s HEAD >msg &&
296 test_expect_success
'--amend to set empty message needs --allow-empty-message' '
299 git commit -m "unamended" &&
300 test_must_fail git commit --amend -m "" &&
301 git diff-tree -s --format=%s HEAD >msg &&
302 echo "unamended" >expect &&
306 test_expect_success
'-m --edit' '
307 echo amended >expect &&
308 git commit --allow-empty -m buffer &&
309 echo bongo bongo >file &&
311 EDITOR=./editor git commit -m unamended --edit &&
312 git diff-tree -s --format=%s HEAD >msg &&
316 test_expect_success
'-m and -F do not mix' '
317 echo enough with the bongos >file &&
318 test_must_fail git commit -F msg -m amending .
321 test_expect_success
'using message from other commit' '
322 git commit -C HEAD^ .
325 test_expect_success
'editing message from other commit' '
326 cat >editor <<-\EOF &&
328 sed -e "s/amend/older/g" <"$1" >"$1-"
332 echo hula hula >file &&
333 EDITOR=./editor git commit -c HEAD^ -a
336 test_expect_success
'message from stdin' '
337 echo silly new contents >file &&
338 echo commit message from stdin |
342 test_expect_success
'overriding author from command line' '
344 git commit -m author \
345 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
346 grep Rubber.Duck output
349 test_expect_success PERL
'interactive add' '
350 echo 7 | test_must_fail git commit --interactive >out &&
354 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
356 test_must_fail git diff --exit-code >diff1 &&
357 test_write_lines u "*" q |
361 test_must_fail git commit --interactive
364 compare_diff_patch diff1 diff2
367 test_expect_success
'editor not invoked if -F is given' '
368 cat >editor <<-\EOF &&
370 sed -e s/good/bad/g <"$1" >"$1-"
375 echo A good commit message. >msg &&
378 EDITOR=./editor git commit -a -F msg &&
379 git show -s --pretty=format:%s >subject &&
380 grep -q good subject &&
383 echo Another good message. |
384 EDITOR=./editor git commit -a -F - &&
385 git show -s --pretty=format:%s >subject &&
389 test_expect_success
'partial commit that involves removal (1)' '
391 git rm --cached file &&
394 git commit -m "Partial: add elif" elif &&
395 git diff-tree --name-status HEAD^ HEAD >current &&
396 echo "A elif" >expected &&
397 test_cmp expected current
401 test_expect_success
'partial commit that involves removal (2)' '
403 git commit -m "Partial: remove file" file &&
404 git diff-tree --name-status HEAD^ HEAD >current &&
405 echo "D file" >expected &&
406 test_cmp expected current
410 test_expect_success
'partial commit that involves removal (3)' '
412 git rm --cached elif &&
414 git commit -m "Partial: modify elif" elif &&
415 git diff-tree --name-status HEAD^ HEAD >current &&
416 echo "M elif" >expected &&
417 test_cmp expected current
421 test_expect_success
'amend commit to fix author' '
423 oldtick=$GIT_AUTHOR_DATE &&
426 git cat-file -p HEAD >commit &&
427 sed -e "s/author.*/author $author $oldtick/" \
428 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
430 git commit --amend --author="$author" &&
431 git cat-file -p HEAD >current &&
432 test_cmp expected current
436 test_expect_success
'amend commit to fix date' '
439 newtick=$GIT_AUTHOR_DATE &&
441 git cat-file -p HEAD >commit &&
442 sed -e "s/author.*/author $author $newtick/" \
443 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
445 git commit --amend --date="$newtick" &&
446 git cat-file -p HEAD >current &&
447 test_cmp expected current
451 test_expect_success
'amend commit to add signoff' '
453 test_commit "msg" file content &&
454 git commit --amend --signoff &&
455 test_commit_message HEAD <<-EOF
458 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
462 test_expect_success
'amend does not add signoff if it already exists' '
464 test_commit --signoff "tenor" file newcontent &&
465 git commit --amend --signoff &&
466 test_commit_message HEAD <<-EOF
469 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
473 test_expect_success
'commit mentions forced date in output' '
474 git commit --amend --date=2010-01-02T03:04:05 >output &&
475 grep "Date: *Sat Jan 2 03:04:05 2010" output
478 test_expect_success
'commit complains about completely bogus dates' '
479 test_must_fail git commit --amend --date=seventeen
482 test_expect_success
'commit --date allows approxidate' '
484 --date="midnight the 12th of october, anno domini 1979" &&
485 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
486 git log -1 --format=%ad >actual &&
487 test_cmp expect actual
490 test_expect_success
'sign off (1)' '
494 git commit -s -m "thank you" &&
495 git cat-file commit HEAD >commit &&
496 sed -e "1,/^\$/d" commit >actual &&
500 git var GIT_COMMITTER_IDENT >ident &&
501 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
503 test_cmp expected actual
507 test_expect_success
'sign off (2)' '
511 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
512 git commit -s -m "thank you
515 git cat-file commit HEAD >commit &&
516 sed -e "1,/^\$/d" commit >actual &&
521 git var GIT_COMMITTER_IDENT >ident &&
522 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
524 test_cmp expected actual
528 test_expect_success
'signoff gap' '
532 alt="Alt-RFC-822-Header: Value" &&
533 git commit -s -m "welcome
536 git cat-file commit HEAD >commit &&
537 sed -e "1,/^\$/d" commit >actual &&
542 git var GIT_COMMITTER_IDENT >ident &&
543 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
545 test_cmp expected actual
548 test_expect_success
'signoff gap 2' '
553 git commit -s -m "welcome
557 git cat-file commit HEAD >commit &&
558 sed -e "1,/^\$/d" commit >actual &&
565 git var GIT_COMMITTER_IDENT >ident &&
566 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
568 test_cmp expected actual
571 test_expect_success
'signoff respects trailer config' '
575 git commit -s -m "subject
579 git cat-file commit HEAD >commit &&
580 sed -e "1,/^\$/d" commit >actual &&
584 echo non-trailer line &&
587 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
589 test_cmp expected actual &&
593 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
597 git cat-file commit HEAD >commit &&
598 sed -e "1,/^\$/d" commit >actual &&
602 echo non-trailer line &&
604 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
606 test_cmp expected actual
609 test_expect_success
'signoff not confused by ---' '
610 cat >expected <<-EOF &&
615 these dashes confuse the parser!
617 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
619 # should be a noop, since we already signed
620 git commit --allow-empty --signoff -F expected &&
621 git log -1 --pretty=format:%B >actual &&
622 test_cmp expected actual
625 test_expect_success
'multiple -m' '
629 git commit -m "one" -m "two" -m "three" &&
630 git cat-file commit HEAD >commit &&
631 sed -e "1,/^\$/d" commit >actual &&
639 test_cmp expected actual
643 test_expect_success
'amend commit to fix author' '
645 oldtick=$GIT_AUTHOR_DATE &&
648 git cat-file -p HEAD >commit &&
649 sed -e "s/author.*/author $author $oldtick/" \
650 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
652 git commit --amend --author="$author" &&
653 git cat-file -p HEAD >current &&
654 test_cmp expected current
658 test_expect_success
'git commit <file> with dirty index' '
659 echo tacocat >elif &&
662 git commit elif -m "tacocat is a palindrome" &&
663 git show --stat >stat &&
665 git diff --cached >diff &&
669 test_expect_success
'same tree (single parent)' '
672 test_must_fail git commit -m empty
676 test_expect_success
'same tree (single parent) --allow-empty' '
678 git commit --allow-empty -m "forced empty" &&
679 git cat-file commit HEAD >commit &&
684 test_expect_success
'same tree (merge and amend merge)' '
686 git checkout -b side HEAD^ &&
689 git commit -m "add zero" &&
692 git merge -s ours side -m "empty ok" &&
693 git diff HEAD^ HEAD >actual &&
694 test_must_be_empty actual &&
696 git commit --amend -m "empty really ok" &&
697 git diff HEAD^ HEAD >actual &&
698 test_must_be_empty actual
702 test_expect_success
'amend using the message from another commit' '
706 git commit --allow-empty -m "old commit" &&
707 old=$(git rev-parse --verify HEAD) &&
709 git commit --allow-empty -m "new commit" &&
710 new=$(git rev-parse --verify HEAD) &&
712 git commit --allow-empty --amend -C "$old" &&
713 git show --pretty="format:%ad %s" "$old" >expected &&
714 git show --pretty="format:%ad %s" HEAD >actual &&
715 test_cmp expected actual
719 test_expect_success
'amend using the message from a commit named with tag' '
723 git commit --allow-empty -m "old commit" &&
724 old=$(git rev-parse --verify HEAD) &&
725 git tag -a -m "tag on old" tagged-old HEAD &&
727 git commit --allow-empty -m "new commit" &&
728 new=$(git rev-parse --verify HEAD) &&
730 git commit --allow-empty --amend -C tagged-old &&
731 git show --pretty="format:%ad %s" "$old" >expected &&
732 git show --pretty="format:%ad %s" HEAD >actual &&
733 test_cmp expected actual
737 test_expect_success
'amend can copy notes' '
739 git config notes.rewrite.amend true &&
740 git config notes.rewriteRef "refs/notes/*" &&
742 git notes add -m"a note" &&
744 git commit --amend -m"new foo" &&
745 test "$(git notes show)" = "a note"
749 test_expect_success
'commit a file whose name is a dash' '
751 test_write_lines 1 2 3 4 5 >./- &&
754 git commit -m "add dash" >output </dev/null &&
755 test_grep " changed, 5 insertions" output
758 test_expect_success
'--only works on to-be-born branch' '
759 # This test relies on having something in the index, as it
760 # would not otherwise actually prove much. So check this.
761 test -n "$(git ls-files)" &&
762 git checkout --orphan orphan &&
765 git commit --only newfile -m"--only on unborn branch" &&
766 echo newfile >expected &&
767 git ls-tree -r --name-only HEAD >actual &&
768 test_cmp expected actual
771 test_expect_success
'--dry-run with conflicts fixed from a merge' '
772 # setup two branches with conflicting information
773 # in the same file, resolve the conflict,
774 # call commit with --dry-run
775 echo "Initial contents, unimportant" >test-file &&
777 git commit -m "Initial commit" &&
778 echo "commit-1-state" >test-file &&
779 git commit -m "commit 1" -i test-file &&
781 git checkout -b branch-2 HEAD^1 &&
782 echo "commit-2-state" >test-file &&
783 git commit -m "commit 2" -i test-file &&
784 test_must_fail git merge --no-commit commit-1 &&
785 echo "commit-2-state" >test-file &&
787 git commit --dry-run &&
788 git commit -m "conflicts fixed from merge."
791 test_expect_success
'--dry-run --short' '
794 git commit --dry-run --short