Skip to content

Commit 27014cb

Browse files
peffgitster
authored andcommitted
commit: do not ignore an empty message given by -m ''
When f956853 (builtin-commit: resurrect behavior for multiple -m options, 2007-11-11) converted a "char *message" to "struct strbuf message" to hold the messages given with the "-m" option, it incorrectly changed the checks "did we get a message with the -m option?" to "is message.len 0?". Later, we noticed one breakage from this change and corrected it with 2520677 (commit: don't start editor if empty message is given with -m, 2013-05-25). However, "we got a message with -m, even though an empty one, so we shouldn't be launching an editor" was not the only breakage. * "git commit --amend -m '' --allow-empty", even though it looks strange, is a valid request to amend the commit to have no message at all. Due to the misdetection of the presence of -m on the command line, we ended up keeping the log messsage from the original commit. * "git commit -m "$msg" -F file" should be rejected whether $msg is an empty string or not, but due to the same bug, was not rejected when $msg is empty. * "git -c template=file -m "$msg"" should ignore the template even when $msg is empty, but it didn't and instead used the contents from the template file. Correct these by checking have_option_m, which the earlier 2520677 introduced to fix the same bug. Reported-by: Adam Dinwoodie <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 178e814 commit 27014cb

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

builtin/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
694694
}
695695
}
696696

697-
if (message.len) {
697+
if (have_option_m) {
698698
strbuf_addbuf(&sb, &message);
699699
hook_arg1 = "message";
700700
} else if (logfile && !strcmp(logfile, "-")) {
@@ -1162,9 +1162,9 @@ static int parse_and_validate_options(int argc, const char *argv[],
11621162
f++;
11631163
if (f > 1)
11641164
die(_("Only one of -c/-C/-F/--fixup can be used."));
1165-
if (message.len && f > 0)
1165+
if (have_option_m && f > 0)
11661166
die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1167-
if (f || message.len)
1167+
if (f || have_option_m)
11681168
template_file = NULL;
11691169
if (edit_message)
11701170
use_message = edit_message;

t/t7501-commit.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ test_expect_success '--amend --edit of empty message' '
200200
test_cmp expect msg
201201
'
202202

203-
test_expect_failure '--amend to set message to empty' '
204-
echo batá >file &&
203+
test_expect_success '--amend to set message to empty' '
204+
echo bata >file &&
205205
git add file &&
206206
git commit -m "unamended" &&
207207
git commit --amend --allow-empty-message -m "" &&
@@ -210,7 +210,7 @@ test_expect_failure '--amend to set message to empty' '
210210
test_cmp expect msg
211211
'
212212

213-
test_expect_failure '--amend to set empty message needs --allow-empty-message' '
213+
test_expect_success '--amend to set empty message needs --allow-empty-message' '
214214
echo conga >file &&
215215
git add file &&
216216
git commit -m "unamended" &&

0 commit comments

Comments
 (0)