Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeng Long <dyroneteng@gmail.com>2022-03-23 12:13:09 +0300
committerJunio C Hamano <gitster@pobox.com>2022-03-23 21:38:39 +0300
commit315f22c853c08b1f3473fc50985d76618f31ebd0 (patch)
tree8cc67a434fbfc0ac54e46d356a5606c930583c56 /builtin/ls-tree.c
parentf6b224d5ebfe9fcf58ef34eddea68d9fb4384d50 (diff)
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>
Diffstat (limited to 'builtin/ls-tree.c')
-rw-r--r--builtin/ls-tree.c98
1 files changed, 61 insertions, 37 deletions
diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c
index 7be4c13228..173a534e92 100644
--- a/builtin/ls-tree.c
+++ b/builtin/ls-tree.c
@@ -16,10 +16,8 @@
static int line_termination = '\n';
#define LS_RECURSIVE 1
-#define LS_TREE_ONLY 2
-#define LS_SHOW_TREES 4
-#define LS_NAME_ONLY 8
-#define LS_SHOW_SIZE 16
+#define LS_TREE_ONLY (1 << 1)
+#define LS_SHOW_TREES (1 << 2)
static int abbrev;
static int ls_options;
static struct pathspec pathspec;
@@ -31,6 +29,11 @@ static const char * const ls_tree_usage[] = {
NULL
};
+static enum ls_tree_cmdmode {
+ MODE_LONG = 1,
+ MODE_NAME_ONLY,
+} cmdmode;
+
static int show_recursive(const char *base, size_t baselen, const char *pathname)
{
int i;
@@ -61,6 +64,39 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname
return 0;
}
+static int show_default(const struct object_id *oid, enum object_type type,
+ const char *pathname, unsigned mode,
+ struct strbuf *base)
+{
+ size_t baselen = base->len;
+
+ if (cmdmode == MODE_LONG) {
+ char size_text[24];
+ if (type == OBJ_BLOB) {
+ unsigned long size;
+ if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
+ xsnprintf(size_text, sizeof(size_text), "BAD");
+ else
+ xsnprintf(size_text, sizeof(size_text),
+ "%" PRIuMAX, (uintmax_t)size);
+ } else {
+ xsnprintf(size_text, sizeof(size_text), "-");
+ }
+ printf("%06o %s %s %7s\t", mode, type_name(type),
+ find_unique_abbrev(oid, abbrev), size_text);
+ } else {
+ printf("%06o %s %s\t", mode, type_name(type),
+ find_unique_abbrev(oid, abbrev));
+ }
+ baselen = base->len;
+ strbuf_addstr(base, pathname);
+ write_name_quoted_relative(base->buf,
+ chomp_prefix ? ls_tree_prefix : NULL, stdout,
+ line_termination);
+ strbuf_setlen(base, baselen);
+ return 1;
+}
+
static int show_tree(const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode, void *context)
{
@@ -78,34 +114,22 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,
return recurse;
}
- if (!(ls_options & LS_NAME_ONLY)) {
- if (ls_options & LS_SHOW_SIZE) {
- char size_text[24];
- if (type == OBJ_BLOB) {
- unsigned long size;
- if (oid_object_info(the_repository, oid, &size) == OBJ_BAD)
- xsnprintf(size_text, sizeof(size_text),
- "BAD");
- else
- xsnprintf(size_text, sizeof(size_text),
- "%"PRIuMAX, (uintmax_t)size);
- } else {
- xsnprintf(size_text, sizeof(size_text), "-");
- }
- printf("%06o %s %s %7s\t", mode, type_name(type),
- find_unique_abbrev(oid, abbrev),
- size_text);
- } else {
- printf("%06o %s %s\t", mode, type_name(type),
- find_unique_abbrev(oid, abbrev));
- }
+ if (cmdmode == MODE_NAME_ONLY) {
+ baselen = base->len;
+ strbuf_addstr(base, pathname);
+ write_name_quoted_relative(base->buf,
+ chomp_prefix ? ls_tree_prefix : NULL,
+ stdout, line_termination);
+ strbuf_setlen(base, baselen);
+ return recurse;
}
- baselen = base->len;
- strbuf_addstr(base, pathname);
- write_name_quoted_relative(base->buf,
- chomp_prefix ? ls_tree_prefix : NULL,
- stdout, line_termination);
- strbuf_setlen(base, baselen);
+
+ if (cmdmode == MODE_LONG ||
+ (!ls_options || (ls_options & LS_RECURSIVE)
+ || (ls_options & LS_SHOW_TREES)
+ || (ls_options & LS_TREE_ONLY)))
+ show_default(oid, type, pathname, mode, base);
+
return recurse;
}
@@ -123,12 +147,12 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
LS_SHOW_TREES),
OPT_SET_INT('z', NULL, &line_termination,
N_("terminate entries with NUL byte"), 0),
- OPT_CMDMODE('l', "long", &ls_options, N_("include object size"),
- LS_SHOW_SIZE),
- OPT_CMDMODE(0, "name-only", &ls_options, N_("list only filenames"),
- LS_NAME_ONLY),
- OPT_CMDMODE(0, "name-status", &ls_options, N_("list only filenames"),
- LS_NAME_ONLY),
+ OPT_CMDMODE('l', "long", &cmdmode, N_("include object size"),
+ MODE_LONG),
+ OPT_CMDMODE(0, "name-only", &cmdmode, N_("list only filenames"),
+ MODE_NAME_ONLY),
+ OPT_CMDMODE(0, "name-status", &cmdmode, N_("list only filenames"),
+ MODE_NAME_ONLY),
OPT_SET_INT(0, "full-name", &chomp_prefix,
N_("use full path names"), 0),
OPT_BOOL(0, "full-tree", &full_tree,