Git 2.45
[git/gitster.git] / t / t7501-commit-basic-functionality.sh
blobcc12f99f11534b898d11a0fc7bbb5d339730ccc8
1 #!/bin/sh
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-lib.sh
13 . "$TEST_DIRECTORY/lib-diff.sh"
15 author='The Real Author <someguy@his.email.org>'
17 test_tick
19 test_expect_success 'initial status' '
20 echo bongo bongo >file &&
21 git add file &&
22 git status >actual &&
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' '
31 git commit -m initial
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" &&
59 git init foo &&
60 >foo/foo.txt &&
61 git -C foo add foo.txt &&
62 git -C foo commit -m first &&
63 git -C foo rm foo.txt &&
65 mkdir -p foo/bar &&
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' '
74 git reset --hard &&
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)' '
95 echo content >baz &&
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 &&
138 git add 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
152 done
154 test_expect_success '-i/--include includes staged changes' '
155 echo content >>file &&
156 echo content >>baz &&
157 git add file &&
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 &&
208 git commit -F msg -a
211 test_expect_success 'amend commit' '
212 cat >editor <<-\EOF &&
213 #!/bin/sh
214 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
215 mv "$1-" "$1"
217 chmod 755 editor &&
218 EDITOR=./editor git commit --amend
221 test_expect_success 'amend --only ignores staged contents' '
222 cp file file.expect &&
223 echo changed >file &&
224 git add file &&
225 git commit --no-edit --amend --only &&
226 git cat-file blob HEAD:file >file.actual &&
227 test_cmp file.expect file.actual &&
228 git diff --exit-code
231 test_expect_success 'allow-empty --only ignores staged contents' '
232 echo changed-again >file &&
233 git add 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 &&
237 git diff --exit-code
240 test_expect_success 'set up editor' '
241 cat >editor <<-\EOF &&
242 #!/bin/sh
243 sed -e "s/unamended/amended/g" <"$1" >"$1-"
244 mv "$1-" "$1"
246 chmod 755 editor
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 &&
253 git add 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 &&
257 test_cmp expect msg
260 test_expect_success '--amend --edit' '
261 echo amended >expect &&
262 git commit --allow-empty -m "unamended" &&
263 echo bongo again >file &&
264 git add file &&
265 EDITOR=./editor git commit --edit --amend &&
266 git diff-tree -s --format=%s HEAD >msg &&
267 test_cmp expect msg
270 test_expect_success '--amend --edit of empty message' '
271 cat >replace <<-\EOF &&
272 #!/bin/sh
273 echo "amended" >"$1"
275 chmod 755 replace &&
276 git commit --allow-empty --allow-empty-message -m "" &&
277 echo more bongo >file &&
278 git add file &&
279 EDITOR=./replace git commit --edit --amend &&
280 git diff-tree -s --format=%s HEAD >msg &&
281 ./replace expect &&
282 test_cmp expect msg
285 test_expect_success '--amend to set message to empty' '
286 echo bata >file &&
287 git add file &&
288 git commit -m "unamended" &&
289 git commit --amend --allow-empty-message -m "" &&
290 git diff-tree -s --format=%s HEAD >msg &&
291 echo "" >expect &&
292 test_cmp expect msg
295 test_expect_success '--amend to set empty message needs --allow-empty-message' '
296 echo conga >file &&
297 git add file &&
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 &&
302 test_cmp expect msg
305 test_expect_success '-m --edit' '
306 echo amended >expect &&
307 git commit --allow-empty -m buffer &&
308 echo bongo bongo >file &&
309 git add file &&
310 EDITOR=./editor git commit -m unamended --edit &&
311 git diff-tree -s --format=%s HEAD >msg &&
312 test_cmp expect 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 &&
326 #!/bin/sh
327 sed -e "s/amend/older/g" <"$1" >"$1-"
328 mv "$1-" "$1"
330 chmod 755 editor &&
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 |
338 git commit -F - -a
341 test_expect_success 'overriding author from command line' '
342 echo gak >file &&
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 &&
350 grep "What now" out
353 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
354 echo zoo >file &&
355 test_must_fail git diff --exit-code >diff1 &&
356 test_write_lines u "*" q |
358 EDITOR=: &&
359 export EDITOR &&
360 test_must_fail git commit --interactive
361 ) &&
362 git diff >diff2 &&
363 compare_diff_patch diff1 diff2
366 test_expect_success 'editor not invoked if -F is given' '
367 cat >editor <<-\EOF &&
368 #!/bin/sh
369 sed -e s/good/bad/g <"$1" >"$1-"
370 mv "$1-" "$1"
372 chmod 755 editor &&
374 echo A good commit message. >msg &&
375 echo moo >file &&
377 EDITOR=./editor git commit -a -F msg &&
378 git show -s --pretty=format:%s >subject &&
379 grep -q good subject &&
381 echo quack >file &&
382 echo Another good message. |
383 EDITOR=./editor git commit -a -F - &&
384 git show -s --pretty=format:%s >subject &&
385 grep -q good subject
388 test_expect_success 'partial commit that involves removal (1)' '
390 git rm --cached file &&
391 mv file elif &&
392 git add elif &&
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 &&
412 echo elif >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 &&
423 test_tick &&
424 git reset --hard &&
425 git cat-file -p HEAD >commit &&
426 sed -e "s/author.*/author $author $oldtick/" \
427 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
428 commit >expected &&
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' '
437 test_tick &&
438 newtick=$GIT_AUTHOR_DATE &&
439 git reset --hard &&
440 git cat-file -p HEAD >commit &&
441 sed -e "s/author.*/author $author $newtick/" \
442 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
443 commit >expected &&
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
466 tenor
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' '
482 git commit --amend \
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)' '
491 echo 1 >positive &&
492 git add positive &&
493 git commit -s -m "thank you" &&
494 git cat-file commit HEAD >commit &&
495 sed -e "1,/^\$/d" commit >actual &&
497 echo thank you &&
498 echo &&
499 git var GIT_COMMITTER_IDENT >ident &&
500 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
501 ) >expected &&
502 test_cmp expected actual
506 test_expect_success 'sign off (2)' '
508 echo 2 >positive &&
509 git add positive &&
510 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
511 git commit -s -m "thank you
513 $existing" &&
514 git cat-file commit HEAD >commit &&
515 sed -e "1,/^\$/d" commit >actual &&
517 echo thank you &&
518 echo &&
519 echo $existing &&
520 git var GIT_COMMITTER_IDENT >ident &&
521 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
522 ) >expected &&
523 test_cmp expected actual
527 test_expect_success 'signoff gap' '
529 echo 3 >positive &&
530 git add positive &&
531 alt="Alt-RFC-822-Header: Value" &&
532 git commit -s -m "welcome
534 $alt" &&
535 git cat-file commit HEAD >commit &&
536 sed -e "1,/^\$/d" commit >actual &&
538 echo welcome &&
539 echo &&
540 echo $alt &&
541 git var GIT_COMMITTER_IDENT >ident &&
542 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
543 ) >expected &&
544 test_cmp expected actual
547 test_expect_success 'signoff gap 2' '
549 echo 4 >positive &&
550 git add positive &&
551 alt="fixed: 34" &&
552 git commit -s -m "welcome
554 We have now
555 $alt" &&
556 git cat-file commit HEAD >commit &&
557 sed -e "1,/^\$/d" commit >actual &&
559 echo welcome &&
560 echo &&
561 echo We have now &&
562 echo $alt &&
563 echo &&
564 git var GIT_COMMITTER_IDENT >ident &&
565 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
566 ) >expected &&
567 test_cmp expected actual
570 test_expect_success 'signoff respects trailer config' '
572 echo 5 >positive &&
573 git add positive &&
574 git commit -s -m "subject
576 non-trailer line
577 Myfooter: x" &&
578 git cat-file commit HEAD >commit &&
579 sed -e "1,/^\$/d" commit >actual &&
581 echo subject &&
582 echo &&
583 echo non-trailer line &&
584 echo Myfooter: x &&
585 echo &&
586 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
587 ) >expected &&
588 test_cmp expected actual &&
590 echo 6 >positive &&
591 git add positive &&
592 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
594 non-trailer line
595 Myfooter: x" &&
596 git cat-file commit HEAD >commit &&
597 sed -e "1,/^\$/d" commit >actual &&
599 echo subject &&
600 echo &&
601 echo non-trailer line &&
602 echo Myfooter: x &&
603 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
604 ) >expected &&
605 test_cmp expected actual
608 test_expect_success 'signoff not confused by ---' '
609 cat >expected <<-EOF &&
610 subject
612 body
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' '
626 >negative &&
627 git add negative &&
628 git commit -m "one" -m "two" -m "three" &&
629 git cat-file commit HEAD >commit &&
630 sed -e "1,/^\$/d" commit >actual &&
632 echo one &&
633 echo &&
634 echo two &&
635 echo &&
636 echo three
637 ) >expected &&
638 test_cmp expected actual
642 test_expect_success 'amend commit to fix author' '
644 oldtick=$GIT_AUTHOR_DATE &&
645 test_tick &&
646 git reset --hard &&
647 git cat-file -p HEAD >commit &&
648 sed -e "s/author.*/author $author $oldtick/" \
649 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
650 commit >expected &&
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 &&
659 echo tehlulz >chz &&
660 git add chz &&
661 git commit elif -m "tacocat is a palindrome" &&
662 git show --stat >stat &&
663 grep elif stat &&
664 git diff --cached >diff &&
665 grep chz diff
668 test_expect_success 'same tree (single parent)' '
670 git reset --hard &&
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 &&
679 grep forced commit
683 test_expect_success 'same tree (merge and amend merge)' '
685 git checkout -b side HEAD^ &&
686 echo zero >zero &&
687 git add zero &&
688 git commit -m "add zero" &&
689 git checkout main &&
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' '
703 git reset --hard &&
704 test_tick &&
705 git commit --allow-empty -m "old commit" &&
706 old=$(git rev-parse --verify HEAD) &&
707 test_tick &&
708 git commit --allow-empty -m "new commit" &&
709 new=$(git rev-parse --verify HEAD) &&
710 test_tick &&
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' '
720 git reset --hard &&
721 test_tick &&
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 &&
725 test_tick &&
726 git commit --allow-empty -m "new commit" &&
727 new=$(git rev-parse --verify HEAD) &&
728 test_tick &&
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/*" &&
740 test_commit foo &&
741 git notes add -m"a note" &&
742 test_tick &&
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' '
749 git reset --hard &&
750 test_write_lines 1 2 3 4 5 >./- &&
751 git add ./- &&
752 test_tick &&
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 &&
762 echo foo >newfile &&
763 git add newfile &&
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 &&
775 git add test-file &&
776 git commit -m "Initial commit" &&
777 echo "commit-1-state" >test-file &&
778 git commit -m "commit 1" -i test-file &&
779 git tag commit-1 &&
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 &&
785 git add test-file &&
786 git commit --dry-run &&
787 git commit -m "conflicts fixed from merge."
790 test_expect_success '--dry-run --short' '
791 >test-file &&
792 git add test-file &&
793 git commit --dry-run --short
796 test_done