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/ui-shared.c | 87 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 27 deletions(-) (limited to 'source/ui-shared.c') 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) -- cgit