From fa871b31f6c3309d3c3d0b4bebcd5b5b62706f0a Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 29 Aug 2026 15:50:14 -1000 Subject: Point clone rows at a url a browser can follow --- source/ui-empty.c | 8 +----- source/ui-shared.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ source/ui-shared.h | 1 + source/ui-summary.c | 8 +----- tests/t0102-summary.sh | 21 +++++++++++++++ 5 files changed, 93 insertions(+), 14 deletions(-) diff --git a/source/ui-empty.c b/source/ui-empty.c index a65f49b..dd8dc64 100644 --- a/source/ui-empty.c +++ b/source/ui-empty.c @@ -13,13 +13,7 @@ static void print_clone_url(const char *url) { - html(""); - html_txt(url); - html("\n"); + cgit_print_clone_row(url, 0); } /* diff --git a/source/ui-shared.c b/source/ui-shared.c index 85712bf..5cf9428 100644 --- a/source/ui-shared.c +++ b/source/ui-shared.c @@ -317,8 +317,26 @@ static void print_rel_date(time_t t, int tz, double count, const char *class, htmlf("'>%.0f %s", count, suffix); } +/* + * Whether a clone url opens with a URI scheme. The scp form git@host:path + * has none, and a link element's href must hold a URI, so that form stays + * out of the document head. + */ +static int has_uri_scheme(const char *url) +{ + const char *p = url; + + if (!isalpha((unsigned char)*p)) + return 0; + while (isalnum((unsigned char)*p) || *p == '+' || *p == '-' || *p == '.') + p++; + return *p == ':'; +} + static void print_rel_vcs_link(const char *url) { + if (!has_uri_scheme(url)) + return; html("url); + } + + if (colspan) + htmlf("", colspan); + else + html(""); + html(""); + html_txt(url); + html("\n"); + free(fallback); +} + void cgit_add_hidden_formfields(int incl_head, int incl_search, const char *page) { diff --git a/source/ui-shared.h b/source/ui-shared.h index c266951..c421178 100644 --- a/source/ui-shared.h +++ b/source/ui-shared.h @@ -24,6 +24,7 @@ extern char *cgit_currenturl(void); extern char *cgit_currentfullurl(void); extern const char *cgit_loginurl(void); extern char *cgit_repourl(const char *reponame); +extern void cgit_print_clone_row(const char *url, int colspan); extern char *cgit_fileurl(const char *reponame, const char *pagename, const char *filename, const char *query); extern char *cgit_pageurl(const char *reponame, const char *pagename, diff --git a/source/ui-summary.c b/source/ui-summary.c index 3658bc5..dacf0f7 100644 --- a/source/ui-summary.c +++ b/source/ui-summary.c @@ -48,13 +48,7 @@ static void print_clone_url(const char *url) htmlf("Clone\n", columns); } - htmlf(""); - html_txt(url); - html("\n"); + cgit_print_clone_row(url, columns); } /* diff --git a/tests/t0102-summary.sh b/tests/t0102-summary.sh index 79f2b11..5ce1fab 100755 --- a/tests/t0102-summary.sh +++ b/tests/t0102-summary.sh @@ -32,4 +32,25 @@ test_expect_success 'clone-url expanded correctly' ' grep "git://example.org/bar.git" tmp ' +# A clone url is shown as written, but a browser cannot follow the ssh, scp +# or git forms, so those rows link to the first http url in the list, or to +# the repository's own page when the list has none. +test_expect_success 'a git clone url row falls back to the page url' ' + grep "clone-url.>git://example.org/bar.git" tmp +' + +test_expect_success 'an scp clone url row links to the http url' ' + { + echo "virtual-root=/" && + echo "cache-size=0" && + echo "repo.url=foo" && + echo "repo.path=$PWD/repos/foo/.git" && + echo "repo.clone-url=git@example.org:foo https://example.org/foo" + } >clonerc && + CGIT_CONFIG="$PWD/clonerc" QUERY_STRING="url=foo" cgit >tmp && + grep "title=.foo Git repository. href=.https://example.org/foo.>git@example.org:foo" tmp && + grep "vcs-git. href=.https://example.org/foo." tmp && + ! grep "vcs-git. href=.git@" tmp +' + test_done -- cgit v2.8.0