Skip to content

Commit 7b7203e

Browse files
rscharfegitster
authored andcommitted
ls-tree: simplify prefix handling
git ls-tree has two prefixes: The one handed to cmd_ls_tree(), i.e. the current subdirectory in the repository (if any) and the "display" prefix used by the show_tree_*() functions. The option --full-name clears the last one, i.e. it shows full paths, and --full-tree clears both, i.e. it acts as if the command was started in the root of the repository. The show_tree_*() functions use the ls_tree_options members chomp_prefix and ls_tree_prefix to determine their prefix values. Calculate it once in cmd_ls_tree() instead, once the main prefix value is finalized. This allows chomp_prefix to become a local variable. Stop using strlen(3) to determine its initial value -- we only care whether we got a non-empty string, not precisely how long it is. Rename ls_tree_prefix to prefix to demonstrate that we converted all users and because the ls_tree_ part is no longer necessary since 030a3d5 (ls-tree: use a "struct options", 2023-01-12) turned it from a global variable to a struct member. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 061c586 commit 7b7203e

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

builtin/ls-tree.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ struct ls_tree_options {
5050
LS_SHOW_TREES = 1 << 2,
5151
} ls_options;
5252
struct pathspec pathspec;
53-
int chomp_prefix;
54-
const char *ls_tree_prefix;
53+
const char *prefix;
5554
const char *format;
5655
};
5756

@@ -128,8 +127,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
128127
strbuf_add_unique_abbrev(&sb, oid, options->abbrev);
129128
else if (skip_prefix(format, "(path)", &format)) {
130129
const char *name;
131-
const char *prefix = options->chomp_prefix ?
132-
options->ls_tree_prefix : NULL;
130+
const char *prefix = options->prefix;
133131
struct strbuf sbuf = STRBUF_INIT;
134132
size_t baselen = base->len;
135133

@@ -173,7 +171,7 @@ static void show_tree_common_default_long(struct ls_tree_options *options,
173171
const char *pathname,
174172
const size_t baselen)
175173
{
176-
const char *prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
174+
const char *prefix = options->prefix;
177175

178176
strbuf_addstr(base, pathname);
179177

@@ -258,7 +256,7 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
258256
if (early >= 0)
259257
return early;
260258

261-
prefix = options->chomp_prefix ? options->ls_tree_prefix : NULL;
259+
prefix = options->prefix;
262260
strbuf_addstr(base, pathname);
263261
if (options->null_termination) {
264262
struct strbuf sb = STRBUF_INIT;
@@ -345,6 +343,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
345343
struct object_id oid;
346344
struct tree *tree;
347345
int i, full_tree = 0;
346+
int chomp_prefix = prefix && *prefix;
348347
read_tree_fn_t fn = NULL;
349348
enum ls_tree_cmdmode cmdmode = MODE_DEFAULT;
350349
int null_termination = 0;
@@ -366,7 +365,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
366365
MODE_NAME_STATUS),
367366
OPT_CMDMODE(0, "object-only", &cmdmode, N_("list only objects"),
368367
MODE_OBJECT_ONLY),
369-
OPT_SET_INT(0, "full-name", &options.chomp_prefix,
368+
OPT_SET_INT(0, "full-name", &chomp_prefix,
370369
N_("use full path names"), 0),
371370
OPT_BOOL(0, "full-tree", &full_tree,
372371
N_("list entire tree; not just current directory "
@@ -381,18 +380,15 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
381380
int ret;
382381

383382
git_config(git_default_config, NULL);
384-
options.ls_tree_prefix = prefix;
385-
if (prefix)
386-
options.chomp_prefix = strlen(prefix);
387383

388384
argc = parse_options(argc, argv, prefix, ls_tree_options,
389385
ls_tree_usage, 0);
390386
options.null_termination = null_termination;
391387

392-
if (full_tree) {
393-
options.ls_tree_prefix = prefix = NULL;
394-
options.chomp_prefix = 0;
395-
}
388+
if (full_tree)
389+
prefix = NULL;
390+
options.prefix = chomp_prefix ? prefix : NULL;
391+
396392
/*
397393
* We wanted to detect conflicts between --name-only and
398394
* --name-status, but once we're done with that subsequent

0 commit comments

Comments
 (0)