All of lore.kernel.org
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Git List <git@vger.kernel.org>
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [BONUS][PATCH] ls-tree: remove dead store and strbuf for quote_c_style()
Date: Sat, 14 Jan 2023 16:03:16 +0100	[thread overview]
Message-ID: <55ae7333-3a13-0575-93ed-f858a1c2877e@web.de> (raw)
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

  reply	other threads:[~2023-01-14 15:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-14 14:37 [PATCH] ls-tree: fix expansion of repeated %(path) René Scharfe
2023-01-14 15:03 ` René Scharfe [this message]
2023-01-14 16:46 ` Junio C Hamano
2023-01-14 18:24   ` René Scharfe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55ae7333-3a13-0575-93ed-f858a1c2877e@web.de \
    --to=l.s.r@web.de \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.