Skip to content

Commit 14c79b1

Browse files
committed
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
2 parents 295e393 + 46fbf75 commit 14c79b1

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

Documentation/git-commit.txt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,25 @@ OPTIONS
174174
linkgit:git-commit-tree[1].
175175

176176
--cleanup=<mode>::
177-
This option sets how the commit message is cleaned up.
178-
The '<mode>' can be one of 'verbatim', 'whitespace', 'strip',
179-
and 'default'. The 'default' mode will strip leading and
180-
trailing empty lines and #commentary from the commit message
181-
only if the message is to be edited. Otherwise only whitespace
182-
removed. The 'verbatim' mode does not change message at all,
183-
'whitespace' removes just leading/trailing whitespace lines
184-
and 'strip' removes both whitespace and commentary. The default
185-
can be changed by the 'commit.cleanup' configuration variable
186-
(see linkgit:git-config[1]).
177+
This option determines how the supplied commit message should be
178+
cleaned up before committing. The '<mode>' can be `strip`,
179+
`whitespace`, `verbatim`, or `default`.
180+
+
181+
--
182+
strip::
183+
Strip leading and trailing empty lines, trailing whitespace, and
184+
#commentary and collapse consecutive empty lines.
185+
whitespace::
186+
Same as `strip` except #commentary is not removed.
187+
verbatim::
188+
Do not change the message at all.
189+
default::
190+
Same as `strip` if the message is to be edited.
191+
Otherwise `whitespace`.
192+
--
193+
+
194+
The default can be changed by the 'commit.cleanup' configuration
195+
variable (see linkgit:git-config[1]).
187196

188197
-e::
189198
--edit::

builtin/commit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
124124
if (unset)
125125
strbuf_setlen(buf, 0);
126126
else {
127+
if (buf->len)
128+
strbuf_addch(buf, '\n');
127129
strbuf_addstr(buf, arg);
128-
strbuf_addstr(buf, "\n\n");
130+
strbuf_complete_line(buf);
129131
}
130132
return 0;
131133
}

t/t7502-commit.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,20 @@ test_expect_success 'verbose respects diff config' '
177177
git config --unset color.diff
178178
'
179179

180+
mesg_with_comment_and_newlines='
181+
# text
182+
183+
'
184+
185+
test_expect_success 'prepare file with comment line and trailing newlines' '
186+
printf "%s" "$mesg_with_comment_and_newlines" >expect
187+
'
188+
180189
test_expect_success 'cleanup commit messages (verbatim option,-t)' '
181190
182191
echo >>negative &&
183-
{ echo;echo "# text";echo; } >expect &&
184-
git commit --cleanup=verbatim -t expect -a &&
185-
git cat-file -p HEAD |sed -e "1,/^\$/d" |head -n 3 >actual &&
192+
git commit --cleanup=verbatim --no-status -t expect -a &&
193+
git cat-file -p HEAD |sed -e "1,/^\$/d" >actual &&
186194
test_cmp expect actual
187195
188196
'
@@ -199,7 +207,7 @@ test_expect_success 'cleanup commit messages (verbatim option,-F)' '
199207
test_expect_success 'cleanup commit messages (verbatim option,-m)' '
200208
201209
echo >>negative &&
202-
git commit --cleanup=verbatim -m "$(cat expect)" -a &&
210+
git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
203211
git cat-file -p HEAD |sed -e "1,/^\$/d">actual &&
204212
test_cmp expect actual
205213

0 commit comments

Comments
 (0)