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 # TODO: as for --include, the below command will fail because
105 # nothing is staged. If something was staged, it would not fail
106 # even though the provided pathspec does not match any tracked
107 # path. (However, the untracked paths that match the pathspec are
108 # not committed and only the staged changes get committed.)
109 # In either cases, no error is returned to stderr like in (--only
110 # and without --only/--include) cases. In a similar manner,
111 # "git add -u baz" also does not error out.
113 # Therefore, the below test is just to document the current behavior
114 # and is not an endorsement to the current behavior, and we may
115 # want to fix this. And when that happens, this test should be
116 # updated accordingly.
118 test_must_fail git commit --include -m "baz" baz 2>err &&
119 test_must_be_empty err
122 test_expect_success
'setup: non-initial commit' '
123 echo bongo bongo bongo >file &&
124 git commit -m next -a
127 test_expect_success
'--dry-run with stuff to commit returns ok' '
128 echo bongo bongo bongo >>file &&
129 git commit -m next -a --dry-run
132 test_expect_success
'--short with stuff to commit returns ok' '
133 echo bongo bongo bongo >>file &&
134 git commit -m next -a --short
137 test_expect_success
'--porcelain with stuff to commit returns ok' '
138 echo bongo bongo bongo >>file &&
139 git commit -m next -a --porcelain
142 test_expect_success
'--long with stuff to commit returns ok' '
143 echo bongo bongo bongo >>file &&
144 git commit -m next -a --long
147 for opt
in "" "-o" "--only"
149 test_expect_success
'exclude additional staged changes when given pathspec' '
150 echo content >>file &&
151 echo content >>baz &&
153 git commit $opt -m "file" file &&
155 git diff --name-only >actual &&
156 test_must_be_empty actual &&
158 test_write_lines baz >expect &&
159 git diff --name-only --cached >actual &&
160 test_cmp expect actual &&
162 test_write_lines file >expect &&
163 git diff --name-only HEAD^ HEAD >actual &&
164 test_cmp expect actual
168 test_expect_success
'-i/--include includes staged changes' '
169 echo content >>file &&
170 echo content >>baz &&
173 # baz is in the index, therefore, it will be committed
174 git commit --include -m "file and baz" baz &&
176 git diff --name-only HEAD >remaining &&
177 test_must_be_empty remaining &&
179 test_write_lines baz file >expect &&
180 git diff --name-only HEAD^ HEAD >actual &&
181 test_cmp expect actual
184 test_expect_success
'--include and --only do not mix' '
185 test_when_finished "git reset --hard" &&
186 echo content >>file &&
187 echo content >>baz &&
188 test_must_fail git commit --include --only -m "file baz" file baz 2>actual &&
189 test_grep -e "fatal: options .-i/--include. and .-o/--only. cannot be used together" actual
192 test_expect_success
'commit message from non-existing file' '
193 echo more bongo: bongo bongo bongo bongo >file &&
194 test_must_fail git commit -F gah -a
197 test_expect_success
'empty commit message' '
198 # Empty except stray tabs and spaces on a few lines.
199 sed -e "s/@//g" >msg <<-\EOF &&
203 @Signed-off-by: hula@
205 test_must_fail git commit -F msg -a
208 test_expect_success
'template "emptyness" check does not kick in with -F' '
209 git checkout HEAD file && echo >>file && git add file &&
210 git commit -t file -F file
213 test_expect_success
'template "emptyness" check' '
214 git checkout HEAD file && echo >>file && git add file &&
215 test_must_fail git commit -t file 2>err &&
216 test_grep "did not edit" err
219 test_expect_success
'setup: commit message from file' '
220 git checkout HEAD file && echo >>file && git add file &&
221 echo this is the commit message, coming from a file >msg &&
225 test_expect_success
'amend commit' '
226 cat >editor <<-\EOF &&
228 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
232 EDITOR=./editor git commit --amend
235 test_expect_success
'amend --only ignores staged contents' '
236 cp file file.expect &&
237 echo changed >file &&
239 git commit --no-edit --amend --only &&
240 git cat-file blob HEAD:file >file.actual &&
241 test_cmp file.expect file.actual &&
245 test_expect_success
'allow-empty --only ignores staged contents' '
246 echo changed-again >file &&
248 git commit --allow-empty --only -m "empty" &&
249 git cat-file blob HEAD:file >file.actual &&
250 test_cmp file.expect file.actual &&
254 test_expect_success
'set up editor' '
255 cat >editor <<-\EOF &&
257 sed -e "s/unamended/amended/g" <"$1" >"$1-"
263 test_expect_success
'amend without launching editor' '
264 echo unamended >expect &&
265 git commit --allow-empty -m "unamended" &&
266 echo needs more bongo >file &&
268 EDITOR=./editor git commit --no-edit --amend &&
269 git diff --exit-code HEAD -- file &&
270 git diff-tree -s --format=%s HEAD >msg &&
274 test_expect_success
'--amend --edit' '
275 echo amended >expect &&
276 git commit --allow-empty -m "unamended" &&
277 echo bongo again >file &&
279 EDITOR=./editor git commit --edit --amend &&
280 git diff-tree -s --format=%s HEAD >msg &&
284 test_expect_success
'--amend --edit of empty message' '
285 cat >replace <<-\EOF &&
290 git commit --allow-empty --allow-empty-message -m "" &&
291 echo more bongo >file &&
293 EDITOR=./replace git commit --edit --amend &&
294 git diff-tree -s --format=%s HEAD >msg &&
299 test_expect_success
'--amend to set message to empty' '
302 git commit -m "unamended" &&
303 git commit --amend --allow-empty-message -m "" &&
304 git diff-tree -s --format=%s HEAD >msg &&
309 test_expect_success
'--amend to set empty message needs --allow-empty-message' '
312 git commit -m "unamended" &&
313 test_must_fail git commit --amend -m "" &&
314 git diff-tree -s --format=%s HEAD >msg &&
315 echo "unamended" >expect &&
319 test_expect_success
'-m --edit' '
320 echo amended >expect &&
321 git commit --allow-empty -m buffer &&
322 echo bongo bongo >file &&
324 EDITOR=./editor git commit -m unamended --edit &&
325 git diff-tree -s --format=%s HEAD >msg &&
329 test_expect_success
'-m and -F do not mix' '
330 echo enough with the bongos >file &&
331 test_must_fail git commit -F msg -m amending .
334 test_expect_success
'using message from other commit' '
335 git commit -C HEAD^ .
338 test_expect_success
'editing message from other commit' '
339 cat >editor <<-\EOF &&
341 sed -e "s/amend/older/g" <"$1" >"$1-"
345 echo hula hula >file &&
346 EDITOR=./editor git commit -c HEAD^ -a
349 test_expect_success
'message from stdin' '
350 echo silly new contents >file &&
351 echo commit message from stdin |
355 test_expect_success
'overriding author from command line' '
357 git commit -m author \
358 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
359 grep Rubber.Duck output
362 test_expect_success PERL
'interactive add' '
363 echo 7 | test_must_fail git commit --interactive >out &&
367 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
369 test_must_fail git diff --exit-code >diff1 &&
370 test_write_lines u "*" q |
374 test_must_fail git commit --interactive
377 compare_diff_patch diff1 diff2
380 test_expect_success
'editor not invoked if -F is given' '
381 cat >editor <<-\EOF &&
383 sed -e s/good/bad/g <"$1" >"$1-"
388 echo A good commit message. >msg &&
391 EDITOR=./editor git commit -a -F msg &&
392 git show -s --pretty=format:%s >subject &&
393 grep -q good subject &&
396 echo Another good message. |
397 EDITOR=./editor git commit -a -F - &&
398 git show -s --pretty=format:%s >subject &&
402 test_expect_success
'partial commit that involves removal (1)' '
404 git rm --cached file &&
407 git commit -m "Partial: add elif" elif &&
408 git diff-tree --name-status HEAD^ HEAD >current &&
409 echo "A elif" >expected &&
410 test_cmp expected current
414 test_expect_success
'partial commit that involves removal (2)' '
416 git commit -m "Partial: remove file" file &&
417 git diff-tree --name-status HEAD^ HEAD >current &&
418 echo "D file" >expected &&
419 test_cmp expected current
423 test_expect_success
'partial commit that involves removal (3)' '
425 git rm --cached elif &&
427 git commit -m "Partial: modify elif" elif &&
428 git diff-tree --name-status HEAD^ HEAD >current &&
429 echo "M elif" >expected &&
430 test_cmp expected current
434 test_expect_success
'amend commit to fix author' '
436 oldtick=$GIT_AUTHOR_DATE &&
439 git cat-file -p HEAD >commit &&
440 sed -e "s/author.*/author $author $oldtick/" \
441 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
443 git commit --amend --author="$author" &&
444 git cat-file -p HEAD >current &&
445 test_cmp expected current
449 test_expect_success
'amend commit to fix date' '
452 newtick=$GIT_AUTHOR_DATE &&
454 git cat-file -p HEAD >commit &&
455 sed -e "s/author.*/author $author $newtick/" \
456 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
458 git commit --amend --date="$newtick" &&
459 git cat-file -p HEAD >current &&
460 test_cmp expected current
464 test_expect_success
'amend commit to add signoff' '
466 test_commit "msg" file content &&
467 git commit --amend --signoff &&
468 test_commit_message HEAD <<-EOF
471 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
475 test_expect_success
'amend does not add signoff if it already exists' '
477 test_commit --signoff "tenor" file newcontent &&
478 git commit --amend --signoff &&
479 test_commit_message HEAD <<-EOF
482 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
486 test_expect_success
'commit mentions forced date in output' '
487 git commit --amend --date=2010-01-02T03:04:05 >output &&
488 grep "Date: *Sat Jan 2 03:04:05 2010" output
491 test_expect_success
'commit complains about completely bogus dates' '
492 test_must_fail git commit --amend --date=seventeen
495 test_expect_success
'commit --date allows approxidate' '
497 --date="midnight the 12th of october, anno domini 1979" &&
498 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
499 git log -1 --format=%ad >actual &&
500 test_cmp expect actual
503 test_expect_success
'sign off (1)' '
507 git commit -s -m "thank you" &&
508 git cat-file commit HEAD >commit &&
509 sed -e "1,/^\$/d" commit >actual &&
513 git var GIT_COMMITTER_IDENT >ident &&
514 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
516 test_cmp expected actual
520 test_expect_success
'sign off (2)' '
524 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
525 git commit -s -m "thank you
528 git cat-file commit HEAD >commit &&
529 sed -e "1,/^\$/d" commit >actual &&
534 git var GIT_COMMITTER_IDENT >ident &&
535 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
537 test_cmp expected actual
541 test_expect_success
'signoff gap' '
545 alt="Alt-RFC-822-Header: Value" &&
546 git commit -s -m "welcome
549 git cat-file commit HEAD >commit &&
550 sed -e "1,/^\$/d" commit >actual &&
555 git var GIT_COMMITTER_IDENT >ident &&
556 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
558 test_cmp expected actual
561 test_expect_success
'signoff gap 2' '
566 git commit -s -m "welcome
570 git cat-file commit HEAD >commit &&
571 sed -e "1,/^\$/d" commit >actual &&
578 git var GIT_COMMITTER_IDENT >ident &&
579 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
581 test_cmp expected actual
584 test_expect_success
'signoff respects trailer config' '
588 git commit -s -m "subject
592 git cat-file commit HEAD >commit &&
593 sed -e "1,/^\$/d" commit >actual &&
597 echo non-trailer line &&
600 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
602 test_cmp expected actual &&
606 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
610 git cat-file commit HEAD >commit &&
611 sed -e "1,/^\$/d" commit >actual &&
615 echo non-trailer line &&
617 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
619 test_cmp expected actual
622 test_expect_success
'signoff not confused by ---' '
623 cat >expected <<-EOF &&
628 these dashes confuse the parser!
630 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
632 # should be a noop, since we already signed
633 git commit --allow-empty --signoff -F expected &&
634 git log -1 --pretty=format:%B >actual &&
635 test_cmp expected actual
638 test_expect_success
'multiple -m' '
642 git commit -m "one" -m "two" -m "three" &&
643 git cat-file commit HEAD >commit &&
644 sed -e "1,/^\$/d" commit >actual &&
652 test_cmp expected actual
656 test_expect_success
'amend commit to fix author' '
658 oldtick=$GIT_AUTHOR_DATE &&
661 git cat-file -p HEAD >commit &&
662 sed -e "s/author.*/author $author $oldtick/" \
663 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
665 git commit --amend --author="$author" &&
666 git cat-file -p HEAD >current &&
667 test_cmp expected current
671 test_expect_success
'git commit <file> with dirty index' '
672 echo tacocat >elif &&
675 git commit elif -m "tacocat is a palindrome" &&
676 git show --stat >stat &&
678 git diff --cached >diff &&
682 test_expect_success
'same tree (single parent)' '
685 test_must_fail git commit -m empty
689 test_expect_success
'same tree (single parent) --allow-empty' '
691 git commit --allow-empty -m "forced empty" &&
692 git cat-file commit HEAD >commit &&
697 test_expect_success
'same tree (merge and amend merge)' '
699 git checkout -b side HEAD^ &&
702 git commit -m "add zero" &&
705 git merge -s ours side -m "empty ok" &&
706 git diff HEAD^ HEAD >actual &&
707 test_must_be_empty actual &&
709 git commit --amend -m "empty really ok" &&
710 git diff HEAD^ HEAD >actual &&
711 test_must_be_empty actual
715 test_expect_success
'amend using the message from another commit' '
719 git commit --allow-empty -m "old commit" &&
720 old=$(git rev-parse --verify HEAD) &&
722 git commit --allow-empty -m "new commit" &&
723 new=$(git rev-parse --verify HEAD) &&
725 git commit --allow-empty --amend -C "$old" &&
726 git show --pretty="format:%ad %s" "$old" >expected &&
727 git show --pretty="format:%ad %s" HEAD >actual &&
728 test_cmp expected actual
732 test_expect_success
'amend using the message from a commit named with tag' '
736 git commit --allow-empty -m "old commit" &&
737 old=$(git rev-parse --verify HEAD) &&
738 git tag -a -m "tag on old" tagged-old HEAD &&
740 git commit --allow-empty -m "new commit" &&
741 new=$(git rev-parse --verify HEAD) &&
743 git commit --allow-empty --amend -C tagged-old &&
744 git show --pretty="format:%ad %s" "$old" >expected &&
745 git show --pretty="format:%ad %s" HEAD >actual &&
746 test_cmp expected actual
750 test_expect_success
'amend can copy notes' '
752 git config notes.rewrite.amend true &&
753 git config notes.rewriteRef "refs/notes/*" &&
755 git notes add -m"a note" &&
757 git commit --amend -m"new foo" &&
758 test "$(git notes show)" = "a note"
762 test_expect_success
'commit a file whose name is a dash' '
764 test_write_lines 1 2 3 4 5 >./- &&
767 git commit -m "add dash" >output </dev/null &&
768 test_grep " changed, 5 insertions" output
771 test_expect_success
'--only works on to-be-born branch' '
772 # This test relies on having something in the index, as it
773 # would not otherwise actually prove much. So check this.
774 test -n "$(git ls-files)" &&
775 git checkout --orphan orphan &&
778 git commit --only newfile -m"--only on unborn branch" &&
779 echo newfile >expected &&
780 git ls-tree -r --name-only HEAD >actual &&
781 test_cmp expected actual
784 test_expect_success
'--dry-run with conflicts fixed from a merge' '
785 # setup two branches with conflicting information
786 # in the same file, resolve the conflict,
787 # call commit with --dry-run
788 echo "Initial contents, unimportant" >test-file &&
790 git commit -m "Initial commit" &&
791 echo "commit-1-state" >test-file &&
792 git commit -m "commit 1" -i test-file &&
794 git checkout -b branch-2 HEAD^1 &&
795 echo "commit-2-state" >test-file &&
796 git commit -m "commit 2" -i test-file &&
797 test_must_fail git merge --no-commit commit-1 &&
798 echo "commit-2-state" >test-file &&
800 git commit --dry-run &&
801 git commit -m "conflicts fixed from merge."
804 test_expect_success
'--dry-run --short' '
807 git commit --dry-run --short