all repos — cgit @ 6952f164c10689e3c112dbef85e461a6246654be

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 && !prefixcmp(&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		      int toggle_ssdiff)
333{
334	if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
335		name[ctx.cfg.max_msg_len] = '\0';
336		name[ctx.cfg.max_msg_len - 1] = '.';
337		name[ctx.cfg.max_msg_len - 2] = '.';
338		name[ctx.cfg.max_msg_len - 3] = '.';
339	}
340
341	char *delim;
342
343	delim = repolink(title, class, "commit", head, path);
344	if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
345		html(delim);
346		html("id=");
347		html_url_arg(rev);
348		delim = "&amp;";
349	}
350	if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
351		html(delim);
352		html("ss=1");
353		delim = "&amp;";
354	}
355	if (ctx.qry.context > 0 && ctx.qry.context != 3) {
356		html(delim);
357		html("context=");
358		htmlf("%d", ctx.qry.context);
359		delim = "&amp;";
360	}
361	if (ctx.qry.ignorews) {
362		html(delim);
363		html("ignorews=1");
364		delim = "&amp;";
365	}
366	html("'>");
367	if (name[0] != '\0')
368		html_txt(name);
369	else
370		html_txt("(no commit message)");
371	html("</a>");
372}
373
374void cgit_refs_link(const char *name, const char *title, const char *class,
375		    const char *head, const char *rev, const char *path)
376{
377	reporevlink("refs", name, title, class, head, rev, path);
378}
379
380void cgit_snapshot_link(const char *name, const char *title, const char *class,
381			const char *head, const char *rev,
382			const char *archivename)
383{
384	reporevlink("snapshot", name, title, class, head, rev, archivename);
385}
386
387void cgit_diff_link(const char *name, const char *title, const char *class,
388		    const char *head, const char *new_rev, const char *old_rev,
389		    const char *path, int toggle_ssdiff)
390{
391	char *delim;
392
393	delim = repolink(title, class, "diff", head, path);
394	if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
395		html(delim);
396		html("id=");
397		html_url_arg(new_rev);
398		delim = "&amp;";
399	}
400	if (old_rev) {
401		html(delim);
402		html("id2=");
403		html_url_arg(old_rev);
404		delim = "&amp;";
405	}
406	if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
407		html(delim);
408		html("ss=1");
409		delim = "&amp;";
410	}
411	if (ctx.qry.context > 0 && ctx.qry.context != 3) {
412		html(delim);
413		html("context=");
414		htmlf("%d", ctx.qry.context);
415		delim = "&amp;";
416	}
417	if (ctx.qry.ignorews) {
418		html(delim);
419		html("ignorews=1");
420		delim = "&amp;";
421	}
422	html("'>");
423	html_txt(name);
424	html("</a>");
425}
426
427void cgit_patch_link(const char *name, const char *title, const char *class,
428		     const char *head, const char *rev, const char *path)
429{
430	reporevlink("patch", name, title, class, head, rev, path);
431}
432
433void cgit_stats_link(const char *name, const char *title, const char *class,
434		     const char *head, const char *path)
435{
436	reporevlink("stats", name, title, class, head, NULL, path);
437}
438
439static void cgit_self_link(char *name, const char *title, const char *class)
440{
441	if (!strcmp(ctx.qry.page, "repolist"))
442		cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
443				ctx.qry.ofs);
444	else if (!strcmp(ctx.qry.page, "summary"))
445		cgit_summary_link(name, title, class, ctx.qry.head);
446	else if (!strcmp(ctx.qry.page, "tag"))
447		cgit_tag_link(name, title, class, ctx.qry.head,
448			      ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL);
449	else if (!strcmp(ctx.qry.page, "tree"))
450		cgit_tree_link(name, title, class, ctx.qry.head,
451			       ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
452			       ctx.qry.path);
453	else if (!strcmp(ctx.qry.page, "plain"))
454		cgit_plain_link(name, title, class, ctx.qry.head,
455				ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
456				ctx.qry.path);
457	else if (!strcmp(ctx.qry.page, "log"))
458		cgit_log_link(name, title, class, ctx.qry.head,
459			      ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
460			      ctx.qry.path, ctx.qry.ofs,
461			      ctx.qry.grep, ctx.qry.search,
462			      ctx.qry.showmsg);
463	else if (!strcmp(ctx.qry.page, "commit"))
464		cgit_commit_link(name, title, class, ctx.qry.head,
465				 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
466				 ctx.qry.path, 0);
467	else if (!strcmp(ctx.qry.page, "patch"))
468		cgit_patch_link(name, title, class, ctx.qry.head,
469				ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
470				ctx.qry.path);
471	else if (!strcmp(ctx.qry.page, "refs"))
472		cgit_refs_link(name, title, class, ctx.qry.head,
473			       ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
474			       ctx.qry.path);
475	else if (!strcmp(ctx.qry.page, "snapshot"))
476		cgit_snapshot_link(name, title, class, ctx.qry.head,
477				   ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
478				   ctx.qry.path);
479	else if (!strcmp(ctx.qry.page, "diff"))
480		cgit_diff_link(name, title, class, ctx.qry.head,
481			       ctx.qry.sha1, ctx.qry.sha2,
482			       ctx.qry.path, 0);
483	else if (!strcmp(ctx.qry.page, "stats"))
484		cgit_stats_link(name, title, class, ctx.qry.head,
485				ctx.qry.path);
486	else {
487		/* Don't known how to make link for this page */
488		repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
489		html("><!-- cgit_self_link() doesn't know how to make link for page '");
490		html_txt(ctx.qry.page);
491		html("' -->");
492		html_txt(name);
493		html("</a>");
494	}
495}
496
497void cgit_object_link(struct object *obj)
498{
499	char *page, *shortrev, *fullrev, *name;
500
501	fullrev = sha1_to_hex(obj->sha1);
502	shortrev = xstrdup(fullrev);
503	shortrev[10] = '\0';
504	if (obj->type == OBJ_COMMIT) {
505		cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
506				 ctx.qry.head, fullrev, NULL, 0);
507		return;
508	} else if (obj->type == OBJ_TREE)
509		page = "tree";
510	else if (obj->type == OBJ_TAG)
511		page = "tag";
512	else
513		page = "blob";
514	name = fmt("%s %s...", typename(obj->type), shortrev);
515	reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
516}
517
518static struct string_list_item *lookup_path(struct string_list *list,
519					    const char *path)
520{
521	struct string_list_item *item;
522
523	while (path && path[0]) {
524		if ((item = string_list_lookup(list, path)))
525			return item;
526		if (!(path = strchr(path, '/')))
527			break;
528		path++;
529	}
530	return NULL;
531}
532
533void cgit_submodule_link(const char *class, char *path, const char *rev)
534{
535	struct string_list *list;
536	struct string_list_item *item;
537	char tail, *dir;
538	size_t len;
539
540	len = 0;
541	tail = 0;
542	list = &ctx.repo->submodules;
543	item = lookup_path(list, path);
544	if (!item) {
545		len = strlen(path);
546		tail = path[len - 1];
547		if (tail == '/') {
548			path[len - 1] = 0;
549			item = lookup_path(list, path);
550		}
551	}
552	html("<a ");
553	if (class)
554		htmlf("class='%s' ", class);
555	html("href='");
556	if (item) {
557		html_attrf(item->util, rev);
558	} else if (ctx.repo->module_link) {
559		dir = strrchr(path, '/');
560		if (dir)
561			dir++;
562		else
563			dir = path;
564		html_attrf(ctx.repo->module_link, dir, rev);
565	} else {
566		html("#");
567	}
568	html("'>");
569	html_txt(path);
570	html("</a>");
571	html_txtf(" @ %.7s", rev);
572	if (item && tail)
573		path[len - 1] = tail;
574}
575
576void cgit_print_date(time_t secs, const char *format, int local_time)
577{
578	char buf[64];
579	struct tm *time;
580
581	if (!secs)
582		return;
583	if (local_time)
584		time = localtime(&secs);
585	else
586		time = gmtime(&secs);
587	strftime(buf, sizeof(buf)-1, format, time);
588	html_txt(buf);
589}
590
591void cgit_print_age(time_t t, time_t max_relative, const char *format)
592{
593	time_t now, secs;
594
595	if (!t)
596		return;
597	time(&now);
598	secs = now - t;
599	if (secs < 0)
600		secs = 0;
601
602	if (secs > max_relative && max_relative >= 0) {
603		cgit_print_date(t, format, ctx.cfg.local_time);
604		return;
605	}
606
607	if (secs < TM_HOUR * 2) {
608		htmlf("<span class='age-mins'>%.0f min.</span>",
609		      secs * 1.0 / TM_MIN);
610		return;
611	}
612	if (secs < TM_DAY * 2) {
613		htmlf("<span class='age-hours'>%.0f hours</span>",
614		      secs * 1.0 / TM_HOUR);
615		return;
616	}
617	if (secs < TM_WEEK * 2) {
618		htmlf("<span class='age-days'>%.0f days</span>",
619		      secs * 1.0 / TM_DAY);
620		return;
621	}
622	if (secs < TM_MONTH * 2) {
623		htmlf("<span class='age-weeks'>%.0f weeks</span>",
624		      secs * 1.0 / TM_WEEK);
625		return;
626	}
627	if (secs < TM_YEAR * 2) {
628		htmlf("<span class='age-months'>%.0f months</span>",
629		      secs * 1.0 / TM_MONTH);
630		return;
631	}
632	htmlf("<span class='age-years'>%.0f years</span>",
633	      secs * 1.0 / TM_YEAR);
634}
635
636void cgit_print_http_headers(void)
637{
638	if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
639		return;
640
641	if (ctx.page.status)
642		htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
643	if (ctx.page.mimetype && ctx.page.charset)
644		htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
645		      ctx.page.charset);
646	else if (ctx.page.mimetype)
647		htmlf("Content-Type: %s\n", ctx.page.mimetype);
648	if (ctx.page.size)
649		htmlf("Content-Length: %zd\n", ctx.page.size);
650	if (ctx.page.filename)
651		htmlf("Content-Disposition: inline; filename=\"%s\"\n",
652		      ctx.page.filename);
653	if (!ctx.env.authenticated)
654		html("Cache-Control: no-cache, no-store\n");
655	htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
656	htmlf("Expires: %s\n", http_date(ctx.page.expires));
657	if (ctx.page.etag)
658		htmlf("ETag: \"%s\"\n", ctx.page.etag);
659	html("\n");
660	if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
661		exit(0);
662}
663
664void cgit_print_docstart(void)
665{
666	if (ctx.cfg.embedded) {
667		if (ctx.cfg.header)
668			html_include(ctx.cfg.header);
669		return;
670	}
671
672	const char *host = cgit_hosturl();
673	html(cgit_doctype);
674	html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
675	html("<head>\n");
676	html("<title>");
677	html_txt(ctx.page.title);
678	html("</title>\n");
679	htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
680	if (ctx.cfg.robots && *ctx.cfg.robots)
681		htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
682	html("<link rel='stylesheet' type='text/css' href='");
683	html_attr(ctx.cfg.css);
684	html("'/>\n");
685	if (ctx.cfg.favicon) {
686		html("<link rel='shortcut icon' href='");
687		html_attr(ctx.cfg.favicon);
688		html("'/>\n");
689	}
690	if (host && ctx.repo && ctx.qry.head) {
691		struct strbuf sb = STRBUF_INIT;
692		strbuf_addf(&sb, "h=%s", ctx.qry.head);
693
694		html("<link rel='alternate' title='Atom feed' href='");
695		html(cgit_httpscheme());
696		html_attr(cgit_hosturl());
697		html_attr(cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
698				       sb.buf));
699		html("' type='application/atom+xml'/>\n");
700		strbuf_release(&sb);
701	}
702	if (ctx.cfg.head_include)
703		html_include(ctx.cfg.head_include);
704	html("</head>\n");
705	html("<body>\n");
706	if (ctx.cfg.header)
707		html_include(ctx.cfg.header);
708}
709
710void cgit_print_docend()
711{
712	html("</div> <!-- class=content -->\n");
713	if (ctx.cfg.embedded) {
714		html("</div> <!-- id=cgit -->\n");
715		if (ctx.cfg.footer)
716			html_include(ctx.cfg.footer);
717		return;
718	}
719	if (ctx.cfg.footer)
720		html_include(ctx.cfg.footer);
721	else {
722		htmlf("<div class='footer'>generated  by cgit %s at ",
723			cgit_version);
724		cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
725		html("</div>\n");
726	}
727	html("</div> <!-- id=cgit -->\n");
728	html("</body>\n</html>\n");
729}
730
731static int print_branch_option(const char *refname, const unsigned char *sha1,
732			       int flags, void *cb_data)
733{
734	char *name = (char *)refname;
735	html_option(name, name, ctx.qry.head);
736	return 0;
737}
738
739void cgit_add_hidden_formfields(int incl_head, int incl_search,
740				const char *page)
741{
742	if (!ctx.cfg.virtual_root) {
743		struct strbuf url = STRBUF_INIT;
744
745		strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
746		if (ctx.qry.vpath)
747			strbuf_addf(&url, "/%s", ctx.qry.vpath);
748		html_hidden("url", url.buf);
749		strbuf_release(&url);
750	}
751
752	if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
753	    strcmp(ctx.qry.head, ctx.repo->defbranch))
754		html_hidden("h", ctx.qry.head);
755
756	if (ctx.qry.sha1)
757		html_hidden("id", ctx.qry.sha1);
758	if (ctx.qry.sha2)
759		html_hidden("id2", ctx.qry.sha2);
760	if (ctx.qry.showmsg)
761		html_hidden("showmsg", "1");
762
763	if (incl_search) {
764		if (ctx.qry.grep)
765			html_hidden("qt", ctx.qry.grep);
766		if (ctx.qry.search)
767			html_hidden("q", ctx.qry.search);
768	}
769}
770
771static const char *hc(const char *page)
772{
773	return strcmp(ctx.qry.page, page) ? NULL : "active";
774}
775
776static void cgit_print_path_crumbs(char *path)
777{
778	char *old_path = ctx.qry.path;
779	char *p = path, *q, *end = path + strlen(path);
780
781	ctx.qry.path = NULL;
782	cgit_self_link("root", NULL, NULL);
783	ctx.qry.path = p = path;
784	while (p < end) {
785		if (!(q = strchr(p, '/')))
786			q = end;
787		*q = '\0';
788		html_txt("/");
789		cgit_self_link(p, NULL, NULL);
790		if (q < end)
791			*q = '/';
792		p = q + 1;
793	}
794	ctx.qry.path = old_path;
795}
796
797static void print_header(void)
798{
799	char *logo = NULL, *logo_link = NULL;
800
801	html("<table id='header'>\n");
802	html("<tr>\n");
803
804	if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
805		logo = ctx.repo->logo;
806	else
807		logo = ctx.cfg.logo;
808	if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
809		logo_link = ctx.repo->logo_link;
810	else
811		logo_link = ctx.cfg.logo_link;
812	if (logo && *logo) {
813		html("<td class='logo' rowspan='2'><a href='");
814		if (logo_link && *logo_link)
815			html_attr(logo_link);
816		else
817			html_attr(cgit_rooturl());
818		html("'><img src='");
819		html_attr(logo);
820		html("' alt='cgit logo'/></a></td>\n");
821	}
822
823	html("<td class='main'>");
824	if (ctx.repo) {
825		cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
826		html(" : ");
827		cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
828		if (ctx.env.authenticated) {
829			html("</td><td class='form'>");
830			html("<form method='get' action=''>\n");
831			cgit_add_hidden_formfields(0, 1, ctx.qry.page);
832			html("<select name='h' onchange='this.form.submit();'>\n");
833			for_each_branch_ref(print_branch_option, ctx.qry.head);
834			html("</select> ");
835			html("<input type='submit' name='' value='switch'/>");
836			html("</form>");
837		}
838	} else
839		html_txt(ctx.cfg.root_title);
840	html("</td></tr>\n");
841
842	html("<tr><td class='sub'>");
843	if (ctx.repo) {
844		html_txt(ctx.repo->desc);
845		html("</td><td class='sub right'>");
846		html_txt(ctx.repo->owner);
847	} else {
848		if (ctx.cfg.root_desc)
849			html_txt(ctx.cfg.root_desc);
850		else if (ctx.cfg.index_info)
851			html_include(ctx.cfg.index_info);
852	}
853	html("</td></tr></table>\n");
854}
855
856void cgit_print_pageheader(void)
857{
858	html("<div id='cgit'>");
859	if (!ctx.env.authenticated || !ctx.cfg.noheader)
860		print_header();
861
862	html("<table class='tabs'><tr><td>\n");
863	if (ctx.env.authenticated && ctx.repo) {
864		if (ctx.repo->readme.nr)
865			reporevlink("about", "about", NULL,
866				    hc("about"), ctx.qry.head, NULL,
867				    NULL);
868		cgit_summary_link("summary", NULL, hc("summary"),
869				  ctx.qry.head);
870		cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
871			       ctx.qry.sha1, NULL);
872		cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
873			      NULL, ctx.qry.vpath, 0, NULL, NULL,
874			      ctx.qry.showmsg);
875		cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
876			       ctx.qry.sha1, ctx.qry.vpath);
877		cgit_commit_link("commit", NULL, hc("commit"),
878				 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath, 0);
879		cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
880			       ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath, 0);
881		if (ctx.repo->max_stats)
882			cgit_stats_link("stats", NULL, hc("stats"),
883					ctx.qry.head, ctx.qry.vpath);
884		html("</td><td class='form'>");
885		html("<form class='right' method='get' action='");
886		if (ctx.cfg.virtual_root)
887			html_url_path(cgit_fileurl(ctx.qry.repo, "log",
888						   ctx.qry.vpath, NULL));
889		html("'>\n");
890		cgit_add_hidden_formfields(1, 0, "log");
891		html("<select name='qt'>\n");
892		html_option("grep", "log msg", ctx.qry.grep);
893		html_option("author", "author", ctx.qry.grep);
894		html_option("committer", "committer", ctx.qry.grep);
895		html_option("range", "range", ctx.qry.grep);
896		html("</select>\n");
897		html("<input class='txt' type='text' size='10' name='q' value='");
898		html_attr(ctx.qry.search);
899		html("'/>\n");
900		html("<input type='submit' value='search'/>\n");
901		html("</form>\n");
902	} else if (ctx.env.authenticated) {
903		site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0);
904		if (ctx.cfg.root_readme)
905			site_link("about", "about", NULL, hc("about"),
906				  NULL, NULL, 0);
907		html("</td><td class='form'>");
908		html("<form method='get' action='");
909		html_attr(cgit_rooturl());
910		html("'>\n");
911		html("<input type='text' name='q' size='10' value='");
912		html_attr(ctx.qry.search);
913		html("'/>\n");
914		html("<input type='submit' value='search'/>\n");
915		html("</form>");
916	}
917	html("</td></tr></table>\n");
918	if (ctx.env.authenticated && ctx.qry.vpath) {
919		html("<div class='path'>");
920		html("path: ");
921		cgit_print_path_crumbs(ctx.qry.vpath);
922		html("</div>");
923	}
924	html("<div class='content'>");
925}
926
927void cgit_print_filemode(unsigned short mode)
928{
929	if (S_ISDIR(mode))
930		html("d");
931	else if (S_ISLNK(mode))
932		html("l");
933	else if (S_ISGITLINK(mode))
934		html("m");
935	else
936		html("-");
937	html_fileperm(mode >> 6);
938	html_fileperm(mode >> 3);
939	html_fileperm(mode);
940}
941
942void cgit_print_snapshot_links(const char *repo, const char *head,
943			       const char *hex, int snapshots)
944{
945	const struct cgit_snapshot_format* f;
946	struct strbuf filename = STRBUF_INIT;
947	size_t prefixlen;
948	unsigned char sha1[20];
949
950	if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
951	    (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
952		hex++;
953	strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
954	prefixlen = filename.len;
955	for (f = cgit_snapshot_formats; f->suffix; f++) {
956		if (!(snapshots & f->bit))
957			continue;
958		strbuf_setlen(&filename, prefixlen);
959		strbuf_addstr(&filename, f->suffix);
960		cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
961				   filename.buf);
962		html("<br/>");
963	}
964	strbuf_release(&filename);
965}