3 test_description
='git commit porcelain-ish'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 expect
=commit_msg_is.expect
12 actual
=commit_msg_is.actual
14 printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
15 printf "%s" "$1" >$expect &&
16 test_cmp
$expect $actual
19 # Arguments: [<prefix] [<commit message>] [<commit options>]
20 check_summary_oneline
() {
22 git commit
${3+"$3"} -m "$2" >raw
&&
26 SUMMARY_PREFIX
="$(git name-rev --name-only HEAD)" &&
28 # append the "special" prefix, like "root-commit", "detached HEAD"
31 SUMMARY_PREFIX
="$SUMMARY_PREFIX ($1)"
35 SUMMARY_POSTFIX
="$(git log -1 --pretty='format:%h')"
36 echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp
&&
41 trailer_commit_base
() {
44 git commit
-s --trailer "Signed-off-by=C1 E1 " \
45 --trailer "Helped-by:C2 E2 " \
46 --trailer "Reported-by=C3 E3" \
47 --trailer "Mentored-by:C4 E4" \
51 test_expect_success
'output summary format' '
55 check_summary_oneline "root-commit" "initial" &&
57 echo change >>file1 &&
61 test_expect_success
'output summary format: root-commit' '
62 check_summary_oneline "" "a change"
65 test_expect_success
'output summary format for commit with an empty diff' '
67 check_summary_oneline "" "empty" "--allow-empty"
70 test_expect_success
'output summary format for merges' '
72 git checkout -b recursive-base &&
73 test_commit base file1 &&
75 git checkout -b recursive-a recursive-base &&
76 test_commit commit-a file1 &&
78 git checkout -b recursive-b recursive-base &&
79 test_commit commit-b file1 &&
82 git checkout recursive-a &&
83 test_must_fail git merge recursive-b &&
84 # resolve the conflict
85 echo commit-a >file1 &&
87 check_summary_oneline "" "Merge"
90 output_tests_cleanup
() {
91 # this is needed for "do not fire editor in the presence of conflicts"
94 # this is needed for the "partial removal" test to pass
96 git commit
-m "cleanup"
99 test_expect_success
'the basics' '
101 output_tests_cleanup &&
103 echo doing partial >"commit is" &&
105 echo very much encouraged but we should >not/forbid &&
106 git add "commit is" not &&
107 echo update added "commit is" file >"commit is" &&
108 echo also update another >not/forbid &&
110 git commit -a -m "initial with -a" &&
112 git cat-file blob HEAD:"commit is" >current.1 &&
113 git cat-file blob HEAD:not/forbid >current.2 &&
115 cmp current.1 "commit is" &&
116 cmp current.2 not/forbid
120 test_expect_success
'partial' '
122 echo another >"commit is" &&
123 echo another >not/forbid &&
125 git commit -m "partial commit to handle a file" "commit is" &&
127 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
128 test "$changed" = "commit is"
132 test_expect_success
'partial modification in a subdirectory' '
135 git commit -m "partial commit to subdirectory" not &&
137 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
138 test "$changed" = "not/forbid"
142 test_expect_success
'partial removal' '
145 git commit -m "partial commit to remove not/forbid" not &&
147 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
148 test "$changed" = "not/forbid" &&
149 remain=$(git ls-tree -r --name-only HEAD) &&
150 test "$remain" = "commit is"
154 test_expect_success
'sign off' '
158 git commit -s -m "thank you" &&
159 git cat-file commit HEAD >commit.msg &&
160 sed -ne "s/Signed-off-by: //p" commit.msg >actual &&
161 git var GIT_COMMITTER_IDENT >ident &&
162 sed -e "s/>.*/>/" ident >expected &&
163 test_cmp expected actual
167 test_expect_success
'commit --trailer with "="' '
168 trailer_commit_base &&
169 cat >expected <<-\EOF &&
172 Signed-off-by: C O Mitter <committer@example.com>
178 git cat-file commit HEAD >commit.msg &&
179 sed -e "1,/^\$/d" commit.msg >actual &&
180 test_cmp expected actual
183 test_expect_success
'commit --trailer with -c and "replace" as ifexists' '
184 trailer_commit_base &&
185 cat >expected <<-\EOF &&
188 Signed-off-by: C O Mitter <committer@example.com>
194 git -c trailer.ifexists="replace" \
195 commit --trailer "Mentored-by: C4 E4" \
196 --trailer "Helped-by: C3 E3" \
198 git cat-file commit HEAD >commit.msg &&
199 sed -e "1,/^\$/d" commit.msg >actual &&
200 test_cmp expected actual
203 test_expect_success
'commit --trailer with -c and "add" as ifexists' '
204 trailer_commit_base &&
205 cat >expected <<-\EOF &&
208 Signed-off-by: C O Mitter <committer@example.com>
216 git -c trailer.ifexists="add" \
217 commit --trailer "Reported-by: C3 E3" \
218 --trailer "Mentored-by: C4 E4" \
220 git cat-file commit HEAD >commit.msg &&
221 sed -e "1,/^\$/d" commit.msg >actual &&
222 test_cmp expected actual
225 test_expect_success
'commit --trailer with -c and "donothing" as ifexists' '
226 trailer_commit_base &&
227 cat >expected <<-\EOF &&
230 Signed-off-by: C O Mitter <committer@example.com>
237 git -c trailer.ifexists="donothing" \
238 commit --trailer "Mentored-by: C5 E5" \
239 --trailer "Reviewed-by: C6 E6" \
241 git cat-file commit HEAD >commit.msg &&
242 sed -e "1,/^\$/d" commit.msg >actual &&
243 test_cmp expected actual
246 test_expect_success
'commit --trailer with -c and "addIfDifferent" as ifexists' '
247 trailer_commit_base &&
248 cat >expected <<-\EOF &&
251 Signed-off-by: C O Mitter <committer@example.com>
258 git -c trailer.ifexists="addIfDifferent" \
259 commit --trailer "Reported-by: C3 E3" \
260 --trailer "Mentored-by: C5 E5" \
262 git cat-file commit HEAD >commit.msg &&
263 sed -e "1,/^\$/d" commit.msg >actual &&
264 test_cmp expected actual
267 test_expect_success
'commit --trailer with -c and "addIfDifferentNeighbor" as ifexists' '
268 trailer_commit_base &&
269 cat >expected <<-\EOF &&
272 Signed-off-by: C O Mitter <committer@example.com>
279 git -c trailer.ifexists="addIfDifferentNeighbor" \
280 commit --trailer "Mentored-by: C4 E4" \
281 --trailer "Reported-by: C3 E3" \
283 git cat-file commit HEAD >commit.msg &&
284 sed -e "1,/^\$/d" commit.msg >actual &&
285 test_cmp expected actual
288 test_expect_success
'commit --trailer with -c and "end" as where' '
289 trailer_commit_base &&
290 cat >expected <<-\EOF &&
293 Signed-off-by: C O Mitter <committer@example.com>
301 git -c trailer.where="end" \
302 commit --trailer "Reported-by: C3 E3" \
303 --trailer "Mentored-by: C4 E4" \
305 git cat-file commit HEAD >commit.msg &&
306 sed -e "1,/^\$/d" commit.msg >actual &&
307 test_cmp expected actual
310 test_expect_success
'commit --trailer with -c and "start" as where' '
311 trailer_commit_base &&
312 cat >expected <<-\EOF &&
316 Signed-off-by: C O Mitter <committer@example.com>
322 git -c trailer.where="start" \
323 commit --trailer "Signed-off-by: C O Mitter <committer@example.com>" \
324 --trailer "Signed-off-by: C1 E1" \
326 git cat-file commit HEAD >commit.msg &&
327 sed -e "1,/^\$/d" commit.msg >actual &&
328 test_cmp expected actual
331 test_expect_success
'commit --trailer with -c and "after" as where' '
332 trailer_commit_base &&
333 cat >expected <<-\EOF &&
336 Signed-off-by: C O Mitter <committer@example.com>
343 git -c trailer.where="after" \
344 commit --trailer "Mentored-by: C4 E4" \
345 --trailer "Mentored-by: C5 E5" \
347 git cat-file commit HEAD >commit.msg &&
348 sed -e "1,/^\$/d" commit.msg >actual &&
349 test_cmp expected actual
352 test_expect_success
'commit --trailer with -c and "before" as where' '
353 trailer_commit_base &&
354 cat >expected <<-\EOF &&
357 Signed-off-by: C O Mitter <committer@example.com>
365 git -c trailer.where="before" \
366 commit --trailer "Mentored-by: C3 E3" \
367 --trailer "Mentored-by: C2 E2" \
369 git cat-file commit HEAD >commit.msg &&
370 sed -e "1,/^\$/d" commit.msg >actual &&
371 test_cmp expected actual
374 test_expect_success
'commit --trailer with -c and "donothing" as ifmissing' '
375 trailer_commit_base &&
376 cat >expected <<-\EOF &&
379 Signed-off-by: C O Mitter <committer@example.com>
386 git -c trailer.ifmissing="donothing" \
387 commit --trailer "Helped-by: C5 E5" \
388 --trailer "Based-by: C6 E6" \
390 git cat-file commit HEAD >commit.msg &&
391 sed -e "1,/^\$/d" commit.msg >actual &&
392 test_cmp expected actual
395 test_expect_success
'commit --trailer with -c and "add" as ifmissing' '
396 trailer_commit_base &&
397 cat >expected <<-\EOF &&
400 Signed-off-by: C O Mitter <committer@example.com>
408 git -c trailer.ifmissing="add" \
409 commit --trailer "Helped-by: C5 E5" \
410 --trailer "Based-by: C6 E6" \
412 git cat-file commit HEAD >commit.msg &&
413 sed -e "1,/^\$/d" commit.msg >actual &&
414 test_cmp expected actual
417 test_expect_success
'commit --trailer with -c ack.key ' '
418 echo "fun" >>file1 &&
420 cat >expected <<-\EOF &&
425 git -c trailer.ack.key="Acked-by" \
426 commit --trailer "ack = Peff" -m "hello" &&
427 git cat-file commit HEAD >commit.msg &&
428 sed -e "1,/^\$/d" commit.msg >actual &&
429 test_cmp expected actual
432 test_expect_success
'commit --trailer with -c and ":=#" as separators' '
433 echo "fun" >>file1 &&
435 cat >expected <<-\EOF &&
440 git -c trailer.separators=":=#" \
441 -c trailer.bug.key="Bug #" \
442 commit --trailer "bug = 42" -m "I hate bug" &&
443 git cat-file commit HEAD >commit.msg &&
444 sed -e "1,/^\$/d" commit.msg >actual &&
445 test_cmp expected actual
448 test_expect_success
'commit --trailer with -c and command' '
449 trailer_commit_base &&
450 cat >expected <<-\EOF &&
453 Signed-off-by: C O Mitter <committer@example.com>
457 Reported-by: A U Thor <author@example.com>
459 git -c trailer.report.key="Reported-by: " \
460 -c trailer.report.ifexists="replace" \
461 -c trailer.report.command="NAME=\"\$ARG\"; test -n \"\$NAME\" && \
462 git log --author=\"\$NAME\" -1 --format=\"format:%aN <%aE>\" || true" \
463 commit --trailer "report = author" --amend &&
464 git cat-file commit HEAD >commit.msg &&
465 sed -e "1,/^\$/d" commit.msg >actual &&
466 test_cmp expected actual
469 test_expect_success
'commit --trailer not confused by --- separator' '
477 git commit --allow-empty --trailer="my-trailer: value" -F msg &&
481 echo "my-trailer: value"
483 git cat-file commit HEAD >commit.msg &&
484 sed -e "1,/^\$/d" commit.msg >actual &&
485 test_cmp expected actual
488 test_expect_success
'multiple -m' '
492 git commit -m "one" -m "two" -m "three" &&
493 actual=$(git cat-file commit HEAD >tmp && sed -e "1,/^\$/d" tmp && rm tmp) &&
494 expected=$(test_write_lines "one" "" "two" "" "three") &&
495 test "z$actual" = "z$expected"
499 test_expect_success
'verbose' '
501 echo minus >negative &&
503 git status -v >raw &&
504 sed -ne "/^diff --git /p" raw >actual &&
505 echo "diff --git a/negative b/negative" >expect &&
506 test_cmp expect actual
510 test_expect_success
'verbose respects diff config' '
512 test_config diff.noprefix true &&
513 git status -v >actual &&
514 grep "diff --git negative negative" actual
517 mesg_with_comment_and_newlines
='
522 test_expect_success
'prepare file with comment line and trailing newlines' '
523 printf "%s" "$mesg_with_comment_and_newlines" >expect
526 test_expect_success
'cleanup commit messages (verbatim option,-t)' '
529 git commit --cleanup=verbatim --no-status -t expect -a &&
530 git cat-file -p HEAD >raw &&
531 sed -e "1,/^\$/d" raw >actual &&
532 test_cmp expect actual
536 test_expect_success
'cleanup commit messages (verbatim option,-F)' '
539 git commit --cleanup=verbatim -F expect -a &&
540 git cat-file -p HEAD >raw &&
541 sed -e "1,/^\$/d" raw >actual &&
542 test_cmp expect actual
546 test_expect_success
'cleanup commit messages (verbatim option,-m)' '
549 git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
550 git cat-file -p HEAD >raw &&
551 sed -e "1,/^\$/d" raw >actual &&
552 test_cmp expect actual
556 test_expect_success
'cleanup commit messages (whitespace option,-F)' '
559 test_write_lines "" "# text" "" >text &&
560 echo "# text" >expect &&
561 git commit --cleanup=whitespace -F text -a &&
562 git cat-file -p HEAD >raw &&
563 sed -e "1,/^\$/d" raw >actual &&
564 test_cmp expect actual
568 test_expect_success
'cleanup commit messages (scissors option,-F,-e)' '
575 # ------------------------ >8 ------------------------
577 # ------------------------ >8 ------------------------
579 # ------------------------ >8 ------------------------
583 cat >expect <<-\EOF &&
586 # ------------------------ >8 ------------------------
589 git commit --cleanup=scissors -e -F text -a &&
590 git cat-file -p HEAD >raw &&
591 sed -e "1,/^\$/d" raw >actual &&
592 test_cmp expect actual
595 test_expect_success
'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
599 # ------------------------ >8 ------------------------
602 git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
603 git cat-file -p HEAD >raw &&
604 sed -e "1,/^\$/d" raw >actual &&
605 test_must_be_empty actual
608 test_expect_success
'cleanup commit messages (strip option,-F)' '
611 test_write_lines "" "# text" "sample" "" >text &&
612 echo sample >expect &&
613 git commit --cleanup=strip -F text -a &&
614 git cat-file -p HEAD >raw &&
615 sed -e "1,/^\$/d" raw >actual &&
616 test_cmp expect actual
620 test_expect_success
'cleanup commit messages (strip option,-F,-e)' '
623 test_write_lines "" "sample" "" >text &&
624 git commit -e -F text -a &&
625 head -n 4 .git/COMMIT_EDITMSG >actual
630 # Please enter the commit message for your changes. Lines starting
631 # with '#' will be ignored, and an empty message aborts the commit." >expect
633 test_expect_success
'cleanup commit messages (strip option,-F,-e): output' '
634 test_cmp expect actual
637 test_expect_success
'cleanup commit message (fail on invalid cleanup mode option)' '
638 test_must_fail git commit --cleanup=non-existent
641 test_expect_success
'cleanup commit message (fail on invalid cleanup mode configuration)' '
642 test_must_fail git -c commit.cleanup=non-existent commit
645 test_expect_success
'cleanup commit message (no config and no option uses default)' '
646 echo content >>file &&
649 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
650 git commit --no-status
652 commit_msg_is "commit message"
655 test_expect_success
'cleanup commit message (option overrides default)' '
656 echo content >>file &&
659 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
660 git commit --cleanup=whitespace --no-status
662 commit_msg_is "commit message # comment"
665 test_expect_success
'cleanup commit message (config overrides default)' '
666 echo content >>file &&
669 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
670 git -c commit.cleanup=whitespace commit --no-status
672 commit_msg_is "commit message # comment"
675 test_expect_success
'cleanup commit message (option overrides config)' '
676 echo content >>file &&
679 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
680 git -c commit.cleanup=whitespace commit --cleanup=default
682 commit_msg_is "commit message"
685 test_expect_success
'cleanup commit message (default, -m)' '
686 echo content >>file &&
688 git commit -m "message #comment " &&
689 commit_msg_is "message #comment"
692 test_expect_success
'cleanup commit message (whitespace option, -m)' '
693 echo content >>file &&
695 git commit --cleanup=whitespace --no-status -m "message #comment " &&
696 commit_msg_is "message #comment"
699 test_expect_success
'cleanup commit message (whitespace config, -m)' '
700 echo content >>file &&
702 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
703 commit_msg_is "message #comment"
706 test_expect_success
'message shows author when it is not equal to committer' '
708 git commit -e -m "sample" -a &&
710 "^# Author: *A U Thor <author@example.com>\$" \
714 test_expect_success
'message shows date when it is explicitly set' '
715 git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
717 "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
721 test_expect_success AUTOIDENT
'message shows committer when it is automatic' '
725 sane_unset GIT_COMMITTER_EMAIL &&
726 sane_unset GIT_COMMITTER_NAME &&
727 git commit -e -m "sample" -a
729 # the ident is calculated from the system, so we cannot
730 # check the actual value, only that it is there
731 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
734 write_script .git
/FAKE_EDITOR
<<EOF
735 echo editor started >"$(pwd)/.git/result"
739 test_expect_success
!FAIL_PREREQS
,!AUTOIDENT
'do not fire editor when committer is bogus' '
744 sane_unset GIT_COMMITTER_EMAIL &&
745 sane_unset GIT_COMMITTER_NAME &&
746 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
748 test_must_fail git commit -e -m sample -a
750 test_must_be_empty .git/result
753 test_expect_success
'do not fire editor if -m <msg> was given' '
756 echo "editor not started" >.git/result &&
757 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
758 test "$(cat .git/result)" = "editor not started"
761 test_expect_success
'do not fire editor if -m "" was given' '
764 echo "editor not started" >.git/result &&
765 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
766 git commit -m "" --allow-empty-message) &&
767 test "$(cat .git/result)" = "editor not started"
770 test_expect_success
'do not fire editor in the presence of conflicts' '
775 git commit -m "add g" &&
780 git commit -m "modify g and add h" &&
781 git checkout second &&
784 git commit -m second &&
785 # Must fail due to conflict
786 test_must_fail git cherry-pick -n main &&
787 echo "editor not started" >.git/result &&
789 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
791 test_must_fail git commit
793 test "$(cat .git/result)" = "editor not started"
796 write_script .git
/FAKE_EDITOR
<<EOF
797 # kill -TERM command added below.
800 test_expect_success EXECKEEPSPID
'a SIGTERM should break locks' '
802 ! "$SHELL_PATH" -c '\''
803 echo kill -TERM $$ >>.git/FAKE_EDITOR
804 GIT_EDITOR=.git/FAKE_EDITOR
806 exec git commit -a'\'' &&
807 test ! -f .git/index.lock
810 rm -f .git
/MERGE_MSG .git
/COMMIT_EDITMSG
813 test_expect_success
'Hand committing of a redundant merge removes dups' '
815 git rev-parse second main >expect &&
816 test_must_fail git merge second main &&
817 git checkout main g &&
818 EDITOR=: git commit -a &&
819 git cat-file commit HEAD >raw &&
820 sed -n -e "s/^parent //p" -e "/^$/q" raw >actual &&
821 test_cmp expect actual
825 test_expect_success
'A single-liner subject with a token plus colon is not a footer' '
828 git commit -s -m "hello: kitty" --allow-empty &&
829 git cat-file commit HEAD >raw &&
830 sed -e "1,/^$/d" raw >actual &&
831 test_line_count = 3 actual
835 test_expect_success
'commit -s places sob on third line after two empty lines' '
836 git commit -s --allow-empty --allow-empty-message &&
837 cat <<-EOF >expect &&
840 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
843 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
844 test_cmp expect actual
847 write_script .git
/FAKE_EDITOR
<<\EOF
855 echo '## Custom template' >template
860 GIT_EDITOR
=.git
/FAKE_EDITOR git commit
-a $
* $use_template &&
861 case "$use_template" in
863 test_i18ngrep
! "^## Custom template" .git
/COMMIT_EDITMSG
;;
865 test_i18ngrep
"^## Custom template" .git
/COMMIT_EDITMSG
;;
869 try_commit_status_combo
() {
871 test_expect_success
'commit' '
873 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
876 test_expect_success
'commit --status' '
877 try_commit --status &&
878 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
881 test_expect_success
'commit --no-status' '
882 try_commit --no-status &&
883 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
886 test_expect_success
'commit with commit.status = yes' '
887 test_config commit.status yes &&
889 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
892 test_expect_success
'commit with commit.status = no' '
893 test_config commit.status no &&
895 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
898 test_expect_success
'commit --status with commit.status = yes' '
899 test_config commit.status yes &&
900 try_commit --status &&
901 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
904 test_expect_success
'commit --no-status with commit.status = yes' '
905 test_config commit.status yes &&
906 try_commit --no-status &&
907 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
910 test_expect_success
'commit --status with commit.status = no' '
911 test_config commit.status no &&
912 try_commit --status &&
913 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
916 test_expect_success
'commit --no-status with commit.status = no' '
917 test_config commit.status no &&
918 try_commit --no-status &&
919 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
924 try_commit_status_combo
926 use_template
="-t template"
928 try_commit_status_combo
930 test_expect_success
'commit --status with custom comment character' '
931 test_config core.commentchar ";" &&
932 try_commit --status &&
933 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
936 test_expect_success
'switch core.commentchar' '
937 test_commit "#foo" foo &&
938 GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
939 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
942 test_expect_success
'switch core.commentchar but out of options' '
955 git commit --amend -F text &&
957 test_set_editor .git/FAKE_EDITOR &&
958 test_must_fail git -c core.commentChar=auto commit --amend