Skip to content

Commit e6c75d8

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Teng Long <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 65d1f6c commit e6c75d8

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

builtin/ls-tree.c

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
3939
}
4040

4141
struct ls_tree_options {
42-
int line_termination;
42+
unsigned null_termination:1;
4343
int abbrev;
4444
enum ls_tree_path_options {
4545
LS_RECURSIVE = 1 << 0,
@@ -166,7 +166,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
166166

167167
baselen = base->len;
168168
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');
170170
fwrite(sb.buf, sb.len, 1, stdout);
171171
strbuf_release(&sb);
172172
strbuf_setlen(base, baselen);
@@ -198,10 +198,22 @@ static void show_tree_common_default_long(struct ls_tree_options *options,
198198
const char *pathname,
199199
const size_t baselen)
200200
{
201+
const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
202+
201203
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+
205217
strbuf_setlen(base, baselen);
206218
}
207219

@@ -264,15 +276,25 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
264276
int recurse;
265277
const size_t baselen = base->len;
266278
enum object_type type = object_type(mode);
279+
const char *prefix;
267280

268281
early = show_tree_common(options, &recurse, base, pathname, type);
269282
if (early >= 0)
270283
return early;
271284

285+
prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
272286
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+
}
276298
strbuf_setlen(base, baselen);
277299
return recurse;
278300
}
@@ -285,12 +307,19 @@ static int show_tree_object(const struct object_id *oid, struct strbuf *base,
285307
int early;
286308
int recurse;
287309
enum object_type type = object_type(mode);
310+
const char *str;
288311

289312
early = show_tree_common(options, &recurse, base, pathname, type);
290313
if (early >= 0)
291314
return early;
292315

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+
}
294323
return recurse;
295324
}
296325

@@ -342,18 +371,17 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
342371
int i, full_tree = 0;
343372
read_tree_fn_t fn = NULL;
344373
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 };
348376
const struct option ls_tree_options[] = {
349377
OPT_BIT('d', NULL, &options.ls_options, N_("only show trees"),
350378
LS_TREE_ONLY),
351379
OPT_BIT('r', NULL, &options.ls_options, N_("recurse into subtrees"),
352380
LS_RECURSIVE),
353381
OPT_BIT('t', NULL, &options.ls_options, N_("show trees when recursing"),
354382
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")),
357385
OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
358386
MODE_LONG),
359387
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)
383411

384412
argc = parse_options(argc, argv, prefix, ls_tree_options,
385413
ls_tree_usage, 0);
414+
options.null_termination = null_termination;
415+
386416
if (full_tree) {
387417
options.ls_tree_prefix = prefix = NULL;
388418
options.chomp_prefix = 0;

0 commit comments

Comments
 (0)