From 1ca23059758b5d1af843aa62a4aeb5de5b6bd5c8 Mon Sep 17 00:00:00 2001 From: lookshe Date: Tue, 2 Feb 2016 15:12:42 +0100 Subject: call read_tree_recursive two times with different context to list directories first --- ui-tree.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ui-tree.c b/ui-tree.c index a8172ac..1f99d97 100644 --- a/ui-tree.c +++ b/ui-tree.c @@ -15,6 +15,7 @@ struct walk_tree_context { char *curr_rev; char *match_path; int state; + bool dir_mode; }; struct flatten_walk_tree_context { @@ -173,6 +174,10 @@ static int ls_item(const unsigned char *sha1, struct strbuf *base, enum object_type type; unsigned long size = 0; + if ((!S_ISDIR(mode) && walk_tree_ctx->dir_mode) || (S_ISDIR(mode) && !walk_tree_ctx->dir_mode)) { + return 0; + } + name = xstrdup(pathname); strbuf_addf(&fullpath, "%s%s%s", ctx.qry.path ? ctx.qry.path : "", ctx.qry.path ? "/" : "", name); @@ -286,6 +291,9 @@ static void ls_tree(const unsigned char *sha1, char *path, struct walk_tree_cont } ls_head(); + walk_tree_ctx->dir_mode = true; + read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx); + walk_tree_ctx->dir_mode = false; read_tree_recursive(tree, "", 0, 1, &paths, ls_item, walk_tree_ctx); ls_tail(); } @@ -305,11 +313,15 @@ static int walk_tree(const unsigned char *sha1, struct strbuf *base, if (S_ISDIR(mode)) { walk_tree_ctx->state = 1; - ls_head(); + if (walk_tree_ctx->dir_mode) { + ls_head(); + } return READ_TREE_RECURSIVE; } else { walk_tree_ctx->state = 2; - print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev); + if (walk_tree_ctx->dir_mode) { + print_object(sha1, buffer, pathname, walk_tree_ctx->curr_rev); + } return 0; } } @@ -361,6 +373,10 @@ void cgit_print_tree(const char *rev, char *path) goto cleanup; } + walk_tree_ctx.dir_mode = true; + read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); + walk_tree_ctx.state = 0; + walk_tree_ctx.dir_mode = false; read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); if (walk_tree_ctx.state == 1) ls_tail(); -- cgit v1.2.3