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

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

From
NRNoam Cohen via B4 Relay <devnull+noam.noam.me@kernel.org>
Date
Nov 19, 2026, 03:43 UTC
Message-ID
<20261119-ls-tree-format-z-verbatim-path-v2-1-c014e4e4c96f@noam.me>
In-Reply-To
<20261118-ls-tree-format-z-verbatim-path-v1-1-718987241861@noam.me>
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(-)
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
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 6 in “ls-tree: do not quote %(path) with -z”
  1. ls-tree: do not quote %(path) with -zNoam Cohen via B4 Relay, Nov 18, 2026
  2. Junio C HamanoNov 18, 2026
  3. Noam CohenNov 18, 2026
  4. Junio C HamanoNov 19, 2026
  5. ls-tree: do not quote %(path) with -zNoam Cohen via B4 Relay, Nov 19, 2026
  6. Junio C HamanoNov 19, 2026

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.