diff options
| author | Patrick | 2025-12-30 22:27:24 +0100 |
|---|---|---|
| committer | Patrick | 2025-12-30 22:27:24 +0100 |
| commit | 1556fd9a6b0e3b7166bec57db827edaa3b63f8a0 (patch) | |
| tree | d23a75a5394214911d9dcea5cff9ac1b509e2f0c /ui-summary.c | |
| parent | 5e5818f650513663322c485cfcdfb268eb232990 (diff) | |
| download | ps-cgit-1556fd9a6b0e3b7166bec57db827edaa3b63f8a0.tar.gz ps-cgit-1556fd9a6b0e3b7166bec57db827edaa3b63f8a0.zip | |
revert to find readme by filename, but now also:
- search multiple names (readme.md, readme)
- case insensitive
Diffstat (limited to 'ui-summary.c')
| -rw-r--r-- | ui-summary.c | 90 |
1 files changed, 82 insertions, 8 deletions
diff --git a/ui-summary.c b/ui-summary.c index c990bfd..6725634 100644 --- a/ui-summary.c +++ b/ui-summary.c @@ -150,20 +150,94 @@ done: cgit_print_layout_end(); } +struct get_readme_oid_ctx { + struct object_id *oid; + char **filename; + const char *path; + bool found; +}; +static int get_readme_oid_cb(const struct object_id *oid, struct strbuf *base, + const char *pathname, unsigned mode, int stage, void *cbdata) { + + struct get_readme_oid_ctx *ctx = (struct get_readme_oid_ctx*)cbdata; + + // TODO: make readme.md configurable + 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; + + strbuf_release(&buffer); + + for (int i = 0; i < sizeof(readme_names)/sizeof(*readme_names); i++) { + if (match_base && stricmp(pathname, readme_names[i]) == 0) { + *(ctx->oid) = *oid; + *(ctx->filename) = xstrdup(pathname); + ctx->found = true; + break; + } + else if (match_path) { + return READ_TREE_RECURSIVE; + } + } + + return 0; +} +bool get_readme_oid(const struct tree *tree, struct pathspec *paths, struct object_id *oid, char **filename) { + struct get_readme_oid_ctx gro_ctx = (struct get_readme_oid_ctx){ + .oid = oid, + .filename = filename, + .path = (paths->nr == 1 ? paths->items[0].match : NULL), + .found = false }; + read_tree_recursive(the_repository, tree, "", 0, 0, + paths, get_readme_oid_cb, &gro_ctx); + return gro_ctx.found; +} + void cgit_print_repo_readme_no_layout(const struct tree *tree, struct pathspec *paths) { - if (ctx.repo->readme.nr) { - const char *filename = ctx.repo->readme.items[0].string; - + char *filename = NULL; + struct object_id readme_oid; + if (get_readme_oid(tree, paths, &readme_oid, &filename)) { html("<div id='summary'>"); - cgit_open_filter(ctx.repo->about_filter, filename); - - char *ref = ctx.repo->readme.items[0].util; - cgit_print_file(filename, ref, 1); - + cgit_print_oid(&readme_oid); cgit_close_filter(ctx.repo->about_filter); html("</div>"); + + free(filename); } } +// void cgit_print_repo_readme_no_layout(const struct tree *tree, struct pathspec *paths) +// { +// if (ctx.repo->readme.nr) { +// const char *filename = ctx.repo->readme.items[0].string; +// +// html("<div id='summary'>"); +// +// cgit_open_filter(ctx.repo->about_filter, filename); +// +// char *ref = ctx.repo->readme.items[0].util; +// cgit_print_file(filename, ref, 1); +// +// cgit_close_filter(ctx.repo->about_filter); +// +// html("</div>"); +// } +// } |
