From 73474fa40571ef6ab13a1e0d065a21df92c21009 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 11 Jan 2026 17:35:46 +0100 Subject: fix About page and readme selection --- ui-summary.c | 137 +++++++++++++++++++++++++---------------------------------- 1 file changed, 58 insertions(+), 79 deletions(-) (limited to 'ui-summary.c') diff --git a/ui-summary.c b/ui-summary.c index 57182e3..e2003b1 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -31,14 +31,13 @@ static void print_url(const char *url) htmlf("Clone\n", columns); } - htmlf(""); - html("\n"); + htmlf(""); + html_txt(url); + html("\n"); } void cgit_print_summary(void) @@ -101,55 +100,6 @@ static char* append_readme_path(const char *filename, const char *ref, const cha return full_path; } -void cgit_print_repo_readme(const char *path) -{ - char *filename, *ref, *mimetype; - int free_filename = 0; - - mimetype = get_mimetype_for_filename(path); - if (mimetype && (!strncmp(mimetype, "image/", 6) || !strncmp(mimetype, "video/", 6))) { - ctx.page.mimetype = mimetype; - ctx.page.charset = NULL; - cgit_print_plain(); - free(mimetype); - return; - } - free(mimetype); - - cgit_print_layout_start(); - if (ctx.repo->readme.nr == 0) - goto done; - - filename = ctx.repo->readme.items[0].string; - ref = ctx.repo->readme.items[0].util; - - if (path) { - free_filename = 1; - filename = append_readme_path(filename, ref, path); - if (!filename) - goto done; - } - - /* Print the calculated readme, either from the git repo or from the - * filesystem, while applying the about-filter. - */ - html("
"); - cgit_open_filter(ctx.repo->about_filter, filename); - if (ref) - cgit_print_file(filename, ref, 1); - else - html_include(filename); - cgit_close_filter(ctx.repo->about_filter); - - html("
"); - if (free_filename) - free(filename); - - -done: - cgit_print_layout_end(); -} - struct get_readme_oid_ctx { struct object_id *oid; char **filename; @@ -161,43 +111,40 @@ static int get_readme_oid_cb(const struct object_id *oid, struct strbuf *base, struct get_readme_oid_ctx *ctx = (struct get_readme_oid_ctx*)cbdata; - // TODO: make readme.md configurable - const size_t readme_names_count = 2; + // TODO: make readme.md configurable, ctx.readme (stringlist) + const int readme_names_count = 2; const char **readme_names = (const char*[]){"readme.md", "readme"}; struct strbuf buffer = STRBUF_INIT; + if (base->len > 0) strbuf_addbuf(&buffer, base); - - bool match_base = false; - if (ctx->path == NULL) - match_base = base->len == 0; - else if (strncmp(buffer.buf, ctx->path, buffer.len > 0 ? buffer.len - 1 : 0) == 0) - match_base = true; - strbuf_addstr(&buffer, pathname); - - bool match_path = false; - if (ctx->path == NULL) - match_path = false; - else if (strcmp(buffer.buf, ctx->path) == 0) - match_path = true; + + size_t path_len = (ctx->path == NULL) ? 0 : strlen(ctx->path); + size_t cmp_count = (buffer.len > 0 && buffer.buf[buffer.len-1] == '/') ? buffer.len-1 : buffer.len; + if (path_len < cmp_count) cmp_count = path_len; + bool match_base = strncmp(buffer.buf, ctx->path, cmp_count) == 0; + bool match_path = match_base && buffer.len == path_len; strbuf_release(&buffer); + bool match_pathname = match_base && match_path && (oid_object_info(the_repository, oid, NULL) == OBJ_BLOB); + + // htmlf("[%s %s %s %b %b]", base->buf, pathname, ctx->path, match_base, match_path); + for (int i = 0; i < readme_names_count; i++) { - if (match_base && strcasecmp(pathname, readme_names[i]) == 0) { - *(ctx->oid) = *oid; - *(ctx->filename) = xstrdup(pathname); + if ( match_pathname || (match_base && strcasecmp(pathname, readme_names[i]) == 0) ) { + if (ctx->oid) + *(ctx->oid) = *oid; + if (ctx->filename) + *(ctx->filename) = xstrdup(pathname); ctx->found = true; break; } - else if (match_path) { - return READ_TREE_RECURSIVE; - } } - return 0; + return READ_TREE_RECURSIVE; } bool get_readme_oid(struct tree *tree, struct pathspec *paths, struct object_id *oid, char **filename) { struct get_readme_oid_ctx gro_ctx = (struct get_readme_oid_ctx){ @@ -210,6 +157,22 @@ bool get_readme_oid(struct tree *tree, struct pathspec *paths, struct object_id return gro_ctx.found; } +bool cgit_has_readme(void) { + struct object_id oid; + if (get_oid(ctx.qry.head, &oid)) { + return false; + } + + struct commit *commit = lookup_commit_reference(the_repository, &oid); + struct object_id *commit_tree_oid = get_commit_tree_oid(commit); + + struct tree *tree = parse_tree_indirect(commit_tree_oid); + struct pathspec paths = { + .nr = 0, + }; + return get_readme_oid(tree, &paths, NULL, NULL); +} + void cgit_print_repo_readme_no_layout(struct tree *tree, struct pathspec *paths) { char *filename = NULL; @@ -225,6 +188,22 @@ void cgit_print_repo_readme_no_layout(struct tree *tree, struct pathspec *paths) free(filename); } } +void cgit_print_repo_readme(const char *path, struct object_id *oid) { + struct commit *commit = lookup_commit_reference(the_repository, oid); + struct object_id *commit_tree_oid = get_commit_tree_oid(commit); + + struct tree *tree = parse_tree_indirect(commit_tree_oid); + struct pathspec_item path_items = { + .match = path, + .len = path ? strlen(path) : 0 + }; + struct pathspec paths = { + .nr = path ? 1 : 0, + .items = &path_items + }; + cgit_print_layout_start(); + cgit_print_repo_readme_no_layout(tree, &paths); +} // void cgit_print_repo_readme_no_layout(const struct tree *tree, struct pathspec *paths) // { // if (ctx.repo->readme.nr) { -- cgit v1.2.3