/*
* The output every cgit page has in common. A link from one page to another is
* built here because only this file knows whether a site addresses a request
* as a path below a virtual root or as a query string, and the two are spelled
* differently down to the separator between arguments. The headers, the
* document head, the header block, the footer, the error page and the age
* column are here so that a page handler is left with only its own body to
* produce.
*/
#define USE_THE_REPOSITORY_VARIABLE
#include "cgit.h"
#include "html.h"
#include "shared.h"
#include "ui-shared.h"
#include "ui-snapshot.h"
#define DEFAULT_HTTP_PORT 80
#define SHORT_HASH_LEN 10
// A max-msg-len below this leaves too little of a commit subject to be worth
// truncating, and the ellipsis is paid for out of that same budget.
#define MIN_TRUNCATE_LEN 15
#define ELLIPSIS_LEN 3
// Lines of context git itself defaults to, which a link has no reason to name.
#define DEFAULT_DIFF_CONTEXT 3
// Bounds the breadcrumbs, so that one request cannot turn into an unbounded
// row of links.
#define MAX_CRUMB_LEVELS 15
// cgit_fmt exits the request rather than truncate, so a path is only worked
// into a tab title while it still leaves room to spare in that buffer.
#define MAX_TITLE_PATH_LEN 256
static const char *repo_basename(const char *reponame)
{
// The result lives in a static buffer, so it is only good until the
// next call.
static char buf[1024];
const char *slash;
size_t len;
int last;
len = strlcpy(buf, reponame, sizeof(buf));
if (len >= sizeof(buf))
die("repo_basename: truncated repository name '%s'", reponame);
last = len - 1;
while (last && buf[last] == '/')
buf[last--] = '\0';
if (last >= 3 && starts_with(&buf[last - 3], ".git")) {
last -= 3;
buf[last--] = '\0';
}
while (last && buf[last] == '/')
buf[last--] = '\0';
slash = strrchr(buf, '/');
if (slash)
return ++slash;
return buf;
}
static const char *root_url(void)
{
if (ctx.cfg.virtual_root)
return ctx.cfg.virtual_root;
else
return ctx.cfg.script_name;
}
static void site_url(const char *page, const char *search, const char *sort,
int ofs, int always_root)
{
const char *delim = "?";
if (always_root || page)
html_attr(root_url());
else {
char *currenturl = cgit_currenturl();
html_attr(currenturl);
free(currenturl);
}
if (page) {
htmlf("?p=%s", page);
delim = "&";
}
if (search) {
html(delim);
html("q=");
html_url_arg(search);
delim = "&";
}
if (sort) {
html(delim);
html("s=");
html_url_arg(sort);
delim = "&";
}
if (ofs) {
html(delim);
htmlf("ofs=%d", ofs);
}
}
static void site_link(const char *page, const char *name, const char *title,
const char *class, const char *search, const char *sort,
int ofs, int always_root)
{
html("");
html_txt(name);
html("");
}
/*
* 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
* arguments and close. The return is the delimiter to put before the first of
* those, which is still a question mark unless a query string has been opened.
*/
static const char *repolink(const char *title, const char *class,
const char *page, const char *head,
const char *path)
{
const char *delim = "?";
html("url);
if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
html("/");
if (page) {
html_url_arg(page);
html("/");
if (path)
html_url_arg(path);
}
delim = "&";
}
if (head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch)) {
html(delim);
html("h=");
html_url_arg(head);
delim = "&";
}
return cgit_fmt("%s", delim);
}
static const char *emit_rev_arg(const char *delim, const char *rev)
{
if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
html(delim);
html("id=");
html_url_arg(rev);
return "&";
}
return delim;
}
static void reporevlink(const char *page, const char *name, const char *title,
const char *class, const char *head, const char *rev,
const char *path)
{
const char *delim;
delim = repolink(title, class, page, head, path);
emit_rev_arg(delim, rev);
html("'>");
html_txt(name);
html("");
}
static void emit_diff_args(const char *delim)
{
if (ctx.qry.difftype) {
html(delim);
htmlf("dt=%d", ctx.qry.difftype);
delim = "&";
}
if (ctx.qry.context > 0 && ctx.qry.context != DEFAULT_DIFF_CONTEXT) {
html(delim);
html("context=");
htmlf("%d", ctx.qry.context);
delim = "&";
}
if (ctx.qry.ignorews) {
html(delim);
html("ignorews=1");
delim = "&";
}
if (ctx.qry.follow) {
html(delim);
html("follow=1");
}
}
static struct string_list_item *lookup_path(struct string_list *list,
const char *path)
{
struct string_list_item *item;
while (path && path[0]) {
if ((item = string_list_lookup(list, path)))
return item;
if (!(path = strchr(path, '/')))
break;
path++;
}
return NULL;
}
/*
* A repository can supply the module-link template through a repo-local
* cgitrc, so the %s placeholders are expanded here instead of the template
* being handed to printf, where a surplus conversion would read past the
* argument list.
*/
static void emit_module_link(const char *tmpl, const char **args, int nargs)
{
struct strbuf sb = STRBUF_INIT;
int used = 0;
while (*tmpl) {
if (*tmpl != '%') {
strbuf_addch(&sb, *tmpl++);
continue;
}
tmpl++;
if (*tmpl == '%') {
strbuf_addch(&sb, '%');
tmpl++;
} else if (*tmpl == 's' && used < nargs) {
strbuf_addstr(&sb, args[used++]);
tmpl++;
} else {
strbuf_addch(&sb, '%');
}
}
html_attr(sb.buf);
strbuf_release(&sb);
}
static struct date_mode configured_date_mode(void)
{
struct date_mode mode = ctx.cfg.date_mode;
if (ctx.cfg.local_time)
mode.local = 1;
return mode;
}
static void print_abs_date(time_t t, int tz)
{
html("");
html_txt(show_date(t, tz, configured_date_mode()));
html("");
}
static void print_rel_date(time_t t, int tz, double count, const char *class,
const char *suffix)
{
htmlf("%.0f %s", count, suffix);
}
static char *http_date(time_t t)
{
static char day[][4] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static char month[][4] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct tm tm;
gmtime_r(&t, &tm);
return cgit_fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm.tm_wday],
tm.tm_mday, month[tm.tm_mon], 1900 + tm.tm_year,
tm.tm_hour, tm.tm_min, tm.tm_sec);
}
static void print_rel_vcs_link(const char *url)
{
html("\n");
}
static int emit_css_link(struct string_list_item *item, void *fallback)
{
// An empty css entry is how a site turns the stylesheet off.
if (item && *item->string == '\0')
return 0;
html("\n");
return 0;
}
static int emit_js_link(struct string_list_item *item, void *fallback)
{
// An empty js entry is how a site turns the script off.
if (item && *item->string == '\0')
return 0;
html("\n");
return 0;
}
static void add_clone_urls(void (*fn)(const char *), char *urls, char *suffix)
{
struct strbuf **url_list = strbuf_split_str(urls, ' ', 0);
int i;
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_list_free(url_list);
}
static int print_branch_option(const struct reference *ref, void *counter)
{
int *count = counter;
const char *name = ref->name;
// The switcher runs on every page, so it is bounded like the refs list.
if (ctx.cfg.max_ref_count && *count >= ctx.cfg.max_ref_count)
return -1;
(*count)++;
html_option(name, name, ctx.qry.head);
return 0;
}
static void print_header(void)
{
const char *logo = NULL, *logo_link = NULL;
if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
logo = ctx.repo->logo;
else
logo = ctx.cfg.logo;
if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
logo_link = ctx.repo->logo_link;
else
logo_link = ctx.cfg.logo_link;
html("\n");
if (logo && *logo) {
html("\n");
}
html("
\n");
// A repository with no commits has no branches to list, so the
// switcher would be an empty select next to a switch button.
if (ctx.repo && ctx.env.authenticated && !ctx.empty_repo) {
html("\n");
}
if (ctx.repo) {
html("
");
cgit_refs_link("refs", "Branches and tags", tab_class("refs"),
ctx.qry.head, ctx.qry.oid, NULL);
html("
\n
");
cgit_log_link("log",
tab_title("Commit history of %s", "Commit history", vpath),
tab_class("log"), ctx.qry.head,
NULL, ctx.qry.vpath, 0, NULL, NULL,
ctx.qry.showmsg, ctx.qry.follow);
html("
\n
");
if (ctx.qry.page && !strcmp(ctx.qry.page, "blame"))
cgit_blame_link("blame",
tab_title("Line-by-line blame of %s", "Blame", vpath),
tab_class("blame"), ctx.qry.head,
ctx.qry.oid, ctx.qry.vpath);
else
cgit_tree_link("tree",
tab_title("Browse the tree at %s", "Browse the file tree", vpath),
tab_class("tree"), ctx.qry.head,
ctx.qry.oid, ctx.qry.vpath);
html("
\n
");
cgit_commit_link("commit", "The current commit", tab_class("commit"),
ctx.qry.head, ctx.qry.oid, ctx.qry.vpath);
html("
\n
");
cgit_diff_link("diff",
tab_title("Changes to %s", "Changes in the current commit", vpath),
tab_class("diff"), ctx.qry.head,
ctx.qry.oid, ctx.qry.oid2, ctx.qry.vpath);
html("
");
if (!ctx.env.authenticated || !ctx.cfg.noheader)
print_header();
if (ctx.env.authenticated)
html("\n");
if (ctx.env.authenticated && ctx.repo && ctx.qry.vpath) {
html("");
}
html("\n");
}
void cgit_print_filemode(unsigned short mode)
{
if (S_ISDIR(mode))
html("d");
else if (S_ISLNK(mode))
html("l");
else if (S_ISGITLINK(mode))
html("m");
else
html("-");
html_fileperm(mode >> 6);
html_fileperm(mode >> 3);
html_fileperm(mode);
}
static void compose_snapshot_prefix(struct strbuf *filename, const char *base,
const char *ref)
{
struct object_id oid;
// A tag named v1.2 or V1.2 gives its snapshot the prettier name 1.2,
// but only where dropping the letter cannot land two different tags on
// one name, so that a snapshot can still be traced back to the tag it
// was made from.
if (repo_get_oid(the_repository, cgit_fmt("refs/tags/%s", ref), &oid) == 0 &&
(ref[0] == 'v' || ref[0] == 'V') && isdigit((unsigned char)ref[1]) &&
((repo_get_oid(the_repository, cgit_fmt("refs/tags/%s", ref + 1), &oid) == 0) +
(repo_get_oid(the_repository, cgit_fmt("refs/tags/v%s", ref + 1), &oid) == 0) +
(repo_get_oid(the_repository, cgit_fmt("refs/tags/V%s", ref + 1), &oid) == 0) == 1))
ref++;
strbuf_addf(filename, "%s-%s", base, ref);
}
void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
const char *separator)
{
const struct cgit_snapshot_format *f;
struct strbuf filename = STRBUF_INIT;
const char *basename;
size_t prefixlen;
basename = cgit_snapshot_prefix(repo);
if (starts_with(ref, basename))
strbuf_addstr(&filename, ref);
else
compose_snapshot_prefix(&filename, basename, ref);
prefixlen = filename.len;
for (f = cgit_snapshot_formats; f->suffix; f++) {
if (!(repo->snapshots & cgit_snapshot_format_bit(f)))
continue;
strbuf_setlen(&filename, prefixlen);
strbuf_addstr(&filename, f->suffix);
snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
filename.buf);
if (cgit_snapshot_get_sig(ref, f)) {
strbuf_addstr(&filename, ".asc");
html(" (");
snapshot_link("sig", NULL, NULL, NULL, NULL,
filename.buf);
html(")");
} else if (starts_with(f->suffix, ".tar") && cgit_snapshot_get_sig(ref, &cgit_snapshot_formats[0])) {
// A compressed tarball offers the signature made for
// the plain tar it expands to, which is the first
// format in the table.
strbuf_setlen(&filename,
filename.len - strlen(f->suffix));
strbuf_addstr(&filename, ".tar.asc");
html(" (");
snapshot_link("sig", NULL, NULL, NULL, NULL,
filename.buf);
html(")");
}
html(separator);
}
strbuf_release(&filename);
}
void cgit_set_title_from_path(const char *path)
{
struct strbuf sb = STRBUF_INIT;
const char *slash, *last_slash;
if (!path)
return;
// The path is read from the end so that the title names the file first
// and the directories it sits in after it. The separator between them
// is a guillemet, spelled here as the UTF-8 bytes of one.
last_slash = path + strlen(path);
while ((slash = find_last_char(path, last_slash, '/')) != NULL) {
strbuf_add(&sb, slash + 1, last_slash - slash - 1);
strbuf_addstr(&sb, " \xc2\xab ");
last_slash = slash;
}
strbuf_add(&sb, path, last_slash - path);
strbuf_addf(&sb, " - %s", ctx.page.title);
ctx.page.title = strbuf_detach(&sb, NULL);
}