The generic "show_tree_fmt()" is slower than "show_tree()", so we want to take the fast path if possible. when "--format=<format>" is passed, "fast_path()" will determine whether to use "show_tree()" or insist on using "show_tree_fmt()" by a try of finding out if the built-int format is hit. This commit take out the related codes from "cmd_ls_tree()" and package them into a new funtion "fast_path()". Explain it a little bit further, whether fast_path is hit or not, the final correctness should not break. Abstracting a separate method helps improve the readability of "cmd_ls_tree()" and the cohesiveness and extensibility of fast path logic. Signed-off-by: Teng Long <dyroneteng@xxxxxxxxx> --- builtin/ls-tree.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/builtin/ls-tree.c b/builtin/ls-tree.c index 1c71e5d543..ba96bcf602 100644 --- a/builtin/ls-tree.c +++ b/builtin/ls-tree.c @@ -58,6 +58,19 @@ enum { static int cmdmode = MODE_UNSPECIFIED; +static int fast_path(void){ + if (!strcmp(format, default_format)) { + return 1; + } else if (!strcmp(format, long_format)) { + shown_fields = shown_fields | FIELD_SIZE; + return 1; + } else if (!strcmp(format, name_only_format)) { + shown_fields = FIELD_PATH_NAME; + return 1; + } + return 0; +} + static void expand_objectsize(struct strbuf *line, const struct object_id *oid, const enum object_type type, unsigned int padded) { @@ -350,15 +363,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix) * The generic show_tree_fmt() is slower than show_tree(), so * take the fast path if possible. */ - if (format && (!strcmp(format, default_format))) { - fn = show_tree; - } else if (format && (!strcmp(format, long_format))) { - shown_fields = shown_fields | FIELD_SIZE; - fn = show_tree; - } else if (format && (!strcmp(format, name_only_format))) { - shown_fields = FIELD_PATH_NAME; - fn = show_tree; - } else if (format) + if (format && !fast_path()) fn = show_tree_fmt; return !!read_tree(the_repository, tree, &pathspec, fn, NULL); -- 2.34.1.403.gb35f2687cf.dirty