ls-tree: slightly refactor `show_tree()`

This is a non-functional change, we introduce an enum "ls_tree_cmdmode" then use it to mark which columns to output. This has the advantage of making the show_tree logic simpler and more readable, as well as making it easier to extend new options (for example, if we want to add a "--object-only" option, we just need to add a similar "short-circuit logic in "show_tree()"). Helped-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>

Teng Long committed Mar 23, 2022 at 17:13 UTC 315f22c853c08b1f3473fc50985d76618f31ebd0
1 file changed +61 -37
builtin/ls-tree.c
+61 -37
@@ -16,10 +16,8 @@
16
17 static int line_termination = '\n';
18 #define LS_RECURSIVE 1
19 -#define LS_TREE_ONLY 2
20 -#define LS_SHOW_TREES 4
21 -#define LS_NAME_ONLY 8
22 -#define LS_SHOW_SIZE 16
19 +#define LS_TREE_ONLY (1 << 1)
20 +#define LS_SHOW_TREES (1 << 2)
21 static int abbrev;
22 static int ls_options;
23 static struct pathspec pathspec;
@@ -31,6 +29,11 @@ static const char * const ls_tree_usage[] = {
29 NULL
30 };
31
32 +static enum ls_tree_cmdmode {
33 + MODE_LONG = 1,
34 + MODE_NAME_ONLY,
35 +} cmdmode;
36 +
37 static int show_recursive(const char *base, size_t baselen, const char *pathname)
38 {
39 int i;
@@ -61,6 +64,39 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname
64 return 0;
65 }
66
67 +static int show_default(const struct object_id *oid, enum object_type type,
68 + const char *pathname, unsigned mode,
69 + struct strbuf *base)
70 +{
71 + size_t baselen = base->len;
72 +
73 + if (cmdmode == MODE_LONG) {
74 + char size_text[24];
75 + if (type == OBJ_BLOB) {
76 + unsigned long size;
77 + if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
78 + xsnprintf(size_text, sizeof(size_text), "BAD");
79 + else
80 + xsnprintf(size_text, sizeof(size_text),
81 + "%" PRIuMAX, (uintmax_t)size);
82 + } else {
83 + xsnprintf(size_text, sizeof(size_text), "-");
84 + }
85 + printf("%06o %s %s %7s\t", mode, type_name(type),
86 + find_unique_abbrev(oid, abbrev), size_text);
87 + } else {
88 + printf("%06o %s %s\t", mode, type_name(type),
89 + find_unique_abbrev(oid, abbrev));
90 + }
91 + baselen = base->len;
92 + strbuf_addstr(base, pathname);
93 + write_name_quoted_relative(base->buf,
94 + chomp_prefix ? ls_tree_prefix : NULL, stdout,
95 + line_termination);
96 + strbuf_setlen(base, baselen);
97 + return 1;
98 +}
99 +
100 static int show_tree(const struct object_id *oid, struct strbuf *base,
101 const char *pathname, unsigned mode, void *context)
102 {
@@ -78,34 +114,22 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,
114 return recurse;
115 }
116
81 - if (!(ls_options & LS_NAME_ONLY)) {
82 - if (ls_options & LS_SHOW_SIZE) {
83 - char size_text[24];
84 - if (type == OBJ_BLOB) {
85 - unsigned long size;
86 - if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
87 - xsnprintf(size_text, sizeof(size_text),
88 - "BAD");
89 - else
90 - xsnprintf(size_text, sizeof(size_text),
91 - "%"PRIuMAX, (uintmax_t)size);
92 - } else {
93 - xsnprintf(size_text, sizeof(size_text), "-");
94 - }
95 - printf("%06o %s %s %7s\t", mode, type_name(type),
96 - find_unique_abbrev(oid, abbrev),
97 - size_text);
98 - } else {
99 - printf("%06o %s %s\t", mode, type_name(type),
100 - find_unique_abbrev(oid, abbrev));
101 - }
117 + if (cmdmode == MODE_NAME_ONLY) {
118 + baselen = base->len;
119 + strbuf_addstr(base, pathname);
120 + write_name_quoted_relative(base->buf,
121 + chomp_prefix ? ls_tree_prefix : NULL,
122 + stdout, line_termination);
123 + strbuf_setlen(base, baselen);
124 + return recurse;
125 }
103 - baselen = base->len;
104 - strbuf_addstr(base, pathname);
105 - write_name_quoted_relative(base->buf,
106 - chomp_prefix ? ls_tree_prefix : NULL,
107 - stdout, line_termination);
108 - strbuf_setlen(base, baselen);
126 +
127 + if (cmdmode == MODE_LONG ||
128 + (!ls_options || (ls_options & LS_RECURSIVE)
129 + || (ls_options & LS_SHOW_TREES)
130 + || (ls_options & LS_TREE_ONLY)))
131 + show_default(oid, type, pathname, mode, base);
132 +
133 return recurse;
134 }
135
@@ -123,12 +147,12 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
147 LS_SHOW_TREES),
148 OPT_SET_INT('z', NULL, &line_termination,
149 N_("terminate entries with NUL byte"), 0),
126 - OPT_CMDMODE('l', "long", &ls_options, N_("include object size"),
127 - LS_SHOW_SIZE),
128 - OPT_CMDMODE(0, "name-only", &ls_options, N_("list only filenames"),
129 - LS_NAME_ONLY),
130 - OPT_CMDMODE(0, "name-status", &ls_options, N_("list only filenames"),
131 - LS_NAME_ONLY),
150 + OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
151 + MODE_LONG),
152 + OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"),
153 + MODE_NAME_ONLY),
154 + OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"),
155 + MODE_NAME_ONLY),
156 OPT_SET_INT(0, "full-name", &chomp_prefix,
157 N_("use full path names"), 0),
158 OPT_BOOL(0, "full-tree", &full_tree,