all repos — cgit @ c1cd290d1f83d3d1c2d081d734e8d213f12cc06b

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