all repos — cgit @ d31be4ccc2f978edd2a40c2721e1efdc1eee2343

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