ls-tree: make "line_termination" less generic

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> Signed-off-by: Teng Long <dyroneteng@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jan 12, 2023 at 17:11 UTC e6c75d8dd7625e47e767b6f3c38b83827639e2f7
1 file changed +44 -14
builtin/ls-tree.c
+44 -14
@@ -39,7 +39,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
39 }
40
41 struct ls_tree_options {
42 - int line_termination;
42 + unsigned null_termination:1;
43 int abbrev;
44 enum ls_tree_path_options {
45 LS_RECURSIVE = 1 << 0,
@@ -166,7 +166,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
166
167 baselen = base->len;
168 strbuf_expand(&sb, options->format, expand_show_tree, &cb_data);
169 - strbuf_addch(&sb, options->line_termination);
169 + strbuf_addch(&sb, options->null_termination ? '\0' : '\n');
170 fwrite(sb.buf, sb.len, 1, stdout);
171 strbuf_release(&sb);
172 strbuf_setlen(base, baselen);
@@ -198,10 +198,22 @@ static void show_tree_common_default_long(struct ls_tree_options *options,
198 const char *pathname,
199 const size_t baselen)
200 {
201 + const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
202 +
203 strbuf_addstr(base, pathname);
202 - write_name_quoted_relative(base->buf,
203 - options->chomp_prefix ? options->ls_tree_prefix : NULL, stdout,
204 - options->line_termination);
204 +
205 + if (options->null_termination) {
206 + struct strbuf sb = STRBUF_INIT;
207 + const char *name = relative_path(base->buf, prefix, &sb);
208 +
209 + fputs(name, stdout);
210 + fputc('\0', stdout);
211 +
212 + strbuf_release(&sb);
213 + } else {
214 + write_name_quoted_relative(base->buf, prefix, stdout, '\n');
215 + }
216 +
217 strbuf_setlen(base, baselen);
218 }
219
@@ -264,15 +276,25 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
276 int recurse;
277 const size_t baselen = base->len;
278 enum object_type type = object_type(mode);
279 + const char *prefix;
280
281 early = show_tree_common(options, &recurse, base, pathname, type);
282 if (early >= 0)
283 return early;
284
285 + prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
286 strbuf_addstr(base, pathname);
273 - write_name_quoted_relative(base->buf,
274 - options->chomp_prefix ? options->ls_tree_prefix : NULL,
275 - stdout, options->line_termination);
287 + if (options->null_termination) {
288 + struct strbuf sb = STRBUF_INIT;
289 + const char *name = relative_path(base->buf, prefix, &sb);
290 +
291 + fputs(name, stdout);
292 + fputc('\0', stdout);
293 +
294 + strbuf_release(&sb);
295 + } else {
296 + write_name_quoted_relative(base->buf, prefix, stdout, '\n');
297 + }
298 strbuf_setlen(base, baselen);
299 return recurse;
300 }
@@ -285,12 +307,19 @@ static int show_tree_object(const struct object_id *oid, struct strbuf *base,
307 int early;
308 int recurse;
309 enum object_type type = object_type(mode);
310 + const char *str;
311
312 early = show_tree_common(options, &recurse, base, pathname, type);
313 if (early >= 0)
314 return early;
315
293 - printf("%s%c", find_unique_abbrev(oid, options->abbrev), options->line_termination);
316 + str = find_unique_abbrev(oid, options->abbrev);
317 + if (options->null_termination) {
318 + fputs(str, stdout);
319 + fputc('\0', stdout);
320 + } else {
321 + puts(str);
322 + }
323 return recurse;
324 }
325
@@ -342,9 +371,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
371 int i, full_tree = 0;
372 read_tree_fn_t fn = NULL;
373 enum ls_tree_cmdmode cmdmode = MODE_DEFAULT;
345 - struct ls_tree_options options = {
346 - .line_termination = '\n',
347 - };
374 + int null_termination = 0;
375 + struct ls_tree_options options = { 0 };
376 const struct option ls_tree_options[] = {
377 OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"),
378 LS_TREE_ONLY),
@@ -352,8 +380,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
380 LS_RECURSIVE),
381 OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"),
382 LS_SHOW_TREES),
355 - OPT_SET_INT('z', NULL, &options.line_termination,
356 - N_("terminate entries with NUL byte"), 0),
383 + OPT_BOOL('z', NULL, &null_termination,
384 + N_("terminate entries with NUL byte")),
385 OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
386 MODE_LONG),
387 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)
411
412 argc = parse_options(argc, argv, prefix, ls_tree_options,
413 ls_tree_usage, 0);
414 + options.null_termination = null_termination;
415 +
416 if (full_tree) {
417 options.ls_tree_prefix = prefix = NULL;
418 options.chomp_prefix = 0;