git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] commit: --amend -m '' silently fails to wipe message

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 7, 2016, 19:56 UTC
Message-ID
<xmqq1t6hyyz9.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160407190210.GA4478@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
> Yes, FWIW, those were the sites and reasons I identified last night.
> Your patch looks like the right thing to me.
Thanks, let's do this then.  I'd already anticipated your sign-off ;-).
-- >8 --
From: Jeff King <peff@peff.net>
Subject: commit: do not ignore an empty message given by -m ''

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 | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 98e1527..391126e 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -695,7 +695,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, "-")) {
@@ -1172,9 +1172,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;
Previous: Jeff KingNext: Jeff King
Message 6 of 9 in “commit: --amend -m '' silently fails to wipe message”
  1. commit: --amend -m '' silently fails to wipe messageAdam Dinwoodie, Apr 6, 2016
  2. Jeff KingApr 7, 2016
  3. Jeff KingApr 7, 2016
  4. Junio C HamanoApr 7, 2016
  5. Jeff KingApr 7, 2016
  6. Junio C HamanoApr 7, 2016
  7. Jeff KingApr 7, 2016
  8. Adam DinwoodieApr 7, 2016
  9. Jeff KingApr 7, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.