1 /* ui-shared.c: common web output functions
3 * Copyright (C) 2006 Lars Hjemli
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 xstrdup(fmt("%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 char *cgit_repourl(const char *reponame)
78 if (ctx.cfg.virtual_root) {
79 return fmt("%s%s/", ctx.cfg.virtual_root, reponame);
81 return fmt("?r=%s", reponame);
85 char *cgit_fileurl(const char *reponame, const char *pagename,
86 const char *filename, const char *query)
91 if (ctx.cfg.virtual_root) {
92 tmp = fmt("%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
93 pagename, (filename ? filename:""));
96 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
97 (filename ? filename : ""));
101 tmp = fmt("%s%s%s", tmp, delim, query);
105 char *cgit_pageurl(const char *reponame, const char *pagename,
108 return cgit_fileurl(reponame, pagename, 0, query);
111 const char *cgit_repobasename(const char *reponame)
113 /* I assume we don't need to store more than one repo basename */
114 static char rvbuf[1024];
117 strncpy(rvbuf, reponame, sizeof(rvbuf));
118 if (rvbuf[sizeof(rvbuf)-1])
119 die("cgit_repobasename: truncated repository name '%s'", reponame);
121 /* strip trailing slashes */
122 while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
123 /* strip trailing .git */
124 if (p >= 3 && !strncmp(&rvbuf[p-3], ".git", 4)) {
125 p -= 3; rvbuf[p--] = 0;
127 /* strip more trailing slashes if any */
128 while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
129 /* find last slash in the remaining string */
130 rv = strrchr(rvbuf,'/');
136 static void site_url(const char *page, const char *search, const char *sort, int ofs)
140 if (ctx.cfg.virtual_root)
141 html_attr(ctx.cfg.virtual_root);
143 html(ctx.cfg.script_name);
146 htmlf("?p=%s", page);
163 htmlf("ofs=%d", ofs);
167 static void site_link(const char *page, const char *name, const char *title,
168 const char *class, const char *search, const char *sort, int ofs)
182 site_url(page, search, sort, ofs);
188 void cgit_index_link(const char *name, const char *title, const char *class,
189 const char *pattern, const char *sort, int ofs)
191 site_link(NULL, name, title, class, pattern, sort, ofs);
194 static char *repolink(const char *title, const char *class, const char *page,
195 const char *head, const char *path)
211 if (ctx.cfg.virtual_root) {
212 html_url_path(ctx.cfg.virtual_root);
213 html_url_path(ctx.repo->url);
214 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
223 html(ctx.cfg.script_name);
225 html_url_arg(ctx.repo->url);
226 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
236 if (head && strcmp(head, ctx.repo->defbranch)) {
242 return fmt("%s", delim);
245 static void reporevlink(const char *page, const char *name, const char *title,
246 const char *class, const char *head, const char *rev,
251 delim = repolink(title, class, page, head, path);
252 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
262 void cgit_summary_link(const char *name, const char *title, const char *class,
265 reporevlink(NULL, name, title, class, head, NULL, NULL);
268 void cgit_tag_link(const char *name, const char *title, const char *class,
269 const char *head, const char *rev)
271 reporevlink("tag", name, title, class, head, rev, NULL);
274 void cgit_tree_link(const char *name, const char *title, const char *class,
275 const char *head, const char *rev, const char *path)
277 reporevlink("tree", name, title, class, head, rev, path);
280 void cgit_plain_link(const char *name, const char *title, const char *class,
281 const char *head, const char *rev, const char *path)
283 reporevlink("plain", name, title, class, head, rev, path);
286 void cgit_log_link(const char *name, const char *title, const char *class,
287 const char *head, const char *rev, const char *path,
288 int ofs, const char *grep, const char *pattern, int showmsg)
292 delim = repolink(title, class, "log", head, path);
293 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
299 if (grep && pattern) {
306 html_url_arg(pattern);
323 void cgit_commit_link(char *name, const char *title, const char *class,
324 const char *head, const char *rev, const char *path,
327 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
328 name[ctx.cfg.max_msg_len] = '\0';
329 name[ctx.cfg.max_msg_len - 1] = '.';
330 name[ctx.cfg.max_msg_len - 2] = '.';
331 name[ctx.cfg.max_msg_len - 3] = '.';
336 delim = repolink(title, class, "commit", head, path);
337 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
343 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
348 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
351 htmlf("%d", ctx.qry.context);
354 if (ctx.qry.ignorews) {
363 html_txt("(no commit message)");
367 void cgit_refs_link(const char *name, const char *title, const char *class,
368 const char *head, const char *rev, const char *path)
370 reporevlink("refs", name, title, class, head, rev, path);
373 void cgit_snapshot_link(const char *name, const char *title, const char *class,
374 const char *head, const char *rev,
375 const char *archivename)
377 reporevlink("snapshot", name, title, class, head, rev, archivename);
380 void cgit_diff_link(const char *name, const char *title, const char *class,
381 const char *head, const char *new_rev, const char *old_rev,
382 const char *path, int toggle_ssdiff)
386 delim = repolink(title, class, "diff", head, path);
387 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
390 html_url_arg(new_rev);
396 html_url_arg(old_rev);
399 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
404 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
407 htmlf("%d", ctx.qry.context);
410 if (ctx.qry.ignorews) {
420 void cgit_patch_link(const char *name, const char *title, const char *class,
421 const char *head, const char *rev, const char *path)
423 reporevlink("patch", name, title, class, head, rev, path);
426 void cgit_stats_link(const char *name, const char *title, const char *class,
427 const char *head, const char *path)
429 reporevlink("stats", name, title, class, head, NULL, path);
432 static void cgit_self_link(char *name, const char *title, const char *class,
433 struct cgit_context *ctx)
435 if (!strcmp(ctx->qry.page, "repolist"))
436 cgit_index_link(name, title, class, ctx->qry.search, ctx->qry.sort,
438 else if (!strcmp(ctx->qry.page, "summary"))
439 cgit_summary_link(name, title, class, ctx->qry.head);
440 else if (!strcmp(ctx->qry.page, "tag"))
441 cgit_tag_link(name, title, class, ctx->qry.head,
442 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL);
443 else if (!strcmp(ctx->qry.page, "tree"))
444 cgit_tree_link(name, title, class, ctx->qry.head,
445 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
447 else if (!strcmp(ctx->qry.page, "plain"))
448 cgit_plain_link(name, title, class, ctx->qry.head,
449 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
451 else if (!strcmp(ctx->qry.page, "log"))
452 cgit_log_link(name, title, class, ctx->qry.head,
453 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
454 ctx->qry.path, ctx->qry.ofs,
455 ctx->qry.grep, ctx->qry.search,
457 else if (!strcmp(ctx->qry.page, "commit"))
458 cgit_commit_link(name, title, class, ctx->qry.head,
459 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
461 else if (!strcmp(ctx->qry.page, "patch"))
462 cgit_patch_link(name, title, class, ctx->qry.head,
463 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
465 else if (!strcmp(ctx->qry.page, "refs"))
466 cgit_refs_link(name, title, class, ctx->qry.head,
467 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
469 else if (!strcmp(ctx->qry.page, "snapshot"))
470 cgit_snapshot_link(name, title, class, ctx->qry.head,
471 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
473 else if (!strcmp(ctx->qry.page, "diff"))
474 cgit_diff_link(name, title, class, ctx->qry.head,
475 ctx->qry.sha1, ctx->qry.sha2,
477 else if (!strcmp(ctx->qry.page, "stats"))
478 cgit_stats_link(name, title, class, ctx->qry.head,
481 /* Don't known how to make link for this page */
482 repolink(title, class, ctx->qry.page, ctx->qry.head, ctx->qry.path);
483 html("><!-- cgit_self_link() doesn't know how to make link for page '");
484 html_txt(ctx->qry.page);
491 void cgit_object_link(struct object *obj)
493 char *page, *shortrev, *fullrev, *name;
495 fullrev = sha1_to_hex(obj->sha1);
496 shortrev = xstrdup(fullrev);
498 if (obj->type == OBJ_COMMIT) {
499 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
500 ctx.qry.head, fullrev, NULL, 0);
502 } else if (obj->type == OBJ_TREE)
504 else if (obj->type == OBJ_TAG)
508 name = fmt("%s %s...", typename(obj->type), shortrev);
509 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
512 static struct string_list_item *lookup_path(struct string_list *list,
515 struct string_list_item *item;
517 while (path && path[0]) {
518 if ((item = string_list_lookup(list, path)))
520 if (!(path = strchr(path, '/')))
527 void cgit_submodule_link(const char *class, char *path, const char *rev)
529 struct string_list *list;
530 struct string_list_item *item;
536 list = &ctx.repo->submodules;
537 item = lookup_path(list, path);
540 tail = path[len - 1];
543 item = lookup_path(list, path);
548 htmlf("class='%s' ", class);
551 html_attr(fmt(item->util, rev));
552 } else if (ctx.repo->module_link) {
553 dir = strrchr(path, '/');
558 html_attr(fmt(ctx.repo->module_link, dir, rev));
565 html_txt(fmt(" @ %.7s", rev));
567 path[len - 1] = tail;
570 void cgit_print_date(time_t secs, const char *format, int local_time)
578 time = localtime(&secs);
580 time = gmtime(&secs);
581 strftime(buf, sizeof(buf)-1, format, time);
585 void cgit_print_age(time_t t, time_t max_relative, const char *format)
594 if (secs > max_relative && max_relative >= 0) {
595 cgit_print_date(t, format, ctx.cfg.local_time);
599 if (secs < TM_HOUR * 2) {
600 htmlf("<span class='age-mins'>%.0f min.</span>",
601 secs * 1.0 / TM_MIN);
604 if (secs < TM_DAY * 2) {
605 htmlf("<span class='age-hours'>%.0f hours</span>",
606 secs * 1.0 / TM_HOUR);
609 if (secs < TM_WEEK * 2) {
610 htmlf("<span class='age-days'>%.0f days</span>",
611 secs * 1.0 / TM_DAY);
614 if (secs < TM_MONTH * 2) {
615 htmlf("<span class='age-weeks'>%.0f weeks</span>",
616 secs * 1.0 / TM_WEEK);
619 if (secs < TM_YEAR * 2) {
620 htmlf("<span class='age-months'>%.0f months</span>",
621 secs * 1.0 / TM_MONTH);
624 htmlf("<span class='age-years'>%.0f years</span>",
625 secs * 1.0 / TM_YEAR);
628 void cgit_print_http_headers(struct cgit_context *ctx)
630 if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1"))
633 if (ctx->page.status)
634 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg);
635 if (ctx->page.mimetype && ctx->page.charset)
636 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
638 else if (ctx->page.mimetype)
639 htmlf("Content-Type: %s\n", ctx->page.mimetype);
641 htmlf("Content-Length: %zd\n", ctx->page.size);
642 if (ctx->page.filename)
643 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
645 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
646 htmlf("Expires: %s\n", http_date(ctx->page.expires));
648 htmlf("ETag: \"%s\"\n", ctx->page.etag);
650 if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD"))
654 void cgit_print_docstart(struct cgit_context *ctx)
656 if (ctx->cfg.embedded) {
658 html_include(ctx->cfg.header);
662 const char *host = cgit_hosturl();
664 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
667 html_txt(ctx->page.title);
669 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
670 if (ctx->cfg.robots && *ctx->cfg.robots)
671 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
672 html("<link rel='stylesheet' type='text/css' href='");
673 html_attr(ctx->cfg.css);
675 if (ctx->cfg.favicon) {
676 html("<link rel='shortcut icon' href='");
677 html_attr(ctx->cfg.favicon);
680 if (host && ctx->repo && ctx->qry.head) {
681 html("<link rel='alternate' title='Atom feed' href='");
682 html(cgit_httpscheme());
683 html_attr(cgit_hosturl());
684 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
685 fmt("h=%s", ctx->qry.head)));
686 html("' type='application/atom+xml'/>\n");
688 if (ctx->cfg.head_include)
689 html_include(ctx->cfg.head_include);
693 html_include(ctx->cfg.header);
696 void cgit_print_docend()
698 html("</div> <!-- class=content -->\n");
699 if (ctx.cfg.embedded) {
700 html("</div> <!-- id=cgit -->\n");
702 html_include(ctx.cfg.footer);
706 html_include(ctx.cfg.footer);
708 htmlf("<div class='footer'>generated by cgit %s at ",
710 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
713 html("</div> <!-- id=cgit -->\n");
714 html("</body>\n</html>\n");
717 static int print_branch_option(const char *refname, const unsigned char *sha1,
718 int flags, void *cb_data)
720 char *name = (char *)refname;
721 html_option(name, name, ctx.qry.head);
725 void cgit_add_hidden_formfields(int incl_head, int incl_search,
730 if (!ctx.cfg.virtual_root) {
731 url = fmt("%s/%s", ctx.qry.repo, page);
733 url = fmt("%s/%s", url, ctx.qry.vpath);
734 html_hidden("url", url);
737 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
738 strcmp(ctx.qry.head, ctx.repo->defbranch))
739 html_hidden("h", ctx.qry.head);
742 html_hidden("id", ctx.qry.sha1);
744 html_hidden("id2", ctx.qry.sha2);
746 html_hidden("showmsg", "1");
750 html_hidden("qt", ctx.qry.grep);
752 html_hidden("q", ctx.qry.search);
756 static const char *hc(struct cgit_context *ctx, const char *page)
758 return strcmp(ctx->qry.page, page) ? NULL : "active";
761 static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
763 char *old_path = ctx->qry.path;
764 char *p = path, *q, *end = path + strlen(path);
766 ctx->qry.path = NULL;
767 cgit_self_link("root", NULL, NULL, ctx);
768 ctx->qry.path = p = path;
770 if (!(q = strchr(p, '/')))
774 cgit_self_link(p, NULL, NULL, ctx);
779 ctx->qry.path = old_path;
782 static void print_header(struct cgit_context *ctx)
784 char *logo = NULL, *logo_link = NULL;
786 html("<table id='header'>\n");
789 if (ctx->repo && ctx->repo->logo && *ctx->repo->logo)
790 logo = ctx->repo->logo;
792 logo = ctx->cfg.logo;
793 if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link)
794 logo_link = ctx->repo->logo_link;
796 logo_link = ctx->cfg.logo_link;
798 html("<td class='logo' rowspan='2'><a href='");
799 if (logo_link && *logo_link)
800 html_attr(logo_link);
802 html_attr(cgit_rooturl());
803 html("'><img src='");
805 html("' alt='cgit logo'/></a></td>\n");
808 html("<td class='main'>");
810 cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
812 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
813 html("</td><td class='form'>");
814 html("<form method='get' action=''>\n");
815 cgit_add_hidden_formfields(0, 1, ctx->qry.page);
816 html("<select name='h' onchange='this.form.submit();'>\n");
817 for_each_branch_ref(print_branch_option, ctx->qry.head);
819 html("<input type='submit' name='' value='switch'/>");
822 html_txt(ctx->cfg.root_title);
823 html("</td></tr>\n");
825 html("<tr><td class='sub'>");
827 html_txt(ctx->repo->desc);
828 html("</td><td class='sub right'>");
829 html_txt(ctx->repo->owner);
831 if (ctx->cfg.root_desc)
832 html_txt(ctx->cfg.root_desc);
833 else if (ctx->cfg.index_info)
834 html_include(ctx->cfg.index_info);
836 html("</td></tr></table>\n");
839 void cgit_print_pageheader(struct cgit_context *ctx)
841 html("<div id='cgit'>");
842 if (!ctx->cfg.noheader)
845 html("<table class='tabs'><tr><td>\n");
847 cgit_summary_link("summary", NULL, hc(ctx, "summary"),
849 cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head,
850 ctx->qry.sha1, NULL);
851 cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head,
852 NULL, ctx->qry.vpath, 0, NULL, NULL,
854 cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head,
855 ctx->qry.sha1, ctx->qry.vpath);
856 cgit_commit_link("commit", NULL, hc(ctx, "commit"),
857 ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0);
858 cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head,
859 ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0);
860 if (ctx->repo->max_stats)
861 cgit_stats_link("stats", NULL, hc(ctx, "stats"),
862 ctx->qry.head, ctx->qry.vpath);
863 if (ctx->repo->readme)
864 reporevlink("about", "about", NULL,
865 hc(ctx, "about"), ctx->qry.head, NULL,
867 html("</td><td class='form'>");
868 html("<form class='right' method='get' action='");
869 if (ctx->cfg.virtual_root)
870 html_url_path(cgit_fileurl(ctx->qry.repo, "log",
871 ctx->qry.vpath, NULL));
873 cgit_add_hidden_formfields(1, 0, "log");
874 html("<select name='qt'>\n");
875 html_option("grep", "log msg", ctx->qry.grep);
876 html_option("author", "author", ctx->qry.grep);
877 html_option("committer", "committer", ctx->qry.grep);
878 html_option("range", "range", ctx->qry.grep);
880 html("<input class='txt' type='text' size='10' name='q' value='");
881 html_attr(ctx->qry.search);
883 html("<input type='submit' value='search'/>\n");
886 site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, NULL, 0);
887 if (ctx->cfg.root_readme)
888 site_link("about", "about", NULL, hc(ctx, "about"),
890 html("</td><td class='form'>");
891 html("<form method='get' action='");
892 html_attr(cgit_rooturl());
894 html("<input type='text' name='q' size='10' value='");
895 html_attr(ctx->qry.search);
897 html("<input type='submit' value='search'/>\n");
900 html("</td></tr></table>\n");
901 if (ctx->qry.vpath) {
902 html("<div class='path'>");
904 cgit_print_path_crumbs(ctx, ctx->qry.vpath);
907 html("<div class='content'>");
910 void cgit_print_filemode(unsigned short mode)
914 else if (S_ISLNK(mode))
916 else if (S_ISGITLINK(mode))
920 html_fileperm(mode >> 6);
921 html_fileperm(mode >> 3);
925 void cgit_print_snapshot_links(const char *repo, const char *head,
926 const char *hex, int snapshots)
928 const struct cgit_snapshot_format* f;
931 unsigned char sha1[20];
933 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
934 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
936 prefix = xstrdup(fmt("%s-%s", cgit_repobasename(repo), hex));
937 for (f = cgit_snapshot_formats; f->suffix; f++) {
938 if (!(snapshots & f->bit))
940 filename = fmt("%s%s", prefix, f->suffix);
941 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);