"show_tree_data" is a struct that packages the necessary fields for "show_tree()". This commit is a pre-prepared commit for supporting "--format" option and it does not affect any existing functionality. Signed-off-by: Teng Long <dyroneteng@xxxxxxxxx> --- builtin/ls-tree.c | 50 +++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 6b5e3ab9dd..12beb02423 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -35,6 +35,14 @@ static unsigned int shown_fields; #define FIELD_DEFAULT 29 /* 11101 size is not shown to output by default */ #define FIELD_LONG_DEFAULT (FIELD_DEFAULT | FIELD_SIZE) +struct show_tree_data { + unsigned mode; + enum object_type type; + const struct object_id *oid; + const char *pathname; + struct strbuf *base; +}; + static const char * const ls_tree_usage[] = { N_("git ls-tree [<options>] <tree-ish> [<path>...]"), NULL @@ -99,17 +107,15 @@ static int show_recursive(const char *base, size_t baselen, return 0; } -static int show_default(const struct object_id *oid, enum object_type type, - const char *pathname, unsigned mode, - struct strbuf *base) +static int show_default(struct show_tree_data *data) { - size_t baselen = base->len; + size_t baselen = data->base->len; if (shown_fields & FIELD_SIZE) { char size_text[24]; - if (type == OBJ_BLOB) { + if (data->type == OBJ_BLOB) { unsigned long size; - if (oid_object_info(the_repository, oid, &size) == OBJ_BAD) + if (oid_object_info(the_repository, data->oid, &size) == OBJ_BAD) xsnprintf(size_text, sizeof(size_text), "BAD"); else xsnprintf(size_text, sizeof(size_text), @@ -117,18 +123,18 @@ static int show_default(const struct object_id *oid, enum object_type type, } else { xsnprintf(size_text, sizeof(size_text), "-"); } - printf("%06o %s %s %7s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev), size_text); + printf("%06o %s %s %7s\t", data->mode, type_name(data->type), + find_unique_abbrev(data->oid, abbrev), size_text); } else { - printf("%06o %s %s\t", mode, type_name(type), - find_unique_abbrev(oid, abbrev)); + printf("%06o %s %s\t", data->mode, type_name(data->type), + find_unique_abbrev(data->oid, abbrev)); } - baselen = base->len; - strbuf_addstr(base, pathname); - write_name_quoted_relative(base->buf, + 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(base, baselen); + strbuf_setlen(data->base, baselen); return 1; } @@ -152,14 +158,20 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, { int recursive = 0; size_t baselen; - enum object_type type = OBJ_BLOB; + struct show_tree_data data = { + .mode = mode, + .type = OBJ_BLOB, + .oid = oid, + .pathname = pathname, + .base = base, + }; - init_type(mode, &type); + init_type(mode, &data.type); init_recursive(base, pathname, &recursive); - if (type == OBJ_TREE && recursive && !(ls_options & LS_SHOW_TREES)) + if (data.type == OBJ_TREE && recursive && !(ls_options & LS_SHOW_TREES)) return recursive; - if (type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) + if (data.type == OBJ_BLOB && (ls_options & LS_TREE_ONLY)) return !READ_TREE_RECURSIVE; if (shown_fields == FIELD_OBJECT_NAME) { @@ -178,7 +190,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base, } if (shown_fields >= FIELD_DEFAULT) - show_default(oid, type, pathname, mode, base); + show_default(&data); return recursive; } -- 2.33.0.rc1.1794.g2ae0a9cb82