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

[PATCH v2] commit: allow {--amend|-c foo} when {HEAD|foo} has empty message

From
Thomas Rast <trast@student.ethz.ch>
Date
Feb 28, 2012, 10:36 UTC
Message-ID
<010901fbfffe0f806bb19d556ebc1e512a4697f4.1330425111.git.trast@student.ethz.ch>
In-Reply-To
<20120228091422.GC5757@sigill.intra.peff.net>

Because --amend (-c foo) internally load the message from HEAD (foo, resp.) using the same code paths as -C, they erroneously refuse to work at all when the message of HEAD (foo) is empty.

Remove the corresponding check under --amend and -c.

None of this behavior was ever tested (not even for -C empty_message), so we add a whole batch of new tests.

Reported-by: Lazar Florentin <florentin.lazar@gmail.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---

Like the last version, plus Peff's guard for the invalid commit format. I also received Lazar (starlay) 's identity for the attribution.

 builtin/commit.c  |    4 +++-
 t/t7501-commit.sh |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index 3714582..5e9a832 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -690,7 +690,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		hook_arg1 = "message";
 	} else if (use_message) {
 		buffer = strstr(use_message_buffer, "\n\n");
-		if (!buffer || buffer[2] == '\0')
+		if (!buffer)
+			die(_("commit object has invalid format"));
+		if (!amend && !edit_message && buffer[2] == '\0')
 			die(_("commit has empty message"));
 		strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
 		hook_arg1 = "commit";
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index 8bb3833..6ab7712 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -473,4 +473,70 @@ test_expect_success 'amend can copy notes' '
 
 '
 
+test_expect_success 'amend on empty commit message' '
+
+	echo bar > bar &&
+	git add bar &&
+	test_tick &&
+	git commit --allow-empty-message -m "" &&
+	git tag empty_message &&
+	git commit --amend -mnonempty &&
+	git cat-file commit HEAD | grep nonempty
+
+'
+
+test_expect_success 'amend with editor on empty commit message' '
+
+	git reset --hard empty_message &&
+	cat >editor <<-\EOF &&
+	#!/bin/sh
+	echo nonempty_one >"$1"
+	EOF
+	chmod 755 editor &&
+	EDITOR=./editor git commit --amend &&
+	git cat-file commit HEAD | grep nonempty_one
+
+'
+
+test_expect_success '--amend -C empty_message fails' '
+
+	test_commit nonempty &&
+	test_must_fail git commit --amend -C empty_message
+
+'
+
+test_expect_success '-C empty_message fails' '
+
+	echo 1 > bar &&
+	git add bar &&
+	test_must_fail git commit --amend -C empty_message
+
+'
+
+test_expect_success '--amend -c empty_message works' '
+
+	cat >editor <<-\EOF &&
+	#!/bin/sh
+	echo nonempty_two >"$1"
+	EOF
+	chmod 755 editor &&
+	EDITOR=./editor git commit --amend -c empty_message &&
+	git cat-file commit HEAD | grep nonempty_two
+
+'
+
+test_expect_success '-c empty_message works' '
+
+	echo 2 > bar &&
+	git add bar &&
+	cat >editor <<-\EOF &&
+	#!/bin/sh
+	echo nonempty_three >"$1"
+	EOF
+	chmod 755 editor &&
+	EDITOR=./editor git commit -c empty_message &&
+	git cat-file commit HEAD | grep nonempty_three
+
+'
+
 test_done
-- 
1.7.9.2.467.g7fee4
Previous: Junio C HamanoNext: Thomas Rast
Message 8 of 13 in “commit: allow {--amend|-c foo} when {HEAD|foo} has empty message”
  1. commit: allow {--amend|-c foo} when {HEAD|foo} has empty messageThomas Rast, Feb 28, 2012
  2. Jeff KingFeb 28, 2012
  3. Jeff KingFeb 28, 2012
  4. Thomas RastFeb 28, 2012
  5. Junio C HamanoFeb 28, 2012
  6. Jeff KingFeb 28, 2012
  7. Junio C HamanoFeb 28, 2012
  8. commit: allow {--amend|-c foo} when {HEAD|foo} has empty messageThomas Rast, Feb 28, 2012
  9. pretty: detect missing \n\n in commit messageThomas Rast, Feb 28, 2012
  10. Nguyen Thai Ngoc DuyFeb 28, 2012
  11. Junio C HamanoFeb 28, 2012
  12. parse_commit: refuse to load commit without \n\n separatorThomas Rast, Feb 28, 2012
  13. Junio C HamanoFeb 28, 2012

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.