From e7eff4197d809a221586d9b28f08fb8ac40ebc8c Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 7 Nov 2026 19:26:58 -1000 Subject: Keep a space in a repository url in one clone url The shared repository "with space" listed git://example.org/with and space.git as two clone urls, and the head linked the first alone. The manual calls clone-url a list of templates, so the list is now split before each template is expanded. A clone-prefix was right already, as the url is joined to it after the split. --- source/ui-shared.c | 16 +++++++++++++--- tests/t0102-summary.sh | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/source/ui-shared.c b/source/ui-shared.c index 788551d..985f33d 100644 --- a/source/ui-shared.c +++ b/source/ui-shared.c @@ -1662,7 +1662,8 @@ int cgit_refuse_pathspec_magic(const char *path) return -1; } -static void add_clone_urls(void (*fn)(const char *), char *urls, char *suffix) +static void add_clone_urls(void (*fn)(const char *), const char *urls, + const char *suffix, int expand) { struct strbuf **url_list = strbuf_split_str(urls, ' ', 0); int i; @@ -1671,6 +1672,15 @@ static void add_clone_urls(void (*fn)(const char *), char *urls, char *suffix) strbuf_rtrim(url_list[i]); if (url_list[i]->len == 0) continue; + // Each template is expanded after the split, as a value such + // as $CGIT_REPO_URL may hold a space. The expansion is copied + // out of its static buffer, since fn may expand the list again. + if (expand) { + const char *url = cgit_expand_macros(url_list[i]->buf); + + strbuf_reset(url_list[i]); + strbuf_addstr(url_list[i], url); + } if (suffix && *suffix) strbuf_addf(url_list[i], "/%s", suffix); fn(url_list[i]->buf); @@ -1682,9 +1692,9 @@ static void add_clone_urls(void (*fn)(const char *), char *urls, char *suffix) void cgit_add_clone_urls(void (*fn)(const char *)) { if (ctx.repo->clone_url) - add_clone_urls(fn, cgit_expand_macros(ctx.repo->clone_url), NULL); + add_clone_urls(fn, ctx.repo->clone_url, NULL, 1); else if (ctx.cfg.clone_prefix) - add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url); + add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url, 0); } static char *http_clone_url; diff --git a/tests/t0102-summary.sh b/tests/t0102-summary.sh index b31d476..632698c 100755 --- a/tests/t0102-summary.sh +++ b/tests/t0102-summary.sh @@ -69,6 +69,22 @@ test_expect_success 'clone-prefix gives a clone url per prefix' ' test_grep "clone-url.>git://b/foo" tmp ' +# The list is split into its templates before each is expanded, so a space in +# the repository url stays inside one clone url, in the rows and in the head +# alike. A clone-prefix has the url joined to it after the split. +test_expect_success 'a space in the repository url stays inside one clone url' ' + cgit_ok_url "with+space" >tmp && + test_grep "clone-url.>git://example.org/with space.git" tmp && + test $(grep -c "class=.clone-url." tmp) = 1 && + test_grep "" tmp && + test $(grep -c "tmp && + test_grep ">https://a/with space" tmp && + test_grep ">git://b/with space" tmp && + test $(grep -c "class=.clone-url." tmp) = 2 +' + # A repository can turn the global clone-url off with an empty one of its own. # Neither its summary nor the page an empty repository gets then draws a Clone # heading over no rows. -- cgit 2.12.1