ls-tree: introduce struct "show_tree_data"

"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: Æ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 Mar 23, 2022 at 17:13 UTC e81517155e0370ae5433d256046ab287bb10d04d
1 file changed +27 -15
builtin/ls-tree.c
+27 -15
@@ -23,6 +23,13 @@ static int ls_options;
23 static struct pathspec pathspec;
24 static int chomp_prefix;
25 static const char *ls_tree_prefix;
26 +struct show_tree_data {
27 + unsigned mode;
28 + enum object_type type;
29 + const struct object_id *oid;
30 + const char *pathname;
31 + struct strbuf *base;
32 +};
33
34 static const char * const ls_tree_usage[] = {
35 N_("git ls-tree [<options>] <tree-ish> [<path>...]"),
@@ -64,17 +71,15 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname
71 return 0;
72 }
73
67 -static int show_default(const struct object_id *oid, enum object_type type,
68 - const char *pathname, unsigned mode,
69 - struct strbuf *base)
74 +static int show_default(struct show_tree_data *data)
75 {
71 - size_t baselen = base->len;
76 + size_t baselen = data->base->len;
77
78 if (cmdmode == MODE_LONG) {
79 char size_text[24];
75 - if (type == OBJ_BLOB) {
80 + if (data->type == OBJ_BLOB) {
81 unsigned long size;
77 - if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
82 + if (oid_object_info(the_repository, data->oid, &size) == OBJ_BAD)
83 xsnprintf(size_text, sizeof(size_text), "BAD");
84 else
85 xsnprintf(size_text, sizeof(size_text),
@@ -82,18 +87,18 @@ static int show_default(const struct object_id *oid, enum object_type type,
87 } else {
88 xsnprintf(size_text, sizeof(size_text), "-");
89 }
85 - printf("%06o %s %s %7s\t", mode, type_name(type),
86 - find_unique_abbrev(oid, abbrev), size_text);
90 + printf("%06o %s %s %7s\t", data->mode, type_name(data->type),
91 + find_unique_abbrev(data->oid, abbrev), size_text);
92 } else {
88 - printf("%06o %s %s\t", mode, type_name(type),
89 - find_unique_abbrev(oid, abbrev));
93 + printf("%06o %s %s\t", data->mode, type_name(data->type),
94 + find_unique_abbrev(data->oid, abbrev));
95 }
91 - baselen = base->len;
92 - strbuf_addstr(base, pathname);
93 - write_name_quoted_relative(base->buf,
96 + baselen = data->base->len;
97 + strbuf_addstr(data->base, data->pathname);
98 + write_name_quoted_relative(data->base->buf,
99 chomp_prefix ? ls_tree_prefix : NULL, stdout,
100 line_termination);
96 - strbuf_setlen(base, baselen);
101 + strbuf_setlen(data->base, baselen);
102 return 1;
103 }
104
@@ -103,6 +108,13 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,
108 int recurse = 0;
109 size_t baselen;
110 enum object_type type = object_type(mode);
111 + struct show_tree_data data = {
112 + .mode = mode,
113 + .type = type,
114 + .oid = oid,
115 + .pathname = pathname,
116 + .base = base,
117 + };
118
119 if (type == OBJ_BLOB) {
120 if (ls_options & LS_TREE_ONLY)
@@ -128,7 +140,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,
140 (!ls_options || (ls_options & LS_RECURSIVE)
141 || (ls_options & LS_SHOW_TREES)
142 || (ls_options & LS_TREE_ONLY)))
131 - show_default(oid, type, pathname, mode, base);
143 + show_default(&data);
144
145 return recurse;
146 }