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 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(void)
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(void)
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_currenturl(void)
69{
70 if (!ctx.qry.url)
71 return cgit_rooturl();
72 const char *root = cgit_rooturl();
73 size_t len = strlen(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 = "&";
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 html_attr(cgit_currenturl());
162
163 if (page) {
164 htmlf("?p=%s", page);
165 delim = "&";
166 }
167 if (search) {
168 html(delim);
169 html("q=");
170 html_attr(search);
171 delim = "&";
172 }
173 if (sort) {
174 html(delim);
175 html("s=");
176 html_attr(sort);
177 delim = "&";
178 }
179 if (ofs) {
180 html(delim);
181 htmlf("ofs=%d", ofs);
182 }
183}
184
185static void site_link(const char *page, const char *name, const char *title,
186 const char *class, const char *search, const char *sort, int ofs, int always_root)
187{
188 html("<a");
189 if (title) {
190 html(" title='");
191 html_attr(title);
192 html("'");
193 }
194 if (class) {
195 html(" class='");
196 html_attr(class);
197 html("'");
198 }
199 html(" href='");
200 site_url(page, search, sort, ofs, always_root);
201 html("'>");
202 html_txt(name);
203 html("</a>");
204}
205
206void cgit_index_link(const char *name, const char *title, const char *class,
207 const char *pattern, const char *sort, int ofs, int always_root)
208{
209 site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
210}
211
212static char *repolink(const char *title, const char *class, const char *page,
213 const char *head, const char *path)
214{
215 char *delim = "?";
216
217 html("<a");
218 if (title) {
219 html(" title='");
220 html_attr(title);
221 html("'");
222 }
223 if (class) {
224 html(" class='");
225 html_attr(class);
226 html("'");
227 }
228 html(" href='");
229 if (ctx.cfg.virtual_root) {
230 html_url_path(ctx.cfg.virtual_root);
231 html_url_path(ctx.repo->url);
232 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
233 html("/");
234 if (page) {
235 html_url_path(page);
236 html("/");
237 if (path)
238 html_url_path(path);
239 }
240 } else {
241 html_url_path(ctx.cfg.script_name);
242 html("?url=");
243 html_url_arg(ctx.repo->url);
244 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
245 html("/");
246 if (page) {
247 html_url_arg(page);
248 html("/");
249 if (path)
250 html_url_arg(path);
251 }
252 delim = "&";
253 }
254 if (head && strcmp(head, ctx.repo->defbranch)) {
255 html(delim);
256 html("h=");
257 html_url_arg(head);
258 delim = "&";
259 }
260 return fmt("%s", delim);
261}
262
263static void reporevlink(const char *page, const char *name, const char *title,
264 const char *class, const char *head, const char *rev,
265 const char *path)
266{
267 char *delim;
268
269 delim = repolink(title, class, page, head, path);
270 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
271 html(delim);
272 html("id=");
273 html_url_arg(rev);
274 }
275 html("'>");
276 html_txt(name);
277 html("</a>");
278}
279
280void cgit_summary_link(const char *name, const char *title, const char *class,
281 const char *head)
282{
283 reporevlink(NULL, name, title, class, head, NULL, NULL);
284}
285
286void cgit_tag_link(const char *name, const char *title, const char *class,
287 const char *tag)
288{
289 reporevlink("tag", name, title, class, tag, NULL, NULL);
290}
291
292void cgit_tree_link(const char *name, const char *title, const char *class,
293 const char *head, const char *rev, const char *path)
294{
295 reporevlink("tree", name, title, class, head, rev, path);
296}
297
298void cgit_plain_link(const char *name, const char *title, const char *class,
299 const char *head, const char *rev, const char *path)
300{
301 reporevlink("plain", name, title, class, head, rev, path);
302}
303
304void cgit_log_link(const char *name, const char *title, const char *class,
305 const char *head, const char *rev, const char *path,
306 int ofs, const char *grep, const char *pattern, int showmsg,
307 int follow)
308{
309 char *delim;
310
311 delim = repolink(title, class, "log", head, path);
312 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
313 html(delim);
314 html("id=");
315 html_url_arg(rev);
316 delim = "&";
317 }
318 if (grep && pattern) {
319 html(delim);
320 html("qt=");
321 html_url_arg(grep);
322 delim = "&";
323 html(delim);
324 html("q=");
325 html_url_arg(pattern);
326 }
327 if (ofs > 0) {
328 html(delim);
329 html("ofs=");
330 htmlf("%d", ofs);
331 delim = "&";
332 }
333 if (showmsg) {
334 html(delim);
335 html("showmsg=1");
336 delim = "&";
337 }
338 if (follow) {
339 html(delim);
340 html("follow=1");
341 }
342 html("'>");
343 html_txt(name);
344 html("</a>");
345}
346
347void cgit_commit_link(char *name, const char *title, const char *class,
348 const char *head, const char *rev, const char *path)
349{
350 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
351 name[ctx.cfg.max_msg_len] = '\0';
352 name[ctx.cfg.max_msg_len - 1] = '.';
353 name[ctx.cfg.max_msg_len - 2] = '.';
354 name[ctx.cfg.max_msg_len - 3] = '.';
355 }
356
357 char *delim;
358
359 delim = repolink(title, class, "commit", head, path);
360 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
361 html(delim);
362 html("id=");
363 html_url_arg(rev);
364 delim = "&";
365 }
366 if (ctx.qry.difftype) {
367 html(delim);
368 htmlf("dt=%d", ctx.qry.difftype);
369 delim = "&";
370 }
371 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
372 html(delim);
373 html("context=");
374 htmlf("%d", ctx.qry.context);
375 delim = "&";
376 }
377 if (ctx.qry.ignorews) {
378 html(delim);
379 html("ignorews=1");
380 delim = "&";
381 }
382 if (ctx.qry.follow) {
383 html(delim);
384 html("follow=1");
385 }
386 html("'>");
387 if (name[0] != '\0')
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 = "&";
419 }
420 if (old_rev) {
421 html(delim);
422 html("id2=");
423 html_url_arg(old_rev);
424 delim = "&";
425 }
426 if (ctx.qry.difftype) {
427 html(delim);
428 htmlf("dt=%d", ctx.qry.difftype);
429 delim = "&";
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 = "&";
436 }
437 if (ctx.qry.ignorews) {
438 html(delim);
439 html("ignorews=1");
440 delim = "&";
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 = sha1_to_hex(obj->sha1);
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
607static const char *fmt_date(time_t secs, const char *format, int local_time)
608{
609 static char buf[64];
610 struct tm *time;
611
612 if (!secs)
613 return "";
614 if (local_time)
615 time = localtime(&secs);
616 else
617 time = gmtime(&secs);
618 strftime(buf, sizeof(buf)-1, format, time);
619 return buf;
620}
621
622void cgit_print_date(time_t secs, const char *format, int local_time)
623{
624 html_txt(fmt_date(secs, format, local_time));
625}
626
627static void print_rel_date(time_t t, double value,
628 const char *class, const char *suffix)
629{
630 char buf[64];
631 struct tm *time;
632
633 if (ctx.cfg.local_time)
634 time = localtime(&t);
635 else
636 time = gmtime(&t);
637 strftime(buf, sizeof(buf) - 1, FMT_LONGDATE, time);
638
639 htmlf("<span class='%s' title='", class);
640 html_attr(buf);
641 htmlf("'>%.0f %s</span>", value, suffix);
642}
643
644void cgit_print_age(time_t t, time_t max_relative, const char *format)
645{
646 time_t now, secs;
647
648 if (!t)
649 return;
650 time(&now);
651 secs = now - t;
652 if (secs < 0)
653 secs = 0;
654
655 if (secs > max_relative && max_relative >= 0) {
656 cgit_print_date(t, format, ctx.cfg.local_time);
657 return;
658 }
659
660 if (secs < TM_HOUR * 2) {
661 print_rel_date(t, secs * 1.0 / TM_MIN, "age-mins", "min.");
662 return;
663 }
664 if (secs < TM_DAY * 2) {
665 print_rel_date(t, secs * 1.0 / TM_HOUR, "age-hours", "hours");
666 return;
667 }
668 if (secs < TM_WEEK * 2) {
669 print_rel_date(t, secs * 1.0 / TM_DAY, "age-days", "days");
670 return;
671 }
672 if (secs < TM_MONTH * 2) {
673 print_rel_date(t, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
674 return;
675 }
676 if (secs < TM_YEAR * 2) {
677 print_rel_date(t, secs * 1.0 / TM_MONTH, "age-months", "months");
678 return;
679 }
680 print_rel_date(t, secs * 1.0 / TM_YEAR, "age-years", "years");
681}
682
683void cgit_print_http_headers(void)
684{
685 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
686 return;
687
688 if (ctx.page.status)
689 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
690 if (ctx.page.mimetype && ctx.page.charset)
691 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
692 ctx.page.charset);
693 else if (ctx.page.mimetype)
694 htmlf("Content-Type: %s\n", ctx.page.mimetype);
695 if (ctx.page.size)
696 htmlf("Content-Length: %zd\n", ctx.page.size);
697 if (ctx.page.filename)
698 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
699 ctx.page.filename);
700 if (!ctx.env.authenticated)
701 html("Cache-Control: no-cache, no-store\n");
702 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
703 htmlf("Expires: %s\n", http_date(ctx.page.expires));
704 if (ctx.page.etag)
705 htmlf("ETag: \"%s\"\n", ctx.page.etag);
706 html("\n");
707 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
708 exit(0);
709}
710
711void cgit_redirect(const char *url, bool permanent)
712{
713 htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
714 htmlf("Location: %s\n\n", url);
715 exit(0);
716}
717
718static void print_rel_vcs_link(const char *url)
719{
720 html("<link rel='vcs-git' href='");
721 html_attr(url);
722 html("' title='");
723 html_attr(ctx.repo->name);
724 html(" Git repository'/>\n");
725}
726
727void cgit_print_docstart(void)
728{
729 if (ctx.cfg.embedded) {
730 if (ctx.cfg.header)
731 html_include(ctx.cfg.header);
732 return;
733 }
734
735 const char *host = cgit_hosturl();
736 html(cgit_doctype);
737 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
738 html("<head>\n");
739 html("<title>");
740 html_txt(ctx.page.title);
741 html("</title>\n");
742 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
743 if (ctx.cfg.robots && *ctx.cfg.robots)
744 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
745 html("<link rel='stylesheet' type='text/css' href='");
746 html_attr(ctx.cfg.css);
747 html("'/>\n");
748 if (ctx.cfg.favicon) {
749 html("<link rel='shortcut icon' href='");
750 html_attr(ctx.cfg.favicon);
751 html("'/>\n");
752 }
753 if (host && ctx.repo && ctx.qry.head) {
754 struct strbuf sb = STRBUF_INIT;
755 strbuf_addf(&sb, "h=%s", ctx.qry.head);
756
757 html("<link rel='alternate' title='Atom feed' href='");
758 html(cgit_httpscheme());
759 html_attr(cgit_hosturl());
760 html_attr(cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
761 sb.buf));
762 html("' type='application/atom+xml'/>\n");
763 strbuf_release(&sb);
764 }
765 if (ctx.repo)
766 cgit_add_clone_urls(print_rel_vcs_link);
767 if (ctx.cfg.head_include)
768 html_include(ctx.cfg.head_include);
769 html("</head>\n");
770 html("<body>\n");
771 if (ctx.cfg.header)
772 html_include(ctx.cfg.header);
773}
774
775void cgit_print_docend(void)
776{
777 html("</div> <!-- class=content -->\n");
778 if (ctx.cfg.embedded) {
779 html("</div> <!-- id=cgit -->\n");
780 if (ctx.cfg.footer)
781 html_include(ctx.cfg.footer);
782 return;
783 }
784 if (ctx.cfg.footer)
785 html_include(ctx.cfg.footer);
786 else {
787 htmlf("<div class='footer'>generated by <a href='http://git.zx2c4.com/cgit/about/'>cgit %s</a> at ",
788 cgit_version);
789 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
790 html("</div>\n");
791 }
792 html("</div> <!-- id=cgit -->\n");
793 html("</body>\n</html>\n");
794}
795
796static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
797{
798 struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
799 int i;
800
801 for (i = 0; url_list[i]; i++) {
802 strbuf_rtrim(url_list[i]);
803 if (url_list[i]->len == 0)
804 continue;
805 if (suffix && *suffix)
806 strbuf_addf(url_list[i], "/%s", suffix);
807 fn(url_list[i]->buf);
808 }
809
810 strbuf_list_free(url_list);
811}
812
813void cgit_add_clone_urls(void (*fn)(const char *))
814{
815 if (ctx.repo->clone_url)
816 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
817 else if (ctx.cfg.clone_prefix)
818 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
819}
820
821static int print_branch_option(const char *refname, const struct object_id *oid,
822 int flags, void *cb_data)
823{
824 char *name = (char *)refname;
825 html_option(name, name, ctx.qry.head);
826 return 0;
827}
828
829void cgit_add_hidden_formfields(int incl_head, int incl_search,
830 const char *page)
831{
832 if (!ctx.cfg.virtual_root) {
833 struct strbuf url = STRBUF_INIT;
834
835 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
836 if (ctx.qry.vpath)
837 strbuf_addf(&url, "/%s", ctx.qry.vpath);
838 html_hidden("url", url.buf);
839 strbuf_release(&url);
840 }
841
842 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
843 strcmp(ctx.qry.head, ctx.repo->defbranch))
844 html_hidden("h", ctx.qry.head);
845
846 if (ctx.qry.sha1)
847 html_hidden("id", ctx.qry.sha1);
848 if (ctx.qry.sha2)
849 html_hidden("id2", ctx.qry.sha2);
850 if (ctx.qry.showmsg)
851 html_hidden("showmsg", "1");
852
853 if (incl_search) {
854 if (ctx.qry.grep)
855 html_hidden("qt", ctx.qry.grep);
856 if (ctx.qry.search)
857 html_hidden("q", ctx.qry.search);
858 }
859}
860
861static const char *hc(const char *page)
862{
863 return strcmp(ctx.qry.page, page) ? NULL : "active";
864}
865
866static void cgit_print_path_crumbs(char *path)
867{
868 char *old_path = ctx.qry.path;
869 char *p = path, *q, *end = path + strlen(path);
870
871 ctx.qry.path = NULL;
872 cgit_self_link("root", NULL, NULL);
873 ctx.qry.path = p = path;
874 while (p < end) {
875 if (!(q = strchr(p, '/')))
876 q = end;
877 *q = '\0';
878 html_txt("/");
879 cgit_self_link(p, NULL, NULL);
880 if (q < end)
881 *q = '/';
882 p = q + 1;
883 }
884 ctx.qry.path = old_path;
885}
886
887static void print_header(void)
888{
889 char *logo = NULL, *logo_link = NULL;
890
891 html("<table id='header'>\n");
892 html("<tr>\n");
893
894 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
895 logo = ctx.repo->logo;
896 else
897 logo = ctx.cfg.logo;
898 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
899 logo_link = ctx.repo->logo_link;
900 else
901 logo_link = ctx.cfg.logo_link;
902 if (logo && *logo) {
903 html("<td class='logo' rowspan='2'><a href='");
904 if (logo_link && *logo_link)
905 html_attr(logo_link);
906 else
907 html_attr(cgit_rooturl());
908 html("'><img src='");
909 html_attr(logo);
910 html("' alt='cgit logo'/></a></td>\n");
911 }
912
913 html("<td class='main'>");
914 if (ctx.repo) {
915 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
916 html(" : ");
917 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
918 if (ctx.env.authenticated) {
919 html("</td><td class='form'>");
920 html("<form method='get' action=''>\n");
921 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
922 html("<select name='h' onchange='this.form.submit();'>\n");
923 for_each_branch_ref(print_branch_option, ctx.qry.head);
924 if (ctx.repo->enable_remote_branches)
925 for_each_remote_ref(print_branch_option, ctx.qry.head);
926 html("</select> ");
927 html("<input type='submit' name='' value='switch'/>");
928 html("</form>");
929 }
930 } else
931 html_txt(ctx.cfg.root_title);
932 html("</td></tr>\n");
933
934 html("<tr><td class='sub'>");
935 if (ctx.repo) {
936 html_txt(ctx.repo->desc);
937 html("</td><td class='sub right'>");
938 html_txt(ctx.repo->owner);
939 } else {
940 if (ctx.cfg.root_desc)
941 html_txt(ctx.cfg.root_desc);
942 else if (ctx.cfg.index_info)
943 html_include(ctx.cfg.index_info);
944 }
945 html("</td></tr></table>\n");
946}
947
948void cgit_print_pageheader(void)
949{
950 html("<div id='cgit'>");
951 if (!ctx.env.authenticated || !ctx.cfg.noheader)
952 print_header();
953
954 html("<table class='tabs'><tr><td>\n");
955 if (ctx.env.authenticated && ctx.repo) {
956 if (ctx.repo->readme.nr)
957 reporevlink("about", "about", NULL,
958 hc("about"), ctx.qry.head, NULL,
959 NULL);
960 cgit_summary_link("summary", NULL, hc("summary"),
961 ctx.qry.head);
962 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
963 ctx.qry.sha1, NULL);
964 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
965 NULL, ctx.qry.vpath, 0, NULL, NULL,
966 ctx.qry.showmsg, ctx.qry.follow);
967 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
968 ctx.qry.sha1, ctx.qry.vpath);
969 cgit_commit_link("commit", NULL, hc("commit"),
970 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath);
971 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
972 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath);
973 if (ctx.repo->max_stats)
974 cgit_stats_link("stats", NULL, hc("stats"),
975 ctx.qry.head, ctx.qry.vpath);
976 html("</td><td class='form'>");
977 html("<form class='right' method='get' action='");
978 if (ctx.cfg.virtual_root)
979 html_url_path(cgit_fileurl(ctx.qry.repo, "log",
980 ctx.qry.vpath, NULL));
981 html("'>\n");
982 cgit_add_hidden_formfields(1, 0, "log");
983 html("<select name='qt'>\n");
984 html_option("grep", "log msg", ctx.qry.grep);
985 html_option("author", "author", ctx.qry.grep);
986 html_option("committer", "committer", ctx.qry.grep);
987 html_option("range", "range", ctx.qry.grep);
988 html("</select>\n");
989 html("<input class='txt' type='text' size='10' name='q' value='");
990 html_attr(ctx.qry.search);
991 html("'/>\n");
992 html("<input type='submit' value='search'/>\n");
993 html("</form>\n");
994 } else if (ctx.env.authenticated) {
995 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
996 if (ctx.cfg.root_readme)
997 site_link("about", "about", NULL, hc("about"),
998 NULL, NULL, 0, 1);
999 html("</td><td class='form'>");
1000 html("<form method='get' action='");
1001 html_attr(cgit_currenturl());
1002 html("'>\n");
1003 html("<input type='text' name='q' size='10' value='");
1004 html_attr(ctx.qry.search);
1005 html("'/>\n");
1006 html("<input type='submit' value='search'/>\n");
1007 html("</form>");
1008 }
1009 html("</td></tr></table>\n");
1010 if (ctx.env.authenticated && ctx.qry.vpath) {
1011 html("<div class='path'>");
1012 html("path: ");
1013 cgit_print_path_crumbs(ctx.qry.vpath);
1014 if (ctx.cfg.enable_follow_links && !strcmp(ctx.qry.page, "log")) {
1015 html(" (");
1016 ctx.qry.follow = !ctx.qry.follow;
1017 cgit_self_link(ctx.qry.follow ? "follow" : "unfollow",
1018 NULL, NULL);
1019 ctx.qry.follow = !ctx.qry.follow;
1020 html(")");
1021 }
1022 html("</div>");
1023 }
1024 html("<div class='content'>");
1025}
1026
1027void cgit_print_filemode(unsigned short mode)
1028{
1029 if (S_ISDIR(mode))
1030 html("d");
1031 else if (S_ISLNK(mode))
1032 html("l");
1033 else if (S_ISGITLINK(mode))
1034 html("m");
1035 else
1036 html("-");
1037 html_fileperm(mode >> 6);
1038 html_fileperm(mode >> 3);
1039 html_fileperm(mode);
1040}
1041
1042void cgit_print_snapshot_links(const char *repo, const char *head,
1043 const char *hex, int snapshots)
1044{
1045 const struct cgit_snapshot_format* f;
1046 struct strbuf filename = STRBUF_INIT;
1047 size_t prefixlen;
1048 unsigned char sha1[20];
1049
1050 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
1051 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
1052 hex++;
1053 strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
1054 prefixlen = filename.len;
1055 for (f = cgit_snapshot_formats; f->suffix; f++) {
1056 if (!(snapshots & f->bit))
1057 continue;
1058 strbuf_setlen(&filename, prefixlen);
1059 strbuf_addstr(&filename, f->suffix);
1060 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1061 filename.buf);
1062 html("<br/>");
1063 }
1064 strbuf_release(&filename);
1065}