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

[RFC PATCH 4/4] ls-tree: make "line_termination" less generic

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Nov 17, 2022, 13:48 UTC
Message-ID
<RFC-patch-4.4-6efba6223a2-20221117T134528Z-avarab@gmail.com>
In-Reply-To
<RFC-cover-0.4-00000000000-20221117T134528Z-avarab@gmail.com>

The "ls-tree" command isn't capable of ending "lines" with anything except '\n' or '\0', and in the latter case we can avoid calling write_name_quoted_relative() entirely. Let's do that, less for optimization and more for clarity, the write_name_quoted_relative() API itself does much the same thing.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/ls-tree.c | 58 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 14 deletions(-)
diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index da664eecfb9..a743959f2b3 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -39,7 +39,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
 }
 
 struct ls_tree_options {
-	int line_termination;
+	unsigned null_termination:1;
 	int abbrev;
 	enum ls_tree_path_options {
 		LS_RECURSIVE = 1 << 0,
@@ -166,7 +166,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
 
 	baselen = base->len;
 	strbuf_expand(&sb, options->format, expand_show_tree, &cb_data);
-	strbuf_addch(&sb, options->line_termination);
+	strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
 	fwrite(sb.buf, sb.len, 1, stdout);
 	strbuf_release(&sb);
 	strbuf_setlen(base, baselen);
@@ -198,10 +198,22 @@ static void show_tree_common_default_long(struct ls_tree_options *options,
 					  const char *pathname,
 					  const size_t baselen)
 {
+	const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
+
 	strbuf_addstr(base, pathname);
-	write_name_quoted_relative(base->buf,
-				   options->chomp_prefix ? options->ls_tree_prefix : NULL, stdout,
-				   options->line_termination);
+
+	if (options->null_termination) {
+		struct strbuf sb = STRBUF_INIT;
+		const char *name = relative_path(base->buf, prefix, &sb);
+
+		fputs(name, stdout);
+		fputc('\0', stdout);
+
+		strbuf_release(&sb);
+	} else {
+		write_name_quoted_relative(base->buf, prefix, stdout, '\n');
+	}
+
 	strbuf_setlen(base, baselen);
 }
 
@@ -264,15 +276,25 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
 	int recurse;
 	const size_t baselen = base->len;
 	enum object_type type = object_type(mode);
+	const char *prefix;
 
 	early = show_tree_common(options, &recurse, base, pathname, type);
 	if (early >= 0)
 		return early;
 
+	prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
 	strbuf_addstr(base, pathname);
-	write_name_quoted_relative(base->buf,
-				   options->chomp_prefix ? options->ls_tree_prefix : NULL,
-				   stdout, options->line_termination);
+	if (options->null_termination) {
+		struct strbuf sb = STRBUF_INIT;
+		const char *name = relative_path(base->buf, prefix, &sb);
+
+		fputs(name, stdout);
+		fputc('\0', stdout);
+
+		strbuf_release(&sb);
+	} else {
+		write_name_quoted_relative(base->buf, prefix, stdout, '\n');
+	}
 	strbuf_setlen(base, baselen);
 	return recurse;
 }
@@ -285,12 +307,19 @@ static int show_tree_object(const struct object_id *oid, struct strbuf *base,
 	int early;
 	int recurse;
 	enum object_type type = object_type(mode);
+	const char *str;
 
 	early = show_tree_common(options, &recurse, base, pathname, type);
 	if (early >= 0)
 		return early;
 
-	printf("%s%c", find_unique_abbrev(oid, options->abbrev), options->line_termination);
+	str = find_unique_abbrev(oid, options->abbrev);
+	if (options->null_termination) {
+		fputs(str, stdout);
+		fputc('\0', stdout);
+	} else  {
+		puts(str);
+	}
 	return recurse;
 }
 
@@ -342,9 +371,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 	int i, full_tree = 0;
 	read_tree_fn_t fn = NULL;
 	enum ls_tree_cmdmode cmdmode = MODE_DEFAULT;
-	struct ls_tree_options options = {
-		.line_termination = '\n',
-	};
+	int null_termination = 0;
+	struct ls_tree_options options = { 0 };
 	const struct option ls_tree_options[] = {
 		OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"),
 			LS_TREE_ONLY),
@@ -352,8 +380,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 			LS_RECURSIVE),
 		OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"),
 			LS_SHOW_TREES),
-		OPT_SET_INT('z', NULL, &options.line_termination,
-			    N_("terminate entries with NUL byte"), 0),
+		OPT_BOOL('z', NULL, &null_termination,
+			 N_("terminate entries with NUL byte")),
 		OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
 			    MODE_LONG),
 		OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"),
@@ -383,6 +411,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
 
 	argc = parse_options(argc, argv, prefix, ls_tree_options,
 			     ls_tree_usage, 0);
+	options.null_termination = null_termination;
+
 	if (full_tree) {
 		options.ls_tree_prefix = prefix = NULL;
 		options.chomp_prefix = 0;
-- 
2.38.0.1473.g172bcc0511c
Previous: Ævar Arnfjörð BjarmasonNext: Teng Long
Message 33 of 34 in “ls-tree: introduce '--pattern' option”
  1. 0/6 ls-tree: introduce '--pattern' optionTeng Long, Nov 17, 2022
  2. 1/6 ls-tree: cleanup the redundant SPACETeng Long, Nov 17, 2022
  3. 2/6 t3104: remove shift code in 'test_ls_tree_format'Teng Long, Nov 17, 2022
  4. 3/6 ls-tree: optimize params of 'show_tree_common_default_long()'Teng Long, Nov 17, 2022
  5. 4/6 ls-tree: improving cohension in the print codeTeng Long, Nov 17, 2022
  6. Ævar Arnfjörð BjarmasonNov 17, 2022
  7. 5/6 ls-tree: introduce 'match_pattern()' functionTeng Long, Nov 17, 2022
  8. Ævar Arnfjörð BjarmasonNov 17, 2022
  9. Ævar Arnfjörð BjarmasonNov 30, 2022
  10. 6/6 ls-tree: introduce '--pattern' optionTeng Long, Nov 17, 2022
  11. Ævar Arnfjörð BjarmasonNov 17, 2022
  12. Johannes SchindelinDec 12, 2022
  13. Junio C HamanoDec 12, 2022
  14. Junio C HamanoDec 14, 2022
  15. Ævar Arnfjörð BjarmasonDec 14, 2022
  16. Junio C HamanoDec 14, 2022
  17. win-test: unknown terminal "xterm-256color", was Re: [RFC PATCH 6/6] ls-tree: introduce '--pattern' optionJohannes Schindelin, Mar 27, 2023
  18. Junio C HamanoMar 27, 2023
  19. Jeff KingMar 28, 2023
  20. Junio C HamanoMar 28, 2023
  21. Jeff KingMar 28, 2023
  22. Jeff KingMar 28, 2023
  23. Junio C HamanoMar 28, 2023
  24. Ævar Arnfjörð BjarmasonNov 17, 2022
  25. Taylor BlauNov 17, 2022
  26. Teng LongNov 21, 2022
  27. Ævar Arnfjörð BjarmasonNov 21, 2022
  28. 0/4 ls-tree: pass state in struct, not globalsÆvar Arnfjörð Bjarmason, Nov 17, 2022
  29. 1/4 ls-tree: don't use "show_tree_data" for "fast" callbacksÆvar Arnfjörð Bjarmason, Nov 17, 2022
  30. 1/4 ls-tree: don't use "show_tree_data" for "fast" callbacksTeng Long, Dec 21, 2022
  31. 3/4 ls-tree: fold "show_tree_data" into "cb" structÆvar Arnfjörð Bjarmason, Nov 17, 2022
  32. 2/4 ls-tree: use a "struct options"Ævar Arnfjörð Bjarmason, Nov 17, 2022
  33. 4/4 ls-tree: make "line_termination" less genericÆvar Arnfjörð Bjarmason, Nov 17, 2022
  34. 0/4 ls-tree: pass state in struct, not globalsTeng Long, Nov 21, 2022

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.