From e8671e5f11c1d64d539cf4677808e7617c94d050 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Fri, 16 Oct 2026 06:36:07 -1000 Subject: Hand the about filter the tree url its links need With nothing below the about page, a readme's relative links have to be written against the tree, and only cgit knows how this site spells the url of a tree page and which ref the readme was read from. Both reach the filter already escaped for a url, built by the same writer as cgit's own links. --- source/filter.c | 4 ++- source/ui-repolist.c | 2 +- source/ui-shared.c | 87 ++++++++++++++++++++++++++++++++++++---------------- source/ui-shared.h | 8 +++++ source/ui-summary.c | 10 +++++- 5 files changed, 81 insertions(+), 30 deletions(-) (limited to 'source') diff --git a/source/filter.c b/source/filter.c index 5f96cc0..ec48007 100644 --- a/source/filter.c +++ b/source/filter.c @@ -498,8 +498,10 @@ struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype) case TRAILER: argument_count = 2; break; - case SOURCE: case ABOUT: + argument_count = 3; + break; + case SOURCE: argument_count = 1; break; case COMMIT: diff --git a/source/ui-repolist.c b/source/ui-repolist.c index daf18f5..1c081c7 100644 --- a/source/ui-repolist.c +++ b/source/ui-repolist.c @@ -463,7 +463,7 @@ void cgit_print_site_readme(void) cgit_print_layout_start(); if (!ctx.cfg.root_readme) goto done; - cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme); + cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme, "", ""); html_include(ctx.cfg.root_readme); cgit_close_filter(ctx.cfg.about_filter); done: diff --git a/source/ui-shared.c b/source/ui-shared.c index fd9cd20..94a749e 100644 --- a/source/ui-shared.c +++ b/source/ui-shared.c @@ -137,6 +137,47 @@ static void site_link(const char *page, const char *name, const char *title, html(""); } +/* + * Write the url naming the repository, the page and the path, and return + * whether the url has opened its query string already. + */ +static int write_page_url(const char *page, const char *path) +{ + if (ctx.cfg.virtual_root) { + html_url_path(ctx.cfg.virtual_root); + html_url_path(ctx.repo->url); + if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') + html("/"); + if (page) { + html_url_path(page); + html("/"); + if (path) + html_url_path(path); + } + return 0; + } + html_url_path(ctx.cfg.script_name); + html("?url="); + html_url_arg(ctx.repo->url); + if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') + html("/"); + if (page) { + html_url_arg(page); + html("/"); + if (path) + html_url_arg(path); + } + return 1; +} + +/* + * Whether head needs naming in a url, which the default branch does not. + */ +static int head_is_named(const char *head) +{ + return head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch); +} + /* * Open an anchor and write the part of its href naming the repository, the * page and the path, leaving the href quote open for the caller to add its own @@ -146,7 +187,7 @@ static void site_link(const char *page, const char *name, const char *title, static const char *repolink(const char *title, const char *class, const char *page, const char *head, const char *path) { - const char *delim = "?"; + const char *delim; html("defbranch && strcmp(head, ctx.repo->defbranch)) { + delim = write_page_url(page, path) ? "&" : "?"; + if (head_is_named(head)) { html(delim); html("h="); html_url_arg(head); @@ -911,6 +928,22 @@ char *cgit_pageurl(const char *reponame, const char *pagename, const char *query return cgit_fileurl(reponame, pagename, NULL, query); } +void cgit_tree_url(const char *head, struct strbuf *base, struct strbuf *query) +{ + int query_open; + + html_capture_begin(base); + query_open = write_page_url("tree", NULL); + html_capture_end(); + if (!head_is_named(head)) + return; + html_capture_begin(query); + html(query_open ? "&" : "?"); + html("h="); + html_url_arg(head); + html_capture_end(); +} + const char *cgit_snapshot_prefix(const struct cgit_repo *repo) { if (repo->snapshot_prefix) diff --git a/source/ui-shared.h b/source/ui-shared.h index 93cc9d2..dd90196 100644 --- a/source/ui-shared.h +++ b/source/ui-shared.h @@ -31,6 +31,14 @@ extern char *cgit_fileurl(const char *reponame, const char *pagename, const char const char *query); extern char *cgit_pageurl(const char *reponame, const char *pagename, const char *query); +/* + * The url of the current repository's tree page at head, split where a path + * in the tree goes, for a filter to build links of its own from. Both parts + * are escaped for a url but not for HTML, and query stays empty for the + * default branch. + */ +extern void cgit_tree_url(const char *head, struct strbuf *base, struct strbuf *query); + /* * Call fn once for every URL this repository can be cloned from. */ diff --git a/source/ui-summary.c b/source/ui-summary.c index e7b229a..44abc6b 100644 --- a/source/ui-summary.c +++ b/source/ui-summary.c @@ -91,14 +91,22 @@ void cgit_print_repo_readme(void) } html(""); } else { + struct strbuf tree_base = STRBUF_INIT, tree_query = STRBUF_INIT; + + // A tracked readme's links lead into the tree at the ref it was + // read from, while one on disk has no tree to lead into. + if (ref) + cgit_tree_url(ref, &tree_base, &tree_query); // The filter is what makes the source safe here, so it is fed // through unescaped. - cgit_open_filter(ctx.repo->about_filter, filename); + cgit_open_filter(ctx.repo->about_filter, filename, tree_base.buf, tree_query.buf); if (ref) cgit_print_file(filename, ref, 1, 0); else html_include(filename); cgit_close_filter(ctx.repo->about_filter); + strbuf_release(&tree_base); + strbuf_release(&tree_query); } html(""); cgit_print_layout_end(); -- cgit