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

[BONUS][PATCH] ls-tree: remove dead store and strbuf for quote_c_style()

From
René Scharfe <l.s.r@web.de>
Date
Jan 14, 2023, 15:03 UTC
Message-ID
<55ae7333-3a13-0575-93ed-f858a1c2877e@web.de>
In-Reply-To
<59f0a3f8-2dae-db47-5075-0cf50aada335@web.de>
Stop initializing "name" because it is set again before use.

Let quote_c_style() write directly to "sb" instead of taking a detour through "quoted". This avoids an allocation and a string copy. The result is the same because the function only appends.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 builtin/ls-tree.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index 120fff9fa0..9b804cd13b 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -94,18 +94,15 @@ static size_t expand_show_tree(struct strbuf *sb, const char *start,
 	} else if (skip_prefix(start, "(objectname)", &p)) {
 		strbuf_add_unique_abbrev(sb, data->oid, abbrev);
 	} else if (skip_prefix(start, "(path)", &p)) {
-		const char *name = data->base->buf;
+		const char *name;
 		const char *prefix = chomp_prefix ? ls_tree_prefix : NULL;
-		struct strbuf quoted = STRBUF_INIT;
 		struct strbuf sbuf = STRBUF_INIT;
 		size_t baselen = data->base->len;
 		strbuf_addstr(data->base, data->pathname);
 		name = relative_path(data->base->buf, prefix, &sbuf);
-		quote_c_style(name, &quoted, NULL, 0);
+		quote_c_style(name, sb, NULL, 0);
 		strbuf_setlen(data->base, baselen);
-		strbuf_addbuf(sb, &quoted);
 		strbuf_release(&sbuf);
-		strbuf_release(&quoted);
 	} else {
 		errlen = (unsigned long)len;
 		die(_("bad ls-tree format: %%%.*s"), errlen, start);
--
2.39.0
Previous: René ScharfeNext: Junio C Hamano
Message 2 of 4 in “ls-tree: fix expansion of repeated %(path)”
  1. ls-tree: fix expansion of repeated %(path)René Scharfe, Jan 14, 2023
  2. [BONUS][PATCH] ls-tree: remove dead store and strbuf for quote_c_style()René Scharfe, Jan 14, 2023
  3. Junio C HamanoJan 14, 2023
  4. René ScharfeJan 14, 2023

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.