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'
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_i18ngrep "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
'setup: non-initial commit' '
96 echo bongo bongo bongo >file &&
100 test_expect_success
'--dry-run with stuff to commit returns ok' '
101 echo bongo bongo bongo >>file &&
102 git commit -m next -a --dry-run
105 test_expect_success
'--short with stuff to commit returns ok' '
106 echo bongo bongo bongo >>file &&
107 git commit -m next -a --short
110 test_expect_success
'--porcelain with stuff to commit returns ok' '
111 echo bongo bongo bongo >>file &&
112 git commit -m next -a --porcelain
115 test_expect_success
'--long with stuff to commit returns ok' '
116 echo bongo bongo bongo >>file &&
117 git commit -m next -a --long
120 test_expect_success
'commit message from non-existing file' '
121 echo more bongo: bongo bongo bongo bongo >file &&
122 test_must_fail git commit -F gah -a
125 test_expect_success
'empty commit message' '
126 # Empty except stray tabs and spaces on a few lines.
127 sed -e "s/@//g" >msg <<-\EOF &&
131 @Signed-off-by: hula@
133 test_must_fail git commit -F msg -a
136 test_expect_success
'template "emptyness" check does not kick in with -F' '
137 git checkout HEAD file && echo >>file && git add file &&
138 git commit -t file -F file
141 test_expect_success
'template "emptyness" check' '
142 git checkout HEAD file && echo >>file && git add file &&
143 test_must_fail git commit -t file 2>err &&
144 test_i18ngrep "did not edit" err
147 test_expect_success
'setup: commit message from file' '
148 git checkout HEAD file && echo >>file && git add file &&
149 echo this is the commit message, coming from a file >msg &&
153 test_expect_success
'amend commit' '
154 cat >editor <<-\EOF &&
156 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
160 EDITOR=./editor git commit --amend
163 test_expect_success
'amend --only ignores staged contents' '
164 cp file file.expect &&
165 echo changed >file &&
167 git commit --no-edit --amend --only &&
168 git cat-file blob HEAD:file >file.actual &&
169 test_cmp file.expect file.actual &&
173 test_expect_success
'allow-empty --only ignores staged contents' '
174 echo changed-again >file &&
176 git commit --allow-empty --only -m "empty" &&
177 git cat-file blob HEAD:file >file.actual &&
178 test_cmp file.expect file.actual &&
182 test_expect_success
'set up editor' '
183 cat >editor <<-\EOF &&
185 sed -e "s/unamended/amended/g" <"$1" >"$1-"
191 test_expect_success
'amend without launching editor' '
192 echo unamended >expect &&
193 git commit --allow-empty -m "unamended" &&
194 echo needs more bongo >file &&
196 EDITOR=./editor git commit --no-edit --amend &&
197 git diff --exit-code HEAD -- file &&
198 git diff-tree -s --format=%s HEAD >msg &&
202 test_expect_success
'--amend --edit' '
203 echo amended >expect &&
204 git commit --allow-empty -m "unamended" &&
205 echo bongo again >file &&
207 EDITOR=./editor git commit --edit --amend &&
208 git diff-tree -s --format=%s HEAD >msg &&
212 test_expect_success
'--amend --edit of empty message' '
213 cat >replace <<-\EOF &&
218 git commit --allow-empty --allow-empty-message -m "" &&
219 echo more bongo >file &&
221 EDITOR=./replace git commit --edit --amend &&
222 git diff-tree -s --format=%s HEAD >msg &&
227 test_expect_success
'--amend to set message to empty' '
230 git commit -m "unamended" &&
231 git commit --amend --allow-empty-message -m "" &&
232 git diff-tree -s --format=%s HEAD >msg &&
237 test_expect_success
'--amend to set empty message needs --allow-empty-message' '
240 git commit -m "unamended" &&
241 test_must_fail git commit --amend -m "" &&
242 git diff-tree -s --format=%s HEAD >msg &&
243 echo "unamended" >expect &&
247 test_expect_success
'-m --edit' '
248 echo amended >expect &&
249 git commit --allow-empty -m buffer &&
250 echo bongo bongo >file &&
252 EDITOR=./editor git commit -m unamended --edit &&
253 git diff-tree -s --format=%s HEAD >msg &&
257 test_expect_success
'-m and -F do not mix' '
258 echo enough with the bongos >file &&
259 test_must_fail git commit -F msg -m amending .
262 test_expect_success
'using message from other commit' '
263 git commit -C HEAD^ .
266 test_expect_success
'editing message from other commit' '
267 cat >editor <<-\EOF &&
269 sed -e "s/amend/older/g" <"$1" >"$1-"
273 echo hula hula >file &&
274 EDITOR=./editor git commit -c HEAD^ -a
277 test_expect_success
'message from stdin' '
278 echo silly new contents >file &&
279 echo commit message from stdin |
283 test_expect_success
'overriding author from command line' '
285 git commit -m author \
286 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
287 grep Rubber.Duck output
290 test_expect_success PERL
'interactive add' '
291 echo 7 | test_must_fail git commit --interactive >out &&
295 test_expect_success PERL
"commit --interactive doesn't change index if editor aborts" '
297 test_must_fail git diff --exit-code >diff1 &&
298 test_write_lines u "*" q |
302 test_must_fail git commit --interactive
305 compare_diff_patch diff1 diff2
308 test_expect_success
'editor not invoked if -F is given' '
309 cat >editor <<-\EOF &&
311 sed -e s/good/bad/g <"$1" >"$1-"
316 echo A good commit message. >msg &&
319 EDITOR=./editor git commit -a -F msg &&
320 git show -s --pretty=format:%s >subject &&
321 grep -q good subject &&
324 echo Another good message. |
325 EDITOR=./editor git commit -a -F - &&
326 git show -s --pretty=format:%s >subject &&
330 test_expect_success
'partial commit that involves removal (1)' '
332 git rm --cached file &&
335 git commit -m "Partial: add elif" elif &&
336 git diff-tree --name-status HEAD^ HEAD >current &&
337 echo "A elif" >expected &&
338 test_cmp expected current
342 test_expect_success
'partial commit that involves removal (2)' '
344 git commit -m "Partial: remove file" file &&
345 git diff-tree --name-status HEAD^ HEAD >current &&
346 echo "D file" >expected &&
347 test_cmp expected current
351 test_expect_success
'partial commit that involves removal (3)' '
353 git rm --cached elif &&
355 git commit -m "Partial: modify elif" elif &&
356 git diff-tree --name-status HEAD^ HEAD >current &&
357 echo "M elif" >expected &&
358 test_cmp expected current
362 test_expect_success
'amend commit to fix author' '
364 oldtick=$GIT_AUTHOR_DATE &&
367 git cat-file -p HEAD >commit &&
368 sed -e "s/author.*/author $author $oldtick/" \
369 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
371 git commit --amend --author="$author" &&
372 git cat-file -p HEAD >current &&
373 test_cmp expected current
377 test_expect_success
'amend commit to fix date' '
380 newtick=$GIT_AUTHOR_DATE &&
382 git cat-file -p HEAD >commit &&
383 sed -e "s/author.*/author $author $newtick/" \
384 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
386 git commit --amend --date="$newtick" &&
387 git cat-file -p HEAD >current &&
388 test_cmp expected current
392 test_expect_success
'commit mentions forced date in output' '
393 git commit --amend --date=2010-01-02T03:04:05 >output &&
394 grep "Date: *Sat Jan 2 03:04:05 2010" output
397 test_expect_success
'commit complains about completely bogus dates' '
398 test_must_fail git commit --amend --date=seventeen
401 test_expect_success
'commit --date allows approxidate' '
403 --date="midnight the 12th of october, anno domini 1979" &&
404 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
405 git log -1 --format=%ad >actual &&
406 test_cmp expect actual
409 test_expect_success
'sign off (1)' '
413 git commit -s -m "thank you" &&
414 git cat-file commit HEAD >commit &&
415 sed -e "1,/^\$/d" commit >actual &&
419 git var GIT_COMMITTER_IDENT >ident &&
420 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
422 test_cmp expected actual
426 test_expect_success
'sign off (2)' '
430 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
431 git commit -s -m "thank you
434 git cat-file commit HEAD >commit &&
435 sed -e "1,/^\$/d" commit >actual &&
440 git var GIT_COMMITTER_IDENT >ident &&
441 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
443 test_cmp expected actual
447 test_expect_success
'signoff gap' '
451 alt="Alt-RFC-822-Header: Value" &&
452 git commit -s -m "welcome
455 git cat-file commit HEAD >commit &&
456 sed -e "1,/^\$/d" commit >actual &&
461 git var GIT_COMMITTER_IDENT >ident &&
462 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
464 test_cmp expected actual
467 test_expect_success
'signoff gap 2' '
472 git commit -s -m "welcome
476 git cat-file commit HEAD >commit &&
477 sed -e "1,/^\$/d" commit >actual &&
484 git var GIT_COMMITTER_IDENT >ident &&
485 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
487 test_cmp expected actual
490 test_expect_success
'signoff respects trailer config' '
494 git commit -s -m "subject
498 git cat-file commit HEAD >commit &&
499 sed -e "1,/^\$/d" commit >actual &&
503 echo non-trailer line &&
506 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
508 test_cmp expected actual &&
512 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
516 git cat-file commit HEAD >commit &&
517 sed -e "1,/^\$/d" commit >actual &&
521 echo non-trailer line &&
523 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
525 test_cmp expected actual
528 test_expect_success
'signoff not confused by ---' '
529 cat >expected <<-EOF &&
534 these dashes confuse the parser!
536 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
538 # should be a noop, since we already signed
539 git commit --allow-empty --signoff -F expected &&
540 git log -1 --pretty=format:%B >actual &&
541 test_cmp expected actual
544 test_expect_success
'multiple -m' '
548 git commit -m "one" -m "two" -m "three" &&
549 git cat-file commit HEAD >commit &&
550 sed -e "1,/^\$/d" commit >actual &&
558 test_cmp expected actual
562 test_expect_success
'amend commit to fix author' '
564 oldtick=$GIT_AUTHOR_DATE &&
567 git cat-file -p HEAD >commit &&
568 sed -e "s/author.*/author $author $oldtick/" \
569 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
571 git commit --amend --author="$author" &&
572 git cat-file -p HEAD >current &&
573 test_cmp expected current
577 test_expect_success
'git commit <file> with dirty index' '
578 echo tacocat >elif &&
581 git commit elif -m "tacocat is a palindrome" &&
582 git show --stat >stat &&
584 git diff --cached >diff &&
588 test_expect_success
'same tree (single parent)' '
591 test_must_fail git commit -m empty
595 test_expect_success
'same tree (single parent) --allow-empty' '
597 git commit --allow-empty -m "forced empty" &&
598 git cat-file commit HEAD >commit &&
603 test_expect_success
'same tree (merge and amend merge)' '
605 git checkout -b side HEAD^ &&
608 git commit -m "add zero" &&
611 git merge -s ours side -m "empty ok" &&
612 git diff HEAD^ HEAD >actual &&
613 test_must_be_empty actual &&
615 git commit --amend -m "empty really ok" &&
616 git diff HEAD^ HEAD >actual &&
617 test_must_be_empty actual
621 test_expect_success
'amend using the message from another commit' '
625 git commit --allow-empty -m "old commit" &&
626 old=$(git rev-parse --verify HEAD) &&
628 git commit --allow-empty -m "new commit" &&
629 new=$(git rev-parse --verify HEAD) &&
631 git commit --allow-empty --amend -C "$old" &&
632 git show --pretty="format:%ad %s" "$old" >expected &&
633 git show --pretty="format:%ad %s" HEAD >actual &&
634 test_cmp expected actual
638 test_expect_success
'amend using the message from a commit named with tag' '
642 git commit --allow-empty -m "old commit" &&
643 old=$(git rev-parse --verify HEAD) &&
644 git tag -a -m "tag on old" tagged-old HEAD &&
646 git commit --allow-empty -m "new commit" &&
647 new=$(git rev-parse --verify HEAD) &&
649 git commit --allow-empty --amend -C tagged-old &&
650 git show --pretty="format:%ad %s" "$old" >expected &&
651 git show --pretty="format:%ad %s" HEAD >actual &&
652 test_cmp expected actual
656 test_expect_success
'amend can copy notes' '
658 git config notes.rewrite.amend true &&
659 git config notes.rewriteRef "refs/notes/*" &&
661 git notes add -m"a note" &&
663 git commit --amend -m"new foo" &&
664 test "$(git notes show)" = "a note"
668 test_expect_success
'commit a file whose name is a dash' '
670 test_write_lines 1 2 3 4 5 >./- &&
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