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

Re: [PATCH] ls-tree: fix expansion of repeated %(path)

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 14, 2023, 16:46 UTC
Message-ID
<xmqqmt6lkqrp.fsf@gitster.g>
In-Reply-To
<59f0a3f8-2dae-db47-5075-0cf50aada335@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 19 quoted lines
> expand_show_tree() borrows the base strbuf given to us by read_tree() to
> build the full path of the current entry when handling %(path).  Only
> its indirect caller, show_tree_fmt(), removes the added entry name.
> That works fine as long as %(path) is only included once in the format
> string, but accumulates duplicates if it's repeated:
>
>    $ git ls-tree --format='%(path) %(path) %(path)' HEAD M*
>    Makefile MakefileMakefile MakefileMakefileMakefile
>
> Reset the length after each use to get the same expansion every time;
> here's the behavior with this patch:
>
>    $ ./git ls-tree --format='%(path) %(path) %(path)' HEAD M*
>    Makefile Makefile Makefile
>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  builtin/ls-tree.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)

I wonder if this was broken from its introduction at 455923e0 (ls-tree: introduce "--format" option, 2022-03-23)?

It seems to be the case. With the following applied on top of 455923e0, the new test fails as expected, and your patch fixes the breakage, so I am tempted to squash the tests in ;-)

Thanks.
 t/t3104-ls-tree-format.sh | 6 ++++++
 1 file changed, 6 insertions(+)
diff --git c/t/t3104-ls-tree-format.sh w/t/t3104-ls-tree-format.sh
index 7f1eb699d3..7e6c4dc5da 100755
--- c/t/t3104-ls-tree-format.sh
+++ w/t/t3104-ls-tree-format.sh
@@ -37,6 +37,12 @@ test_ls_tree_format () {
 	'
 }
 
+test_expect_success "ls-tree --format='%(path) %(path) %(path)' HEAD top-file" '
+	git ls-tree --format="%(path) %(path) %(path)" HEAD top-file.t >actual &&
+	echo top-file.t top-file.t top-file.t >expect &&
+	test_cmp expect actual
+'
+
 test_ls_tree_format \
 	"%(objectmode) %(objecttype) %(objectname)%x09%(path)" \
 	""
Previous: René ScharfeNext: René Scharfe
Message 3 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.