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 *msg)
33 html("<div class='error'>");
38 const char *cgit_httpscheme()
40 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
46 const char *cgit_hosturl()
48 if (ctx.env.http_host)
49 return ctx.env.http_host;
50 if (!ctx.env.server_name)
52 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
53 return ctx.env.server_name;
54 return xstrdup(fmt("%s:%s", ctx.env.server_name, ctx.env.server_port));
57 const char *cgit_rooturl()
59 if (ctx.cfg.virtual_root)
60 return fmt("%s/", ctx.cfg.virtual_root);
62 return ctx.cfg.script_name;
65 char *cgit_repourl(const char *reponame)
67 if (ctx.cfg.virtual_root) {
68 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
70 return fmt("?r=%s", reponame);
74 char *cgit_fileurl(const char *reponame, const char *pagename,
75 const char *filename, const char *query)
80 if (ctx.cfg.virtual_root) {
81 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
82 pagename, (filename ? filename:""));
85 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
86 (filename ? filename : ""));
90 tmp = fmt("%s%s%s", tmp, delim, query);
94 char *cgit_pageurl(const char *reponame, const char *pagename,
97 return cgit_fileurl(reponame, pagename, 0, query);
100 const char *cgit_repobasename(const char *reponame)
102 /* I assume we don't need to store more than one repo basename */
103 static char rvbuf[1024];
106 strncpy(rvbuf, reponame, sizeof(rvbuf));
107 if (rvbuf[sizeof(rvbuf)-1])
108 die("cgit_repobasename: truncated repository name '%s'", reponame);
110 /* strip trailing slashes */
111 while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
112 /* strip trailing .git */
113 if (p >= 3 && !strncmp(&rvbuf[p-3], ".git", 4)) {
114 p -= 3; rvbuf[p--] = 0;
116 /* strip more trailing slashes if any */
117 while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
118 /* find last slash in the remaining string */
119 rv = strrchr(rvbuf,'/');
125 static void site_url(const char *page, const char *search, const char *sort, int ofs)
129 if (ctx.cfg.virtual_root) {
130 html_attr(ctx.cfg.virtual_root);
131 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
134 html(ctx.cfg.script_name);
137 htmlf("?p=%s", page);
154 htmlf("ofs=%d", ofs);
158 static void site_link(const char *page, const char *name, const char *title,
159 const char *class, const char *search, const char *sort, int ofs)
173 site_url(page, search, sort, ofs);
179 void cgit_index_link(const char *name, const char *title, const char *class,
180 const char *pattern, const char *sort, int ofs)
182 site_link(NULL, name, title, class, pattern, sort, ofs);
185 static char *repolink(const char *title, const char *class, const char *page,
186 const char *head, const char *path)
202 if (ctx.cfg.virtual_root) {
203 html_url_path(ctx.cfg.virtual_root);
204 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
206 html_url_path(ctx.repo->url);
207 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
216 html(ctx.cfg.script_name);
218 html_url_arg(ctx.repo->url);
219 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
229 if (head && strcmp(head, ctx.repo->defbranch)) {
235 return fmt("%s", delim);
238 static void reporevlink(const char *page, const char *name, const char *title,
239 const char *class, const char *head, const char *rev,
244 delim = repolink(title, class, page, head, path);
245 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
255 void cgit_summary_link(const char *name, const char *title, const char *class,
258 reporevlink(NULL, name, title, class, head, NULL, NULL);
261 void cgit_tag_link(const char *name, const char *title, const char *class,
262 const char *head, const char *rev)
264 reporevlink("tag", name, title, class, head, rev, NULL);
267 void cgit_tree_link(const char *name, const char *title, const char *class,
268 const char *head, const char *rev, const char *path)
270 reporevlink("tree", name, title, class, head, rev, path);
273 void cgit_plain_link(const char *name, const char *title, const char *class,
274 const char *head, const char *rev, const char *path)
276 reporevlink("plain", name, title, class, head, rev, path);
279 void cgit_log_link(const char *name, const char *title, const char *class,
280 const char *head, const char *rev, const char *path,
281 int ofs, const char *grep, const char *pattern, int showmsg)
285 delim = repolink(title, class, "log", head, path);
286 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
292 if (grep && pattern) {
299 html_url_arg(pattern);
316 void cgit_commit_link(char *name, const char *title, const char *class,
317 const char *head, const char *rev, const char *path,
320 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
321 name[ctx.cfg.max_msg_len] = '\0';
322 name[ctx.cfg.max_msg_len - 1] = '.';
323 name[ctx.cfg.max_msg_len - 2] = '.';
324 name[ctx.cfg.max_msg_len - 3] = '.';
329 delim = repolink(title, class, "commit", head, path);
330 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
336 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
341 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
344 htmlf("%d", ctx.qry.context);
347 if (ctx.qry.ignorews) {
356 html_txt("(no commit message)");
360 void cgit_refs_link(const char *name, const char *title, const char *class,
361 const char *head, const char *rev, const char *path)
363 reporevlink("refs", name, title, class, head, rev, path);
366 void cgit_snapshot_link(const char *name, const char *title, const char *class,
367 const char *head, const char *rev,
368 const char *archivename)
370 reporevlink("snapshot", name, title, class, head, rev, archivename);
373 void cgit_diff_link(const char *name, const char *title, const char *class,
374 const char *head, const char *new_rev, const char *old_rev,
375 const char *path, int toggle_ssdiff)
379 delim = repolink(title, class, "diff", head, path);
380 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
383 html_url_arg(new_rev);
389 html_url_arg(old_rev);
392 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
397 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
400 htmlf("%d", ctx.qry.context);
403 if (ctx.qry.ignorews) {
413 void cgit_patch_link(const char *name, const char *title, const char *class,
414 const char *head, const char *rev, const char *path)
416 reporevlink("patch", name, title, class, head, rev, path);
419 void cgit_stats_link(const char *name, const char *title, const char *class,
420 const char *head, const char *path)
422 reporevlink("stats", name, title, class, head, NULL, path);
425 static void cgit_self_link(char *name, const char *title, const char *class,
426 struct cgit_context *ctx)
428 if (!strcmp(ctx->qry.page, "repolist"))
429 cgit_index_link(name, title, class, ctx->qry.search, ctx->qry.sort,
431 else if (!strcmp(ctx->qry.page, "summary"))
432 cgit_summary_link(name, title, class, ctx->qry.head);
433 else if (!strcmp(ctx->qry.page, "tag"))
434 cgit_tag_link(name, title, class, ctx->qry.head,
435 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL);
436 else if (!strcmp(ctx->qry.page, "tree"))
437 cgit_tree_link(name, title, class, ctx->qry.head,
438 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
440 else if (!strcmp(ctx->qry.page, "plain"))
441 cgit_plain_link(name, title, class, ctx->qry.head,
442 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
444 else if (!strcmp(ctx->qry.page, "log"))
445 cgit_log_link(name, title, class, ctx->qry.head,
446 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
447 ctx->qry.path, ctx->qry.ofs,
448 ctx->qry.grep, ctx->qry.search,
450 else if (!strcmp(ctx->qry.page, "commit"))
451 cgit_commit_link(name, title, class, ctx->qry.head,
452 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
454 else if (!strcmp(ctx->qry.page, "patch"))
455 cgit_patch_link(name, title, class, ctx->qry.head,
456 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
458 else if (!strcmp(ctx->qry.page, "refs"))
459 cgit_refs_link(name, title, class, ctx->qry.head,
460 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
462 else if (!strcmp(ctx->qry.page, "snapshot"))
463 cgit_snapshot_link(name, title, class, ctx->qry.head,
464 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
466 else if (!strcmp(ctx->qry.page, "diff"))
467 cgit_diff_link(name, title, class, ctx->qry.head,
468 ctx->qry.sha1, ctx->qry.sha2,
470 else if (!strcmp(ctx->qry.page, "stats"))
471 cgit_stats_link(name, title, class, ctx->qry.head,
474 /* Don't known how to make link for this page */
475 repolink(title, class, ctx->qry.page, ctx->qry.head, ctx->qry.path);
476 html("><!-- cgit_self_link() doesn't know how to make link for page '");
477 html_txt(ctx->qry.page);
484 void cgit_object_link(struct object *obj)
486 char *page, *shortrev, *fullrev, *name;
488 fullrev = sha1_to_hex(obj->sha1);
489 shortrev = xstrdup(fullrev);
491 if (obj->type == OBJ_COMMIT) {
492 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
493 ctx.qry.head, fullrev, NULL, 0);
495 } else if (obj->type == OBJ_TREE)
497 else if (obj->type == OBJ_TAG)
501 name = fmt("%s %s...", typename(obj->type), shortrev);
502 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
505 static struct string_list_item *lookup_path(struct string_list *list,
508 struct string_list_item *item;
510 while (path && path[0]) {
511 if ((item = string_list_lookup(list, path)))
513 if (!(path = strchr(path, '/')))
520 void cgit_submodule_link(const char *class, char *path, const char *rev)
522 struct string_list *list;
523 struct string_list_item *item;
529 list = &ctx.repo->submodules;
530 item = lookup_path(list, path);
533 tail = path[len - 1];
536 item = lookup_path(list, path);
541 htmlf("class='%s' ", class);
544 html_attr(fmt(item->util, rev));
545 } else if (ctx.repo->module_link) {
546 dir = strrchr(path, '/');
551 html_attr(fmt(ctx.repo->module_link, dir, rev));
558 html_txt(fmt(" @ %.7s", rev));
560 path[len - 1] = tail;
563 void cgit_print_date(time_t secs, const char *format, int local_time)
571 time = localtime(&secs);
573 time = gmtime(&secs);
574 strftime(buf, sizeof(buf)-1, format, time);
578 void cgit_print_age(time_t t, time_t max_relative, const char *format)
587 if (secs > max_relative && max_relative >= 0) {
588 cgit_print_date(t, format, ctx.cfg.local_time);
592 if (secs < TM_HOUR * 2) {
593 htmlf("<span class='age-mins'>%.0f min.</span>",
594 secs * 1.0 / TM_MIN);
597 if (secs < TM_DAY * 2) {
598 htmlf("<span class='age-hours'>%.0f hours</span>",
599 secs * 1.0 / TM_HOUR);
602 if (secs < TM_WEEK * 2) {
603 htmlf("<span class='age-days'>%.0f days</span>",
604 secs * 1.0 / TM_DAY);
607 if (secs < TM_MONTH * 2) {
608 htmlf("<span class='age-weeks'>%.0f weeks</span>",
609 secs * 1.0 / TM_WEEK);
612 if (secs < TM_YEAR * 2) {
613 htmlf("<span class='age-months'>%.0f months</span>",
614 secs * 1.0 / TM_MONTH);
617 htmlf("<span class='age-years'>%.0f years</span>",
618 secs * 1.0 / TM_YEAR);
621 void cgit_print_http_headers(struct cgit_context *ctx)
623 if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1"))
626 if (ctx->page.status)
627 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg);
628 if (ctx->page.mimetype && ctx->page.charset)
629 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
631 else if (ctx->page.mimetype)
632 htmlf("Content-Type: %s\n", ctx->page.mimetype);
634 htmlf("Content-Length: %zd\n", ctx->page.size);
635 if (ctx->page.filename)
636 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
638 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
639 htmlf("Expires: %s\n", http_date(ctx->page.expires));
641 htmlf("ETag: \"%s\"\n", ctx->page.etag);
643 if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD"))
647 void cgit_print_docstart(struct cgit_context *ctx)
649 if (ctx->cfg.embedded) {
651 html_include(ctx->cfg.header);
655 const char *host = cgit_hosturl();
657 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
660 html_txt(ctx->page.title);
662 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
663 if (ctx->cfg.robots && *ctx->cfg.robots)
664 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
665 html("<link rel='stylesheet' type='text/css' href='");
666 html_attr(ctx->cfg.css);
668 if (ctx->cfg.favicon) {
669 html("<link rel='shortcut icon' href='");
670 html_attr(ctx->cfg.favicon);
673 if (host && ctx->repo && ctx->qry.head) {
674 html("<link rel='alternate' title='Atom feed' href='");
675 html(cgit_httpscheme());
676 html_attr(cgit_hosturl());
677 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
678 fmt("h=%s", ctx->qry.head)));
679 html("' type='application/atom+xml'/>\n");
681 if (ctx->cfg.head_include)
682 html_include(ctx->cfg.head_include);
686 html_include(ctx->cfg.header);
689 void cgit_print_docend()
691 html("</div> <!-- class=content -->\n");
692 if (ctx.cfg.embedded) {
693 html("</div> <!-- id=cgit -->\n");
695 html_include(ctx.cfg.footer);
699 html_include(ctx.cfg.footer);
701 htmlf("<div class='footer'>generated by cgit %s at ",
703 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
706 html("</div> <!-- id=cgit -->\n");
707 html("</body>\n</html>\n");
710 static int print_branch_option(const char *refname, const unsigned char *sha1,
711 int flags, void *cb_data)
713 char *name = (char *)refname;
714 html_option(name, name, ctx.qry.head);
718 void cgit_add_hidden_formfields(int incl_head, int incl_search,
723 if (!ctx.cfg.virtual_root) {
724 url = fmt("%s/%s", ctx.qry.repo, page);
726 url = fmt("%s/%s", url, ctx.qry.vpath);
727 html_hidden("url", url);
730 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
731 strcmp(ctx.qry.head, ctx.repo->defbranch))
732 html_hidden("h", ctx.qry.head);
735 html_hidden("id", ctx.qry.sha1);
737 html_hidden("id2", ctx.qry.sha2);
739 html_hidden("showmsg", "1");
743 html_hidden("qt", ctx.qry.grep);
745 html_hidden("q", ctx.qry.search);
749 static const char *hc(struct cgit_context *ctx, const char *page)
751 return strcmp(ctx->qry.page, page) ? NULL : "active";
754 static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
756 char *old_path = ctx->qry.path;
757 char *p = path, *q, *end = path + strlen(path);
759 ctx->qry.path = NULL;
760 cgit_self_link("root", NULL, NULL, ctx);
761 ctx->qry.path = p = path;
763 if (!(q = strchr(p, '/')))
767 cgit_self_link(p, NULL, NULL, ctx);
772 ctx->qry.path = old_path;
775 static void print_header(struct cgit_context *ctx)
777 char *logo = NULL, *logo_link = NULL;
779 html("<table id='header'>\n");
782 if (ctx->repo && ctx->repo->logo && *ctx->repo->logo)
783 logo = ctx->repo->logo;
785 logo = ctx->cfg.logo;
786 if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link)
787 logo_link = ctx->repo->logo_link;
789 logo_link = ctx->cfg.logo_link;
791 html("<td class='logo' rowspan='2'><a href='");
792 if (logo_link && *logo_link)
793 html_attr(logo_link);
795 html_attr(cgit_rooturl());
796 html("'><img src='");
798 html("' alt='cgit logo'/></a></td>\n");
801 html("<td class='main'>");
803 cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
805 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
806 html("</td><td class='form'>");
807 html("<form method='get' action=''>\n");
808 cgit_add_hidden_formfields(0, 1, ctx->qry.page);
809 html("<select name='h' onchange='this.form.submit();'>\n");
810 for_each_branch_ref(print_branch_option, ctx->qry.head);
812 html("<input type='submit' name='' value='switch'/>");
815 html_txt(ctx->cfg.root_title);
816 html("</td></tr>\n");
818 html("<tr><td class='sub'>");
820 html_txt(ctx->repo->desc);
821 html("</td><td class='sub right'>");
822 html_txt(ctx->repo->owner);
824 if (ctx->cfg.root_desc)
825 html_txt(ctx->cfg.root_desc);
826 else if (ctx->cfg.index_info)
827 html_include(ctx->cfg.index_info);
829 html("</td></tr></table>\n");
832 void cgit_print_pageheader(struct cgit_context *ctx)
834 html("<div id='cgit'>");
835 if (!ctx->cfg.noheader)
838 html("<table class='tabs'><tr><td>\n");
840 cgit_summary_link("summary", NULL, hc(ctx, "summary"),
842 cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head,
843 ctx->qry.sha1, NULL);
844 cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head,
845 NULL, ctx->qry.vpath, 0, NULL, NULL,
847 cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head,
848 ctx->qry.sha1, ctx->qry.vpath);
849 cgit_commit_link("commit", NULL, hc(ctx, "commit"),
850 ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0);
851 cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head,
852 ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0);
853 if (ctx->repo->max_stats)
854 cgit_stats_link("stats", NULL, hc(ctx, "stats"),
855 ctx->qry.head, ctx->qry.vpath);
856 if (ctx->repo->readme)
857 reporevlink("about", "about", NULL,
858 hc(ctx, "about"), ctx->qry.head, NULL,
860 html("</td><td class='form'>");
861 html("<form class='right' method='get' action='");
862 if (ctx->cfg.virtual_root)
863 html_url_path(cgit_fileurl(ctx->qry.repo, "log",
864 ctx->qry.vpath, NULL));
866 cgit_add_hidden_formfields(1, 0, "log");
867 html("<select name='qt'>\n");
868 html_option("grep", "log msg", ctx->qry.grep);
869 html_option("author", "author", ctx->qry.grep);
870 html_option("committer", "committer", ctx->qry.grep);
871 html_option("range", "range", ctx->qry.grep);
873 html("<input class='txt' type='text' size='10' name='q' value='");
874 html_attr(ctx->qry.search);
876 html("<input type='submit' value='search'/>\n");
879 site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, NULL, 0);
880 if (ctx->cfg.root_readme)
881 site_link("about", "about", NULL, hc(ctx, "about"),
883 html("</td><td class='form'>");
884 html("<form method='get' action='");
885 html_attr(cgit_rooturl());
887 html("<input type='text' name='q' size='10' value='");
888 html_attr(ctx->qry.search);
890 html("<input type='submit' value='search'/>\n");
893 html("</td></tr></table>\n");
894 if (ctx->qry.vpath) {
895 html("<div class='path'>");
897 cgit_print_path_crumbs(ctx, ctx->qry.vpath);
900 html("<div class='content'>");
903 void cgit_print_filemode(unsigned short mode)
907 else if (S_ISLNK(mode))
909 else if (S_ISGITLINK(mode))
913 html_fileperm(mode >> 6);
914 html_fileperm(mode >> 3);
918 void cgit_print_snapshot_links(const char *repo, const char *head,
919 const char *hex, int snapshots)
921 const struct cgit_snapshot_format* f;
924 unsigned char sha1[20];
926 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
927 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
929 prefix = xstrdup(fmt("%s-%s", cgit_repobasename(repo), hex));
930 for (f = cgit_snapshot_formats; f->suffix; f++) {
931 if (!(snapshots & f->bit))
933 filename = fmt("%s%s", prefix, f->suffix);
934 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);