3 test_description='git commit porcelain-ish'
8 expect=commit_msg_is.expect
9 actual=commit_msg_is.actual
11 printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
12 printf "%s" "$1" >$expect &&
13 test_i18ncmp $expect $actual
16 # Arguments: [<prefix] [<commit message>] [<commit options>]
17 check_summary_oneline() {
19 git commit ${3+"$3"} -m "$2" | head -1 > act &&
22 SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
24 # append the "special" prefix, like "root-commit", "detached HEAD"
27 SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
31 SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
32 echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
37 test_expect_success 'output summary format' '
41 check_summary_oneline "root-commit" "initial" &&
43 echo change >>file1 &&
47 test_expect_success 'output summary format: root-commit' '
48 check_summary_oneline "" "a change"
51 test_expect_success 'output summary format for commit with an empty diff' '
53 check_summary_oneline "" "empty" "--allow-empty"
56 test_expect_success 'output summary format for merges' '
58 git checkout -b recursive-base &&
59 test_commit base file1 &&
61 git checkout -b recursive-a recursive-base &&
62 test_commit commit-a file1 &&
64 git checkout -b recursive-b recursive-base &&
65 test_commit commit-b file1 &&
68 git checkout recursive-a &&
69 test_must_fail git merge recursive-b &&
70 # resolve the conflict
71 echo commit-a > file1 &&
73 check_summary_oneline "" "Merge"
76 output_tests_cleanup() {
77 # this is needed for "do not fire editor in the presence of conflicts"
78 git checkout master &&
80 # this is needed for the "partial removal" test to pass
82 git commit -m "cleanup"
85 test_expect_success 'the basics' '
87 output_tests_cleanup &&
89 echo doing partial >"commit is" &&
91 echo very much encouraged but we should >not/forbid &&
92 git add "commit is" not &&
93 echo update added "commit is" file >"commit is" &&
94 echo also update another >not/forbid &&
96 git commit -a -m "initial with -a" &&
98 git cat-file blob HEAD:"commit is" >current.1 &&
99 git cat-file blob HEAD:not/forbid >current.2 &&
101 cmp current.1 "commit is" &&
102 cmp current.2 not/forbid
106 test_expect_success 'partial' '
108 echo another >"commit is" &&
109 echo another >not/forbid &&
111 git commit -m "partial commit to handle a file" "commit is" &&
113 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
114 test "$changed" = "commit is"
118 test_expect_success 'partial modification in a subdirectory' '
121 git commit -m "partial commit to subdirectory" not &&
123 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
124 test "$changed" = "not/forbid"
128 test_expect_success 'partial removal' '
131 git commit -m "partial commit to remove not/forbid" not &&
133 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
134 test "$changed" = "not/forbid" &&
135 remain=$(git ls-tree -r --name-only HEAD) &&
136 test "$remain" = "commit is"
140 test_expect_success 'sign off' '
144 git commit -s -m "thank you" &&
145 actual=$(git cat-file commit HEAD | sed -ne "s/Signed-off-by: //p") &&
146 expected=$(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/") &&
147 test "z$actual" = "z$expected"
151 test_expect_success 'multiple -m' '
155 git commit -m "one" -m "two" -m "three" &&
156 actual=$(git cat-file commit HEAD | sed -e "1,/^\$/d") &&
157 expected=$(echo one; echo; echo two; echo; echo three) &&
158 test "z$actual" = "z$expected"
162 test_expect_success 'verbose' '
164 echo minus >negative &&
166 git status -v | sed -ne "/^diff --git /p" >actual &&
167 echo "diff --git a/negative b/negative" >expect &&
168 test_cmp expect actual
172 test_expect_success 'verbose respects diff config' '
174 test_config color.diff always &&
175 git status -v >actual &&
176 grep "\[1mdiff --git" actual
179 mesg_with_comment_and_newlines='
184 test_expect_success 'prepare file with comment line and trailing newlines' '
185 printf "%s" "$mesg_with_comment_and_newlines" >expect
188 test_expect_success 'cleanup commit messages (verbatim option,-t)' '
191 git commit --cleanup=verbatim --no-status -t expect -a &&
192 git cat-file -p HEAD |sed -e "1,/^\$/d" >actual &&
193 test_cmp expect actual
197 test_expect_success 'cleanup commit messages (verbatim option,-F)' '
200 git commit --cleanup=verbatim -F expect -a &&
201 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
202 test_cmp expect actual
206 test_expect_success 'cleanup commit messages (verbatim option,-m)' '
209 git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
210 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
211 test_cmp expect actual
215 test_expect_success 'cleanup commit messages (whitespace option,-F)' '
218 { echo;echo "# text";echo; } >text &&
219 echo "# text" >expect &&
220 git commit --cleanup=whitespace -F text -a &&
221 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
222 test_cmp expect actual
226 test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
233 # ------------------------ >8 ------------------------
235 # ------------------------ >8 ------------------------
237 # ------------------------ >8 ------------------------
244 # ------------------------ >8 ------------------------
247 git commit --cleanup=scissors -e -F text -a &&
248 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
249 test_cmp expect actual
252 test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
256 # ------------------------ >8 ------------------------
259 git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
260 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
261 test_must_be_empty actual
264 test_expect_success 'cleanup commit messages (strip option,-F)' '
267 { echo;echo "# text";echo sample;echo; } >text &&
268 echo sample >expect &&
269 git commit --cleanup=strip -F text -a &&
270 git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
271 test_cmp expect actual
275 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
278 { echo;echo sample;echo; } >text &&
279 git commit -e -F text -a &&
280 head -n 4 .git/COMMIT_EDITMSG >actual
285 # Please enter the commit message for your changes. Lines starting
286 # with '#' will be ignored, and an empty message aborts the commit." >expect
288 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
289 test_i18ncmp expect actual
292 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
293 test_must_fail git commit --cleanup=non-existent
296 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
297 test_must_fail git -c commit.cleanup=non-existent commit
300 test_expect_success 'cleanup commit message (no config and no option uses default)' '
301 echo content >>file &&
304 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
305 git commit --no-status
307 commit_msg_is "commit message"
310 test_expect_success 'cleanup commit message (option overrides default)' '
311 echo content >>file &&
314 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
315 git commit --cleanup=whitespace --no-status
317 commit_msg_is "commit message # comment"
320 test_expect_success 'cleanup commit message (config overrides default)' '
321 echo content >>file &&
324 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
325 git -c commit.cleanup=whitespace commit --no-status
327 commit_msg_is "commit message # comment"
330 test_expect_success 'cleanup commit message (option overrides config)' '
331 echo content >>file &&
334 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
335 git -c commit.cleanup=whitespace commit --cleanup=default
337 commit_msg_is "commit message"
340 test_expect_success 'cleanup commit message (default, -m)' '
341 echo content >>file &&
343 git commit -m "message #comment " &&
344 commit_msg_is "message #comment"
347 test_expect_success 'cleanup commit message (whitespace option, -m)' '
348 echo content >>file &&
350 git commit --cleanup=whitespace --no-status -m "message #comment " &&
351 commit_msg_is "message #comment"
354 test_expect_success 'cleanup commit message (whitespace config, -m)' '
355 echo content >>file &&
357 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
358 commit_msg_is "message #comment"
361 test_expect_success 'message shows author when it is not equal to committer' '
363 git commit -e -m "sample" -a &&
365 "^# Author: *A U Thor <author@example.com>\$" \
369 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
373 sane_unset GIT_COMMITTER_EMAIL &&
374 sane_unset GIT_COMMITTER_NAME &&
375 git commit -e -m "sample" -a
377 # the ident is calculated from the system, so we cannot
378 # check the actual value, only that it is there
379 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
382 write_script .git/FAKE_EDITOR <<EOF
383 echo editor started > "$(pwd)/.git/result"
387 test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
393 sane_unset GIT_COMMITTER_EMAIL &&
394 sane_unset GIT_COMMITTER_NAME &&
395 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
397 test_must_fail git commit -e -m sample -a
399 test_cmp expect .git/result
402 test_expect_success 'do not fire editor if -m <msg> was given' '
405 echo "editor not started" >.git/result &&
406 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
407 test "$(cat .git/result)" = "editor not started"
410 test_expect_success 'do not fire editor if -m "" was given' '
413 echo "editor not started" >.git/result &&
414 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
415 git commit -m "" --allow-empty-message) &&
416 test "$(cat .git/result)" = "editor not started"
419 test_expect_success 'do not fire editor in the presence of conflicts' '
424 git commit -m "add g" &&
429 git commit -m "modify g and add h" &&
430 git checkout second &&
433 git commit -m second &&
434 # Must fail due to conflict
435 test_must_fail git cherry-pick -n master &&
436 echo "editor not started" >.git/result &&
438 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
440 test_must_fail git commit
442 test "$(cat .git/result)" = "editor not started"
445 write_script .git/FAKE_EDITOR <<EOF
446 # kill -TERM command added below.
449 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
451 ! "$SHELL_PATH" -c '\''
452 echo kill -TERM $$ >> .git/FAKE_EDITOR
453 GIT_EDITOR=.git/FAKE_EDITOR
455 exec git commit -a'\'' &&
456 test ! -f .git/index.lock
459 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
462 test_expect_success 'Hand committing of a redundant merge removes dups' '
464 git rev-parse second master >expect &&
465 test_must_fail git merge second master &&
466 git checkout master g &&
467 EDITOR=: git commit -a &&
468 git cat-file commit HEAD | sed -n -e "s/^parent //p" -e "/^$/q" >actual &&
469 test_cmp expect actual
473 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
476 git commit -s -m "hello: kitty" --allow-empty &&
477 git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
478 test_line_count = 3 actual
482 test_expect_success 'commit -s places sob on third line after two empty lines' '
483 git commit -s --allow-empty --allow-empty-message &&
484 cat <<-EOF >expect &&
487 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
490 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
491 test_cmp expect actual
494 write_script .git/FAKE_EDITOR <<\EOF
502 echo '## Custom template' >template
507 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
508 case "$use_template" in
510 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
512 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
516 try_commit_status_combo () {
518 test_expect_success 'commit' '
520 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
523 test_expect_success 'commit' '
525 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
528 test_expect_success 'commit --status' '
529 try_commit --status &&
530 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
533 test_expect_success 'commit --no-status' '
534 try_commit --no-status &&
535 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
538 test_expect_success 'commit with commit.status = yes' '
539 test_config commit.status yes &&
541 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
544 test_expect_success 'commit with commit.status = no' '
545 test_config commit.status no &&
547 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
550 test_expect_success 'commit --status with commit.status = yes' '
551 test_config commit.status yes &&
552 try_commit --status &&
553 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
556 test_expect_success 'commit --no-status with commit.status = yes' '
557 test_config commit.status yes &&
558 try_commit --no-status &&
559 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
562 test_expect_success 'commit --status with commit.status = no' '
563 test_config commit.status no &&
564 try_commit --status &&
565 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
568 test_expect_success 'commit --no-status with commit.status = no' '
569 test_config commit.status no &&
570 try_commit --no-status &&
571 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
576 try_commit_status_combo
578 use_template="-t template"
580 try_commit_status_combo
582 test_expect_success 'commit --status with custom comment character' '
583 test_config core.commentchar ";" &&
584 try_commit --status &&
585 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG