Merge branch 'bc/commit-complete-lines-given-via-m-option' into maint

'git commit -m "$msg"' used to add an extra newline even when
$msg already ended with one.

* bc/commit-complete-lines-given-via-m-option:
  Documentation/git-commit.txt: rework the --cleanup section
  git-commit: only append a newline to -m mesg if necessary
  t7502: demonstrate breakage with a commit message with trailing newlines
  t/t7502: compare entire commit message with what was expected
This commit is contained in:
Junio C Hamano 2013-04-03 09:26:07 -07:00
commit 14c79b1faa
3 changed files with 34 additions and 15 deletions

View File

@ -174,16 +174,25 @@ OPTIONS
linkgit:git-commit-tree[1]. linkgit:git-commit-tree[1].
--cleanup=<mode>:: --cleanup=<mode>::
This option sets how the commit message is cleaned up. This option determines how the supplied commit message should be
The '<mode>' can be one of 'verbatim', 'whitespace', 'strip', cleaned up before committing. The '<mode>' can be `strip`,
and 'default'. The 'default' mode will strip leading and `whitespace`, `verbatim`, or `default`.
trailing empty lines and #commentary from the commit message +
only if the message is to be edited. Otherwise only whitespace --
removed. The 'verbatim' mode does not change message at all, strip::
'whitespace' removes just leading/trailing whitespace lines Strip leading and trailing empty lines, trailing whitespace, and
and 'strip' removes both whitespace and commentary. The default #commentary and collapse consecutive empty lines.
can be changed by the 'commit.cleanup' configuration variable whitespace::
(see linkgit:git-config[1]). Same as `strip` except #commentary is not removed.
verbatim::
Do not change the message at all.
default::
Same as `strip` if the message is to be edited.
Otherwise `whitespace`.
--
+
The default can be changed by the 'commit.cleanup' configuration
variable (see linkgit:git-config[1]).
-e:: -e::
--edit:: --edit::

View File

@ -124,8 +124,10 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
if (unset) if (unset)
strbuf_setlen(buf, 0); strbuf_setlen(buf, 0);
else { else {
if (buf->len)
strbuf_addch(buf, '\n');
strbuf_addstr(buf, arg); strbuf_addstr(buf, arg);
strbuf_addstr(buf, "\n\n"); strbuf_complete_line(buf);
} }
return 0; return 0;
} }

View File

@ -177,12 +177,20 @@ test_expect_success 'verbose respects diff config' '
git config --unset color.diff git config --unset color.diff
' '
mesg_with_comment_and_newlines='
# text
'
test_expect_success 'prepare file with comment line and trailing newlines' '
printf "%s" "$mesg_with_comment_and_newlines" >expect
'
test_expect_success 'cleanup commit messages (verbatim option,-t)' ' test_expect_success 'cleanup commit messages (verbatim option,-t)' '
echo >>negative && echo >>negative &&
{ echo;echo "# text";echo; } >expect && git commit --cleanup=verbatim --no-status -t expect -a &&
git commit --cleanup=verbatim -t expect -a && git cat-file -p HEAD |sed -e "1,/^\$/d" >actual &&
git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
test_cmp expect actual test_cmp expect actual
' '
@ -199,7 +207,7 @@ test_expect_success 'cleanup commit messages (verbatim option,-F)' '
test_expect_success 'cleanup commit messages (verbatim option,-m)' ' test_expect_success 'cleanup commit messages (verbatim option,-m)' '
echo >>negative && echo >>negative &&
git commit --cleanup=verbatim -m "$(cat expect)" -a && git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
git cat-file -p HEAD |sed -e "1,/^\$/d">actual && git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
test_cmp expect actual test_cmp expect actual