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

[PATCH 1/3] builtin-commit: fix reflog message generation

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Nov 8, 2007, 12:15 UTC
Message-ID
<Pine.LNX.4.64.0711081215140.4362@racer.site>
In-Reply-To
<Pine.LNX.4.64.0711081213580.4362@racer.site>

Instead of strdup()ing, we can just reuse the buffer in which the commit message is stored, and which is supposed to hold the reflog message anyway.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin-commit.c |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/builtin-commit.c b/builtin-commit.c
index f108e90..bba9b82 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -488,7 +488,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 	int header_len, parent_count = 0;
 	struct strbuf sb;
 	const char *index_file, *reflog_msg;
-	char *nl, *header_line;
+	char *nl;
 	unsigned char commit_sha1[20];
 	struct ref_lock *ref_lock;
 
@@ -585,12 +585,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 					   0);
 
 	nl = strchr(sb.buf + header_len, '\n');
-	header_line = xstrndup(sb.buf + header_len,
-			       nl - (sb.buf + header_len));
-	strbuf_release(&sb);
-	strbuf_addf(&sb, "%s: %s\n", reflog_msg, header_line);
-	strbuf_addch(&sb, '\0');
-	free(header_line);
+	if (nl)
+		strbuf_setlen(&sb, nl + 1 - sb.buf);
+	else
+		strbuf_addch(&sb, '\n');
+	strbuf_remove(&sb, 0, header_len);
+	strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
+	strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
 
 	if (!ref_lock)
 		die("cannot lock HEAD ref");
-- 
1.5.3.5.1634.g0fa78
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 2 of 13 in “builtin-commit fixes”
  1. 0/3 builtin-commit fixesJohannes Schindelin, Nov 8, 2007
  2. 1/3 builtin-commit: fix reflog message generationJohannes Schindelin, Nov 8, 2007
  3. 2/3 launch_editor(): read the file, even when EDITOR=:Johannes Schindelin, Nov 8, 2007
  4. Johannes SixtNov 8, 2007
  5. 2/3 launch_editor(): read the file, even when EDITOR=:Johannes Schindelin, Nov 8, 2007
  6. 3/3 builtin-commit: fix author date with --amend --author=<author>Johannes Schindelin, Nov 8, 2007
  7. 4/3 t3700: avoid racy git situationJohannes Schindelin, Nov 8, 2007
  8. Johannes SixtNov 8, 2007
  9. Johannes SchindelinNov 8, 2007
  10. Kristian HøgsbergNov 8, 2007
  11. Johannes SixtNov 8, 2007
  12. Junio C HamanoNov 8, 2007
  13. Johannes SchindelinNov 9, 2007

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.