summaryrefslogtreecommitdiffstats
path: root/configs/cgit-dirs-first.patch
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-24 11:26:02 +0000
committertv <tv@krebsco.de>2026-11-24 11:26:02 +0000
commitf1731456865d9dbdbfbf570b37635355fd363598 (patch)
tree3fe42f1b47aff0be572c9dd7d4e5ef4d93d06b35 /configs/cgit-dirs-first.patch
parent86de5c32e4fa42cc21749aa1a3cbe0091c6381d4 (diff)
cgit: list directories first
Diffstat (limited to 'configs/cgit-dirs-first.patch')
-rw-r--r--configs/cgit-dirs-first.patch89
1 files changed, 89 insertions, 0 deletions
diff --git a/configs/cgit-dirs-first.patch b/configs/cgit-dirs-first.patch
new file mode 100644
index 00000000..05dc2fb2
--- /dev/null
+++ b/configs/cgit-dirs-first.patch
@@ -0,0 +1,89 @@
+cgit: list directories and submodules first, then everything else
+
+The tree view lists entries in the tree's order. This reads the tree
+twice instead, first for directories and submodules, then for the rest,
+both at the root and in a subdirectory.
+
+diff --git a/ui-tree.c b/ui-tree.c
+index 5ac8c9a..6491c47 100644
+--- a/ui-tree.c
++++ b/ui-tree.c
+@@ -281,6 +281,34 @@ cleanup:
+ return 0;
+ }
+
++/* Directories and submodules first, then everything else. */
++static int is_dir_like(unsigned mode)
++{
++ return S_ISDIR(mode) || S_ISGITLINK(mode);
++}
++
++static int ls_dir_item(const struct object_id *oid, struct strbuf *base,
++ const char *pathname, unsigned mode, void *cbdata)
++{
++ return is_dir_like(mode) ? ls_item(oid, base, pathname, mode, cbdata) : 0;
++}
++
++static int ls_other_item(const struct object_id *oid, struct strbuf *base,
++ const char *pathname, unsigned mode, void *cbdata)
++{
++ return is_dir_like(mode) ? 0 : ls_item(oid, base, pathname, mode, cbdata);
++}
++
++static void ls_entries(struct tree *tree, struct walk_tree_context *walk_tree_ctx)
++{
++ struct pathspec paths = {
++ .nr = 0
++ };
++
++ read_tree(the_repository, tree, &paths, ls_dir_item, walk_tree_ctx);
++ read_tree(the_repository, tree, &paths, ls_other_item, walk_tree_ctx);
++}
++
+ static void ls_head(void)
+ {
+ cgit_print_layout_start();
+@@ -302,9 +330,6 @@ static void ls_tail(void)
+ static void ls_tree(const struct object_id *oid, const char *path, struct walk_tree_context *walk_tree_ctx)
+ {
+ struct tree *tree;
+- struct pathspec paths = {
+- .nr = 0
+- };
+
+ tree = parse_tree_indirect(oid);
+ if (!tree) {
+@@ -314,7 +339,7 @@ static void ls_tree(const struct object_id *oid, const char *path, struct walk_t
+ }
+
+ ls_head();
+- read_tree(the_repository, tree, &paths, ls_item, walk_tree_ctx);
++ ls_entries(tree, walk_tree_ctx);
+ ls_tail();
+ }
+
+@@ -333,11 +358,15 @@ static int walk_tree(const struct object_id *oid, struct strbuf *base,
+ return READ_TREE_RECURSIVE;
+
+ if (S_ISDIR(mode)) {
++ struct tree *tree = parse_tree_indirect(oid);
++
+ walk_tree_ctx->state = 1;
+ cgit_set_title_from_path(buffer.buf);
+ strbuf_release(&buffer);
+ ls_head();
+- return READ_TREE_RECURSIVE;
++ if (tree)
++ ls_entries(tree, walk_tree_ctx);
++ return 0;
+ } else {
+ walk_tree_ctx->state = 2;
+ print_object(oid, buffer.buf, pathname, walk_tree_ctx->curr_rev);
+@@ -345,7 +374,6 @@ static int walk_tree(const struct object_id *oid, struct strbuf *base,
+ return 0;
+ }
+ }
+- ls_item(oid, base, pathname, mode, walk_tree_ctx);
+ return 0;
+ }
+