1 /* ui-shared.c: common web output functions
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
10 #include "ui-shared.h"
14 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";
18 static char *http_date(time_t t)
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);
31 void cgit_print_error(const char *fmt, ...)
35 cgit_vprint_error(fmt, ap);
39 void cgit_vprint_error(const char *fmt, va_list ap)
42 html("<div class='error'>");
49 const char *cgit_httpscheme()
51 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
57 const char *cgit_hosturl()
59 if (ctx.env.http_host)
60 return ctx.env.http_host;
61 if (!ctx.env.server_name)
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);
68 const char *cgit_rooturl()
70 if (ctx.cfg.virtual_root)
71 return ctx.cfg.virtual_root;
73 return ctx.cfg.script_name;
76 const char *cgit_loginurl()
78 static const char *login_url = 0;
80 login_url = fmtalloc("%s?p=login", cgit_rooturl());
84 char *cgit_repourl(const char *reponame)
86 if (ctx.cfg.virtual_root)
87 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
89 return fmtalloc("?r=%s", reponame);
92 char *cgit_fileurl(const char *reponame, const char *pagename,
93 const char *filename, const char *query)
95 struct strbuf sb = STRBUF_INIT;
98 if (ctx.cfg.virtual_root) {
99 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
100 pagename, (filename ? filename:""));
103 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
104 (filename ? filename : ""));
108 strbuf_addf(&sb, "%s%s", delim, query);
109 return strbuf_detach(&sb, NULL);
112 char *cgit_pageurl(const char *reponame, const char *pagename,
115 return cgit_fileurl(reponame, pagename, 0, query);
118 const char *cgit_repobasename(const char *reponame)
120 /* I assume we don't need to store more than one repo basename */
121 static char rvbuf[1024];
124 strncpy(rvbuf, reponame, sizeof(rvbuf));
125 if (rvbuf[sizeof(rvbuf)-1])
126 die("cgit_repobasename: truncated repository name '%s'", reponame);
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;
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,'/');
143 static void site_url(const char *page, const char *search, const char *sort, int ofs)
147 if (ctx.cfg.virtual_root)
148 html_attr(ctx.cfg.virtual_root);
150 html_url_path(ctx.cfg.script_name);
153 htmlf("?p=%s", page);
170 htmlf("ofs=%d", ofs);
174 static void site_link(const char *page, const char *name, const char *title,
175 const char *class, const char *search, const char *sort, int ofs)
189 site_url(page, search, sort, ofs);
195 void cgit_index_link(const char *name, const char *title, const char *class,
196 const char *pattern, const char *sort, int ofs)
198 site_link(NULL, name, title, class, pattern, sort, ofs);
201 static char *repolink(const char *title, const char *class, const char *page,
202 const char *head, const char *path)
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] != '/')
230 html_url_path(ctx.cfg.script_name);
232 html_url_arg(ctx.repo->url);
233 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
243 if (head && strcmp(head, ctx.repo->defbranch)) {
249 return fmt("%s", delim);
252 static void reporevlink(const char *page, const char *name, const char *title,
253 const char *class, const char *head, const char *rev,
258 delim = repolink(title, class, page, head, path);
259 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
269 void cgit_summary_link(const char *name, const char *title, const char *class,
272 reporevlink(NULL, name, title, class, head, NULL, NULL);
275 void cgit_tag_link(const char *name, const char *title, const char *class,
276 const char *head, const char *rev)
278 reporevlink("tag", name, title, class, head, rev, NULL);
281 void cgit_tree_link(const char *name, const char *title, const char *class,
282 const char *head, const char *rev, const char *path)
284 reporevlink("tree", name, title, class, head, rev, path);
287 void cgit_plain_link(const char *name, const char *title, const char *class,
288 const char *head, const char *rev, const char *path)
290 reporevlink("plain", name, title, class, head, rev, path);
293 void 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)
299 delim = repolink(title, class, "log", head, path);
300 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
306 if (grep && pattern) {
313 html_url_arg(pattern);
330 void cgit_commit_link(char *name, const char *title, const char *class,
331 const char *head, const char *rev, const char *path,
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] = '.';
343 delim = repolink(title, class, "commit", head, path);
344 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
350 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
355 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
358 htmlf("%d", ctx.qry.context);
361 if (ctx.qry.ignorews) {
370 html_txt("(no commit message)");
374 void cgit_refs_link(const char *name, const char *title, const char *class,
375 const char *head, const char *rev, const char *path)
377 reporevlink("refs", name, title, class, head, rev, path);
380 void cgit_snapshot_link(const char *name, const char *title, const char *class,
381 const char *head, const char *rev,
382 const char *archivename)
384 reporevlink("snapshot", name, title, class, head, rev, archivename);
387 void 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)
393 delim = repolink(title, class, "diff", head, path);
394 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
397 html_url_arg(new_rev);
403 html_url_arg(old_rev);
406 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
411 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
414 htmlf("%d", ctx.qry.context);
417 if (ctx.qry.ignorews) {
427 void cgit_patch_link(const char *name, const char *title, const char *class,
428 const char *head, const char *rev, const char *path)
430 reporevlink("patch", name, title, class, head, rev, path);
433 void cgit_stats_link(const char *name, const char *title, const char *class,
434 const char *head, const char *path)
436 reporevlink("stats", name, title, class, head, NULL, path);
439 static void cgit_self_link(char *name, const char *title, const char *class)
441 if (!strcmp(ctx.qry.page, "repolist"))
442 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
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,
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,
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,
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,
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,
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,
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,
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,
483 else if (!strcmp(ctx.qry.page, "stats"))
484 cgit_stats_link(name, title, class, ctx.qry.head,
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);
497 void cgit_object_link(struct object *obj)
499 char *page, *shortrev, *fullrev, *name;
501 fullrev = sha1_to_hex(obj->sha1);
502 shortrev = xstrdup(fullrev);
504 if (obj->type == OBJ_COMMIT) {
505 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
506 ctx.qry.head, fullrev, NULL, 0);
508 } else if (obj->type == OBJ_TREE)
510 else if (obj->type == OBJ_TAG)
514 name = fmt("%s %s...", typename(obj->type), shortrev);
515 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
518 static struct string_list_item *lookup_path(struct string_list *list,
521 struct string_list_item *item;
523 while (path && path[0]) {
524 if ((item = string_list_lookup(list, path)))
526 if (!(path = strchr(path, '/')))
533 void cgit_submodule_link(const char *class, char *path, const char *rev)
535 struct string_list *list;
536 struct string_list_item *item;
542 list = &ctx.repo->submodules;
543 item = lookup_path(list, path);
546 tail = path[len - 1];
549 item = lookup_path(list, path);
554 htmlf("class='%s' ", class);
557 html_attrf(item->util, rev);
558 } else if (ctx.repo->module_link) {
559 dir = strrchr(path, '/');
564 html_attrf(ctx.repo->module_link, dir, rev);
571 html_txtf(" @ %.7s", rev);
573 path[len - 1] = tail;
576 void cgit_print_date(time_t secs, const char *format, int local_time)
584 time = localtime(&secs);
586 time = gmtime(&secs);
587 strftime(buf, sizeof(buf)-1, format, time);
591 void cgit_print_age(time_t t, time_t max_relative, const char *format)
600 if (secs > max_relative && max_relative >= 0) {
601 cgit_print_date(t, format, ctx.cfg.local_time);
605 if (secs < TM_HOUR * 2) {
606 htmlf("<span class='age-mins'>%.0f min.</span>",
607 secs * 1.0 / TM_MIN);
610 if (secs < TM_DAY * 2) {
611 htmlf("<span class='age-hours'>%.0f hours</span>",
612 secs * 1.0 / TM_HOUR);
615 if (secs < TM_WEEK * 2) {
616 htmlf("<span class='age-days'>%.0f days</span>",
617 secs * 1.0 / TM_DAY);
620 if (secs < TM_MONTH * 2) {
621 htmlf("<span class='age-weeks'>%.0f weeks</span>",
622 secs * 1.0 / TM_WEEK);
625 if (secs < TM_YEAR * 2) {
626 htmlf("<span class='age-months'>%.0f months</span>",
627 secs * 1.0 / TM_MONTH);
630 htmlf("<span class='age-years'>%.0f years</span>",
631 secs * 1.0 / TM_YEAR);
634 void cgit_print_http_headers(void)
636 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
640 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
641 if (ctx.page.mimetype && ctx.page.charset)
642 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
644 else if (ctx.page.mimetype)
645 htmlf("Content-Type: %s\n", ctx.page.mimetype);
647 htmlf("Content-Length: %zd\n", ctx.page.size);
648 if (ctx.page.filename)
649 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
651 if (!ctx.env.authenticated)
652 html("Cache-Control: no-cache, no-store\n");
653 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
654 htmlf("Expires: %s\n", http_date(ctx.page.expires));
656 htmlf("ETag: \"%s\"\n", ctx.page.etag);
658 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
662 void cgit_print_docstart(void)
664 if (ctx.cfg.embedded) {
666 html_include(ctx.cfg.header);
670 const char *host = cgit_hosturl();
672 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
675 html_txt(ctx.page.title);
677 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
678 if (ctx.cfg.robots && *ctx.cfg.robots)
679 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
680 html("<link rel='stylesheet' type='text/css' href='");
681 html_attr(ctx.cfg.css);
683 if (ctx.cfg.favicon) {
684 html("<link rel='shortcut icon' href='");
685 html_attr(ctx.cfg.favicon);
688 if (host && ctx.repo && ctx.qry.head) {
689 struct strbuf sb = STRBUF_INIT;
690 strbuf_addf(&sb, "h=%s", ctx.qry.head);
692 html("<link rel='alternate' title='Atom feed' href='");
693 html(cgit_httpscheme());
694 html_attr(cgit_hosturl());
695 html_attr(cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
697 html("' type='application/atom+xml'/>\n");
700 if (ctx.cfg.head_include)
701 html_include(ctx.cfg.head_include);
705 html_include(ctx.cfg.header);
708 void cgit_print_docend()
710 html("</div> <!-- class=content -->\n");
711 if (ctx.cfg.embedded) {
712 html("</div> <!-- id=cgit -->\n");
714 html_include(ctx.cfg.footer);
718 html_include(ctx.cfg.footer);
720 htmlf("<div class='footer'>generated by cgit %s at ",
722 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
725 html("</div> <!-- id=cgit -->\n");
726 html("</body>\n</html>\n");
729 static int print_branch_option(const char *refname, const unsigned char *sha1,
730 int flags, void *cb_data)
732 char *name = (char *)refname;
733 html_option(name, name, ctx.qry.head);
737 void cgit_add_hidden_formfields(int incl_head, int incl_search,
740 if (!ctx.cfg.virtual_root) {
741 struct strbuf url = STRBUF_INIT;
743 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
745 strbuf_addf(&url, "/%s", ctx.qry.vpath);
746 html_hidden("url", url.buf);
747 strbuf_release(&url);
750 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
751 strcmp(ctx.qry.head, ctx.repo->defbranch))
752 html_hidden("h", ctx.qry.head);
755 html_hidden("id", ctx.qry.sha1);
757 html_hidden("id2", ctx.qry.sha2);
759 html_hidden("showmsg", "1");
763 html_hidden("qt", ctx.qry.grep);
765 html_hidden("q", ctx.qry.search);
769 static const char *hc(const char *page)
771 return strcmp(ctx.qry.page, page) ? NULL : "active";
774 static void cgit_print_path_crumbs(char *path)
776 char *old_path = ctx.qry.path;
777 char *p = path, *q, *end = path + strlen(path);
780 cgit_self_link("root", NULL, NULL);
781 ctx.qry.path = p = path;
783 if (!(q = strchr(p, '/')))
787 cgit_self_link(p, NULL, NULL);
792 ctx.qry.path = old_path;
795 static void print_header(void)
797 char *logo = NULL, *logo_link = NULL;
799 html("<table id='header'>\n");
802 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
803 logo = ctx.repo->logo;
806 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
807 logo_link = ctx.repo->logo_link;
809 logo_link = ctx.cfg.logo_link;
811 html("<td class='logo' rowspan='2'><a href='");
812 if (logo_link && *logo_link)
813 html_attr(logo_link);
815 html_attr(cgit_rooturl());
816 html("'><img src='");
818 html("' alt='cgit logo'/></a></td>\n");
821 html("<td class='main'>");
823 cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
825 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
826 if (ctx.env.authenticated) {
827 html("</td><td class='form'>");
828 html("<form method='get' action=''>\n");
829 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
830 html("<select name='h' onchange='this.form.submit();'>\n");
831 for_each_branch_ref(print_branch_option, ctx.qry.head);
833 html("<input type='submit' name='' value='switch'/>");
837 html_txt(ctx.cfg.root_title);
838 html("</td></tr>\n");
840 html("<tr><td class='sub'>");
842 html_txt(ctx.repo->desc);
843 html("</td><td class='sub right'>");
844 html_txt(ctx.repo->owner);
846 if (ctx.cfg.root_desc)
847 html_txt(ctx.cfg.root_desc);
848 else if (ctx.cfg.index_info)
849 html_include(ctx.cfg.index_info);
851 html("</td></tr></table>\n");
854 void cgit_print_pageheader(void)
856 html("<div id='cgit'>");
857 if (!ctx.env.authenticated || !ctx.cfg.noheader)
860 html("<table class='tabs'><tr><td>\n");
861 if (ctx.env.authenticated && ctx.repo) {
862 if (ctx.repo->readme.nr)
863 reporevlink("about", "about", NULL,
864 hc("about"), ctx.qry.head, NULL,
866 cgit_summary_link("summary", NULL, hc("summary"),
868 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
870 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
871 NULL, ctx.qry.vpath, 0, NULL, NULL,
873 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
874 ctx.qry.sha1, ctx.qry.vpath);
875 cgit_commit_link("commit", NULL, hc("commit"),
876 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath, 0);
877 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
878 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath, 0);
879 if (ctx.repo->max_stats)
880 cgit_stats_link("stats", NULL, hc("stats"),
881 ctx.qry.head, ctx.qry.vpath);
882 html("</td><td class='form'>");
883 html("<form class='right' method='get' action='");
884 if (ctx.cfg.virtual_root)
885 html_url_path(cgit_fileurl(ctx.qry.repo, "log",
886 ctx.qry.vpath, NULL));
888 cgit_add_hidden_formfields(1, 0, "log");
889 html("<select name='qt'>\n");
890 html_option("grep", "log msg", ctx.qry.grep);
891 html_option("author", "author", ctx.qry.grep);
892 html_option("committer", "committer", ctx.qry.grep);
893 html_option("range", "range", ctx.qry.grep);
895 html("<input class='txt' type='text' size='10' name='q' value='");
896 html_attr(ctx.qry.search);
898 html("<input type='submit' value='search'/>\n");
900 } else if (ctx.env.authenticated) {
901 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0);
902 if (ctx.cfg.root_readme)
903 site_link("about", "about", NULL, hc("about"),
905 html("</td><td class='form'>");
906 html("<form method='get' action='");
907 html_attr(cgit_rooturl());
909 html("<input type='text' name='q' size='10' value='");
910 html_attr(ctx.qry.search);
912 html("<input type='submit' value='search'/>\n");
915 html("</td></tr></table>\n");
916 if (ctx.env.authenticated && ctx.qry.vpath) {
917 html("<div class='path'>");
919 cgit_print_path_crumbs(ctx.qry.vpath);
922 html("<div class='content'>");
925 void cgit_print_filemode(unsigned short mode)
929 else if (S_ISLNK(mode))
931 else if (S_ISGITLINK(mode))
935 html_fileperm(mode >> 6);
936 html_fileperm(mode >> 3);
940 void cgit_print_snapshot_links(const char *repo, const char *head,
941 const char *hex, int snapshots)
943 const struct cgit_snapshot_format* f;
944 struct strbuf filename = STRBUF_INIT;
946 unsigned char sha1[20];
948 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
949 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
951 strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
952 prefixlen = filename.len;
953 for (f = cgit_snapshot_formats; f->suffix; f++) {
954 if (!(snapshots & f->bit))
956 strbuf_setlen(&filename, prefixlen);
957 strbuf_addstr(&filename, f->suffix);
958 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
962 strbuf_release(&filename);