From 4bf7dacfa870fb1fb81edf647fd5c049786c3c2f Mon Sep 17 00:00:00 2001 From: patrick-scho Date: Mon, 1 Dec 2025 15:12:11 +0100 Subject: print readme by oid try 1 --- ui-blob.c | 18 +++++++++++++ ui-blob.h | 1 + ui-repolist.c | 2 +- ui-summary.c | 84 ++++++++++++++++++++++++++++++----------------------------- ui-summary.h | 2 +- ui-tree.c | 9 +++---- 6 files changed, 68 insertions(+), 48 deletions(-) diff --git a/ui-blob.c b/ui-blob.c index f76c641..3267ed1 100644 --- a/ui-blob.c +++ b/ui-blob.c @@ -65,6 +65,24 @@ done: return walk_tree_ctx.found_path; } +int cgit_print_oid(const struct object_id *oid) +{ + enum object_type type; + char *buf; + unsigned long size; + + type = oid_object_info(the_repository, oid, &size); + if (type == OBJ_BAD) + return -1; + buf = read_object_file(oid, &type, &size); + if (!buf) + return -1; + buf[size] = '\0'; + html_raw(buf, size); + free(buf); + return 0; +} + int cgit_print_file(char *path, const char *head, int file_only) { struct object_id oid; diff --git a/ui-blob.h b/ui-blob.h index 16847b2..87e00ad 100644 --- a/ui-blob.h +++ b/ui-blob.h @@ -2,6 +2,7 @@ #define UI_BLOB_H extern int cgit_ref_path_exists(const char *path, const char *ref, int file_only); +extern int cgit_print_oid(const struct object_id *oid); extern int cgit_print_file(char *path, const char *head, int file_only); extern void cgit_print_blob(const char *hex, char *path, const char *head, int file_only); diff --git a/ui-repolist.c b/ui-repolist.c index c162290..749c6d3 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -321,7 +321,7 @@ void cgit_print_repolist(void) } htmlf("", !sorted && section ? "sublevel-repo" : "toplevel-repo"); - cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL); + cgit_tree_link_no_page(ctx.repo->name, ctx.repo->name, NULL, NULL, NULL, NULL); html(""); repourl = cgit_repourl(ctx.repo->url); html_link_open(repourl, NULL, NULL); diff --git a/ui-summary.c b/ui-summary.c index b5224f2..76f7df6 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -149,51 +149,53 @@ done: cgit_print_layout_end(); } -void cgit_print_repo_readme_no_layout(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; +// int read_tree_recursive(struct repository *r, +// struct tree *tree, +// const char *base, int baselen, +// int stage, const struct pathspec *pathspec, +// read_tree_fn_t fn, void *context); +struct get_readme_oid_ctx { + const struct object_id *oid; + char **filename; +}; +static int get_readme_oid_cb(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, int stage, void *cbdata) { + + // TODO: make readme.md configurable + const char *readme_name = "readme.md"; + html(pathname); + if (strcmp(pathname, readme_name) == 0) { + struct get_readme_oid_ctx *ctx = (struct get_readme_oid_ctx*)cbdata; + ctx->oid = oid; + *(ctx->filename) = xstrdup(pathname); + // TODO: maybe skip by returning != 0 } - 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; + return 0; +} +const struct object_id * get_readme_oid(const struct object_id *oid, char **filename) { + struct tree *tree = parse_tree_indirect(oid); + struct pathspec paths = { + .nr = 0 + }; + struct get_readme_oid_ctx ctx = { NULL, filename }; + read_tree_recursive(the_repository, tree, "", 0, 1, + &paths, get_readme_oid_cb, &ctx); + return ctx.oid; +} - if (path) { - free_filename = 1; - filename = append_readme_path(filename, ref, path); - if (!filename) - goto done; - } +void cgit_print_repo_readme_no_layout(const struct object_id *oid) +{ + char *filename = NULL; + const struct object_id *readme_oid = get_readme_oid(oid, &filename); + if (readme_oid) { + html("
"); + cgit_open_filter(ctx.repo->about_filter, filename); + cgit_print_oid(readme_oid); + cgit_close_filter(ctx.repo->about_filter); - /* 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("
"); - html("
"); - if (free_filename) free(filename); - - -done: - // cgit_print_layout_end(); + } } diff --git a/ui-summary.h b/ui-summary.h index 4118fb9..a067010 100644 --- a/ui-summary.h +++ b/ui-summary.h @@ -3,6 +3,6 @@ extern void cgit_print_summary(void); extern void cgit_print_repo_readme(const char *path); -extern void cgit_print_repo_readme_no_layout(const char *path); +extern void cgit_print_repo_readme_no_layout(const struct object_id *oid); #endif /* UI_SUMMARY_H */ diff --git a/ui-tree.c b/ui-tree.c index 685503d..9cbed30 100644 --- a/ui-tree.c +++ b/ui-tree.c @@ -274,11 +274,11 @@ static void ls_head(void) html("\n"); } -static void ls_tail(const char *readme_path) +static void ls_tail(const struct object_id *oid) { html("\n"); - if (readme_path) - cgit_print_repo_readme_no_layout(readme_path); + if (oid) + cgit_print_repo_readme_no_layout(oid); cgit_print_layout_end(); } @@ -299,8 +299,7 @@ static void ls_tree(const struct object_id *oid, const char *path, struct walk_t ls_head(); read_tree_recursive(the_repository, tree, "", 0, 1, &paths, ls_item, walk_tree_ctx); - const char *readme_path = "readme.md"; - ls_tail(readme_path); + ls_tail(oid); } -- cgit v1.2.3