Merge branch 'ps/pack-refs-auto' into jt/reftable-geometric-compaction
[git.git] / t / t7501-commit-basic-functionality.sh
blobbced44a0fc915f430ccc41d54fbd8bc48df2cef8
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 # 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 &&
152 git add 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
166 done
168 test_expect_success '-i/--include includes staged changes' '
169 echo content >>file &&
170 echo content >>baz &&
171 git add file &&
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 &&
222 git commit -F msg -a
225 test_expect_success 'amend commit' '
226 cat >editor <<-\EOF &&
227 #!/bin/sh
228 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
229 mv "$1-" "$1"
231 chmod 755 editor &&
232 EDITOR=./editor git commit --amend
235 test_expect_success 'amend --only ignores staged contents' '
236 cp file file.expect &&
237 echo changed >file &&
238 git add file &&
239 git commit --no-edit --amend --only &&
240 git cat-file blob HEAD:file >file.actual &&
241 test_cmp file.expect file.actual &&
242 git diff --exit-code
245 test_expect_success 'allow-empty --only ignores staged contents' '
246 echo changed-again >file &&
247 git add 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 &&
251 git diff --exit-code
254 test_expect_success 'set up editor' '
255 cat >editor <<-\EOF &&
256 #!/bin/sh
257 sed -e "s/unamended/amended/g" <"$1" >"$1-"
258 mv "$1-" "$1"
260 chmod 755 editor
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 &&
267 git add 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 &&
271 test_cmp expect msg
274 test_expect_success '--amend --edit' '
275 echo amended >expect &&
276 git commit --allow-empty -m "unamended" &&
277 echo bongo again >file &&
278 git add file &&
279 EDITOR=./editor git commit --edit --amend &&
280 git diff-tree -s --format=%s HEAD >msg &&
281 test_cmp expect msg
284 test_expect_success '--amend --edit of empty message' '
285 cat >replace <<-\EOF &&
286 #!/bin/sh
287 echo "amended" >"$1"
289 chmod 755 replace &&
290 git commit --allow-empty --allow-empty-message -m "" &&
291 echo more bongo >file &&
292 git add file &&
293 EDITOR=./replace git commit --edit --amend &&
294 git diff-tree -s --format=%s HEAD >msg &&
295 ./replace expect &&
296 test_cmp expect msg
299 test_expect_success '--amend to set message to empty' '
300 echo bata >file &&
301 git add file &&
302 git commit -m "unamended" &&
303 git commit --amend --allow-empty-message -m "" &&
304 git diff-tree -s --format=%s HEAD >msg &&
305 echo "" >expect &&
306 test_cmp expect msg
309 test_expect_success '--amend to set empty message needs --allow-empty-message' '
310 echo conga >file &&
311 git add file &&
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 &&
316 test_cmp expect msg
319 test_expect_success '-m --edit' '
320 echo amended >expect &&
321 git commit --allow-empty -m buffer &&
322 echo bongo bongo >file &&
323 git add file &&
324 EDITOR=./editor git commit -m unamended --edit &&
325 git diff-tree -s --format=%s HEAD >msg &&
326 test_cmp expect 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 &&
340 #!/bin/sh
341 sed -e "s/amend/older/g" <"$1" >"$1-"
342 mv "$1-" "$1"
344 chmod 755 editor &&
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 |
352 git commit -F - -a
355 test_expect_success 'overriding author from command line' '
356 echo gak >file &&
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 &&
364 grep "What now" out
367 test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
368 echo zoo >file &&
369 test_must_fail git diff --exit-code >diff1 &&
370 test_write_lines u "*" q |
372 EDITOR=: &&
373 export EDITOR &&
374 test_must_fail git commit --interactive
375 ) &&
376 git diff >diff2 &&
377 compare_diff_patch diff1 diff2
380 test_expect_success 'editor not invoked if -F is given' '
381 cat >editor <<-\EOF &&
382 #!/bin/sh
383 sed -e s/good/bad/g <"$1" >"$1-"
384 mv "$1-" "$1"
386 chmod 755 editor &&
388 echo A good commit message. >msg &&
389 echo moo >file &&
391 EDITOR=./editor git commit -a -F msg &&
392 git show -s --pretty=format:%s >subject &&
393 grep -q good subject &&
395 echo quack >file &&
396 echo Another good message. |
397 EDITOR=./editor git commit -a -F - &&
398 git show -s --pretty=format:%s >subject &&
399 grep -q good subject
402 test_expect_success 'partial commit that involves removal (1)' '
404 git rm --cached file &&
405 mv file elif &&
406 git add elif &&
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 &&
426 echo elif >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 &&
437 test_tick &&
438 git reset --hard &&
439 git cat-file -p HEAD >commit &&
440 sed -e "s/author.*/author $author $oldtick/" \
441 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
442 commit >expected &&
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' '
451 test_tick &&
452 newtick=$GIT_AUTHOR_DATE &&
453 git reset --hard &&
454 git cat-file -p HEAD >commit &&
455 sed -e "s/author.*/author $author $newtick/" \
456 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
457 commit >expected &&
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
480 tenor
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' '
496 git commit --amend \
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)' '
505 echo 1 >positive &&
506 git add positive &&
507 git commit -s -m "thank you" &&
508 git cat-file commit HEAD >commit &&
509 sed -e "1,/^\$/d" commit >actual &&
511 echo thank you &&
512 echo &&
513 git var GIT_COMMITTER_IDENT >ident &&
514 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
515 ) >expected &&
516 test_cmp expected actual
520 test_expect_success 'sign off (2)' '
522 echo 2 >positive &&
523 git add positive &&
524 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
525 git commit -s -m "thank you
527 $existing" &&
528 git cat-file commit HEAD >commit &&
529 sed -e "1,/^\$/d" commit >actual &&
531 echo thank you &&
532 echo &&
533 echo $existing &&
534 git var GIT_COMMITTER_IDENT >ident &&
535 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
536 ) >expected &&
537 test_cmp expected actual
541 test_expect_success 'signoff gap' '
543 echo 3 >positive &&
544 git add positive &&
545 alt="Alt-RFC-822-Header: Value" &&
546 git commit -s -m "welcome
548 $alt" &&
549 git cat-file commit HEAD >commit &&
550 sed -e "1,/^\$/d" commit >actual &&
552 echo welcome &&
553 echo &&
554 echo $alt &&
555 git var GIT_COMMITTER_IDENT >ident &&
556 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
557 ) >expected &&
558 test_cmp expected actual
561 test_expect_success 'signoff gap 2' '
563 echo 4 >positive &&
564 git add positive &&
565 alt="fixed: 34" &&
566 git commit -s -m "welcome
568 We have now
569 $alt" &&
570 git cat-file commit HEAD >commit &&
571 sed -e "1,/^\$/d" commit >actual &&
573 echo welcome &&
574 echo &&
575 echo We have now &&
576 echo $alt &&
577 echo &&
578 git var GIT_COMMITTER_IDENT >ident &&
579 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
580 ) >expected &&
581 test_cmp expected actual
584 test_expect_success 'signoff respects trailer config' '
586 echo 5 >positive &&
587 git add positive &&
588 git commit -s -m "subject
590 non-trailer line
591 Myfooter: x" &&
592 git cat-file commit HEAD >commit &&
593 sed -e "1,/^\$/d" commit >actual &&
595 echo subject &&
596 echo &&
597 echo non-trailer line &&
598 echo Myfooter: x &&
599 echo &&
600 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
601 ) >expected &&
602 test_cmp expected actual &&
604 echo 6 >positive &&
605 git add positive &&
606 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
608 non-trailer line
609 Myfooter: x" &&
610 git cat-file commit HEAD >commit &&
611 sed -e "1,/^\$/d" commit >actual &&
613 echo subject &&
614 echo &&
615 echo non-trailer line &&
616 echo Myfooter: x &&
617 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
618 ) >expected &&
619 test_cmp expected actual
622 test_expect_success 'signoff not confused by ---' '
623 cat >expected <<-EOF &&
624 subject
626 body
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' '
640 >negative &&
641 git add negative &&
642 git commit -m "one" -m "two" -m "three" &&
643 git cat-file commit HEAD >commit &&
644 sed -e "1,/^\$/d" commit >actual &&
646 echo one &&
647 echo &&
648 echo two &&
649 echo &&
650 echo three
651 ) >expected &&
652 test_cmp expected actual
656 test_expect_success 'amend commit to fix author' '
658 oldtick=$GIT_AUTHOR_DATE &&
659 test_tick &&
660 git reset --hard &&
661 git cat-file -p HEAD >commit &&
662 sed -e "s/author.*/author $author $oldtick/" \
663 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
664 commit >expected &&
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 &&
673 echo tehlulz >chz &&
674 git add chz &&
675 git commit elif -m "tacocat is a palindrome" &&
676 git show --stat >stat &&
677 grep elif stat &&
678 git diff --cached >diff &&
679 grep chz diff
682 test_expect_success 'same tree (single parent)' '
684 git reset --hard &&
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 &&
693 grep forced commit
697 test_expect_success 'same tree (merge and amend merge)' '
699 git checkout -b side HEAD^ &&
700 echo zero >zero &&
701 git add zero &&
702 git commit -m "add zero" &&
703 git checkout main &&
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' '
717 git reset --hard &&
718 test_tick &&
719 git commit --allow-empty -m "old commit" &&
720 old=$(git rev-parse --verify HEAD) &&
721 test_tick &&
722 git commit --allow-empty -m "new commit" &&
723 new=$(git rev-parse --verify HEAD) &&
724 test_tick &&
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' '
734 git reset --hard &&
735 test_tick &&
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 &&
739 test_tick &&
740 git commit --allow-empty -m "new commit" &&
741 new=$(git rev-parse --verify HEAD) &&
742 test_tick &&
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/*" &&
754 test_commit foo &&
755 git notes add -m"a note" &&
756 test_tick &&
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' '
763 git reset --hard &&
764 test_write_lines 1 2 3 4 5 >./- &&
765 git add ./- &&
766 test_tick &&
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 &&
776 echo foo >newfile &&
777 git add newfile &&
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 &&
789 git add test-file &&
790 git commit -m "Initial commit" &&
791 echo "commit-1-state" >test-file &&
792 git commit -m "commit 1" -i test-file &&
793 git tag commit-1 &&
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 &&
799 git add test-file &&
800 git commit --dry-run &&
801 git commit -m "conflicts fixed from merge."
804 test_expect_success '--dry-run --short' '
805 >test-file &&
806 git add test-file &&
807 git commit --dry-run --short
810 test_done