diff options
context:
space:
mode:
authorBryce Kwon <bryce@brycekwon.com>
committerBryce Kwon <bryce@brycekwon.com>
commit
parent
tree
download
Point clone rows at a url a browser can follow
Diffstat (limited to 'source/ui-shared.c')
-rw-r--r--source/ui-shared.c69
1 file changed, 69 insertions, 0 deletions
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</time>", 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("<link rel='vcs-git' href='");
html_attr(url);
html("' title='");
@@ -1620,6 +1638,57 @@ void cgit_add_clone_urls(void (*fn)(const char *))
add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
}
+static char *http_clone_url;
+static int http_clone_url_scanned;
+
+// Scheme names compare case-insensitively.
+static int is_http_url(const char *url)
+{
+ return istarts_with(url, "http://") || istarts_with(url, "https://");
+}
+
+static void capture_http_clone_url(const char *url)
+{
+ if (!http_clone_url && is_http_url(url))
+ http_clone_url = xstrdup(url);
+}
+
+/*
+ * One row of the clone table. The url is shown as written, and every row is
+ * a link, but a browser cannot follow the ssh, scp or git forms, so those
+ * rows point at the first http url in the clone list, or at the repository's
+ * own page when the list has none, which with http clone enabled is itself a
+ * working clone url. A colspan of zero leaves the attribute out.
+ */
+void cgit_print_clone_row(const char *url, int colspan)
+{
+ const char *href = url;
+ char *fallback = NULL;
+
+ if (!is_http_url(url)) {
+ if (!http_clone_url_scanned) {
+ http_clone_url_scanned = 1;
+ cgit_add_clone_urls(capture_http_clone_url);
+ }
+ href = http_clone_url;
+ if (!href)
+ href = fallback = cgit_repourl(ctx.repo->url);
+ }
+
+ if (colspan)
+ htmlf("<tr><td colspan='%d' class='clone-url'>", colspan);
+ else
+ html("<tr><td class='clone-url'>");
+ html("<a rel='vcs-git' title='");
+ html_attr(ctx.repo->name);
+ html(" Git repository' href='");
+ html_attr(href);
+ html("'>");
+ html_txt(url);
+ html("</a></td></tr>\n");
+ free(fallback);
+}
+
void cgit_add_hidden_formfields(int incl_head, int incl_search,
const char *page)
{