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_i18ncmp $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 test_expect_success 'output summary format' '
45 check_summary_oneline "root-commit" "initial" &&
47 echo change >>file1 &&
51 test_expect_success 'output summary format: root-commit' '
52 check_summary_oneline "" "a change"
55 test_expect_success 'output summary format for commit with an empty diff' '
57 check_summary_oneline "" "empty" "--allow-empty"
60 test_expect_success 'output summary format for merges' '
62 git checkout -b recursive-base &&
63 test_commit base file1 &&
65 git checkout -b recursive-a recursive-base &&
66 test_commit commit-a file1 &&
68 git checkout -b recursive-b recursive-base &&
69 test_commit commit-b file1 &&
72 git checkout recursive-a &&
73 test_must_fail git merge recursive-b &&
74 # resolve the conflict
75 echo commit-a >file1 &&
77 check_summary_oneline "" "Merge"
80 output_tests_cleanup() {
81 # this is needed for "do not fire editor in the presence of conflicts"
84 # this is needed for the "partial removal" test to pass
86 git commit -m "cleanup"
89 test_expect_success 'the basics' '
91 output_tests_cleanup &&
93 echo doing partial >"commit is" &&
95 echo very much encouraged but we should >not/forbid &&
96 git add "commit is" not &&
97 echo update added "commit is" file >"commit is" &&
98 echo also update another >not/forbid &&
100 git commit -a -m "initial with -a" &&
102 git cat-file blob HEAD:"commit is" >current.1 &&
103 git cat-file blob HEAD:not/forbid >current.2 &&
105 cmp current.1 "commit is" &&
106 cmp current.2 not/forbid
110 test_expect_success 'partial' '
112 echo another >"commit is" &&
113 echo another >not/forbid &&
115 git commit -m "partial commit to handle a file" "commit is" &&
117 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
118 test "$changed" = "commit is"
122 test_expect_success 'partial modification in a subdirectory' '
125 git commit -m "partial commit to subdirectory" not &&
127 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
128 test "$changed" = "not/forbid"
132 test_expect_success 'partial removal' '
135 git commit -m "partial commit to remove not/forbid" not &&
137 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
138 test "$changed" = "not/forbid" &&
139 remain=$(git ls-tree -r --name-only HEAD) &&
140 test "$remain" = "commit is"
144 test_expect_success 'sign off' '
148 git commit -s -m "thank you" &&
149 git cat-file commit HEAD >commit.msg &&
150 sed -ne "s/Signed-off-by: //p" commit.msg >actual &&
151 git var GIT_COMMITTER_IDENT >ident &&
152 sed -e "s/>.*/>/" ident >expected &&
153 test_cmp expected actual
157 test_expect_success 'multiple -m' '
161 git commit -m "one" -m "two" -m "three" &&
162 actual=$(git cat-file commit HEAD >tmp && sed -e "1,/^\$/d" tmp && rm tmp) &&
163 expected=$(test_write_lines "one" "" "two" "" "three") &&
164 test "z$actual" = "z$expected"
168 test_expect_success 'verbose' '
170 echo minus >negative &&
172 git status -v >raw &&
173 sed -ne "/^diff --git /p" raw >actual &&
174 echo "diff --git a/negative b/negative" >expect &&
175 test_cmp expect actual
179 test_expect_success 'verbose respects diff config' '
181 test_config diff.noprefix true &&
182 git status -v >actual &&
183 grep "diff --git negative negative" actual
186 mesg_with_comment_and_newlines='
191 test_expect_success 'prepare file with comment line and trailing newlines' '
192 printf "%s" "$mesg_with_comment_and_newlines" >expect
195 test_expect_success 'cleanup commit messages (verbatim option,-t)' '
198 git commit --cleanup=verbatim --no-status -t expect -a &&
199 git cat-file -p HEAD >raw &&
200 sed -e "1,/^\$/d" raw >actual &&
201 test_cmp expect actual
205 test_expect_success 'cleanup commit messages (verbatim option,-F)' '
208 git commit --cleanup=verbatim -F expect -a &&
209 git cat-file -p HEAD >raw &&
210 sed -e "1,/^\$/d" raw >actual &&
211 test_cmp expect actual
215 test_expect_success 'cleanup commit messages (verbatim option,-m)' '
218 git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
219 git cat-file -p HEAD >raw &&
220 sed -e "1,/^\$/d" raw >actual &&
221 test_cmp expect actual
225 test_expect_success 'cleanup commit messages (whitespace option,-F)' '
228 test_write_lines "" "# text" "" >text &&
229 echo "# text" >expect &&
230 git commit --cleanup=whitespace -F text -a &&
231 git cat-file -p HEAD >raw &&
232 sed -e "1,/^\$/d" raw >actual &&
233 test_cmp expect actual
237 test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
244 # ------------------------ >8 ------------------------
246 # ------------------------ >8 ------------------------
248 # ------------------------ >8 ------------------------
252 cat >expect <<-\EOF &&
255 # ------------------------ >8 ------------------------
258 git commit --cleanup=scissors -e -F text -a &&
259 git cat-file -p HEAD >raw &&
260 sed -e "1,/^\$/d" raw >actual &&
261 test_cmp expect actual
264 test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
268 # ------------------------ >8 ------------------------
271 git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
272 git cat-file -p HEAD >raw &&
273 sed -e "1,/^\$/d" raw >actual &&
274 test_must_be_empty actual
277 test_expect_success 'cleanup commit messages (strip option,-F)' '
280 test_write_lines "" "# text" "sample" "" >text &&
281 echo sample >expect &&
282 git commit --cleanup=strip -F text -a &&
283 git cat-file -p HEAD >raw &&
284 sed -e "1,/^\$/d" raw >actual &&
285 test_cmp expect actual
289 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
292 test_write_lines "" "sample" "" >text &&
293 git commit -e -F text -a &&
294 head -n 4 .git/COMMIT_EDITMSG >actual
299 # Please enter the commit message for your changes. Lines starting
300 # with '#' will be ignored, and an empty message aborts the commit." >expect
302 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
303 test_i18ncmp expect actual
306 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
307 test_must_fail git commit --cleanup=non-existent
310 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
311 test_must_fail git -c commit.cleanup=non-existent commit
314 test_expect_success 'cleanup commit message (no config and no option uses default)' '
315 echo content >>file &&
318 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
319 git commit --no-status
321 commit_msg_is "commit message"
324 test_expect_success 'cleanup commit message (option overrides default)' '
325 echo content >>file &&
328 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
329 git commit --cleanup=whitespace --no-status
331 commit_msg_is "commit message # comment"
334 test_expect_success 'cleanup commit message (config overrides default)' '
335 echo content >>file &&
338 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
339 git -c commit.cleanup=whitespace commit --no-status
341 commit_msg_is "commit message # comment"
344 test_expect_success 'cleanup commit message (option overrides config)' '
345 echo content >>file &&
348 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
349 git -c commit.cleanup=whitespace commit --cleanup=default
351 commit_msg_is "commit message"
354 test_expect_success 'cleanup commit message (default, -m)' '
355 echo content >>file &&
357 git commit -m "message #comment " &&
358 commit_msg_is "message #comment"
361 test_expect_success 'cleanup commit message (whitespace option, -m)' '
362 echo content >>file &&
364 git commit --cleanup=whitespace --no-status -m "message #comment " &&
365 commit_msg_is "message #comment"
368 test_expect_success 'cleanup commit message (whitespace config, -m)' '
369 echo content >>file &&
371 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
372 commit_msg_is "message #comment"
375 test_expect_success 'message shows author when it is not equal to committer' '
377 git commit -e -m "sample" -a &&
379 "^# Author: *A U Thor <author@example.com>\$" \
383 test_expect_success 'message shows date when it is explicitly set' '
384 git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
386 "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
390 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
394 sane_unset GIT_COMMITTER_EMAIL &&
395 sane_unset GIT_COMMITTER_NAME &&
396 git commit -e -m "sample" -a
398 # the ident is calculated from the system, so we cannot
399 # check the actual value, only that it is there
400 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
403 write_script .git/FAKE_EDITOR <<EOF
404 echo editor started >"$(pwd)/.git/result"
408 test_expect_success !FAIL_PREREQS,!AUTOIDENT 'do not fire editor when committer is bogus' '
413 sane_unset GIT_COMMITTER_EMAIL &&
414 sane_unset GIT_COMMITTER_NAME &&
415 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
417 test_must_fail git commit -e -m sample -a
419 test_must_be_empty .git/result
422 test_expect_success 'do not fire editor if -m <msg> was given' '
425 echo "editor not started" >.git/result &&
426 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
427 test "$(cat .git/result)" = "editor not started"
430 test_expect_success 'do not fire editor if -m "" was given' '
433 echo "editor not started" >.git/result &&
434 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
435 git commit -m "" --allow-empty-message) &&
436 test "$(cat .git/result)" = "editor not started"
439 test_expect_success 'do not fire editor in the presence of conflicts' '
444 git commit -m "add g" &&
449 git commit -m "modify g and add h" &&
450 git checkout second &&
453 git commit -m second &&
454 # Must fail due to conflict
455 test_must_fail git cherry-pick -n main &&
456 echo "editor not started" >.git/result &&
458 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
460 test_must_fail git commit
462 test "$(cat .git/result)" = "editor not started"
465 write_script .git/FAKE_EDITOR <<EOF
466 # kill -TERM command added below.
469 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
471 ! "$SHELL_PATH" -c '\''
472 echo kill -TERM $$ >>.git/FAKE_EDITOR
473 GIT_EDITOR=.git/FAKE_EDITOR
475 exec git commit -a'\'' &&
476 test ! -f .git/index.lock
479 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
482 test_expect_success 'Hand committing of a redundant merge removes dups' '
484 git rev-parse second main >expect &&
485 test_must_fail git merge second main &&
486 git checkout main g &&
487 EDITOR=: git commit -a &&
488 git cat-file commit HEAD >raw &&
489 sed -n -e "s/^parent //p" -e "/^$/q" raw >actual &&
490 test_cmp expect actual
494 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
497 git commit -s -m "hello: kitty" --allow-empty &&
498 git cat-file commit HEAD >raw &&
499 sed -e "1,/^$/d" raw >actual &&
500 test_line_count = 3 actual
504 test_expect_success 'commit -s places sob on third line after two empty lines' '
505 git commit -s --allow-empty --allow-empty-message &&
506 cat <<-EOF >expect &&
509 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
512 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
513 test_cmp expect actual
516 write_script .git/FAKE_EDITOR <<\EOF
524 echo '## Custom template' >template
529 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
530 case "$use_template" in
532 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
534 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
538 try_commit_status_combo () {
540 test_expect_success 'commit' '
542 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
545 test_expect_success 'commit --status' '
546 try_commit --status &&
547 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
550 test_expect_success 'commit --no-status' '
551 try_commit --no-status &&
552 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
555 test_expect_success 'commit with commit.status = yes' '
556 test_config commit.status yes &&
558 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
561 test_expect_success 'commit with commit.status = no' '
562 test_config commit.status no &&
564 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
567 test_expect_success 'commit --status with commit.status = yes' '
568 test_config commit.status yes &&
569 try_commit --status &&
570 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
573 test_expect_success 'commit --no-status with commit.status = yes' '
574 test_config commit.status yes &&
575 try_commit --no-status &&
576 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
579 test_expect_success 'commit --status with commit.status = no' '
580 test_config commit.status no &&
581 try_commit --status &&
582 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
585 test_expect_success 'commit --no-status with commit.status = no' '
586 test_config commit.status no &&
587 try_commit --no-status &&
588 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
593 try_commit_status_combo
595 use_template="-t template"
597 try_commit_status_combo
599 test_expect_success 'commit --status with custom comment character' '
600 test_config core.commentchar ";" &&
601 try_commit --status &&
602 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
605 test_expect_success 'switch core.commentchar' '
606 test_commit "#foo" foo &&
607 GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
608 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
611 test_expect_success 'switch core.commentchar but out of options' '
624 git commit --amend -F text &&
626 test_set_editor .git/FAKE_EDITOR &&
627 test_must_fail git -c core.commentChar=auto commit --amend