The eighteenth batch
[git.git] / t / t7501-commit-basic-functionality.sh
blob52f5e28154e2ff797bf352ea916e92599912ac19
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_PASSES_SANITIZE_LEAK=true
13 . ./test-lib.sh
14 . "$TEST_DIRECTORY/lib-diff.sh"
16 author='The Real Author <someguy@his.email.org>'
18 test_tick
20 test_expect_success 'initial status' '
21 echo bongo bongo >file &&
22 git add file &&
23 git status >actual &&
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' '
32 git commit -m initial
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" &&
60 git init foo &&
61 >foo/foo.txt &&
62 git -C foo add foo.txt &&
63 git -C foo commit -m first &&
64 git -C foo rm foo.txt &&
66 mkdir -p foo/bar &&
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' '
75 git reset --hard &&
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)' '
96 echo content >baz &&
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 &&
139 git add 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
153 done
155 test_expect_success '-i/--include includes staged changes' '
156 echo content >>file &&
157 echo content >>baz &&
158 git add file &&
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 &&
209 git commit -F msg -a
212 test_expect_success 'amend commit' '
213 cat >editor <<-\EOF &&
214 #!/bin/sh
215 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
216 mv "$1-" "$1"
218 chmod 755 editor &&
219 EDITOR=./editor git commit --amend
222 test_expect_success 'amend --only ignores staged contents' '
223 cp file file.expect &&
224 echo changed >file &&
225 git add file &&
226 git commit --no-edit --amend --only &&
227 git cat-file blob HEAD:file >file.actual &&
228 test_cmp file.expect file.actual &&
229 git diff --exit-code
232 test_expect_success 'allow-empty --only ignores staged contents' '
233 echo changed-again >file &&
234 git add 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 &&
238 git diff --exit-code
241 test_expect_success 'set up editor' '
242 cat >editor <<-\EOF &&
243 #!/bin/sh
244 sed -e "s/unamended/amended/g" <"$1" >"$1-"
245 mv "$1-" "$1"
247 chmod 755 editor
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 &&
254 git add 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 &&
258 test_cmp expect msg
261 test_expect_success '--amend --edit' '
262 echo amended >expect &&
263 git commit --allow-empty -m "unamended" &&
264 echo bongo again >file &&
265 git add file &&
266 EDITOR=./editor git commit --edit --amend &&
267 git diff-tree -s --format=%s HEAD >msg &&
268 test_cmp expect msg
271 test_expect_success '--amend --edit of empty message' '
272 cat >replace <<-\EOF &&
273 #!/bin/sh
274 echo "amended" >"$1"
276 chmod 755 replace &&
277 git commit --allow-empty --allow-empty-message -m "" &&
278 echo more bongo >file &&
279 git add file &&
280 EDITOR=./replace git commit --edit --amend &&
281 git diff-tree -s --format=%s HEAD >msg &&
282 ./replace expect &&
283 test_cmp expect msg
286 test_expect_success '--amend to set message to empty' '
287 echo bata >file &&
288 git add file &&
289 git commit -m "unamended" &&
290 git commit --amend --allow-empty-message -m "" &&
291 git diff-tree -s --format=%s HEAD >msg &&
292 echo "" >expect &&
293 test_cmp expect msg
296 test_expect_success '--amend to set empty message needs --allow-empty-message' '
297 echo conga >file &&
298 git add file &&
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 &&
303 test_cmp expect msg
306 test_expect_success '-m --edit' '
307 echo amended >expect &&
308 git commit --allow-empty -m buffer &&
309 echo bongo bongo >file &&
310 git add file &&
311 EDITOR=./editor git commit -m unamended --edit &&
312 git diff-tree -s --format=%s HEAD >msg &&
313 test_cmp expect 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 &&
327 #!/bin/sh
328 sed -e "s/amend/older/g" <"$1" >"$1-"
329 mv "$1-" "$1"
331 chmod 755 editor &&
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 |
339 git commit -F - -a
342 test_expect_success 'overriding author from command line' '
343 echo gak >file &&
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 &&
351 grep "What now" out
354 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
355 echo zoo >file &&
356 test_must_fail git diff --exit-code >diff1 &&
357 test_write_lines u "*" q |
359 EDITOR=: &&
360 export EDITOR &&
361 test_must_fail git commit --interactive
362 ) &&
363 git diff >diff2 &&
364 compare_diff_patch diff1 diff2
367 test_expect_success 'editor not invoked if -F is given' '
368 cat >editor <<-\EOF &&
369 #!/bin/sh
370 sed -e s/good/bad/g <"$1" >"$1-"
371 mv "$1-" "$1"
373 chmod 755 editor &&
375 echo A good commit message. >msg &&
376 echo moo >file &&
378 EDITOR=./editor git commit -a -F msg &&
379 git show -s --pretty=format:%s >subject &&
380 grep -q good subject &&
382 echo quack >file &&
383 echo Another good message. |
384 EDITOR=./editor git commit -a -F - &&
385 git show -s --pretty=format:%s >subject &&
386 grep -q good subject
389 test_expect_success 'partial commit that involves removal (1)' '
391 git rm --cached file &&
392 mv file elif &&
393 git add elif &&
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 &&
413 echo elif >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 &&
424 test_tick &&
425 git reset --hard &&
426 git cat-file -p HEAD >commit &&
427 sed -e "s/author.*/author $author $oldtick/" \
428 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
429 commit >expected &&
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' '
438 test_tick &&
439 newtick=$GIT_AUTHOR_DATE &&
440 git reset --hard &&
441 git cat-file -p HEAD >commit &&
442 sed -e "s/author.*/author $author $newtick/" \
443 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
444 commit >expected &&
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
467 tenor
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' '
483 git commit --amend \
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)' '
492 echo 1 >positive &&
493 git add positive &&
494 git commit -s -m "thank you" &&
495 git cat-file commit HEAD >commit &&
496 sed -e "1,/^\$/d" commit >actual &&
498 echo thank you &&
499 echo &&
500 git var GIT_COMMITTER_IDENT >ident &&
501 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
502 ) >expected &&
503 test_cmp expected actual
507 test_expect_success 'sign off (2)' '
509 echo 2 >positive &&
510 git add positive &&
511 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
512 git commit -s -m "thank you
514 $existing" &&
515 git cat-file commit HEAD >commit &&
516 sed -e "1,/^\$/d" commit >actual &&
518 echo thank you &&
519 echo &&
520 echo $existing &&
521 git var GIT_COMMITTER_IDENT >ident &&
522 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
523 ) >expected &&
524 test_cmp expected actual
528 test_expect_success 'signoff gap' '
530 echo 3 >positive &&
531 git add positive &&
532 alt="Alt-RFC-822-Header: Value" &&
533 git commit -s -m "welcome
535 $alt" &&
536 git cat-file commit HEAD >commit &&
537 sed -e "1,/^\$/d" commit >actual &&
539 echo welcome &&
540 echo &&
541 echo $alt &&
542 git var GIT_COMMITTER_IDENT >ident &&
543 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
544 ) >expected &&
545 test_cmp expected actual
548 test_expect_success 'signoff gap 2' '
550 echo 4 >positive &&
551 git add positive &&
552 alt="fixed: 34" &&
553 git commit -s -m "welcome
555 We have now
556 $alt" &&
557 git cat-file commit HEAD >commit &&
558 sed -e "1,/^\$/d" commit >actual &&
560 echo welcome &&
561 echo &&
562 echo We have now &&
563 echo $alt &&
564 echo &&
565 git var GIT_COMMITTER_IDENT >ident &&
566 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
567 ) >expected &&
568 test_cmp expected actual
571 test_expect_success 'signoff respects trailer config' '
573 echo 5 >positive &&
574 git add positive &&
575 git commit -s -m "subject
577 non-trailer line
578 Myfooter: x" &&
579 git cat-file commit HEAD >commit &&
580 sed -e "1,/^\$/d" commit >actual &&
582 echo subject &&
583 echo &&
584 echo non-trailer line &&
585 echo Myfooter: x &&
586 echo &&
587 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
588 ) >expected &&
589 test_cmp expected actual &&
591 echo 6 >positive &&
592 git add positive &&
593 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
595 non-trailer line
596 Myfooter: x" &&
597 git cat-file commit HEAD >commit &&
598 sed -e "1,/^\$/d" commit >actual &&
600 echo subject &&
601 echo &&
602 echo non-trailer line &&
603 echo Myfooter: x &&
604 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
605 ) >expected &&
606 test_cmp expected actual
609 test_expect_success 'signoff not confused by ---' '
610 cat >expected <<-EOF &&
611 subject
613 body
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' '
627 >negative &&
628 git add negative &&
629 git commit -m "one" -m "two" -m "three" &&
630 git cat-file commit HEAD >commit &&
631 sed -e "1,/^\$/d" commit >actual &&
633 echo one &&
634 echo &&
635 echo two &&
636 echo &&
637 echo three
638 ) >expected &&
639 test_cmp expected actual
643 test_expect_success 'amend commit to fix author' '
645 oldtick=$GIT_AUTHOR_DATE &&
646 test_tick &&
647 git reset --hard &&
648 git cat-file -p HEAD >commit &&
649 sed -e "s/author.*/author $author $oldtick/" \
650 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
651 commit >expected &&
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 &&
660 echo tehlulz >chz &&
661 git add chz &&
662 git commit elif -m "tacocat is a palindrome" &&
663 git show --stat >stat &&
664 grep elif stat &&
665 git diff --cached >diff &&
666 grep chz diff
669 test_expect_success 'same tree (single parent)' '
671 git reset --hard &&
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 &&
680 grep forced commit
684 test_expect_success 'same tree (merge and amend merge)' '
686 git checkout -b side HEAD^ &&
687 echo zero >zero &&
688 git add zero &&
689 git commit -m "add zero" &&
690 git checkout main &&
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' '
704 git reset --hard &&
705 test_tick &&
706 git commit --allow-empty -m "old commit" &&
707 old=$(git rev-parse --verify HEAD) &&
708 test_tick &&
709 git commit --allow-empty -m "new commit" &&
710 new=$(git rev-parse --verify HEAD) &&
711 test_tick &&
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' '
721 git reset --hard &&
722 test_tick &&
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 &&
726 test_tick &&
727 git commit --allow-empty -m "new commit" &&
728 new=$(git rev-parse --verify HEAD) &&
729 test_tick &&
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/*" &&
741 test_commit foo &&
742 git notes add -m"a note" &&
743 test_tick &&
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' '
750 git reset --hard &&
751 test_write_lines 1 2 3 4 5 >./- &&
752 git add ./- &&
753 test_tick &&
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 &&
763 echo foo >newfile &&
764 git add newfile &&
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 &&
776 git add test-file &&
777 git commit -m "Initial commit" &&
778 echo "commit-1-state" >test-file &&
779 git commit -m "commit 1" -i test-file &&
780 git tag commit-1 &&
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 &&
786 git add test-file &&
787 git commit --dry-run &&
788 git commit -m "conflicts fixed from merge."
791 test_expect_success '--dry-run --short' '
792 >test-file &&
793 git add test-file &&
794 git commit --dry-run --short
797 test_done