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" >raw &&
23 SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
25 # append the "special" prefix, like "root-commit", "detached HEAD"
28 SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
32 SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
33 echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
38 test_expect_success 'output summary format' '
42 check_summary_oneline "root-commit" "initial" &&
44 echo change >>file1 &&
48 test_expect_success 'output summary format: root-commit' '
49 check_summary_oneline "" "a change"
52 test_expect_success 'output summary format for commit with an empty diff' '
54 check_summary_oneline "" "empty" "--allow-empty"
57 test_expect_success 'output summary format for merges' '
59 git checkout -b recursive-base &&
60 test_commit base file1 &&
62 git checkout -b recursive-a recursive-base &&
63 test_commit commit-a file1 &&
65 git checkout -b recursive-b recursive-base &&
66 test_commit commit-b file1 &&
69 git checkout recursive-a &&
70 test_must_fail git merge recursive-b &&
71 # resolve the conflict
72 echo commit-a >file1 &&
74 check_summary_oneline "" "Merge"
77 output_tests_cleanup() {
78 # this is needed for "do not fire editor in the presence of conflicts"
79 git checkout master &&
81 # this is needed for the "partial removal" test to pass
83 git commit -m "cleanup"
86 test_expect_success 'the basics' '
88 output_tests_cleanup &&
90 echo doing partial >"commit is" &&
92 echo very much encouraged but we should >not/forbid &&
93 git add "commit is" not &&
94 echo update added "commit is" file >"commit is" &&
95 echo also update another >not/forbid &&
97 git commit -a -m "initial with -a" &&
99 git cat-file blob HEAD:"commit is" >current.1 &&
100 git cat-file blob HEAD:not/forbid >current.2 &&
102 cmp current.1 "commit is" &&
103 cmp current.2 not/forbid
107 test_expect_success 'partial' '
109 echo another >"commit is" &&
110 echo another >not/forbid &&
112 git commit -m "partial commit to handle a file" "commit is" &&
114 changed=$(git diff-tree --name-only HEAD^ HEAD) &&
115 test "$changed" = "commit is"
119 test_expect_success 'partial modification in a subdirectory' '
122 git commit -m "partial commit to subdirectory" not &&
124 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
125 test "$changed" = "not/forbid"
129 test_expect_success 'partial removal' '
132 git commit -m "partial commit to remove not/forbid" not &&
134 changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
135 test "$changed" = "not/forbid" &&
136 remain=$(git ls-tree -r --name-only HEAD) &&
137 test "$remain" = "commit is"
141 test_expect_success 'sign off' '
145 git commit -s -m "thank you" &&
146 git cat-file commit HEAD >commit.msg &&
147 sed -ne "s/Signed-off-by: //p" commit.msg >actual &&
148 git var GIT_COMMITTER_IDENT >ident &&
149 sed -e "s/>.*/>/" ident >expected &&
150 test_cmp expected actual
154 test_expect_success 'multiple -m' '
158 git commit -m "one" -m "two" -m "three" &&
159 actual=$(git cat-file commit HEAD >tmp && sed -e "1,/^\$/d" tmp && rm tmp) &&
160 expected=$(test_write_lines "one" "" "two" "" "three") &&
161 test "z$actual" = "z$expected"
165 test_expect_success 'verbose' '
167 echo minus >negative &&
169 git status -v >raw &&
170 sed -ne "/^diff --git /p" raw >actual &&
171 echo "diff --git a/negative b/negative" >expect &&
172 test_cmp expect actual
176 test_expect_success 'verbose respects diff config' '
178 test_config diff.noprefix true &&
179 git status -v >actual &&
180 grep "diff --git negative negative" actual
183 mesg_with_comment_and_newlines='
188 test_expect_success 'prepare file with comment line and trailing newlines' '
189 printf "%s" "$mesg_with_comment_and_newlines" >expect
192 test_expect_success 'cleanup commit messages (verbatim option,-t)' '
195 git commit --cleanup=verbatim --no-status -t expect -a &&
196 git cat-file -p HEAD >raw &&
197 sed -e "1,/^\$/d" raw >actual &&
198 test_cmp expect actual
202 test_expect_success 'cleanup commit messages (verbatim option,-F)' '
205 git commit --cleanup=verbatim -F expect -a &&
206 git cat-file -p HEAD >raw &&
207 sed -e "1,/^\$/d" raw >actual &&
208 test_cmp expect actual
212 test_expect_success 'cleanup commit messages (verbatim option,-m)' '
215 git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
216 git cat-file -p HEAD >raw &&
217 sed -e "1,/^\$/d" raw >actual &&
218 test_cmp expect actual
222 test_expect_success 'cleanup commit messages (whitespace option,-F)' '
225 test_write_lines "" "# text" "" >text &&
226 echo "# text" >expect &&
227 git commit --cleanup=whitespace -F text -a &&
228 git cat-file -p HEAD >raw &&
229 sed -e "1,/^\$/d" raw >actual &&
230 test_cmp expect actual
234 test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
241 # ------------------------ >8 ------------------------
243 # ------------------------ >8 ------------------------
245 # ------------------------ >8 ------------------------
249 cat >expect <<-\EOF &&
252 # ------------------------ >8 ------------------------
255 git commit --cleanup=scissors -e -F text -a &&
256 git cat-file -p HEAD >raw &&
257 sed -e "1,/^\$/d" raw >actual &&
258 test_cmp expect actual
261 test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
265 # ------------------------ >8 ------------------------
268 git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
269 git cat-file -p HEAD >raw &&
270 sed -e "1,/^\$/d" raw >actual &&
271 test_must_be_empty actual
274 test_expect_success 'cleanup commit messages (strip option,-F)' '
277 test_write_lines "" "# text" "sample" "" >text &&
278 echo sample >expect &&
279 git commit --cleanup=strip -F text -a &&
280 git cat-file -p HEAD >raw &&
281 sed -e "1,/^\$/d" raw >actual &&
282 test_cmp expect actual
286 test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
289 test_write_lines "" "sample" "" >text &&
290 git commit -e -F text -a &&
291 head -n 4 .git/COMMIT_EDITMSG >actual
296 # Please enter the commit message for your changes. Lines starting
297 # with '#' will be ignored, and an empty message aborts the commit." >expect
299 test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
300 test_i18ncmp expect actual
303 test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
304 test_must_fail git commit --cleanup=non-existent
307 test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
308 test_must_fail git -c commit.cleanup=non-existent commit
311 test_expect_success 'cleanup commit message (no config and no option uses default)' '
312 echo content >>file &&
315 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
316 git commit --no-status
318 commit_msg_is "commit message"
321 test_expect_success 'cleanup commit message (option overrides default)' '
322 echo content >>file &&
325 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
326 git commit --cleanup=whitespace --no-status
328 commit_msg_is "commit message # comment"
331 test_expect_success 'cleanup commit message (config overrides default)' '
332 echo content >>file &&
335 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
336 git -c commit.cleanup=whitespace commit --no-status
338 commit_msg_is "commit message # comment"
341 test_expect_success 'cleanup commit message (option overrides config)' '
342 echo content >>file &&
345 test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
346 git -c commit.cleanup=whitespace commit --cleanup=default
348 commit_msg_is "commit message"
351 test_expect_success 'cleanup commit message (default, -m)' '
352 echo content >>file &&
354 git commit -m "message #comment " &&
355 commit_msg_is "message #comment"
358 test_expect_success 'cleanup commit message (whitespace option, -m)' '
359 echo content >>file &&
361 git commit --cleanup=whitespace --no-status -m "message #comment " &&
362 commit_msg_is "message #comment"
365 test_expect_success 'cleanup commit message (whitespace config, -m)' '
366 echo content >>file &&
368 git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
369 commit_msg_is "message #comment"
372 test_expect_success 'message shows author when it is not equal to committer' '
374 git commit -e -m "sample" -a &&
376 "^# Author: *A U Thor <author@example.com>\$" \
380 test_expect_success 'message shows date when it is explicitly set' '
381 git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
383 "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
387 test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
391 sane_unset GIT_COMMITTER_EMAIL &&
392 sane_unset GIT_COMMITTER_NAME &&
393 git commit -e -m "sample" -a
395 # the ident is calculated from the system, so we cannot
396 # check the actual value, only that it is there
397 test_i18ngrep "^# Committer: " .git/COMMIT_EDITMSG
400 write_script .git/FAKE_EDITOR <<EOF
401 echo editor started >"$(pwd)/.git/result"
405 test_expect_success !AUTOIDENT 'do not fire editor when committer is bogus' '
410 sane_unset GIT_COMMITTER_EMAIL &&
411 sane_unset GIT_COMMITTER_NAME &&
412 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
414 test_must_fail git commit -e -m sample -a
416 test_must_be_empty .git/result
419 test_expect_success 'do not fire editor if -m <msg> was given' '
422 echo "editor not started" >.git/result &&
423 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
424 test "$(cat .git/result)" = "editor not started"
427 test_expect_success 'do not fire editor if -m "" was given' '
430 echo "editor not started" >.git/result &&
431 (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
432 git commit -m "" --allow-empty-message) &&
433 test "$(cat .git/result)" = "editor not started"
436 test_expect_success 'do not fire editor in the presence of conflicts' '
441 git commit -m "add g" &&
446 git commit -m "modify g and add h" &&
447 git checkout second &&
450 git commit -m second &&
451 # Must fail due to conflict
452 test_must_fail git cherry-pick -n master &&
453 echo "editor not started" >.git/result &&
455 GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
457 test_must_fail git commit
459 test "$(cat .git/result)" = "editor not started"
462 write_script .git/FAKE_EDITOR <<EOF
463 # kill -TERM command added below.
466 test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
468 ! "$SHELL_PATH" -c '\''
469 echo kill -TERM $$ >>.git/FAKE_EDITOR
470 GIT_EDITOR=.git/FAKE_EDITOR
472 exec git commit -a'\'' &&
473 test ! -f .git/index.lock
476 rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
479 test_expect_success 'Hand committing of a redundant merge removes dups' '
481 git rev-parse second master >expect &&
482 test_must_fail git merge second master &&
483 git checkout master g &&
484 EDITOR=: git commit -a &&
485 git cat-file commit HEAD >raw &&
486 sed -n -e "s/^parent //p" -e "/^$/q" raw >actual &&
487 test_cmp expect actual
491 test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
494 git commit -s -m "hello: kitty" --allow-empty &&
495 git cat-file commit HEAD >raw &&
496 sed -e "1,/^$/d" raw >actual &&
497 test_line_count = 3 actual
501 test_expect_success 'commit -s places sob on third line after two empty lines' '
502 git commit -s --allow-empty --allow-empty-message &&
503 cat <<-EOF >expect &&
506 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
509 sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
510 test_cmp expect actual
513 write_script .git/FAKE_EDITOR <<\EOF
521 echo '## Custom template' >template
526 GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
527 case "$use_template" in
529 test_i18ngrep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
531 test_i18ngrep "^## Custom template" .git/COMMIT_EDITMSG ;;
535 try_commit_status_combo () {
537 test_expect_success 'commit' '
539 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
542 test_expect_success 'commit --status' '
543 try_commit --status &&
544 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
547 test_expect_success 'commit --no-status' '
548 try_commit --no-status &&
549 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
552 test_expect_success 'commit with commit.status = yes' '
553 test_config commit.status yes &&
555 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
558 test_expect_success 'commit with commit.status = no' '
559 test_config commit.status no &&
561 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
564 test_expect_success 'commit --status with commit.status = yes' '
565 test_config commit.status yes &&
566 try_commit --status &&
567 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
570 test_expect_success 'commit --no-status with commit.status = yes' '
571 test_config commit.status yes &&
572 try_commit --no-status &&
573 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
576 test_expect_success 'commit --status with commit.status = no' '
577 test_config commit.status no &&
578 try_commit --status &&
579 test_i18ngrep "^# Changes to be committed:" .git/COMMIT_EDITMSG
582 test_expect_success 'commit --no-status with commit.status = no' '
583 test_config commit.status no &&
584 try_commit --no-status &&
585 test_i18ngrep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
590 try_commit_status_combo
592 use_template="-t template"
594 try_commit_status_combo
596 test_expect_success 'commit --status with custom comment character' '
597 test_config core.commentchar ";" &&
598 try_commit --status &&
599 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
602 test_expect_success 'switch core.commentchar' '
603 test_commit "#foo" foo &&
604 GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
605 test_i18ngrep "^; Changes to be committed:" .git/COMMIT_EDITMSG
608 test_expect_success 'switch core.commentchar but out of options' '
621 git commit --amend -F text &&
623 test_set_editor .git/FAKE_EDITOR &&
624 test_must_fail git -c core.commentChar=auto commit --amend