[PATCH v8 5/8] ls-tree: split up the "init" part of show_tree()
- From
Teng Long <dyroneteng@gmail.com>
- Date
- Jan 1, 2022, 13:50 UTC
- Message-ID
- <d77c895a4b9d5cb86443279c0156196254895ef4.1641043500.git.dyroneteng@gmail.com>
- In-Reply-To
- <cover.1641043500.git.dyroneteng@gmail.com>
From: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Split up the "init" part of the show_tree() function where we decide what the "type" is, and whether we'll return early. This makes things a bit less readable for now, but we'll soon re-use this in a sibling function, and avoiding the duplication will be worth it.
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> --- builtin/ls-tree.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index eecc7482d5..df8312408d 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -61,25 +61,33 @@ static int show_recursive(const char *base, size_t baselen, const char *pathname return 0; } -static int show_tree(const struct object_id *oid, struct strbuf *base, - const char *pathname, unsigned mode, void *context) +static int show_tree_init(enum object_type *type, struct strbuf *base, + const char *pathname, unsigned mode, int *retval) { - int retval = 0; - size_t baselen; - enum object_type type = OBJ_BLOB; - if (S_ISGITLINK(mode)) { - type = OBJ_COMMIT; + *type = OBJ_COMMIT; } else if (S_ISDIR(mode)) { if (show_recursive(base->buf, base->len, pathname)) { - retval = READ_TREE_RECURSIVE; + *retval = READ_TREE_RECURSIVE; if (!(ls_options & LS_SHOW_TREES)) - return retval; + return 1; } - type = OBJ_TREE; + *type = OBJ_TREE; } else if (ls_options & LS_TREE_ONLY) - return 0; + return 1; + return 0; +} + +static int show_tree(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, void *context) +{ + int retval = 0; + size_t baselen; + enum object_type type = OBJ_BLOB; + + if (show_tree_init(&type, base, pathname, mode, &retval)) + return retval; if (!(ls_options & LS_NAME_ONLY)) { if (ls_options & LS_SHOW_SIZE) {
-- 2.33.0.rc1.1802.gbb1c3936fb.dirty