return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
}
+const char *cgit_currenturl()
+{
+ if (!ctx.qry.url)
+ return cgit_rooturl();
+ return ctx.qry.url;
+}
+
const char *cgit_rooturl()
{
if (ctx.cfg.virtual_root)
return rvbuf;
}
-static void site_url(const char *page, const char *search, const char *sort, int ofs)
+static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
{
char *delim = "?";
- if (ctx.cfg.virtual_root)
- html_attr(ctx.cfg.virtual_root);
+ if (always_root || page)
+ html_attr(cgit_rooturl());
else
- html_url_path(ctx.cfg.script_name);
+ html_attr(cgit_currenturl());
if (page) {
htmlf("?p=%s", page);
}
static void site_link(const char *page, const char *name, const char *title,
- const char *class, const char *search, const char *sort, int ofs)
+ const char *class, const char *search, const char *sort, int ofs, int always_root)
{
html("<a");
if (title) {
html("'");
}
html(" href='");
- site_url(page, search, sort, ofs);
+ site_url(page, search, sort, ofs, always_root);
html("'>");
html_txt(name);
html("</a>");
}
void cgit_index_link(const char *name, const char *title, const char *class,
- const char *pattern, const char *sort, int ofs)
+ const char *pattern, const char *sort, int ofs, int always_root)
{
- site_link(NULL, name, title, class, pattern, sort, ofs);
+ site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
}
static char *repolink(const char *title, const char *class, const char *page,
}
void cgit_tag_link(const char *name, const char *title, const char *class,
- const char *head, const char *rev)
+ const char *tag)
{
- reporevlink("tag", name, title, class, head, rev, NULL);
+ reporevlink("tag", name, title, class, tag, NULL, NULL);
}
void cgit_tree_link(const char *name, const char *title, const char *class,
{
if (!strcmp(ctx.qry.page, "repolist"))
cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
- ctx.qry.ofs);
+ ctx.qry.ofs, 1);
else if (!strcmp(ctx.qry.page, "summary"))
cgit_summary_link(name, title, class, ctx.qry.head);
else if (!strcmp(ctx.qry.page, "tag"))
- cgit_tag_link(name, title, class, ctx.qry.head,
- ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL);
+ cgit_tag_link(name, title, class, ctx.qry.has_sha1 ?
+ ctx.qry.sha1 : ctx.qry.head);
else if (!strcmp(ctx.qry.page, "tree"))
cgit_tree_link(name, title, class, ctx.qry.head,
ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
html_txt(buf);
}
+static void print_rel_date(time_t t, double value,
+ const char *class, const char *suffix)
+{
+ char buf[64];
+ struct tm *time;
+
+ if (ctx.cfg.local_time)
+ time = localtime(&t);
+ else
+ time = gmtime(&t);
+ strftime(buf, sizeof(buf) - 1, FMT_LONGDATE, time);
+
+ htmlf("<span class='%s' title='", class);
+ html_attr(buf);
+ htmlf("'>%.0f %s</span>", value, suffix);
+}
+
void cgit_print_age(time_t t, time_t max_relative, const char *format)
{
time_t now, secs;
}
if (secs < TM_HOUR * 2) {
- htmlf("<span class='age-mins'>%.0f min.</span>",
- secs * 1.0 / TM_MIN);
+ print_rel_date(t, secs * 1.0 / TM_MIN, "age-mins", "min.");
return;
}
if (secs < TM_DAY * 2) {
- htmlf("<span class='age-hours'>%.0f hours</span>",
- secs * 1.0 / TM_HOUR);
+ print_rel_date(t, secs * 1.0 / TM_HOUR, "age-hours", "hours");
return;
}
if (secs < TM_WEEK * 2) {
- htmlf("<span class='age-days'>%.0f days</span>",
- secs * 1.0 / TM_DAY);
+ print_rel_date(t, secs * 1.0 / TM_DAY, "age-days", "days");
return;
}
if (secs < TM_MONTH * 2) {
- htmlf("<span class='age-weeks'>%.0f weeks</span>",
- secs * 1.0 / TM_WEEK);
+ print_rel_date(t, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
return;
}
if (secs < TM_YEAR * 2) {
- htmlf("<span class='age-months'>%.0f months</span>",
- secs * 1.0 / TM_MONTH);
+ print_rel_date(t, secs * 1.0 / TM_MONTH, "age-months", "months");
return;
}
- htmlf("<span class='age-years'>%.0f years</span>",
- secs * 1.0 / TM_YEAR);
+ print_rel_date(t, secs * 1.0 / TM_YEAR, "age-years", "years");
}
void cgit_print_http_headers(void)
if (ctx.cfg.footer)
html_include(ctx.cfg.footer);
else {
- htmlf("<div class='footer'>generated by cgit %s at ",
+ htmlf("<div class='footer'>generated by <a href='http://git.zx2c4.com/cgit/about/'>cgit %s</a> at ",
cgit_version);
cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
html("</div>\n");
static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
{
- struct strbuf buf = STRBUF_INIT;
- char *h = txt, *t, c;
+ struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
+ int i;
- while (h && *h) {
- while (h && *h == ' ')
- h++;
- if (!*h)
- break;
- t = h;
- while (t && *t && *t != ' ')
- t++;
- c = *t;
- *t = 0;
-
- if (suffix && *suffix) {
- strbuf_reset(&buf);
- strbuf_addf(&buf, "%s/%s", h, suffix);
- h = buf.buf;
- }
- fn(h);
- *t = c;
- h = t;
+ for (i = 0; url_list[i]; i++) {
+ strbuf_rtrim(url_list[i]);
+ if (url_list[i]->len == 0)
+ continue;
+ if (suffix && *suffix)
+ strbuf_addf(url_list[i], "/%s", suffix);
+ fn(url_list[i]->buf);
}
- strbuf_release(&buf);
+ strbuf_list_free(url_list);
}
void cgit_add_clone_urls(void (*fn)(const char *))
html("<td class='main'>");
if (ctx.repo) {
- cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
+ cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
html(" : ");
cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
if (ctx.env.authenticated) {
html("<input type='submit' value='search'/>\n");
html("</form>\n");
} else if (ctx.env.authenticated) {
- site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0);
+ site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
if (ctx.cfg.root_readme)
site_link("about", "about", NULL, hc("about"),
- NULL, NULL, 0);
+ NULL, NULL, 0, 1);
html("</td><td class='form'>");
html("<form method='get' action='");
- html_attr(cgit_rooturl());
+ html_attr(cgit_currenturl());
html("'>\n");
html("<input type='text' name='q' size='10' value='");
html_attr(ctx.qry.search);