3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
6 # FIXME: Test the various index usages, test reflog,
9 test_description
='git commit'
10 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
11 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
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 # TODO: as for --include, the below command will fail because
106 # nothing is staged. If something was staged, it would not fail
107 # even though the provided pathspec does not match any tracked
108 # path. (However, the untracked paths that match the pathspec are
109 # not committed and only the staged changes get committed.)
110 # In either cases, no error is returned to stderr like in (--only
111 # and without --only/--include) cases. In a similar manner,
112 # "git add -u baz" also does not error out.
114 # Therefore, the below test is just to document the current behavior
115 # and is not an endorsement to the current behavior, and we may
116 # want to fix this. And when that happens, this test should be
117 # updated accordingly.
119 test_must_fail git commit --include -m "baz" baz 2>err &&
120 test_must_be_empty err
123 test_expect_success
'setup: non-initial commit' '
124 echo bongo bongo bongo >file &&
125 git commit -m next -a
128 test_expect_success
'--dry-run with stuff to commit returns ok' '
129 echo bongo bongo bongo >>file &&
130 git commit -m next -a --dry-run
133 test_expect_success
'--short with stuff to commit returns ok' '
134 echo bongo bongo bongo >>file &&
135 git commit -m next -a --short
138 test_expect_success
'--porcelain with stuff to commit returns ok' '
139 echo bongo bongo bongo >>file &&
140 git commit -m next -a --porcelain
143 test_expect_success
'--long with stuff to commit returns ok' '
144 echo bongo bongo bongo >>file &&
145 git commit -m next -a --long
148 for opt
in "" "-o" "--only"
150 test_expect_success
'exclude additional staged changes when given pathspec' '
151 echo content >>file &&
152 echo content >>baz &&
154 git commit $opt -m "file" file &&
156 git diff --name-only >actual &&
157 test_must_be_empty actual &&
159 test_write_lines baz >expect &&
160 git diff --name-only --cached >actual &&
161 test_cmp expect actual &&
163 test_write_lines file >expect &&
164 git diff --name-only HEAD^ HEAD >actual &&
165 test_cmp expect actual
169 test_expect_success
'-i/--include includes staged changes' '
170 echo content >>file &&
171 echo content >>baz &&
174 # baz is in the index, therefore, it will be committed
175 git commit --include -m "file and baz" baz &&
177 git diff --name-only HEAD >remaining &&
178 test_must_be_empty remaining &&
180 test_write_lines baz file >expect &&
181 git diff --name-only HEAD^ HEAD >actual &&
182 test_cmp expect actual
185 test_expect_success
'--include and --only do not mix' '
186 test_when_finished "git reset --hard" &&
187 echo content >>file &&
188 echo content >>baz &&
189 test_must_fail git commit --include --only -m "file baz" file baz 2>actual &&
190 test_grep -e "fatal: options .-i/--include. and .-o/--only. cannot be used together" actual
193 test_expect_success
'commit message from non-existing file' '
194 echo more bongo: bongo bongo bongo bongo >file &&
195 test_must_fail git commit -F gah -a
198 test_expect_success
'empty commit message' '
199 # Empty except stray tabs and spaces on a few lines.
200 sed -e "s/@//g" >msg <<-\EOF &&
204 @Signed-off-by: hula@
206 test_must_fail git commit -F msg -a
209 test_expect_success
'template "emptyness" check does not kick in with -F' '
210 git checkout HEAD file && echo >>file && git add file &&
211 git commit -t file -F file
214 test_expect_success
'template "emptyness" check' '
215 git checkout HEAD file && echo >>file && git add file &&
216 test_must_fail git commit -t file 2>err &&
217 test_grep "did not edit" err
220 test_expect_success
'setup: commit message from file' '
221 git checkout HEAD file && echo >>file && git add file &&
222 echo this is the commit message, coming from a file >msg &&
226 test_expect_success
'amend commit' '
227 cat >editor <<-\EOF &&
229 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
233 EDITOR=./editor git commit --amend
236 test_expect_success
'amend --only ignores staged contents' '
237 cp file file.expect &&
238 echo changed >file &&
240 git commit --no-edit --amend --only &&
241 git cat-file blob HEAD:file >file.actual &&
242 test_cmp file.expect file.actual &&
246 test_expect_success
'allow-empty --only ignores staged contents' '
247 echo changed-again >file &&
249 git commit --allow-empty --only -m "empty" &&
250 git cat-file blob HEAD:file >file.actual &&
251 test_cmp file.expect file.actual &&
255 test_expect_success
'set up editor' '
256 cat >editor <<-\EOF &&
258 sed -e "s/unamended/amended/g" <"$1" >"$1-"
264 test_expect_success
'amend without launching editor' '
265 echo unamended >expect &&
266 git commit --allow-empty -m "unamended" &&
267 echo needs more bongo >file &&
269 EDITOR=./editor git commit --no-edit --amend &&
270 git diff --exit-code HEAD -- file &&
271 git diff-tree -s --format=%s HEAD >msg &&
275 test_expect_success
'--amend --edit' '
276 echo amended >expect &&
277 git commit --allow-empty -m "unamended" &&
278 echo bongo again >file &&
280 EDITOR=./editor git commit --edit --amend &&
281 git diff-tree -s --format=%s HEAD >msg &&
285 test_expect_success
'--amend --edit of empty message' '
286 cat >replace <<-\EOF &&
291 git commit --allow-empty --allow-empty-message -m "" &&
292 echo more bongo >file &&
294 EDITOR=./replace git commit --edit --amend &&
295 git diff-tree -s --format=%s HEAD >msg &&
300 test_expect_success
'--amend to set message to empty' '
303 git commit -m "unamended" &&
304 git commit --amend --allow-empty-message -m "" &&
305 git diff-tree -s --format=%s HEAD >msg &&
310 test_expect_success
'--amend to set empty message needs --allow-empty-message' '
313 git commit -m "unamended" &&
314 test_must_fail git commit --amend -m "" &&
315 git diff-tree -s --format=%s HEAD >msg &&
316 echo "unamended" >expect &&
320 test_expect_success
'-m --edit' '
321 echo amended >expect &&
322 git commit --allow-empty -m buffer &&
323 echo bongo bongo >file &&
325 EDITOR=./editor git commit -m unamended --edit &&
326 git diff-tree -s --format=%s HEAD >msg &&
330 test_expect_success
'-m and -F do not mix' '
331 echo enough with the bongos >file &&
332 test_must_fail git commit -F msg -m amending .
335 test_expect_success
'using message from other commit' '
336 git commit -C HEAD^ .
339 test_expect_success
'editing message from other commit' '
340 cat >editor <<-\EOF &&
342 sed -e "s/amend/older/g" <"$1" >"$1-"
346 echo hula hula >file &&
347 EDITOR=./editor git commit -c HEAD^ -a
350 test_expect_success
'message from stdin' '
351 echo silly new contents >file &&
352 echo commit message from stdin |
356 test_expect_success
'overriding author from command line' '
358 git commit -m author \
359 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
360 grep Rubber.Duck output
363 test_expect_success PERL
'interactive add' '
364 echo 7 | test_must_fail git commit --interactive >out &&
368 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
370 test_must_fail git diff --exit-code >diff1 &&
371 test_write_lines u "*" q |
375 test_must_fail git commit --interactive
378 compare_diff_patch diff1 diff2
381 test_expect_success
'editor not invoked if -F is given' '
382 cat >editor <<-\EOF &&
384 sed -e s/good/bad/g <"$1" >"$1-"
389 echo A good commit message. >msg &&
392 EDITOR=./editor git commit -a -F msg &&
393 git show -s --pretty=format:%s >subject &&
394 grep -q good subject &&
397 echo Another good message. |
398 EDITOR=./editor git commit -a -F - &&
399 git show -s --pretty=format:%s >subject &&
403 test_expect_success
'partial commit that involves removal (1)' '
405 git rm --cached file &&
408 git commit -m "Partial: add elif" elif &&
409 git diff-tree --name-status HEAD^ HEAD >current &&
410 echo "A elif" >expected &&
411 test_cmp expected current
415 test_expect_success
'partial commit that involves removal (2)' '
417 git commit -m "Partial: remove file" file &&
418 git diff-tree --name-status HEAD^ HEAD >current &&
419 echo "D file" >expected &&
420 test_cmp expected current
424 test_expect_success
'partial commit that involves removal (3)' '
426 git rm --cached elif &&
428 git commit -m "Partial: modify elif" elif &&
429 git diff-tree --name-status HEAD^ HEAD >current &&
430 echo "M elif" >expected &&
431 test_cmp expected current
435 test_expect_success
'amend commit to fix author' '
437 oldtick=$GIT_AUTHOR_DATE &&
440 git cat-file -p HEAD >commit &&
441 sed -e "s/author.*/author $author $oldtick/" \
442 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
444 git commit --amend --author="$author" &&
445 git cat-file -p HEAD >current &&
446 test_cmp expected current
450 test_expect_success
'amend commit to fix date' '
453 newtick=$GIT_AUTHOR_DATE &&
455 git cat-file -p HEAD >commit &&
456 sed -e "s/author.*/author $author $newtick/" \
457 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
459 git commit --amend --date="$newtick" &&
460 git cat-file -p HEAD >current &&
461 test_cmp expected current
465 test_expect_success
'commit mentions forced date in output' '
466 git commit --amend --date=2010-01-02T03:04:05 >output &&
467 grep "Date: *Sat Jan 2 03:04:05 2010" output
470 test_expect_success
'commit complains about completely bogus dates' '
471 test_must_fail git commit --amend --date=seventeen
474 test_expect_success
'commit --date allows approxidate' '
476 --date="midnight the 12th of october, anno domini 1979" &&
477 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
478 git log -1 --format=%ad >actual &&
479 test_cmp expect actual
482 test_expect_success
'sign off (1)' '
486 git commit -s -m "thank you" &&
487 git cat-file commit HEAD >commit &&
488 sed -e "1,/^\$/d" commit >actual &&
492 git var GIT_COMMITTER_IDENT >ident &&
493 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
495 test_cmp expected actual
499 test_expect_success
'sign off (2)' '
503 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
504 git commit -s -m "thank you
507 git cat-file commit HEAD >commit &&
508 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
'signoff gap' '
524 alt="Alt-RFC-822-Header: Value" &&
525 git commit -s -m "welcome
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
540 test_expect_success
'signoff gap 2' '
545 git commit -s -m "welcome
549 git cat-file commit HEAD >commit &&
550 sed -e "1,/^\$/d" commit >actual &&
557 git var GIT_COMMITTER_IDENT >ident &&
558 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
560 test_cmp expected actual
563 test_expect_success
'signoff respects trailer config' '
567 git commit -s -m "subject
571 git cat-file commit HEAD >commit &&
572 sed -e "1,/^\$/d" commit >actual &&
576 echo non-trailer line &&
579 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
581 test_cmp expected actual &&
585 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
589 git cat-file commit HEAD >commit &&
590 sed -e "1,/^\$/d" commit >actual &&
594 echo non-trailer line &&
596 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
598 test_cmp expected actual
601 test_expect_success
'signoff not confused by ---' '
602 cat >expected <<-EOF &&
607 these dashes confuse the parser!
609 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
611 # should be a noop, since we already signed
612 git commit --allow-empty --signoff -F expected &&
613 git log -1 --pretty=format:%B >actual &&
614 test_cmp expected actual
617 test_expect_success
'multiple -m' '
621 git commit -m "one" -m "two" -m "three" &&
622 git cat-file commit HEAD >commit &&
623 sed -e "1,/^\$/d" commit >actual &&
631 test_cmp expected actual
635 test_expect_success
'amend commit to fix author' '
637 oldtick=$GIT_AUTHOR_DATE &&
640 git cat-file -p HEAD >commit &&
641 sed -e "s/author.*/author $author $oldtick/" \
642 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
644 git commit --amend --author="$author" &&
645 git cat-file -p HEAD >current &&
646 test_cmp expected current
650 test_expect_success
'git commit <file> with dirty index' '
651 echo tacocat >elif &&
654 git commit elif -m "tacocat is a palindrome" &&
655 git show --stat >stat &&
657 git diff --cached >diff &&
661 test_expect_success
'same tree (single parent)' '
664 test_must_fail git commit -m empty
668 test_expect_success
'same tree (single parent) --allow-empty' '
670 git commit --allow-empty -m "forced empty" &&
671 git cat-file commit HEAD >commit &&
676 test_expect_success
'same tree (merge and amend merge)' '
678 git checkout -b side HEAD^ &&
681 git commit -m "add zero" &&
684 git merge -s ours side -m "empty ok" &&
685 git diff HEAD^ HEAD >actual &&
686 test_must_be_empty actual &&
688 git commit --amend -m "empty really ok" &&
689 git diff HEAD^ HEAD >actual &&
690 test_must_be_empty actual
694 test_expect_success
'amend using the message from another commit' '
698 git commit --allow-empty -m "old commit" &&
699 old=$(git rev-parse --verify HEAD) &&
701 git commit --allow-empty -m "new commit" &&
702 new=$(git rev-parse --verify HEAD) &&
704 git commit --allow-empty --amend -C "$old" &&
705 git show --pretty="format:%ad %s" "$old" >expected &&
706 git show --pretty="format:%ad %s" HEAD >actual &&
707 test_cmp expected actual
711 test_expect_success
'amend using the message from a commit named with tag' '
715 git commit --allow-empty -m "old commit" &&
716 old=$(git rev-parse --verify HEAD) &&
717 git tag -a -m "tag on old" tagged-old HEAD &&
719 git commit --allow-empty -m "new commit" &&
720 new=$(git rev-parse --verify HEAD) &&
722 git commit --allow-empty --amend -C tagged-old &&
723 git show --pretty="format:%ad %s" "$old" >expected &&
724 git show --pretty="format:%ad %s" HEAD >actual &&
725 test_cmp expected actual
729 test_expect_success
'amend can copy notes' '
731 git config notes.rewrite.amend true &&
732 git config notes.rewriteRef "refs/notes/*" &&
734 git notes add -m"a note" &&
736 git commit --amend -m"new foo" &&
737 test "$(git notes show)" = "a note"
741 test_expect_success
'commit a file whose name is a dash' '
743 test_write_lines 1 2 3 4 5 >./- &&
746 git commit -m "add dash" >output </dev/null &&
747 test_grep " changed, 5 insertions" output
750 test_expect_success
'--only works on to-be-born branch' '
751 # This test relies on having something in the index, as it
752 # would not otherwise actually prove much. So check this.
753 test -n "$(git ls-files)" &&
754 git checkout --orphan orphan &&
757 git commit --only newfile -m"--only on unborn branch" &&
758 echo newfile >expected &&
759 git ls-tree -r --name-only HEAD >actual &&
760 test_cmp expected actual
763 test_expect_success
'--dry-run with conflicts fixed from a merge' '
764 # setup two branches with conflicting information
765 # in the same file, resolve the conflict,
766 # call commit with --dry-run
767 echo "Initial contents, unimportant" >test-file &&
769 git commit -m "Initial commit" &&
770 echo "commit-1-state" >test-file &&
771 git commit -m "commit 1" -i test-file &&
773 git checkout -b branch-2 HEAD^1 &&
774 echo "commit-2-state" >test-file &&
775 git commit -m "commit 2" -i test-file &&
776 test_must_fail git merge --no-commit commit-1 &&
777 echo "commit-2-state" >test-file &&
779 git commit --dry-run &&
780 git commit -m "conflicts fixed from merge."
783 test_expect_success
'--dry-run --short' '
786 git commit --dry-run --short