threads / patch / 66875

patchls-tree: do not quote %(path) with -z

Subject: [PATCH] ls-tree: do not quote %(path) with -z

## tl;dr

With -z, ls-tree --format has C-quoted %(path) since 2022, contrary to its documentation and to ls-files --format. Read the story.

replies: 5people: 3as markdown or json

Noam Cohen via B4 Relay· Nov 18, 2026, 23:04 UTC · lore
From: Noam Cohen <noam@noam.me>

With -z, "git ls-tree" is documented to output the filename verbatim, but since 455923e0a1 (ls-tree: introduce "--format" option, 2022-03-23) the %(path) placeholder of a custom --format is always C-quoted.

Output it verbatim when -z is given, like "git ls-files --format" already does.

Signed-off-by: Noam Cohen <noam@noam.me>
---
This makes "ls-tree -z --format" match "ls-files -z --format", which
already outputs %(path) verbatim.
---
 builtin/ls-tree.c         | 5 ++++-
 t/t3104-ls-tree-format.sh | 9 ++++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
Show changes to 2 files +12 −2

builtin/ls-tree.c, t/t3104-ls-tree-format.sh

diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index 46edaffc2e..d25bbffcdd 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -130,7 +130,10 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
 
 			strbuf_addstr(base, pathname);
 			name = relative_path(base->buf, prefix, &sbuf);
-			quote_c_style(name, &sb, NULL, 0);
+			if (options->null_termination)
+				strbuf_addstr(&sb, name);
+			else
+				quote_c_style(name, &sb, NULL, 0);
 			strbuf_setlen(base, baselen);
 			strbuf_release(&sbuf);
 		} else
diff --git a/t/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh
index a1b2069a25..016c0fd0ff 100755
--- a/t/t3104-ls-tree-format.sh
+++ b/t/t3104-ls-tree-format.sh
@@ -12,7 +12,8 @@ test_expect_success 'ls-tree --format usage' '
 '
 
 test_expect_success 'setup' '
-	setup_basic_ls_tree_data
+	setup_basic_ls_tree_data &&
+	test_commit נעם
 '
 
 test_ls_tree_format () {
@@ -76,4 +77,10 @@ test_ls_tree_format \
 	"--full-tree" \
 	"--full-tree"
 
+test_expect_success 'ls-tree --format with -z does not quote paths' '
+	printf "blob נעם.t\0" >expect &&
+	git ls-tree -z --format="%(objecttype) %(path)" HEAD נעם.t >actual &&
+	test_cmp expect actual
+'
+
 test_done

---
base-commit: 03b41523c576f26ad34085378717a0c1b287470f
change-id: 20261118-ls-tree-format-z-verbatim-path-f5783b61ff8f
Junio C Hamano· Nov 18, 2026, 23:20 UTC · re: Noam Cohen via B4 Relay · lore

Re: [PATCH] ls-tree: do not quote %(path) with -z

Noam Cohen via B4 Relay <devnull+noam.noam.me@kernel.org> writes:
Show 9 quoted lines
> From: Noam Cohen <noam@noam.me>
>
> With -z, "git ls-tree" is documented to output the filename verbatim,
> but since 455923e0a1 (ls-tree: introduce "--format" option,
> 2022-03-23) the %(path) placeholder of a custom --format is always
> C-quoted.
>
> Output it verbatim when -z is given, like "git ls-files --format"
> already does.

ce74de931d (ls-files: introduce "--format" option, 2022-07-23) was the one that added --format, and the documentation added by the commit does talk about -z. But curiously, it does not add any test that exercises --format="%(path)" combined with -z.

The code change is trivially correct.
Show 5 quoted lines
>  test_expect_success 'setup' '
> -	setup_basic_ls_tree_data
> +	setup_basic_ls_tree_data &&
> +	test_commit נעם
>  '
Do we have to really use characters outside US-ASCII?
Show 7 quoted lines
> @@ -76,4 +77,10 @@ test_ls_tree_format \
>  	"--full-tree" \
>  	"--full-tree"
>  
> +test_expect_success 'ls-tree --format with -z does not quote paths' '
> +	printf "blob נעם.t\0" >expect &&
> +	git ls-tree -z --format="%(objecttype) %(path)" HEAD נעם.t >actual &&
OK, perhaps.
Noam Cohen· Nov 18, 2026, 23:45 UTC · re: Junio C Hamano · lore

Re: [PATCH] ls-tree: do not quote %(path) with -z

Junio C Hamano <gitster@pobox.com> writes:
Show 7 quoted lines
>>  test_expect_success 'setup' '
>> -    setup_basic_ls_tree_data
>> +    setup_basic_ls_tree_data &&
>> +    test_commit נעם
>>  '
>
> Do we have to really use characters outside US-ASCII?

The only US-ASCII bytes that quote_c_style() quotes are control characters, DEL, '"' and '\', and Windows does not allow them in filenames, except for DEL, which felt too obscure to rely on.

We could avoid the filesystem and synthesize the tree with "git mktree" instead, which is what I had at first, but that made the test noticeably more complicated than a test_commit in the setup.

We already have tests that use non-ASCII paths, like t3902, t4012, and others.

Do you think the test should set core.quotePath explicitly, rather than relying on its default?

Also, should I just drop the test, or add a similar one for ls-files --format?

Junio C Hamano· Nov 19, 2026, 01:24 UTC · re: Noam Cohen · lore

Re: [PATCH] ls-tree: do not quote %(path) with -z

Noam Cohen <noam@noam.me> writes:
> We could avoid the filesystem and synthesize the tree with "git
> mktree" instead, which is what I had at first, but that made the
> test noticeably more complicated than a test_commit in the setup.

Yeah, that, with forced core.quotePath setting, was what I had in mind.

But it is not a requirement to avoid stepping outside US-ASCII while writing tests. It is much better to have tests that use bytes from outside US-ASCII than not having at all. It is just that it would be nicer if we do not have to worry about platform anomalies that cannot store characters outside US-ASCII range.

Noam Cohen via B4 Relay· Nov 19, 2026, 03:43 UTC · re: Noam Cohen via B4 Relay · lore

[PATCH v2] ls-tree: do not quote %(path) with -z

From: Noam Cohen <noam@noam.me>

With -z, "git ls-tree" is documented to output the filename verbatim, but since 455923e0a1 (ls-tree: introduce "--format" option, 2022-03-23) the %(path) placeholder of a custom --format is always C-quoted.

Output it verbatim when -z is given, like "git ls-files --format" already does.

Signed-off-by: Noam Cohen <noam@noam.me>
---
This makes "ls-tree -z --format" match "ls-files -z --format", which
already outputs %(path) verbatim.
---
Changes in v2:
- Use a path with a double quote, built with "git mktree", so the test
  stays within US-ASCII and does not depend on the filesystem.
- Force core.quotePath=true in the test.
- Link to v1: https://patch.msgid.link/20261118-ls-tree-format-z-verbatim-path-v1-1-718987241861@noam.me
---
 builtin/ls-tree.c         | 5 ++++-
 t/t3104-ls-tree-format.sh | 9 +++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
Show changes to 2 files +13 −1

builtin/ls-tree.c, t/t3104-ls-tree-format.sh

diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index 46edaffc2e..d25bbffcdd 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -130,7 +130,10 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
 
 			strbuf_addstr(base, pathname);
 			name = relative_path(base->buf, prefix, &sbuf);
-			quote_c_style(name, &sb, NULL, 0);
+			if (options->null_termination)
+				strbuf_addstr(&sb, name);
+			else
+				quote_c_style(name, &sb, NULL, 0);
 			strbuf_setlen(base, baselen);
 			strbuf_release(&sbuf);
 		} else
diff --git a/t/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh
index a1b2069a25..0df216909b 100755
--- a/t/t3104-ls-tree-format.sh
+++ b/t/t3104-ls-tree-format.sh
@@ -76,4 +76,13 @@ test_ls_tree_format \
 	"--full-tree" \
 	"--full-tree"
 
+test_expect_success 'ls-tree --format with -z does not quote paths' '
+	blob=$(git rev-parse HEAD:top-file.t) &&
+	tree=$(printf "100644 blob $blob\tdouble\"quote\n" | git mktree) &&
+	printf "blob double\"quote\0" >expect &&
+	git -c core.quotePath=true ls-tree -z \
+		--format="%(objecttype) %(path)" $tree >actual &&
+	test_cmp expect actual
+'
+
 test_done

---
base-commit: 03b41523c576f26ad34085378717a0c1b287470f
change-id: 20261118-ls-tree-format-z-verbatim-path-f5783b61ff8f
Junio C Hamano· Nov 19, 2026, 16:12 UTC · re: Noam Cohen via B4 Relay · lore

Re: [PATCH v2] ls-tree: do not quote %(path) with -z

Noam Cohen via B4 Relay <devnull+noam.noam.me@kernel.org> writes:
Show 25 quoted lines
> From: Noam Cohen <noam@noam.me>
>
> With -z, "git ls-tree" is documented to output the filename verbatim,
> but since 455923e0a1 (ls-tree: introduce "--format" option,
> 2022-03-23) the %(path) placeholder of a custom --format is always
> C-quoted.
>
> Output it verbatim when -z is given, like "git ls-files --format"
> already does.
> ...
> -			quote_c_style(name, &sb, NULL, 0);
> +			if (options->null_termination)
> +				strbuf_addstr(&sb, name);
> +			else
> +				quote_c_style(name, &sb, NULL, 0);
>  			strbuf_setlen(base, baselen);
> ...
> +test_expect_success 'ls-tree --format with -z does not quote paths' '
> +	blob=$(git rev-parse HEAD:top-file.t) &&
> +	tree=$(printf "100644 blob $blob\tdouble\"quote\n" | git mktree) &&
> +	printf "blob double\"quote\0" >expect &&
> +	git -c core.quotePath=true ls-tree -z \
> +		--format="%(objecttype) %(path)" $tree >actual &&
> +	test_cmp expect actual
> +'

Everything looks as expected. Nicely done. Will queue. Thanks.

← back to recent threads