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 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 char *cgit_repourl(const char *reponame)
78 if (ctx.cfg.virtual_root)
79 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
81 return fmtalloc("?r=%s", reponame);
84 char *cgit_fileurl(const char *reponame, const char *pagename,
85 const char *filename, const char *query)
87 struct strbuf sb = STRBUF_INIT;
90 if (ctx.cfg.virtual_root) {
91 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
92 pagename, (filename ? filename:""));
95 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
96 (filename ? filename : ""));
100 strbuf_addf(&sb, "%s%s", delim, query);
101 return strbuf_detach(&sb, NULL);
104 char *cgit_pageurl(const char *reponame, const char *pagename,
107 return cgit_fileurl(reponame, pagename, 0, query);
110 const char *cgit_repobasename(const char *reponame)
112 /* I assume we don't need to store more than one repo basename */
113 static char rvbuf[1024];
116 strncpy(rvbuf, reponame, sizeof(rvbuf));
117 if (rvbuf[sizeof(rvbuf)-1])
118 die("cgit_repobasename: truncated repository name '%s'", reponame);
120 /* strip trailing slashes */
121 while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
122 /* strip trailing .git */
123 if (p >= 3 && !strncmp(&rvbuf[p-3], ".git", 4)) {
124 p -= 3; rvbuf[p--] = 0;
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,'/');
135 static void site_url(const char *page, const char *search, const char *sort, int ofs)
139 if (ctx.cfg.virtual_root)
140 html_attr(ctx.cfg.virtual_root);
142 html(ctx.cfg.script_name);
145 htmlf("?p=%s", page);
162 htmlf("ofs=%d", ofs);
166 static void site_link(const char *page, const char *name, const char *title,
167 const char *class, const char *search, const char *sort, int ofs)
181 site_url(page, search, sort, ofs);
187 void cgit_index_link(const char *name, const char *title, const char *class,
188 const char *pattern, const char *sort, int ofs)
190 site_link(NULL, name, title, class, pattern, sort, ofs);
193 static char *repolink(const char *title, const char *class, const char *page,
194 const char *head, const char *path)
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] != '/')
222 html(ctx.cfg.script_name);
224 html_url_arg(ctx.repo->url);
225 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
235 if (head && strcmp(head, ctx.repo->defbranch)) {
241 return fmt("%s", delim);
244 static void reporevlink(const char *page, const char *name, const char *title,
245 const char *class, const char *head, const char *rev,
250 delim = repolink(title, class, page, head, path);
251 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
261 void cgit_summary_link(const char *name, const char *title, const char *class,
264 reporevlink(NULL, name, title, class, head, NULL, NULL);
267 void cgit_tag_link(const char *name, const char *title, const char *class,
268 const char *head, const char *rev)
270 reporevlink("tag", name, title, class, head, rev, NULL);
273 void cgit_tree_link(const char *name, const char *title, const char *class,
274 const char *head, const char *rev, const char *path)
276 reporevlink("tree", name, title, class, head, rev, path);
279 void cgit_plain_link(const char *name, const char *title, const char *class,
280 const char *head, const char *rev, const char *path)
282 reporevlink("plain", name, title, class, head, rev, path);
285 void 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)
291 delim = repolink(title, class, "log", head, path);
292 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
298 if (grep && pattern) {
305 html_url_arg(pattern);
322 void cgit_commit_link(char *name, const char *title, const char *class,
323 const char *head, const char *rev, const char *path,
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] = '.';
335 delim = repolink(title, class, "commit", head, path);
336 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
342 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
347 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
350 htmlf("%d", ctx.qry.context);
353 if (ctx.qry.ignorews) {
362 html_txt("(no commit message)");
366 void cgit_refs_link(const char *name, const char *title, const char *class,
367 const char *head, const char *rev, const char *path)
369 reporevlink("refs", name, title, class, head, rev, path);
372 void cgit_snapshot_link(const char *name, const char *title, const char *class,
373 const char *head, const char *rev,
374 const char *archivename)
376 reporevlink("snapshot", name, title, class, head, rev, archivename);
379 void 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)
385 delim = repolink(title, class, "diff", head, path);
386 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
389 html_url_arg(new_rev);
395 html_url_arg(old_rev);
398 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
403 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
406 htmlf("%d", ctx.qry.context);
409 if (ctx.qry.ignorews) {
419 void cgit_patch_link(const char *name, const char *title, const char *class,
420 const char *head, const char *rev, const char *path)
422 reporevlink("patch", name, title, class, head, rev, path);
425 void cgit_stats_link(const char *name, const char *title, const char *class,
426 const char *head, const char *path)
428 reporevlink("stats", name, title, class, head, NULL, path);
431 static void cgit_self_link(char *name, const char *title, const char *class,
432 struct cgit_context *ctx)
434 if (!strcmp(ctx->qry.page, "repolist"))
435 cgit_index_link(name, title, class, ctx->qry.search, ctx->qry.sort,
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,
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,
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,
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,
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,
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,
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,
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,
476 else if (!strcmp(ctx->qry.page, "stats"))
477 cgit_stats_link(name, title, class, ctx->qry.head,
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);
490 void cgit_object_link(struct object *obj)
492 char *page, *shortrev, *fullrev, *name;
494 fullrev = sha1_to_hex(obj->sha1);
495 shortrev = xstrdup(fullrev);
497 if (obj->type == OBJ_COMMIT) {
498 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
499 ctx.qry.head, fullrev, NULL, 0);
501 } else if (obj->type == OBJ_TREE)
503 else if (obj->type == OBJ_TAG)
507 name = fmt("%s %s...", typename(obj->type), shortrev);
508 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
511 static struct string_list_item *lookup_path(struct string_list *list,
514 struct string_list_item *item;
516 while (path && path[0]) {
517 if ((item = string_list_lookup(list, path)))
519 if (!(path = strchr(path, '/')))
526 void cgit_submodule_link(const char *class, char *path, const char *rev)
528 struct string_list *list;
529 struct string_list_item *item;
535 list = &ctx.repo->submodules;
536 item = lookup_path(list, path);
539 tail = path[len - 1];
542 item = lookup_path(list, path);
547 htmlf("class='%s' ", class);
550 html_attrf(item->util, rev);
551 } else if (ctx.repo->module_link) {
552 dir = strrchr(path, '/');
557 html_attrf(ctx.repo->module_link, dir, rev);
564 html_txtf(" @ %.7s", rev);
566 path[len - 1] = tail;
569 void cgit_print_date(time_t secs, const char *format, int local_time)
577 time = localtime(&secs);
579 time = gmtime(&secs);
580 strftime(buf, sizeof(buf)-1, format, time);
584 void cgit_print_age(time_t t, time_t max_relative, const char *format)
593 if (secs > max_relative && max_relative >= 0) {
594 cgit_print_date(t, format, ctx.cfg.local_time);
598 if (secs < TM_HOUR * 2) {
599 htmlf("<span class='age-mins'>%.0f min.</span>",
600 secs * 1.0 / TM_MIN);
603 if (secs < TM_DAY * 2) {
604 htmlf("<span class='age-hours'>%.0f hours</span>",
605 secs * 1.0 / TM_HOUR);
608 if (secs < TM_WEEK * 2) {
609 htmlf("<span class='age-days'>%.0f days</span>",
610 secs * 1.0 / TM_DAY);
613 if (secs < TM_MONTH * 2) {
614 htmlf("<span class='age-weeks'>%.0f weeks</span>",
615 secs * 1.0 / TM_WEEK);
618 if (secs < TM_YEAR * 2) {
619 htmlf("<span class='age-months'>%.0f months</span>",
620 secs * 1.0 / TM_MONTH);
623 htmlf("<span class='age-years'>%.0f years</span>",
624 secs * 1.0 / TM_YEAR);
627 void cgit_print_http_headers(struct cgit_context *ctx)
629 if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1"))
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,
637 else if (ctx->page.mimetype)
638 htmlf("Content-Type: %s\n", ctx->page.mimetype);
640 htmlf("Content-Length: %zd\n", ctx->page.size);
641 if (ctx->page.filename)
642 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
644 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
645 htmlf("Expires: %s\n", http_date(ctx->page.expires));
647 htmlf("ETag: \"%s\"\n", ctx->page.etag);
649 if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD"))
653 void cgit_print_docstart(struct cgit_context *ctx)
655 if (ctx->cfg.embedded) {
657 html_include(ctx->cfg.header);
661 const char *host = cgit_hosturl();
663 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
666 html_txt(ctx->page.title);
668 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
669 if (ctx->cfg.robots && *ctx->cfg.robots)
670 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
671 html("<link rel='stylesheet' type='text/css' href='");
672 html_attr(ctx->cfg.css);
674 if (ctx->cfg.favicon) {
675 html("<link rel='shortcut icon' href='");
676 html_attr(ctx->cfg.favicon);
679 if (host && ctx->repo && ctx->qry.head) {
680 struct strbuf sb = STRBUF_INIT;
681 strbuf_addf(&sb, "h=%s", ctx->qry.head);
683 html("<link rel='alternate' title='Atom feed' href='");
684 html(cgit_httpscheme());
685 html_attr(cgit_hosturl());
686 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
688 html("' type='application/atom+xml'/>\n");
691 if (ctx->cfg.head_include)
692 html_include(ctx->cfg.head_include);
696 html_include(ctx->cfg.header);
699 void cgit_print_docend()
701 html("</div> <!-- class=content -->\n");
702 if (ctx.cfg.embedded) {
703 html("</div> <!-- id=cgit -->\n");
705 html_include(ctx.cfg.footer);
709 html_include(ctx.cfg.footer);
711 htmlf("<div class='footer'>generated by cgit %s at ",
713 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
716 html("</div> <!-- id=cgit -->\n");
717 html("</body>\n</html>\n");
720 static int print_branch_option(const char *refname, const unsigned char *sha1,
721 int flags, void *cb_data)
723 char *name = (char *)refname;
724 html_option(name, name, ctx.qry.head);
728 void cgit_add_hidden_formfields(int incl_head, int incl_search,
731 if (!ctx.cfg.virtual_root) {
732 struct strbuf url = STRBUF_INIT;
734 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
736 strbuf_addf(&url, "/%s", ctx.qry.vpath);
737 html_hidden("url", url.buf);
738 strbuf_release(&url);
741 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
742 strcmp(ctx.qry.head, ctx.repo->defbranch))
743 html_hidden("h", ctx.qry.head);
746 html_hidden("id", ctx.qry.sha1);
748 html_hidden("id2", ctx.qry.sha2);
750 html_hidden("showmsg", "1");
754 html_hidden("qt", ctx.qry.grep);
756 html_hidden("q", ctx.qry.search);
760 static const char *hc(struct cgit_context *ctx, const char *page)
762 return strcmp(ctx->qry.page, page) ? NULL : "active";
765 static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
767 char *old_path = ctx->qry.path;
768 char *p = path, *q, *end = path + strlen(path);
770 ctx->qry.path = NULL;
771 cgit_self_link("root", NULL, NULL, ctx);
772 ctx->qry.path = p = path;
774 if (!(q = strchr(p, '/')))
778 cgit_self_link(p, NULL, NULL, ctx);
783 ctx->qry.path = old_path;
786 static void print_header(struct cgit_context *ctx)
788 char *logo = NULL, *logo_link = NULL;
790 html("<table id='header'>\n");
793 if (ctx->repo && ctx->repo->logo && *ctx->repo->logo)
794 logo = ctx->repo->logo;
796 logo = ctx->cfg.logo;
797 if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link)
798 logo_link = ctx->repo->logo_link;
800 logo_link = ctx->cfg.logo_link;
802 html("<td class='logo' rowspan='2'><a href='");
803 if (logo_link && *logo_link)
804 html_attr(logo_link);
806 html_attr(cgit_rooturl());
807 html("'><img src='");
809 html("' alt='cgit logo'/></a></td>\n");
812 html("<td class='main'>");
814 cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
816 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
817 html("</td><td class='form'>");
818 html("<form method='get' action=''>\n");
819 cgit_add_hidden_formfields(0, 1, ctx->qry.page);
820 html("<select name='h' onchange='this.form.submit();'>\n");
821 for_each_branch_ref(print_branch_option, ctx->qry.head);
823 html("<input type='submit' name='' value='switch'/>");
826 html_txt(ctx->cfg.root_title);
827 html("</td></tr>\n");
829 html("<tr><td class='sub'>");
831 html_txt(ctx->repo->desc);
832 html("</td><td class='sub right'>");
833 html_txt(ctx->repo->owner);
835 if (ctx->cfg.root_desc)
836 html_txt(ctx->cfg.root_desc);
837 else if (ctx->cfg.index_info)
838 html_include(ctx->cfg.index_info);
840 html("</td></tr></table>\n");
843 void cgit_print_pageheader(struct cgit_context *ctx)
845 html("<div id='cgit'>");
846 if (!ctx->cfg.noheader)
849 html("<table class='tabs'><tr><td>\n");
851 cgit_summary_link("summary", NULL, hc(ctx, "summary"),
853 cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head,
854 ctx->qry.sha1, NULL);
855 cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head,
856 NULL, ctx->qry.vpath, 0, NULL, NULL,
858 cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head,
859 ctx->qry.sha1, ctx->qry.vpath);
860 cgit_commit_link("commit", NULL, hc(ctx, "commit"),
861 ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0);
862 cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head,
863 ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0);
864 if (ctx->repo->max_stats)
865 cgit_stats_link("stats", NULL, hc(ctx, "stats"),
866 ctx->qry.head, ctx->qry.vpath);
867 if (ctx->repo->readme)
868 reporevlink("about", "about", NULL,
869 hc(ctx, "about"), ctx->qry.head, NULL,
871 html("</td><td class='form'>");
872 html("<form class='right' method='get' action='");
873 if (ctx->cfg.virtual_root)
874 html_url_path(cgit_fileurl(ctx->qry.repo, "log",
875 ctx->qry.vpath, NULL));
877 cgit_add_hidden_formfields(1, 0, "log");
878 html("<select name='qt'>\n");
879 html_option("grep", "log msg", ctx->qry.grep);
880 html_option("author", "author", ctx->qry.grep);
881 html_option("committer", "committer", ctx->qry.grep);
882 html_option("range", "range", ctx->qry.grep);
884 html("<input class='txt' type='text' size='10' name='q' value='");
885 html_attr(ctx->qry.search);
887 html("<input type='submit' value='search'/>\n");
890 site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, NULL, 0);
891 if (ctx->cfg.root_readme)
892 site_link("about", "about", NULL, hc(ctx, "about"),
894 html("</td><td class='form'>");
895 html("<form method='get' action='");
896 html_attr(cgit_rooturl());
898 html("<input type='text' name='q' size='10' value='");
899 html_attr(ctx->qry.search);
901 html("<input type='submit' value='search'/>\n");
904 html("</td></tr></table>\n");
905 if (ctx->qry.vpath) {
906 html("<div class='path'>");
908 cgit_print_path_crumbs(ctx, ctx->qry.vpath);
911 html("<div class='content'>");
914 void cgit_print_filemode(unsigned short mode)
918 else if (S_ISLNK(mode))
920 else if (S_ISGITLINK(mode))
924 html_fileperm(mode >> 6);
925 html_fileperm(mode >> 3);
929 void cgit_print_snapshot_links(const char *repo, const char *head,
930 const char *hex, int snapshots)
932 const struct cgit_snapshot_format* f;
933 struct strbuf filename = STRBUF_INIT;
935 unsigned char sha1[20];
937 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
938 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
940 strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
941 prefixlen = filename.len;
942 for (f = cgit_snapshot_formats; f->suffix; f++) {
943 if (!(snapshots & f->bit))
945 strbuf_setlen(&filename, prefixlen);
946 strbuf_addstr(&filename, f->suffix);
947 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
951 strbuf_release(&filename);