]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit: do not ignore an empty message given by -m ''
authorJeff King <peff@peff.net>
Thu, 7 Apr 2016 19:56:26 +0000 (12:56 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 7 Apr 2016 20:25:12 +0000 (13:25 -0700)
When f9568530 (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 25206778 (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 25206778
introduced to fix the same bug.

Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/commit.c
t/t7501-commit.sh

index c2ebea4ed31d13cfe48603042edfb0ca63f5de6e..9a203bde74db9a4be2d55d4507f00f7c329cd2a7 100644 (file)
@@ -694,7 +694,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                }
        }
 
-       if (message.len) {
+       if (have_option_m) {
                strbuf_addbuf(&sb, &message);
                hook_arg1 = "message";
        } else if (logfile && !strcmp(logfile, "-")) {
@@ -1162,9 +1162,9 @@ static int parse_and_validate_options(int argc, const char *argv[],
                f++;
        if (f > 1)
                die(_("Only one of -c/-C/-F/--fixup can be used."));
-       if (message.len && f > 0)
+       if (have_option_m && f > 0)
                die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
-       if (f || message.len)
+       if (f || have_option_m)
                template_file = NULL;
        if (edit_message)
                use_message = edit_message;
index a7e9322d2f122b1200d4541c6a7e5eb30c063e5e..900f7de05a67424c867e0f149e7e7a448111791c 100755 (executable)
@@ -200,8 +200,8 @@ test_expect_success '--amend --edit of empty message' '
        test_cmp expect msg
 '
 
-test_expect_failure '--amend to set message to empty' '
-       echo batรก >file &&
+test_expect_success '--amend to set message to empty' '
+       echo bata >file &&
        git add file &&
        git commit -m "unamended" &&
        git commit --amend --allow-empty-message -m "" &&
@@ -210,7 +210,7 @@ test_expect_failure '--amend to set message to empty' '
        test_cmp expect msg
 '
 
-test_expect_failure '--amend to set empty message needs --allow-empty-message' '
+test_expect_success '--amend to set empty message needs --allow-empty-message' '
        echo conga >file &&
        git add file &&
        git commit -m "unamended" &&