all repos — cgit @ v1.2

a hyperfast web frontend for git written in c

ui-shared.c (view raw)

   1/* ui-shared.c: common web output functions
   2 *
   3 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
   4 *
   5 * Licensed under GNU General Public License v2
   6 *   (see COPYING for full license text)
   7 */
   8
   9#include "cgit.h"
  10#include "ui-shared.h"
  11#include "cmd.h"
  12#include "html.h"
  13#include "version.h"
  14
  15static const char cgit_doctype[] =
  16"<!DOCTYPE html>\n";
  17
  18static char *http_date(time_t t)
  19{
  20	static char day[][4] =
  21		{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  22	static char month[][4] =
  23		{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  24		 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  25	struct tm *tm = gmtime(&t);
  26	return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
  27		   tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
  28		   tm->tm_hour, tm->tm_min, tm->tm_sec);
  29}
  30
  31void cgit_print_error(const char *fmt, ...)
  32{
  33	va_list ap;
  34	va_start(ap, fmt);
  35	cgit_vprint_error(fmt, ap);
  36	va_end(ap);
  37}
  38
  39void cgit_vprint_error(const char *fmt, va_list ap)
  40{
  41	va_list cp;
  42	html("<div class='error'>");
  43	va_copy(cp, ap);
  44	html_vtxtf(fmt, cp);
  45	va_end(cp);
  46	html("</div>\n");
  47}
  48
  49const char *cgit_httpscheme(void)
  50{
  51	if (ctx.env.https && !strcmp(ctx.env.https, "on"))
  52		return "https://";
  53	else
  54		return "http://";
  55}
  56
  57char *cgit_hosturl(void)
  58{
  59	if (ctx.env.http_host)
  60		return xstrdup(ctx.env.http_host);
  61	if (!ctx.env.server_name)
  62		return NULL;
  63	if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
  64		return xstrdup(ctx.env.server_name);
  65	return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
  66}
  67
  68char *cgit_currenturl(void)
  69{
  70	const char *root = cgit_rooturl();
  71	size_t len = strlen(root);
  72
  73	if (!ctx.qry.url)
  74		return xstrdup(root);
  75	if (len && root[len - 1] == '/')
  76		return fmtalloc("%s%s", root, ctx.qry.url);
  77	return fmtalloc("%s/%s", root, ctx.qry.url);
  78}
  79
  80const char *cgit_rooturl(void)
  81{
  82	if (ctx.cfg.virtual_root)
  83		return ctx.cfg.virtual_root;
  84	else
  85		return ctx.cfg.script_name;
  86}
  87
  88const char *cgit_loginurl(void)
  89{
  90	static const char *login_url;
  91	if (!login_url)
  92		login_url = fmtalloc("%s?p=login", cgit_rooturl());
  93	return login_url;
  94}
  95
  96char *cgit_repourl(const char *reponame)
  97{
  98	if (ctx.cfg.virtual_root)
  99		return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
 100	else
 101		return fmtalloc("?r=%s", reponame);
 102}
 103
 104char *cgit_fileurl(const char *reponame, const char *pagename,
 105		   const char *filename, const char *query)
 106{
 107	struct strbuf sb = STRBUF_INIT;
 108	char *delim;
 109
 110	if (ctx.cfg.virtual_root) {
 111		strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
 112			    pagename, (filename ? filename:""));
 113		delim = "?";
 114	} else {
 115		strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
 116			    (filename ? filename : ""));
 117		delim = "&amp;";
 118	}
 119	if (query)
 120		strbuf_addf(&sb, "%s%s", delim, query);
 121	return strbuf_detach(&sb, NULL);
 122}
 123
 124char *cgit_pageurl(const char *reponame, const char *pagename,
 125		   const char *query)
 126{
 127	return cgit_fileurl(reponame, pagename, NULL, query);
 128}
 129
 130const char *cgit_repobasename(const char *reponame)
 131{
 132	/* I assume we don't need to store more than one repo basename */
 133	static char rvbuf[1024];
 134	int p;
 135	const char *rv;
 136	size_t len;
 137
 138	len = strlcpy(rvbuf, reponame, sizeof(rvbuf));
 139	if (len >= sizeof(rvbuf))
 140		die("cgit_repobasename: truncated repository name '%s'", reponame);
 141	p = len - 1;
 142	/* strip trailing slashes */
 143	while (p && rvbuf[p] == '/')
 144		rvbuf[p--] = '\0';
 145	/* strip trailing .git */
 146	if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
 147		p -= 3;
 148		rvbuf[p--] = '\0';
 149	}
 150	/* strip more trailing slashes if any */
 151	while (p && rvbuf[p] == '/')
 152		rvbuf[p--] = '\0';
 153	/* find last slash in the remaining string */
 154	rv = strrchr(rvbuf, '/');
 155	if (rv)
 156		return ++rv;
 157	return rvbuf;
 158}
 159
 160const char *cgit_snapshot_prefix(const struct cgit_repo *repo)
 161{
 162	if (repo->snapshot_prefix)
 163		return repo->snapshot_prefix;
 164
 165	return cgit_repobasename(repo->url);
 166}
 167
 168static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
 169{
 170	char *delim = "?";
 171
 172	if (always_root || page)
 173		html_attr(cgit_rooturl());
 174	else {
 175		char *currenturl = cgit_currenturl();
 176		html_attr(currenturl);
 177		free(currenturl);
 178	}
 179
 180	if (page) {
 181		htmlf("?p=%s", page);
 182		delim = "&amp;";
 183	}
 184	if (search) {
 185		html(delim);
 186		html("q=");
 187		html_attr(search);
 188		delim = "&amp;";
 189	}
 190	if (sort) {
 191		html(delim);
 192		html("s=");
 193		html_attr(sort);
 194		delim = "&amp;";
 195	}
 196	if (ofs) {
 197		html(delim);
 198		htmlf("ofs=%d", ofs);
 199	}
 200}
 201
 202static void site_link(const char *page, const char *name, const char *title,
 203		      const char *class, const char *search, const char *sort, int ofs, int always_root)
 204{
 205	html("<a");
 206	if (title) {
 207		html(" title='");
 208		html_attr(title);
 209		html("'");
 210	}
 211	if (class) {
 212		html(" class='");
 213		html_attr(class);
 214		html("'");
 215	}
 216	html(" href='");
 217	site_url(page, search, sort, ofs, always_root);
 218	html("'>");
 219	html_txt(name);
 220	html("</a>");
 221}
 222
 223void cgit_index_link(const char *name, const char *title, const char *class,
 224		     const char *pattern, const char *sort, int ofs, int always_root)
 225{
 226	site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
 227}
 228
 229static char *repolink(const char *title, const char *class, const char *page,
 230		      const char *head, const char *path)
 231{
 232	char *delim = "?";
 233
 234	html("<a");
 235	if (title) {
 236		html(" title='");
 237		html_attr(title);
 238		html("'");
 239	}
 240	if (class) {
 241		html(" class='");
 242		html_attr(class);
 243		html("'");
 244	}
 245	html(" href='");
 246	if (ctx.cfg.virtual_root) {
 247		html_url_path(ctx.cfg.virtual_root);
 248		html_url_path(ctx.repo->url);
 249		if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
 250			html("/");
 251		if (page) {
 252			html_url_path(page);
 253			html("/");
 254			if (path)
 255				html_url_path(path);
 256		}
 257	} else {
 258		html_url_path(ctx.cfg.script_name);
 259		html("?url=");
 260		html_url_arg(ctx.repo->url);
 261		if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
 262			html("/");
 263		if (page) {
 264			html_url_arg(page);
 265			html("/");
 266			if (path)
 267				html_url_arg(path);
 268		}
 269		delim = "&amp;";
 270	}
 271	if (head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch)) {
 272		html(delim);
 273		html("h=");
 274		html_url_arg(head);
 275		delim = "&amp;";
 276	}
 277	return fmt("%s", delim);
 278}
 279
 280static void reporevlink(const char *page, const char *name, const char *title,
 281			const char *class, const char *head, const char *rev,
 282			const char *path)
 283{
 284	char *delim;
 285
 286	delim = repolink(title, class, page, head, path);
 287	if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
 288		html(delim);
 289		html("id=");
 290		html_url_arg(rev);
 291	}
 292	html("'>");
 293	html_txt(name);
 294	html("</a>");
 295}
 296
 297void cgit_summary_link(const char *name, const char *title, const char *class,
 298		       const char *head)
 299{
 300	reporevlink(NULL, name, title, class, head, NULL, NULL);
 301}
 302
 303void cgit_tag_link(const char *name, const char *title, const char *class,
 304		   const char *tag)
 305{
 306	reporevlink("tag", name, title, class, tag, NULL, NULL);
 307}
 308
 309void cgit_tree_link(const char *name, const char *title, const char *class,
 310		    const char *head, const char *rev, const char *path)
 311{
 312	reporevlink("tree", name, title, class, head, rev, path);
 313}
 314
 315void cgit_plain_link(const char *name, const char *title, const char *class,
 316		     const char *head, const char *rev, const char *path)
 317{
 318	reporevlink("plain", name, title, class, head, rev, path);
 319}
 320
 321void cgit_blame_link(const char *name, const char *title, const char *class,
 322		     const char *head, const char *rev, const char *path)
 323{
 324	reporevlink("blame", name, title, class, head, rev, path);
 325}
 326
 327void cgit_log_link(const char *name, const char *title, const char *class,
 328		   const char *head, const char *rev, const char *path,
 329		   int ofs, const char *grep, const char *pattern, int showmsg,
 330		   int follow)
 331{
 332	char *delim;
 333
 334	delim = repolink(title, class, "log", head, path);
 335	if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
 336		html(delim);
 337		html("id=");
 338		html_url_arg(rev);
 339		delim = "&amp;";
 340	}
 341	if (grep && pattern) {
 342		html(delim);
 343		html("qt=");
 344		html_url_arg(grep);
 345		delim = "&amp;";
 346		html(delim);
 347		html("q=");
 348		html_url_arg(pattern);
 349	}
 350	if (ofs > 0) {
 351		html(delim);
 352		html("ofs=");
 353		htmlf("%d", ofs);
 354		delim = "&amp;";
 355	}
 356	if (showmsg) {
 357		html(delim);
 358		html("showmsg=1");
 359		delim = "&amp;";
 360	}
 361	if (follow) {
 362		html(delim);
 363		html("follow=1");
 364	}
 365	html("'>");
 366	html_txt(name);
 367	html("</a>");
 368}
 369
 370void cgit_commit_link(const char *name, const char *title, const char *class,
 371		      const char *head, const char *rev, const char *path)
 372{
 373	char *delim;
 374
 375	delim = repolink(title, class, "commit", head, path);
 376	if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
 377		html(delim);
 378		html("id=");
 379		html_url_arg(rev);
 380		delim = "&amp;";
 381	}
 382	if (ctx.qry.difftype) {
 383		html(delim);
 384		htmlf("dt=%d", ctx.qry.difftype);
 385		delim = "&amp;";
 386	}
 387	if (ctx.qry.context > 0 && ctx.qry.context != 3) {
 388		html(delim);
 389		html("context=");
 390		htmlf("%d", ctx.qry.context);
 391		delim = "&amp;";
 392	}
 393	if (ctx.qry.ignorews) {
 394		html(delim);
 395		html("ignorews=1");
 396		delim = "&amp;";
 397	}
 398	if (ctx.qry.follow) {
 399		html(delim);
 400		html("follow=1");
 401	}
 402	html("'>");
 403	if (name[0] != '\0') {
 404		if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
 405			html_ntxt(name, ctx.cfg.max_msg_len - 3);
 406			html("...");
 407		} else
 408			html_txt(name);
 409	} else
 410		html_txt("(no commit message)");
 411	html("</a>");
 412}
 413
 414void cgit_refs_link(const char *name, const char *title, const char *class,
 415		    const char *head, const char *rev, const char *path)
 416{
 417	reporevlink("refs", name, title, class, head, rev, path);
 418}
 419
 420void cgit_snapshot_link(const char *name, const char *title, const char *class,
 421			const char *head, const char *rev,
 422			const char *archivename)
 423{
 424	reporevlink("snapshot", name, title, class, head, rev, archivename);
 425}
 426
 427void cgit_diff_link(const char *name, const char *title, const char *class,
 428		    const char *head, const char *new_rev, const char *old_rev,
 429		    const char *path)
 430{
 431	char *delim;
 432
 433	delim = repolink(title, class, "diff", head, path);
 434	if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
 435		html(delim);
 436		html("id=");
 437		html_url_arg(new_rev);
 438		delim = "&amp;";
 439	}
 440	if (old_rev) {
 441		html(delim);
 442		html("id2=");
 443		html_url_arg(old_rev);
 444		delim = "&amp;";
 445	}
 446	if (ctx.qry.difftype) {
 447		html(delim);
 448		htmlf("dt=%d", ctx.qry.difftype);
 449		delim = "&amp;";
 450	}
 451	if (ctx.qry.context > 0 && ctx.qry.context != 3) {
 452		html(delim);
 453		html("context=");
 454		htmlf("%d", ctx.qry.context);
 455		delim = "&amp;";
 456	}
 457	if (ctx.qry.ignorews) {
 458		html(delim);
 459		html("ignorews=1");
 460		delim = "&amp;";
 461	}
 462	if (ctx.qry.follow) {
 463		html(delim);
 464		html("follow=1");
 465	}
 466	html("'>");
 467	html_txt(name);
 468	html("</a>");
 469}
 470
 471void cgit_patch_link(const char *name, const char *title, const char *class,
 472		     const char *head, const char *rev, const char *path)
 473{
 474	reporevlink("patch", name, title, class, head, rev, path);
 475}
 476
 477void cgit_stats_link(const char *name, const char *title, const char *class,
 478		     const char *head, const char *path)
 479{
 480	reporevlink("stats", name, title, class, head, NULL, path);
 481}
 482
 483static void cgit_self_link(char *name, const char *title, const char *class)
 484{
 485	if (!strcmp(ctx.qry.page, "repolist"))
 486		cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
 487				ctx.qry.ofs, 1);
 488	else if (!strcmp(ctx.qry.page, "summary"))
 489		cgit_summary_link(name, title, class, ctx.qry.head);
 490	else if (!strcmp(ctx.qry.page, "tag"))
 491		cgit_tag_link(name, title, class, ctx.qry.has_sha1 ?
 492			       ctx.qry.sha1 : ctx.qry.head);
 493	else if (!strcmp(ctx.qry.page, "tree"))
 494		cgit_tree_link(name, title, class, ctx.qry.head,
 495			       ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 496			       ctx.qry.path);
 497	else if (!strcmp(ctx.qry.page, "plain"))
 498		cgit_plain_link(name, title, class, ctx.qry.head,
 499				ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 500				ctx.qry.path);
 501	else if (!strcmp(ctx.qry.page, "blame"))
 502		cgit_blame_link(name, title, class, ctx.qry.head,
 503				ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 504				ctx.qry.path);
 505	else if (!strcmp(ctx.qry.page, "log"))
 506		cgit_log_link(name, title, class, ctx.qry.head,
 507			      ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 508			      ctx.qry.path, ctx.qry.ofs,
 509			      ctx.qry.grep, ctx.qry.search,
 510			      ctx.qry.showmsg, ctx.qry.follow);
 511	else if (!strcmp(ctx.qry.page, "commit"))
 512		cgit_commit_link(name, title, class, ctx.qry.head,
 513				 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 514				 ctx.qry.path);
 515	else if (!strcmp(ctx.qry.page, "patch"))
 516		cgit_patch_link(name, title, class, ctx.qry.head,
 517				ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 518				ctx.qry.path);
 519	else if (!strcmp(ctx.qry.page, "refs"))
 520		cgit_refs_link(name, title, class, ctx.qry.head,
 521			       ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 522			       ctx.qry.path);
 523	else if (!strcmp(ctx.qry.page, "snapshot"))
 524		cgit_snapshot_link(name, title, class, ctx.qry.head,
 525				   ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
 526				   ctx.qry.path);
 527	else if (!strcmp(ctx.qry.page, "diff"))
 528		cgit_diff_link(name, title, class, ctx.qry.head,
 529			       ctx.qry.sha1, ctx.qry.sha2,
 530			       ctx.qry.path);
 531	else if (!strcmp(ctx.qry.page, "stats"))
 532		cgit_stats_link(name, title, class, ctx.qry.head,
 533				ctx.qry.path);
 534	else {
 535		/* Don't known how to make link for this page */
 536		repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
 537		html("><!-- cgit_self_link() doesn't know how to make link for page '");
 538		html_txt(ctx.qry.page);
 539		html("' -->");
 540		html_txt(name);
 541		html("</a>");
 542	}
 543}
 544
 545void cgit_object_link(struct object *obj)
 546{
 547	char *page, *shortrev, *fullrev, *name;
 548
 549	fullrev = oid_to_hex(&obj->oid);
 550	shortrev = xstrdup(fullrev);
 551	shortrev[10] = '\0';
 552	if (obj->type == OBJ_COMMIT) {
 553		cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
 554				 ctx.qry.head, fullrev, NULL);
 555		return;
 556	} else if (obj->type == OBJ_TREE)
 557		page = "tree";
 558	else if (obj->type == OBJ_TAG)
 559		page = "tag";
 560	else
 561		page = "blob";
 562	name = fmt("%s %s...", type_name(obj->type), shortrev);
 563	reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
 564}
 565
 566static struct string_list_item *lookup_path(struct string_list *list,
 567					    const char *path)
 568{
 569	struct string_list_item *item;
 570
 571	while (path && path[0]) {
 572		if ((item = string_list_lookup(list, path)))
 573			return item;
 574		if (!(path = strchr(path, '/')))
 575			break;
 576		path++;
 577	}
 578	return NULL;
 579}
 580
 581void cgit_submodule_link(const char *class, char *path, const char *rev)
 582{
 583	struct string_list *list;
 584	struct string_list_item *item;
 585	char tail, *dir;
 586	size_t len;
 587
 588	len = 0;
 589	tail = 0;
 590	list = &ctx.repo->submodules;
 591	item = lookup_path(list, path);
 592	if (!item) {
 593		len = strlen(path);
 594		tail = path[len - 1];
 595		if (tail == '/') {
 596			path[len - 1] = 0;
 597			item = lookup_path(list, path);
 598		}
 599	}
 600	if (item || ctx.repo->module_link) {
 601		html("<a ");
 602		if (class)
 603			htmlf("class='%s' ", class);
 604		html("href='");
 605		if (item) {
 606			html_attrf(item->util, rev);
 607		} else {
 608			dir = strrchr(path, '/');
 609			if (dir)
 610				dir++;
 611			else
 612				dir = path;
 613			html_attrf(ctx.repo->module_link, dir, rev);
 614		}
 615		html("'>");
 616		html_txt(path);
 617		html("</a>");
 618	} else {
 619		html("<span");
 620		if (class)
 621			htmlf(" class='%s'", class);
 622		html(">");
 623		html_txt(path);
 624		html("</span>");
 625	}
 626	html_txtf(" @ %.7s", rev);
 627	if (item && tail)
 628		path[len - 1] = tail;
 629}
 630
 631const struct date_mode *cgit_date_mode(enum date_mode_type type)
 632{
 633	static struct date_mode mode;
 634	mode.type = type;
 635	mode.local = ctx.cfg.local_time;
 636	return &mode;
 637}
 638
 639static void print_rel_date(time_t t, int tz, double value,
 640	const char *class, const char *suffix)
 641{
 642	htmlf("<span class='%s' title='", class);
 643	html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
 644	htmlf("'>%.0f %s</span>", value, suffix);
 645}
 646
 647void cgit_print_age(time_t t, int tz, time_t max_relative)
 648{
 649	time_t now, secs;
 650
 651	if (!t)
 652		return;
 653	time(&now);
 654	secs = now - t;
 655	if (secs < 0)
 656		secs = 0;
 657
 658	if (secs > max_relative && max_relative >= 0) {
 659		html("<span title='");
 660		html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
 661		html("'>");
 662		html_txt(show_date(t, tz, cgit_date_mode(DATE_SHORT)));
 663		html("</span>");
 664		return;
 665	}
 666
 667	if (secs < TM_HOUR * 2) {
 668		print_rel_date(t, tz, secs * 1.0 / TM_MIN, "age-mins", "min.");
 669		return;
 670	}
 671	if (secs < TM_DAY * 2) {
 672		print_rel_date(t, tz, secs * 1.0 / TM_HOUR, "age-hours", "hours");
 673		return;
 674	}
 675	if (secs < TM_WEEK * 2) {
 676		print_rel_date(t, tz, secs * 1.0 / TM_DAY, "age-days", "days");
 677		return;
 678	}
 679	if (secs < TM_MONTH * 2) {
 680		print_rel_date(t, tz, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
 681		return;
 682	}
 683	if (secs < TM_YEAR * 2) {
 684		print_rel_date(t, tz, secs * 1.0 / TM_MONTH, "age-months", "months");
 685		return;
 686	}
 687	print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years");
 688}
 689
 690void cgit_print_http_headers(void)
 691{
 692	if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
 693		return;
 694
 695	if (ctx.page.status)
 696		htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
 697	if (ctx.page.mimetype && ctx.page.charset)
 698		htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
 699		      ctx.page.charset);
 700	else if (ctx.page.mimetype)
 701		htmlf("Content-Type: %s\n", ctx.page.mimetype);
 702	if (ctx.page.size)
 703		htmlf("Content-Length: %zd\n", ctx.page.size);
 704	if (ctx.page.filename) {
 705		html("Content-Disposition: inline; filename=\"");
 706		html_header_arg_in_quotes(ctx.page.filename);
 707		html("\"\n");
 708	}
 709	if (!ctx.env.authenticated)
 710		html("Cache-Control: no-cache, no-store\n");
 711	htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
 712	htmlf("Expires: %s\n", http_date(ctx.page.expires));
 713	if (ctx.page.etag)
 714		htmlf("ETag: \"%s\"\n", ctx.page.etag);
 715	html("\n");
 716	if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
 717		exit(0);
 718}
 719
 720void cgit_redirect(const char *url, bool permanent)
 721{
 722	htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
 723	html("Location: ");
 724	html_url_path(url);
 725	html("\n\n");
 726}
 727
 728static void print_rel_vcs_link(const char *url)
 729{
 730	html("<link rel='vcs-git' href='");
 731	html_attr(url);
 732	html("' title='");
 733	html_attr(ctx.repo->name);
 734	html(" Git repository'/>\n");
 735}
 736
 737void cgit_print_docstart(void)
 738{
 739	char *host = cgit_hosturl();
 740
 741	if (ctx.cfg.embedded) {
 742		if (ctx.cfg.header)
 743			html_include(ctx.cfg.header);
 744		return;
 745	}
 746
 747	html(cgit_doctype);
 748	html("<html lang='en'>\n");
 749	html("<head>\n");
 750	html("<title>");
 751	html_txt(ctx.page.title);
 752	html("</title>\n");
 753	htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
 754	if (ctx.cfg.robots && *ctx.cfg.robots)
 755		htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
 756	html("<link rel='stylesheet' type='text/css' href='");
 757	html_attr(ctx.cfg.css);
 758	html("'/>\n");
 759	if (ctx.cfg.favicon) {
 760		html("<link rel='shortcut icon' href='");
 761		html_attr(ctx.cfg.favicon);
 762		html("'/>\n");
 763	}
 764	if (host && ctx.repo && ctx.qry.head) {
 765		char *fileurl;
 766		struct strbuf sb = STRBUF_INIT;
 767		strbuf_addf(&sb, "h=%s", ctx.qry.head);
 768
 769		html("<link rel='alternate' title='Atom feed' href='");
 770		html(cgit_httpscheme());
 771		html_attr(host);
 772		fileurl = cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
 773				       sb.buf);
 774		html_attr(fileurl);
 775		html("' type='application/atom+xml'/>\n");
 776		strbuf_release(&sb);
 777		free(fileurl);
 778	}
 779	if (ctx.repo)
 780		cgit_add_clone_urls(print_rel_vcs_link);
 781	if (ctx.cfg.head_include)
 782		html_include(ctx.cfg.head_include);
 783	if (ctx.repo && ctx.repo->extra_head_content)
 784		html(ctx.repo->extra_head_content);
 785	html("</head>\n");
 786	html("<body>\n");
 787	if (ctx.cfg.header)
 788		html_include(ctx.cfg.header);
 789	free(host);
 790}
 791
 792void cgit_print_docend(void)
 793{
 794	html("</div> <!-- class=content -->\n");
 795	if (ctx.cfg.embedded) {
 796		html("</div> <!-- id=cgit -->\n");
 797		if (ctx.cfg.footer)
 798			html_include(ctx.cfg.footer);
 799		return;
 800	}
 801	if (ctx.cfg.footer)
 802		html_include(ctx.cfg.footer);
 803	else {
 804		htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
 805			"(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version, git_version_string);
 806		html_txt(show_date(time(NULL), 0, cgit_date_mode(DATE_ISO8601)));
 807		html("</div>\n");
 808	}
 809	html("</div> <!-- id=cgit -->\n");
 810	html("</body>\n</html>\n");
 811}
 812
 813void cgit_print_error_page(int code, const char *msg, const char *fmt, ...)
 814{
 815	va_list ap;
 816	ctx.page.expires = ctx.cfg.cache_dynamic_ttl;
 817	ctx.page.status = code;
 818	ctx.page.statusmsg = msg;
 819	cgit_print_layout_start();
 820	va_start(ap, fmt);
 821	cgit_vprint_error(fmt, ap);
 822	va_end(ap);
 823	cgit_print_layout_end();
 824}
 825
 826void cgit_print_layout_start(void)
 827{
 828	cgit_print_http_headers();
 829	cgit_print_docstart();
 830	cgit_print_pageheader();
 831}
 832
 833void cgit_print_layout_end(void)
 834{
 835	cgit_print_docend();
 836}
 837
 838static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
 839{
 840	struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
 841	int i;
 842
 843	for (i = 0; url_list[i]; i++) {
 844		strbuf_rtrim(url_list[i]);
 845		if (url_list[i]->len == 0)
 846			continue;
 847		if (suffix && *suffix)
 848			strbuf_addf(url_list[i], "/%s", suffix);
 849		fn(url_list[i]->buf);
 850	}
 851
 852	strbuf_list_free(url_list);
 853}
 854
 855void cgit_add_clone_urls(void (*fn)(const char *))
 856{
 857	if (ctx.repo->clone_url)
 858		add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
 859	else if (ctx.cfg.clone_prefix)
 860		add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
 861}
 862
 863static int print_branch_option(const char *refname, const struct object_id *oid,
 864			       int flags, void *cb_data)
 865{
 866	char *name = (char *)refname;
 867	html_option(name, name, ctx.qry.head);
 868	return 0;
 869}
 870
 871void cgit_add_hidden_formfields(int incl_head, int incl_search,
 872				const char *page)
 873{
 874	if (!ctx.cfg.virtual_root) {
 875		struct strbuf url = STRBUF_INIT;
 876
 877		strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
 878		if (ctx.qry.vpath)
 879			strbuf_addf(&url, "/%s", ctx.qry.vpath);
 880		html_hidden("url", url.buf);
 881		strbuf_release(&url);
 882	}
 883
 884	if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
 885	    strcmp(ctx.qry.head, ctx.repo->defbranch))
 886		html_hidden("h", ctx.qry.head);
 887
 888	if (ctx.qry.sha1)
 889		html_hidden("id", ctx.qry.sha1);
 890	if (ctx.qry.sha2)
 891		html_hidden("id2", ctx.qry.sha2);
 892	if (ctx.qry.showmsg)
 893		html_hidden("showmsg", "1");
 894
 895	if (incl_search) {
 896		if (ctx.qry.grep)
 897			html_hidden("qt", ctx.qry.grep);
 898		if (ctx.qry.search)
 899			html_hidden("q", ctx.qry.search);
 900	}
 901}
 902
 903static const char *hc(const char *page)
 904{
 905	if (!ctx.qry.page)
 906		return NULL;
 907
 908	return strcmp(ctx.qry.page, page) ? NULL : "active";
 909}
 910
 911static void cgit_print_path_crumbs(char *path)
 912{
 913	char *old_path = ctx.qry.path;
 914	char *p = path, *q, *end = path + strlen(path);
 915
 916	ctx.qry.path = NULL;
 917	cgit_self_link("root", NULL, NULL);
 918	ctx.qry.path = p = path;
 919	while (p < end) {
 920		if (!(q = strchr(p, '/')))
 921			q = end;
 922		*q = '\0';
 923		html_txt("/");
 924		cgit_self_link(p, NULL, NULL);
 925		if (q < end)
 926			*q = '/';
 927		p = q + 1;
 928	}
 929	ctx.qry.path = old_path;
 930}
 931
 932static void print_header(void)
 933{
 934	char *logo = NULL, *logo_link = NULL;
 935
 936	html("<table id='header'>\n");
 937	html("<tr>\n");
 938
 939	if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
 940		logo = ctx.repo->logo;
 941	else
 942		logo = ctx.cfg.logo;
 943	if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
 944		logo_link = ctx.repo->logo_link;
 945	else
 946		logo_link = ctx.cfg.logo_link;
 947	if (logo && *logo) {
 948		html("<td class='logo' rowspan='2'><a href='");
 949		if (logo_link && *logo_link)
 950			html_attr(logo_link);
 951		else
 952			html_attr(cgit_rooturl());
 953		html("'><img src='");
 954		html_attr(logo);
 955		html("' alt='cgit logo'/></a></td>\n");
 956	}
 957
 958	html("<td class='main'>");
 959	if (ctx.repo) {
 960		cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
 961		html(" : ");
 962		cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
 963		if (ctx.env.authenticated) {
 964			html("</td><td class='form'>");
 965			html("<form method='get'>\n");
 966			cgit_add_hidden_formfields(0, 1, ctx.qry.page);
 967			html("<select name='h' onchange='this.form.submit();'>\n");
 968			for_each_branch_ref(print_branch_option, ctx.qry.head);
 969			if (ctx.repo->enable_remote_branches)
 970				for_each_remote_ref(print_branch_option, ctx.qry.head);
 971			html("</select> ");
 972			html("<input type='submit' value='switch'/>");
 973			html("</form>");
 974		}
 975	} else
 976		html_txt(ctx.cfg.root_title);
 977	html("</td></tr>\n");
 978
 979	html("<tr><td class='sub'>");
 980	if (ctx.repo) {
 981		html_txt(ctx.repo->desc);
 982		html("</td><td class='sub right'>");
 983		html_txt(ctx.repo->owner);
 984	} else {
 985		if (ctx.cfg.root_desc)
 986			html_txt(ctx.cfg.root_desc);
 987	}
 988	html("</td></tr></table>\n");
 989}
 990
 991void cgit_print_pageheader(void)
 992{
 993	html("<div id='cgit'>");
 994	if (!ctx.env.authenticated || !ctx.cfg.noheader)
 995		print_header();
 996
 997	html("<table class='tabs'><tr><td>\n");
 998	if (ctx.env.authenticated && ctx.repo) {
 999		if (ctx.repo->readme.nr)
1000			reporevlink("about", "about", NULL,
1001				    hc("about"), ctx.qry.head, NULL,
1002				    NULL);
1003		cgit_summary_link("summary", NULL, hc("summary"),
1004				  ctx.qry.head);
1005		cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
1006			       ctx.qry.sha1, NULL);
1007		cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
1008			      NULL, ctx.qry.vpath, 0, NULL, NULL,
1009			      ctx.qry.showmsg, ctx.qry.follow);
1010		if (ctx.qry.page && !strcmp(ctx.qry.page, "blame"))
1011			cgit_blame_link("blame", NULL, hc("blame"), ctx.qry.head,
1012				        ctx.qry.sha1, ctx.qry.vpath);
1013		else
1014			cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
1015				       ctx.qry.sha1, ctx.qry.vpath);
1016		cgit_commit_link("commit", NULL, hc("commit"),
1017				 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath);
1018		cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
1019			       ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath);
1020		if (ctx.repo->max_stats)
1021			cgit_stats_link("stats", NULL, hc("stats"),
1022					ctx.qry.head, ctx.qry.vpath);
1023		if (ctx.repo->homepage) {
1024			html("<a href='");
1025			html_attr(ctx.repo->homepage);
1026			html("'>homepage</a>");
1027		}
1028		html("</td><td class='form'>");
1029		html("<form class='right' method='get' action='");
1030		if (ctx.cfg.virtual_root) {
1031			char *fileurl = cgit_fileurl(ctx.qry.repo, "log",
1032						   ctx.qry.vpath, NULL);
1033			html_url_path(fileurl);
1034			free(fileurl);
1035		}
1036		html("'>\n");
1037		cgit_add_hidden_formfields(1, 0, "log");
1038		html("<select name='qt'>\n");
1039		html_option("grep", "log msg", ctx.qry.grep);
1040		html_option("author", "author", ctx.qry.grep);
1041		html_option("committer", "committer", ctx.qry.grep);
1042		html_option("range", "range", ctx.qry.grep);
1043		html("</select>\n");
1044		html("<input class='txt' type='search' size='10' name='q' value='");
1045		html_attr(ctx.qry.search);
1046		html("'/>\n");
1047		html("<input type='submit' value='search'/>\n");
1048		html("</form>\n");
1049	} else if (ctx.env.authenticated) {
1050		char *currenturl = cgit_currenturl();
1051		site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
1052		if (ctx.cfg.root_readme)
1053			site_link("about", "about", NULL, hc("about"),
1054				  NULL, NULL, 0, 1);
1055		html("</td><td class='form'>");
1056		html("<form method='get' action='");
1057		html_attr(currenturl);
1058		html("'>\n");
1059		html("<input type='search' name='q' size='10' value='");
1060		html_attr(ctx.qry.search);
1061		html("'/>\n");
1062		html("<input type='submit' value='search'/>\n");
1063		html("</form>");
1064		free(currenturl);
1065	}
1066	html("</td></tr></table>\n");
1067	if (ctx.env.authenticated && ctx.repo && ctx.qry.vpath) {
1068		html("<div class='path'>");
1069		html("path: ");
1070		cgit_print_path_crumbs(ctx.qry.vpath);
1071		if (ctx.cfg.enable_follow_links && !strcmp(ctx.qry.page, "log")) {
1072			html(" (");
1073			ctx.qry.follow = !ctx.qry.follow;
1074			cgit_self_link(ctx.qry.follow ? "follow" : "unfollow",
1075					NULL, NULL);
1076			ctx.qry.follow = !ctx.qry.follow;
1077			html(")");
1078		}
1079		html("</div>");
1080	}
1081	html("<div class='content'>");
1082}
1083
1084void cgit_print_filemode(unsigned short mode)
1085{
1086	if (S_ISDIR(mode))
1087		html("d");
1088	else if (S_ISLNK(mode))
1089		html("l");
1090	else if (S_ISGITLINK(mode))
1091		html("m");
1092	else
1093		html("-");
1094	html_fileperm(mode >> 6);
1095	html_fileperm(mode >> 3);
1096	html_fileperm(mode);
1097}
1098
1099void cgit_compose_snapshot_prefix(struct strbuf *filename, const char *base,
1100				  const char *ref)
1101{
1102	struct object_id oid;
1103
1104	/*
1105	 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1106	 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1107	 * i.e. each stripped tag can be inverted without ambiguities.
1108	 */
1109	if (get_oid(fmt("refs/tags/%s", ref), &oid) == 0 &&
1110	    (ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]) &&
1111	    ((get_oid(fmt("refs/tags/%s", ref + 1), &oid) == 0) +
1112	     (get_oid(fmt("refs/tags/v%s", ref + 1), &oid) == 0) +
1113	     (get_oid(fmt("refs/tags/V%s", ref + 1), &oid) == 0) == 1))
1114		ref++;
1115
1116	strbuf_addf(filename, "%s-%s", base, ref);
1117}
1118
1119void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
1120			       const char *separator)
1121{
1122	const struct cgit_snapshot_format *f;
1123	struct strbuf filename = STRBUF_INIT;
1124	const char *basename;
1125	size_t prefixlen;
1126
1127	basename = cgit_snapshot_prefix(repo);
1128	if (starts_with(ref, basename))
1129		strbuf_addstr(&filename, ref);
1130	else
1131		cgit_compose_snapshot_prefix(&filename, basename, ref);
1132
1133	prefixlen = filename.len;
1134	for (f = cgit_snapshot_formats; f->suffix; f++) {
1135		if (!(repo->snapshots & cgit_snapshot_format_bit(f)))
1136			continue;
1137		strbuf_setlen(&filename, prefixlen);
1138		strbuf_addstr(&filename, f->suffix);
1139		cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1140				   filename.buf);
1141		if (cgit_snapshot_get_sig(ref, f)) {
1142			strbuf_addstr(&filename, ".asc");
1143			html(" (");
1144			cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1145					   filename.buf);
1146			html(")");
1147		} else if (starts_with(f->suffix, ".tar") && cgit_snapshot_get_sig(ref, &cgit_snapshot_formats[0])) {
1148			strbuf_setlen(&filename, strlen(filename.buf) - strlen(f->suffix));
1149			strbuf_addstr(&filename, ".tar.asc");
1150			html(" (");
1151			cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1152					   filename.buf);
1153			html(")");
1154		}
1155		html(separator);
1156	}
1157	strbuf_release(&filename);
1158}
1159
1160void cgit_set_title_from_path(const char *path)
1161{
1162	size_t path_len, path_index, path_last_end;
1163	char *new_title;
1164
1165	if (!path)
1166		return;
1167
1168	path_len = strlen(path);
1169	new_title = xmalloc(path_len + 3 + strlen(ctx.page.title) + 1);
1170	new_title[0] = '\0';
1171
1172	for (path_index = path_len, path_last_end = path_len; path_index-- > 0;) {
1173		if (path[path_index] == '/') {
1174			if (path_index == path_len - 1) {
1175				path_last_end = path_index - 1;
1176				continue;
1177			}
1178			strncat(new_title, &path[path_index + 1], path_last_end - path_index - 1);
1179			strcat(new_title, "\\");
1180			path_last_end = path_index;
1181		}
1182	}
1183	if (path_last_end)
1184		strncat(new_title, path, path_last_end);
1185
1186	strcat(new_title, " - ");
1187	strcat(new_title, ctx.page.title);
1188	ctx.page.title = new_title;
1189}