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