ls-tree: split up "fast path" callbacks

Make the various if/else in the callbacks for the "fast path" a lot
easier to read by just using common functions for the parts that are
common, and have per-format callbacks for those parts that are
different.

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>
maint
Ævar Arnfjörð Bjarmason 2022-03-23 17:13:15 +08:00 committed by Junio C Hamano
parent 0f88783592
commit 9c4d58ff2c
1 changed files with 125 additions and 74 deletions

View File

@ -173,15 +173,77 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
return recurse; return recurse;
} }


static int show_default(struct show_tree_data *data) static int show_tree_common(struct show_tree_data *data, int *recurse,
const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode)
{ {
size_t baselen = data->base->len; enum object_type type = object_type(mode);
int ret = -1;


if (cmdmode == MODE_LONG) { *recurse = 0;
data->mode = mode;
data->type = type;
data->oid = oid;
data->pathname = pathname;
data->base = base;

if (type == OBJ_BLOB) {
if (ls_options & LS_TREE_ONLY)
ret = 0;
} else if (type == OBJ_TREE &&
show_recursive(base->buf, base->len, pathname)) {
*recurse = READ_TREE_RECURSIVE;
if (!(ls_options & LS_SHOW_TREES))
ret = *recurse;
}

return ret;
}

static void show_tree_common_default_long(struct strbuf *base,
const char *pathname,
const size_t baselen)
{
strbuf_addstr(base, pathname);
write_name_quoted_relative(base->buf,
chomp_prefix ? ls_tree_prefix : NULL, stdout,
line_termination);
strbuf_setlen(base, baselen);
}

static int show_tree_default(const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode,
void *context)
{
int early;
int recurse;
struct show_tree_data data = { 0 };

early = show_tree_common(&data, &recurse, oid, base, pathname, mode);
if (early >= 0)
return early;

printf("%06o %s %s\t", data.mode, type_name(data.type),
find_unique_abbrev(data.oid, abbrev));
show_tree_common_default_long(base, pathname, data.base->len);
return recurse;
}

static int show_tree_long(const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode, void *context)
{
int early;
int recurse;
struct show_tree_data data = { 0 };
char size_text[24]; char size_text[24];
if (data->type == OBJ_BLOB) {
early = show_tree_common(&data, &recurse, oid, base, pathname, mode);
if (early >= 0)
return early;

if (data.type == OBJ_BLOB) {
unsigned long size; unsigned long size;
if (oid_object_info(the_repository, data->oid, &size) == OBJ_BAD) if (oid_object_info(the_repository, data.oid, &size) == OBJ_BAD)
xsnprintf(size_text, sizeof(size_text), "BAD"); xsnprintf(size_text, sizeof(size_text), "BAD");
else else
xsnprintf(size_text, sizeof(size_text), xsnprintf(size_text, sizeof(size_text),
@ -189,92 +251,79 @@ static int show_default(struct show_tree_data *data)
} else { } else {
xsnprintf(size_text, sizeof(size_text), "-"); xsnprintf(size_text, sizeof(size_text), "-");
} }
printf("%06o %s %s %7s\t", data->mode, type_name(data->type),
find_unique_abbrev(data->oid, abbrev), size_text); printf("%06o %s %s %7s\t", data.mode, type_name(data.type),
} else { find_unique_abbrev(data.oid, abbrev), size_text);
printf("%06o %s %s\t", data->mode, type_name(data->type), show_tree_common_default_long(base, pathname, data.base->len);
find_unique_abbrev(data->oid, abbrev));
}
baselen = data->base->len;
strbuf_addstr(data->base, data->pathname);
write_name_quoted_relative(data->base->buf,
chomp_prefix ? ls_tree_prefix : NULL, stdout,
line_termination);
strbuf_setlen(data->base, baselen);
return 1; return 1;
} }


static int show_tree(const struct object_id *oid, struct strbuf *base, static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode, void *context) const char *pathname, unsigned mode, void *context)
{ {
int recurse = 0; int early;
size_t baselen; int recurse;
enum object_type type = object_type(mode); const size_t baselen = base->len;
struct show_tree_data data = { struct show_tree_data data = { 0 };
.mode = mode,
.type = type,
.oid = oid,
.pathname = pathname,
.base = base,
};


if (type == OBJ_BLOB) { early = show_tree_common(&data, &recurse, oid, base, pathname, mode);
if (ls_options & LS_TREE_ONLY) if (early >= 0)
return 0; return early;
} else if (type == OBJ_TREE &&
show_recursive(base->buf, base->len, pathname)) {
recurse = READ_TREE_RECURSIVE;
if (!(ls_options & LS_SHOW_TREES))
return recurse;
}


if (cmdmode == MODE_OBJECT_ONLY) {
printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination);
return recurse;
}

if (cmdmode == MODE_NAME_ONLY) {
baselen = base->len;
strbuf_addstr(base, pathname); strbuf_addstr(base, pathname);
write_name_quoted_relative(base->buf, write_name_quoted_relative(base->buf,
chomp_prefix ? ls_tree_prefix : NULL, chomp_prefix ? ls_tree_prefix : NULL,
stdout, line_termination); stdout, line_termination);
strbuf_setlen(base, baselen); strbuf_setlen(base, baselen);
return recurse; return recurse;
} }


if (cmdmode == MODE_LONG || static int show_tree_object(const struct object_id *oid, struct strbuf *base,
(!ls_options || (ls_options & LS_RECURSIVE) const char *pathname, unsigned mode, void *context)
|| (ls_options & LS_SHOW_TREES) {
|| (ls_options & LS_TREE_ONLY))) int early;
show_default(&data); int recurse;
struct show_tree_data data = { 0 };


early = show_tree_common(&data, &recurse, oid, base, pathname, mode);
if (early >= 0)
return early;

printf("%s%c", find_unique_abbrev(oid, abbrev), line_termination);
return recurse; return recurse;
} }


struct ls_tree_cmdmode_to_fmt { struct ls_tree_cmdmode_to_fmt {
enum ls_tree_cmdmode mode; enum ls_tree_cmdmode mode;
const char *const fmt; const char *const fmt;
read_tree_fn_t fn;
}; };


static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = { static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = {
{ {
.mode = MODE_DEFAULT, .mode = MODE_DEFAULT,
.fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)", .fmt = "%(objectmode) %(objecttype) %(objectname)%x09%(path)",
.fn = show_tree_default,
}, },
{ {
.mode = MODE_LONG, .mode = MODE_LONG,
.fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)", .fmt = "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)",
.fn = show_tree_long,
}, },
{ {
.mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */ .mode = MODE_NAME_ONLY, /* And MODE_NAME_STATUS */
.fmt = "%(path)", .fmt = "%(path)",
.fn = show_tree_name_only,
}, },
{ {
.mode = MODE_OBJECT_ONLY, .mode = MODE_OBJECT_ONLY,
.fmt = "%(objectname)", .fmt = "%(objectname)",
.fn = show_tree_object
},
{
/* fallback */
.fn = show_tree_default,
}, },
{ 0 },
}; };


int cmd_ls_tree(int argc, const char **argv, const char *prefix) int cmd_ls_tree(int argc, const char **argv, const char *prefix)
@ -282,7 +331,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
struct object_id oid; struct object_id oid;
struct tree *tree; struct tree *tree;
int i, full_tree = 0; int i, full_tree = 0;
read_tree_fn_t fn = show_tree; read_tree_fn_t fn = NULL;
const struct option ls_tree_options[] = { const struct option ls_tree_options[] = {
OPT_BIT('d', NULL, &ls_options, N_("only show trees"), OPT_BIT('d', NULL, &ls_options, N_("only show trees"),
LS_TREE_ONLY), LS_TREE_ONLY),
@ -311,6 +360,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
OPT__ABBREV(&abbrev), OPT__ABBREV(&abbrev),
OPT_END() OPT_END()
}; };
struct ls_tree_cmdmode_to_fmt *m2f = ls_tree_cmdmode_format;


git_config(git_default_config, NULL); git_config(git_default_config, NULL);
ls_tree_prefix = prefix; ls_tree_prefix = prefix;
@ -365,18 +415,19 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
* The generic show_tree_fmt() is slower than show_tree(), so * The generic show_tree_fmt() is slower than show_tree(), so
* take the fast path if possible. * take the fast path if possible.
*/ */
if (format) { while (m2f) {
struct ls_tree_cmdmode_to_fmt *m2f; if (!m2f->fmt) {

fn = format ? show_tree_fmt : show_tree_default;
fn = show_tree_fmt; } else if (format && !strcmp(format, m2f->fmt)) {
for (m2f = ls_tree_cmdmode_format; m2f->fmt; m2f++) {
if (strcmp(format, m2f->fmt))
continue;

cmdmode = m2f->mode; cmdmode = m2f->mode;
fn = show_tree; fn = m2f->fn;
break; } else if (!format && cmdmode == m2f->mode) {
fn = m2f->fn;
} else {
m2f++;
continue;
} }
break;
} }


return !!read_tree(the_repository, tree, &pathspec, fn, NULL); return !!read_tree(the_repository, tree, &pathspec, fn, NULL);