Skip to content

Commit 315f22c

Browse files
dyronegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Teng Long <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f6b224d commit 315f22c

File tree

1 file changed

+61
-37
lines changed

1 file changed

+61
-37
lines changed

builtin/ls-tree.c

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
static int line_termination = '\n';
1818
#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)
2321
static int abbrev;
2422
static int ls_options;
2523
static struct pathspec pathspec;
@@ -31,6 +29,11 @@ static const char * const ls_tree_usage[] = {
3129
NULL
3230
};
3331

32+
static enum ls_tree_cmdmode {
33+
MODE_LONG = 1,
34+
MODE_NAME_ONLY,
35+
} cmdmode;
36+
3437
static int show_recursive(const char *base, size_t baselen, const char *pathname)
3538
{
3639
int i;
@@ -61,6 +64,39 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname
6164
return 0;
6265
}
6366

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+
64100
static int show_tree(const struct object_id *oid, struct strbuf *base,
65101
const char *pathname, unsigned mode, void *context)
66102
{
@@ -78,34 +114,22 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,
78114
return recurse;
79115
}
80116

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;
102125
}
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+
109133
return recurse;
110134
}
111135

@@ -123,12 +147,12 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
123147
LS_SHOW_TREES),
124148
OPT_SET_INT('z', NULL, &line_termination,
125149
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),
132156
OPT_SET_INT(0, "full-name", &chomp_prefix,
133157
N_("use full path names"), 0),
134158
OPT_BOOL(0, "full-tree", &full_tree,

0 commit comments

Comments
 (0)