/*
* 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 "cache.h"
#include "cgit.h"
#include "html.h"
#include "shared.h"
#include "submodule-config.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;
}
/*
* Dates render as time elements, whose datetime attribute wants the strict
* ISO 8601 form with the T separator rather than the space git prints by
* default.
*/
static void print_abs_date(time_t t, int tz)
{
html("");
}
static void print_rel_date(time_t t, int tz, double count, const char *class,
const char *suffix)
{
htmlf("", 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("\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);
}
/*
* The id parameter pins a page to one commit while the rest of the chrome
* keeps naming the branch. Answer that commit, or null when the request
* follows the branch or the pin lands on its tip anyway.
*/
static const struct object_id *pinned_oid(void)
{
static struct object_id oid;
struct object_id head_oid;
struct commit *commit;
if (!ctx.repo || !ctx.qry.has_oid || !ctx.qry.oid || !ctx.qry.head)
return NULL;
if (repo_get_oid(the_repository, ctx.qry.oid, &oid) ||
repo_get_oid(the_repository, ctx.qry.head, &head_oid))
return NULL;
// A full hex id resolves without being looked up, so only peeling to a
// commit proves the pin names history, and a tag pinned to the tip
// compares equal only by its peeled commit.
commit = lookup_commit_reference_gently(the_repository, &oid, 1);
if (!commit)
return NULL;
oidcpy(&oid, &commit->object.oid);
if (oideq(&oid, &head_oid))
return NULL;
return &oid;
}
struct branch_option_data {
int count;
const char *selected;
};
/*
* Whether the head the request follows is one of the branches the switcher
* lists, remote ones included when those are shown. A head that is not, a
* raw commit id or a tag, would otherwise leave the select resting on the
* first branch, and submitting the form would quietly move the visitor there.
*/
static int head_is_listed_branch(void)
{
struct ref_store *refs = get_main_ref_store(the_repository);
struct strbuf ref = STRBUF_INIT;
int found;
strbuf_addf(&ref, "refs/heads/%s", ctx.qry.head);
found = refs_ref_exists(refs, ref.buf);
if (!found && ctx.repo->enable_remote_branches) {
strbuf_reset(&ref);
strbuf_addf(&ref, "refs/remotes/%s", ctx.qry.head);
found = refs_ref_exists(refs, ref.buf);
}
strbuf_release(&ref);
return found;
}
static void print_detached_head_option(void)
{
const char *head = ctx.qry.head;
struct object_id oid;
struct strbuf label = STRBUF_INIT;
// A head that is a full id reads better cut to the length the pinned
// entry uses.
if (strlen(head) == the_hash_algo->hexsz && !get_oid_hex(head, &oid))
strbuf_addf(&label, "%.*s (detached)", SHORT_HASH_LEN, head);
else
strbuf_addf(&label, "%s (detached)", head);
html_option(head, label.buf, head);
strbuf_release(&label);
}
static int print_branch_option(const struct reference *ref, void *data)
{
struct branch_option_data *opt = data;
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 && opt->count >= ctx.cfg.max_ref_count)
return -1;
opt->count++;
html_option(name, name, opt->selected);
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) {
const struct object_id *pinned = pinned_oid();
// Only one option may carry selected, and a pinned commit
// outranks the branch it was reached from.
struct branch_option_data opt = {
.selected = pinned ? NULL : ctx.qry.head,
};
char *oid = ctx.qry.oid, *oid2 = ctx.qry.oid2;
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("
\n");
free(fallback);
}
void cgit_add_hidden_formfields(int incl_head, int incl_search,
const char *page)
{
if (!ctx.cfg.virtual_root) {
struct strbuf url = STRBUF_INIT;
strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
if (ctx.qry.vpath)
strbuf_addf(&url, "/%s", ctx.qry.vpath);
html_hidden("url", url.buf);
strbuf_release(&url);
}
if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
strcmp(ctx.qry.head, ctx.repo->defbranch))
html_hidden("h", ctx.qry.head);
if (ctx.qry.oid)
html_hidden("id", ctx.qry.oid);
if (ctx.qry.oid2)
html_hidden("id2", ctx.qry.oid2);
if (ctx.qry.showmsg)
html_hidden("showmsg", "1");
if (incl_search) {
if (ctx.qry.grep)
html_hidden("qt", ctx.qry.grep);
if (ctx.qry.search)
html_hidden("q", ctx.qry.search);
}
}
void cgit_print_pageheader(void)
{
const struct object_id *pinned;
html("
\n");
if (!ctx.env.authenticated || ctx.cfg.enable_header)
print_header();
if (ctx.env.authenticated)
html("\n");
pinned = ctx.env.authenticated && ctx.repo ? pinned_oid() : NULL;
// The commit and diff pages name the commit they show as their own
// content, so the rev crumb would only repeat them.
if (pinned && ctx.qry.page && (!strcmp(ctx.qry.page, "commit") ||
!strcmp(ctx.qry.page, "diff")))
pinned = NULL;
if ((ctx.env.authenticated && ctx.repo && ctx.qry.vpath) || pinned) {
html("\n");
}
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.
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, " < ");
last_slash = slash;
}
strbuf_add(&sb, path, last_slash - path);
strbuf_addf(&sb, " - %s", ctx.page.title);
ctx.page.title = strbuf_detach(&sb, NULL);
}