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-summary.c | 84 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 41 deletions(-) (limited to 'ui-summary.c') 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(); + } } -- cgit v1.2.3