1 /* ui-shared.c: common web output functions
3 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
9 #define USE_THE_REPOSITORY_VARIABLE
12 #include "ui-shared.h"
17 static const char cgit_doctype[] =
20 static char *http_date(time_t t)
22 static char day[][4] =
23 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
24 static char month[][4] =
25 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
26 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
29 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm.tm_wday],
30 tm.tm_mday, month[tm.tm_mon], 1900 + tm.tm_year,
31 tm.tm_hour, tm.tm_min, tm.tm_sec);
34 void cgit_print_error(const char *fmt, ...)
38 cgit_vprint_error(fmt, ap);
42 void cgit_vprint_error(const char *fmt, va_list ap)
45 html("<div class='error'>");
52 const char *cgit_httpscheme(void)
54 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
60 char *cgit_hosturl(void)
62 if (ctx.env.http_host)
63 return xstrdup(ctx.env.http_host);
64 if (!ctx.env.server_name)
66 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
67 return xstrdup(ctx.env.server_name);
68 return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
71 char *cgit_currenturl(void)
73 const char *root = cgit_rooturl();
77 if (root[0] && root[strlen(root) - 1] == '/')
78 return fmtalloc("%s%s", root, ctx.qry.url);
79 return fmtalloc("%s/%s", root, ctx.qry.url);
82 char *cgit_currentfullurl(void)
84 const char *root = cgit_rooturl();
85 const char *orig_query = ctx.env.query_string ? ctx.env.query_string : "";
86 size_t len = strlen(orig_query);
87 char *query = xmalloc(len + 2), *start_url, *ret;
89 /* Remove all url=... parts from query string */
90 memcpy(query + 1, orig_query, len + 1);
93 while ((start_url = strstr(start_url, "url=")) != NULL) {
94 if (start_url[-1] == '?' || start_url[-1] == '&') {
95 const char *end_url = strchr(start_url, '&');
97 memmove(start_url, end_url + 1, strlen(end_url));
107 ret = fmtalloc("%s%s", root, query);
108 else if (root[0] && root[strlen(root) - 1] == '/')
109 ret = fmtalloc("%s%s%s", root, ctx.qry.url, query);
111 ret = fmtalloc("%s/%s%s", root, ctx.qry.url, query);
116 const char *cgit_rooturl(void)
118 if (ctx.cfg.virtual_root)
119 return ctx.cfg.virtual_root;
121 return ctx.cfg.script_name;
124 const char *cgit_loginurl(void)
126 static const char *login_url;
128 login_url = fmtalloc("%s?p=login", cgit_rooturl());
132 char *cgit_repourl(const char *reponame)
134 if (ctx.cfg.virtual_root)
135 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
137 return fmtalloc("?r=%s", reponame);
140 char *cgit_fileurl(const char *reponame, const char *pagename,
141 const char *filename, const char *query)
143 struct strbuf sb = STRBUF_INIT;
146 if (ctx.cfg.virtual_root) {
147 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
148 pagename, (filename ? filename:""));
151 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
152 (filename ? filename : ""));
156 strbuf_addf(&sb, "%s%s", delim, query);
157 return strbuf_detach(&sb, NULL);
160 char *cgit_pageurl(const char *reponame, const char *pagename,
163 return cgit_fileurl(reponame, pagename, NULL, query);
166 const char *cgit_repobasename(const char *reponame)
168 /* I assume we don't need to store more than one repo basename */
169 static char rvbuf[1024];
174 len = strlcpy(rvbuf, reponame, sizeof(rvbuf));
175 if (len >= sizeof(rvbuf))
176 die("cgit_repobasename: truncated repository name '%s'", reponame);
178 /* strip trailing slashes */
179 while (p && rvbuf[p] == '/')
181 /* strip trailing .git */
182 if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
186 /* strip more trailing slashes if any */
187 while (p && rvbuf[p] == '/')
189 /* find last slash in the remaining string */
190 rv = strrchr(rvbuf, '/');
196 const char *cgit_snapshot_prefix(const struct cgit_repo *repo)
198 if (repo->snapshot_prefix)
199 return repo->snapshot_prefix;
201 return cgit_repobasename(repo->url);
204 static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
208 if (always_root || page)
209 html_attr(cgit_rooturl());
211 char *currenturl = cgit_currenturl();
212 html_attr(currenturl);
217 htmlf("?p=%s", page);
234 htmlf("ofs=%d", ofs);
238 static void site_link(const char *page, const char *name, const char *title,
239 const char *class, const char *search, const char *sort, int ofs, int always_root)
253 site_url(page, search, sort, ofs, always_root);
259 void cgit_index_link(const char *name, const char *title, const char *class,
260 const char *pattern, const char *sort, int ofs, int always_root)
262 site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
265 static char *repolink(const char *title, const char *class, const char *page,
266 const char *head, const char *path)
282 if (ctx.cfg.virtual_root) {
283 html_url_path(ctx.cfg.virtual_root);
284 html_url_path(ctx.repo->url);
285 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
294 html_url_path(ctx.cfg.script_name);
296 html_url_arg(ctx.repo->url);
297 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
307 if (head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch)) {
313 return fmt("%s", delim);
316 static void reporevlink(const char *page, const char *name, const char *title,
317 const char *class, const char *head, const char *rev,
322 delim = repolink(title, class, page, head, path);
323 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
333 void cgit_summary_link(const char *name, const char *title, const char *class,
336 reporevlink(NULL, name, title, class, head, NULL, NULL);
339 void cgit_tag_link(const char *name, const char *title, const char *class,
342 reporevlink("tag", name, title, class, tag, NULL, NULL);
345 void cgit_tree_link(const char *name, const char *title, const char *class,
346 const char *head, const char *rev, const char *path)
348 reporevlink("tree", name, title, class, head, rev, path);
351 void cgit_plain_link(const char *name, const char *title, const char *class,
352 const char *head, const char *rev, const char *path)
354 reporevlink("plain", name, title, class, head, rev, path);
357 void cgit_blame_link(const char *name, const char *title, const char *class,
358 const char *head, const char *rev, const char *path)
360 reporevlink("blame", name, title, class, head, rev, path);
363 void cgit_log_link(const char *name, const char *title, const char *class,
364 const char *head, const char *rev, const char *path,
365 int ofs, const char *grep, const char *pattern, int showmsg,
370 delim = repolink(title, class, "log", head, path);
371 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
377 if (grep && pattern) {
384 html_url_arg(pattern);
406 void cgit_commit_link(const char *name, const char *title, const char *class,
407 const char *head, const char *rev, const char *path)
411 delim = repolink(title, class, "commit", head, path);
412 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
418 if (ctx.qry.difftype) {
420 htmlf("dt=%d", ctx.qry.difftype);
423 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
426 htmlf("%d", ctx.qry.context);
429 if (ctx.qry.ignorews) {
434 if (ctx.qry.follow) {
439 if (name[0] != '\0') {
440 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
441 html_ntxt(name, ctx.cfg.max_msg_len - 3);
446 html_txt("(no commit message)");
450 void cgit_refs_link(const char *name, const char *title, const char *class,
451 const char *head, const char *rev, const char *path)
453 reporevlink("refs", name, title, class, head, rev, path);
456 void cgit_snapshot_link(const char *name, const char *title, const char *class,
457 const char *head, const char *rev,
458 const char *archivename)
460 reporevlink("snapshot", name, title, class, head, rev, archivename);
463 void cgit_diff_link(const char *name, const char *title, const char *class,
464 const char *head, const char *new_rev, const char *old_rev,
469 delim = repolink(title, class, "diff", head, path);
470 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
473 html_url_arg(new_rev);
479 html_url_arg(old_rev);
482 if (ctx.qry.difftype) {
484 htmlf("dt=%d", ctx.qry.difftype);
487 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
490 htmlf("%d", ctx.qry.context);
493 if (ctx.qry.ignorews) {
498 if (ctx.qry.follow) {
507 void cgit_patch_link(const char *name, const char *title, const char *class,
508 const char *head, const char *rev, const char *path)
510 reporevlink("patch", name, title, class, head, rev, path);
513 void cgit_stats_link(const char *name, const char *title, const char *class,
514 const char *head, const char *path)
516 reporevlink("stats", name, title, class, head, NULL, path);
519 static void cgit_self_link(char *name, const char *title, const char *class)
521 if (!strcmp(ctx.qry.page, "repolist"))
522 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
524 else if (!strcmp(ctx.qry.page, "summary"))
525 cgit_summary_link(name, title, class, ctx.qry.head);
526 else if (!strcmp(ctx.qry.page, "tag"))
527 cgit_tag_link(name, title, class, ctx.qry.has_oid ?
528 ctx.qry.oid : ctx.qry.head);
529 else if (!strcmp(ctx.qry.page, "tree"))
530 cgit_tree_link(name, title, class, ctx.qry.head,
531 ctx.qry.has_oid ? ctx.qry.oid : NULL,
533 else if (!strcmp(ctx.qry.page, "plain"))
534 cgit_plain_link(name, title, class, ctx.qry.head,
535 ctx.qry.has_oid ? ctx.qry.oid : NULL,
537 else if (!strcmp(ctx.qry.page, "blame"))
538 cgit_blame_link(name, title, class, ctx.qry.head,
539 ctx.qry.has_oid ? ctx.qry.oid : NULL,
541 else if (!strcmp(ctx.qry.page, "log"))
542 cgit_log_link(name, title, class, ctx.qry.head,
543 ctx.qry.has_oid ? ctx.qry.oid : NULL,
544 ctx.qry.path, ctx.qry.ofs,
545 ctx.qry.grep, ctx.qry.search,
546 ctx.qry.showmsg, ctx.qry.follow);
547 else if (!strcmp(ctx.qry.page, "commit"))
548 cgit_commit_link(name, title, class, ctx.qry.head,
549 ctx.qry.has_oid ? ctx.qry.oid : NULL,
551 else if (!strcmp(ctx.qry.page, "patch"))
552 cgit_patch_link(name, title, class, ctx.qry.head,
553 ctx.qry.has_oid ? ctx.qry.oid : NULL,
555 else if (!strcmp(ctx.qry.page, "refs"))
556 cgit_refs_link(name, title, class, ctx.qry.head,
557 ctx.qry.has_oid ? ctx.qry.oid : NULL,
559 else if (!strcmp(ctx.qry.page, "snapshot"))
560 cgit_snapshot_link(name, title, class, ctx.qry.head,
561 ctx.qry.has_oid ? ctx.qry.oid : NULL,
563 else if (!strcmp(ctx.qry.page, "diff"))
564 cgit_diff_link(name, title, class, ctx.qry.head,
565 ctx.qry.oid, ctx.qry.oid2,
567 else if (!strcmp(ctx.qry.page, "stats"))
568 cgit_stats_link(name, title, class, ctx.qry.head,
571 /* Don't known how to make link for this page */
572 repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
573 html("><!-- cgit_self_link() doesn't know how to make link for page '");
574 html_txt(ctx.qry.page);
581 void cgit_object_link(struct object *obj)
583 char *page, *shortrev, *fullrev, *name;
585 fullrev = oid_to_hex(&obj->oid);
586 shortrev = xstrdup(fullrev);
588 if (obj->type == OBJ_COMMIT) {
589 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
590 ctx.qry.head, fullrev, NULL);
592 } else if (obj->type == OBJ_TREE)
594 else if (obj->type == OBJ_TAG)
598 name = fmt("%s %s...", type_name(obj->type), shortrev);
599 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
602 static struct string_list_item *lookup_path(struct string_list *list,
605 struct string_list_item *item;
607 while (path && path[0]) {
608 if ((item = string_list_lookup(list, path)))
610 if (!(path = strchr(path, '/')))
617 void cgit_submodule_link(const char *class, char *path, const char *rev)
619 struct string_list *list;
620 struct string_list_item *item;
626 list = &ctx.repo->submodules;
627 item = lookup_path(list, path);
630 tail = path[len - 1];
633 item = lookup_path(list, path);
636 if (item || ctx.repo->module_link) {
639 htmlf("class='%s' ", class);
642 html_attrf(item->util, rev);
644 dir = strrchr(path, '/');
649 html_attrf(ctx.repo->module_link, dir, rev);
657 htmlf(" class='%s'", class);
662 html_txtf(" @ %.7s", rev);
664 path[len - 1] = tail;
667 const struct date_mode cgit_date_mode(enum date_mode_type type)
669 static struct date_mode mode;
671 mode.local = ctx.cfg.local_time;
675 static void print_rel_date(time_t t, int tz, double value,
676 const char *class, const char *suffix)
678 htmlf("<span class='%s' data-ut='%" PRIu64 "' title='", class, (uint64_t)t);
679 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
680 htmlf("'>%.0f %s</span>", value, suffix);
683 void cgit_print_age(time_t t, int tz, time_t max_relative)
694 if (secs > max_relative && max_relative >= 0) {
695 html("<span title='");
696 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
698 html_txt(show_date(t, tz, cgit_date_mode(DATE_SHORT)));
703 if (secs < TM_HOUR * 2) {
704 print_rel_date(t, tz, secs * 1.0 / TM_MIN, "age-mins", "min.");
707 if (secs < TM_DAY * 2) {
708 print_rel_date(t, tz, secs * 1.0 / TM_HOUR, "age-hours", "hours");
711 if (secs < TM_WEEK * 2) {
712 print_rel_date(t, tz, secs * 1.0 / TM_DAY, "age-days", "days");
715 if (secs < TM_MONTH * 2) {
716 print_rel_date(t, tz, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
719 if (secs < TM_YEAR * 2) {
720 print_rel_date(t, tz, secs * 1.0 / TM_MONTH, "age-months", "months");
723 print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years");
726 void cgit_print_http_headers(void)
728 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
732 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
733 if (ctx.page.mimetype && ctx.page.charset)
734 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
736 else if (ctx.page.mimetype)
737 htmlf("Content-Type: %s\n", ctx.page.mimetype);
739 htmlf("Content-Length: %zd\n", ctx.page.size);
740 if (ctx.page.filename) {
741 html("Content-Disposition: inline; filename=\"");
742 html_header_arg_in_quotes(ctx.page.filename);
745 if (!ctx.env.authenticated)
746 html("Cache-Control: no-cache, no-store\n");
747 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
748 htmlf("Expires: %s\n", http_date(ctx.page.expires));
750 htmlf("ETag: \"%s\"\n", ctx.page.etag);
752 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
756 void cgit_redirect(const char *url, bool permanent)
758 htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
764 static void print_rel_vcs_link(const char *url)
766 html("<link rel='vcs-git' href='");
769 html_attr(ctx.repo->name);
770 html(" Git repository'/>\n");
773 static int emit_css_link(struct string_list_item *s, void *arg)
775 /* Do not emit anything if css= is specified. */
776 if (s && *s->string == '\0')
779 html("<link rel='stylesheet' type='text/css' href='");
781 html_attr(s->string);
783 html_attr((const char *)arg);
789 static int emit_js_link(struct string_list_item *s, void *arg)
791 /* Do not emit anything if js= is specified. */
792 if (s && *s->string == '\0')
795 html("<script type='text/javascript' src='");
797 html_attr(s->string);
799 html_attr((const char *)arg);
800 html("'></script>\n");
805 void cgit_print_docstart(void)
807 char *host = cgit_hosturl();
809 if (ctx.cfg.embedded) {
811 html_include(ctx.cfg.header);
816 html("<html lang='en'>\n");
819 html_txt(ctx.page.title);
821 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
822 if (ctx.cfg.robots && *ctx.cfg.robots)
823 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
825 if (ctx.cfg.css.items)
826 for_each_string_list(&ctx.cfg.css, emit_css_link, NULL);
828 emit_css_link(NULL, "/cgit.css");
830 if (ctx.cfg.js.items)
831 for_each_string_list(&ctx.cfg.js, emit_js_link, NULL);
833 emit_js_link(NULL, "/cgit.js");
835 if (ctx.cfg.favicon) {
836 html("<link rel='shortcut icon' href='");
837 html_attr(ctx.cfg.favicon);
840 if (host && ctx.repo && ctx.qry.head) {
842 struct strbuf sb = STRBUF_INIT;
843 strbuf_addf(&sb, "h=%s", ctx.qry.head);
845 html("<link rel='alternate' title='Atom feed' href='");
846 html(cgit_httpscheme());
848 fileurl = cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
851 html("' type='application/atom+xml'/>\n");
856 cgit_add_clone_urls(print_rel_vcs_link);
857 if (ctx.cfg.head_include)
858 html_include(ctx.cfg.head_include);
859 if (ctx.repo && ctx.repo->extra_head_content)
860 html(ctx.repo->extra_head_content);
864 html_include(ctx.cfg.header);
868 void cgit_print_docend(void)
870 html("</div> <!-- class=content -->\n");
871 if (ctx.cfg.embedded) {
872 html("</div> <!-- id=cgit -->\n");
874 html_include(ctx.cfg.footer);
878 html_include(ctx.cfg.footer);
880 htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
881 "(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version, git_version_string);
882 html_txt(show_date(time(NULL), 0, cgit_date_mode(DATE_ISO8601)));
885 html("</div> <!-- id=cgit -->\n");
886 html("</body>\n</html>\n");
889 void cgit_print_error_page(int code, const char *msg, const char *fmt, ...)
892 ctx.page.expires = ctx.cfg.cache_dynamic_ttl;
893 ctx.page.status = code;
894 ctx.page.statusmsg = msg;
895 cgit_print_layout_start();
897 cgit_vprint_error(fmt, ap);
899 cgit_print_layout_end();
902 void cgit_print_layout_start(void)
904 cgit_print_http_headers();
905 cgit_print_docstart();
906 cgit_print_pageheader();
909 void cgit_print_layout_end(void)
914 static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
916 struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
919 for (i = 0; url_list[i]; i++) {
920 strbuf_rtrim(url_list[i]);
921 if (url_list[i]->len == 0)
923 if (suffix && *suffix)
924 strbuf_addf(url_list[i], "/%s", suffix);
925 fn(url_list[i]->buf);
928 strbuf_list_free(url_list);
931 void cgit_add_clone_urls(void (*fn)(const char *))
933 if (ctx.repo->clone_url)
934 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
935 else if (ctx.cfg.clone_prefix)
936 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
939 static int print_branch_option(const char *refname, const struct object_id *oid,
940 int flags, void *cb_data)
942 char *name = (char *)refname;
943 html_option(name, name, ctx.qry.head);
947 void cgit_add_hidden_formfields(int incl_head, int incl_search,
950 if (!ctx.cfg.virtual_root) {
951 struct strbuf url = STRBUF_INIT;
953 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
955 strbuf_addf(&url, "/%s", ctx.qry.vpath);
956 html_hidden("url", url.buf);
957 strbuf_release(&url);
960 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
961 strcmp(ctx.qry.head, ctx.repo->defbranch))
962 html_hidden("h", ctx.qry.head);
965 html_hidden("id", ctx.qry.oid);
967 html_hidden("id2", ctx.qry.oid2);
969 html_hidden("showmsg", "1");
973 html_hidden("qt", ctx.qry.grep);
975 html_hidden("q", ctx.qry.search);
979 static const char *hc(const char *page)
984 return strcmp(ctx.qry.page, page) ? NULL : "active";
987 static void cgit_print_path_crumbs(char *path)
989 char *old_path = ctx.qry.path;
990 char *p = path, *q, *end = path + strlen(path);
994 cgit_self_link("root", NULL, NULL);
995 ctx.qry.path = p = path;
997 if (!(q = strchr(p, '/')) || levels > 15)
1001 cgit_self_link(p, NULL, NULL);
1007 ctx.qry.path = old_path;
1010 static void print_header(void)
1012 char *logo = NULL, *logo_link = NULL;
1014 html("<table id='header'>\n");
1017 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
1018 logo = ctx.repo->logo;
1020 logo = ctx.cfg.logo;
1021 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
1022 logo_link = ctx.repo->logo_link;
1024 logo_link = ctx.cfg.logo_link;
1025 if (logo && *logo) {
1026 html("<td class='logo' rowspan='2'><a href='");
1027 if (logo_link && *logo_link)
1028 html_attr(logo_link);
1030 html_attr(cgit_rooturl());
1031 html("'><img src='");
1033 html("' alt='cgit logo'/></a></td>\n");
1036 html("<td class='main'>");
1038 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
1040 cgit_summary_link(ctx.repo->name, NULL, NULL, NULL);
1041 if (ctx.env.authenticated) {
1042 html("</td><td class='form'>");
1043 html("<form method='get'>\n");
1044 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
1045 html("<select name='h' onchange='this.form.submit();'>\n");
1046 refs_for_each_branch_ref(get_main_ref_store(the_repository),
1047 print_branch_option, ctx.qry.head);
1048 if (ctx.repo->enable_remote_branches)
1049 refs_for_each_remote_ref(get_main_ref_store(the_repository),
1050 print_branch_option, ctx.qry.head);
1052 html("<input type='submit' value='switch'/>");
1056 html_txt(ctx.cfg.root_title);
1057 html("</td></tr>\n");
1059 html("<tr><td class='sub'>");
1061 html_txt(ctx.repo->desc);
1062 html("</td><td class='sub right'>");
1063 if (ctx.repo->owner_filter) {
1064 cgit_open_filter(ctx.repo->owner_filter);
1065 html_txt(ctx.repo->owner);
1066 cgit_close_filter(ctx.repo->owner_filter);
1068 html_txt(ctx.repo->owner);
1071 if (ctx.cfg.root_desc)
1072 html_txt(ctx.cfg.root_desc);
1074 html("</td></tr></table>\n");
1077 void cgit_print_pageheader(void)
1079 html("<div id='cgit'>");
1080 if (!ctx.env.authenticated || !ctx.cfg.noheader)
1083 html("<table class='tabs'><tr><td>\n");
1084 if (ctx.env.authenticated && ctx.repo) {
1085 if (ctx.repo->readme.nr)
1086 reporevlink("about", "about", NULL,
1087 hc("about"), ctx.qry.head, NULL,
1089 cgit_summary_link("summary", NULL, hc("summary"),
1091 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
1093 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
1094 NULL, ctx.qry.vpath, 0, NULL, NULL,
1095 ctx.qry.showmsg, ctx.qry.follow);
1096 if (ctx.qry.page && !strcmp(ctx.qry.page, "blame"))
1097 cgit_blame_link("blame", NULL, hc("blame"), ctx.qry.head,
1098 ctx.qry.oid, ctx.qry.vpath);
1100 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
1101 ctx.qry.oid, ctx.qry.vpath);
1102 cgit_commit_link("commit", NULL, hc("commit"),
1103 ctx.qry.head, ctx.qry.oid, ctx.qry.vpath);
1104 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
1105 ctx.qry.oid, ctx.qry.oid2, ctx.qry.vpath);
1106 if (ctx.repo->max_stats)
1107 cgit_stats_link("stats", NULL, hc("stats"),
1108 ctx.qry.head, ctx.qry.vpath);
1109 if (ctx.repo->homepage) {
1111 html_attr(ctx.repo->homepage);
1112 html("'>homepage</a>");
1114 html("</td><td class='form'>");
1115 html("<form class='right' method='get' action='");
1116 if (ctx.cfg.virtual_root) {
1117 char *fileurl = cgit_fileurl(ctx.qry.repo, "log",
1118 ctx.qry.vpath, NULL);
1119 html_url_path(fileurl);
1123 cgit_add_hidden_formfields(1, 0, "log");
1124 html("<select name='qt'>\n");
1125 html_option("grep", "log msg", ctx.qry.grep);
1126 html_option("author", "author", ctx.qry.grep);
1127 html_option("committer", "committer", ctx.qry.grep);
1128 html_option("range", "range", ctx.qry.grep);
1129 html("</select>\n");
1130 html("<input class='txt' type='search' size='10' name='q' value='");
1131 html_attr(ctx.qry.search);
1133 html("<input type='submit' value='search'/>\n");
1135 } else if (ctx.env.authenticated) {
1136 char *currenturl = cgit_currenturl();
1137 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
1138 if (ctx.cfg.root_readme)
1139 site_link("about", "about", NULL, hc("about"),
1141 html("</td><td class='form'>");
1142 html("<form method='get' action='");
1143 html_attr(currenturl);
1145 html("<input type='search' name='q' size='10' value='");
1146 html_attr(ctx.qry.search);
1148 html("<input type='submit' value='search'/>\n");
1152 html("</td></tr></table>\n");
1153 if (ctx.env.authenticated && ctx.repo && ctx.qry.vpath) {
1154 html("<div class='path'>");
1156 cgit_print_path_crumbs(ctx.qry.vpath);
1157 if (ctx.cfg.enable_follow_links && !strcmp(ctx.qry.page, "log")) {
1159 ctx.qry.follow = !ctx.qry.follow;
1160 cgit_self_link(ctx.qry.follow ? "follow" : "unfollow",
1162 ctx.qry.follow = !ctx.qry.follow;
1167 html("<div class='content'>");
1170 void cgit_print_filemode(unsigned short mode)
1174 else if (S_ISLNK(mode))
1176 else if (S_ISGITLINK(mode))
1180 html_fileperm(mode >> 6);
1181 html_fileperm(mode >> 3);
1182 html_fileperm(mode);
1185 void cgit_compose_snapshot_prefix(struct strbuf *filename, const char *base,
1188 struct object_id oid;
1191 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1192 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1193 * i.e. each stripped tag can be inverted without ambiguities.
1195 if (repo_get_oid(the_repository, fmt("refs/tags/%s", ref), &oid) == 0 &&
1196 (ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]) &&
1197 ((repo_get_oid(the_repository, fmt("refs/tags/%s", ref + 1), &oid) == 0) +
1198 (repo_get_oid(the_repository, fmt("refs/tags/v%s", ref + 1), &oid) == 0) +
1199 (repo_get_oid(the_repository, fmt("refs/tags/V%s", ref + 1), &oid) == 0) == 1))
1202 strbuf_addf(filename, "%s-%s", base, ref);
1205 void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
1206 const char *separator)
1208 const struct cgit_snapshot_format *f;
1209 struct strbuf filename = STRBUF_INIT;
1210 const char *basename;
1213 basename = cgit_snapshot_prefix(repo);
1214 if (starts_with(ref, basename))
1215 strbuf_addstr(&filename, ref);
1217 cgit_compose_snapshot_prefix(&filename, basename, ref);
1219 prefixlen = filename.len;
1220 for (f = cgit_snapshot_formats; f->suffix; f++) {
1221 if (!(repo->snapshots & cgit_snapshot_format_bit(f)))
1223 strbuf_setlen(&filename, prefixlen);
1224 strbuf_addstr(&filename, f->suffix);
1225 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1227 if (cgit_snapshot_get_sig(ref, f)) {
1228 strbuf_addstr(&filename, ".asc");
1230 cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1233 } else if (starts_with(f->suffix, ".tar") && cgit_snapshot_get_sig(ref, &cgit_snapshot_formats[0])) {
1234 strbuf_setlen(&filename, strlen(filename.buf) - strlen(f->suffix));
1235 strbuf_addstr(&filename, ".tar.asc");
1237 cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1243 strbuf_release(&filename);
1246 void cgit_set_title_from_path(const char *path)
1248 struct strbuf sb = STRBUF_INIT;
1249 const char *slash, *last_slash;
1254 for (last_slash = path + strlen(path); (slash = memrchr(path, '/', last_slash - path)) != NULL; last_slash = slash) {
1255 strbuf_add(&sb, slash + 1, last_slash - slash - 1);
1256 strbuf_addstr(&sb, " \xc2\xab ");
1258 strbuf_add(&sb, path, last_slash - path);
1259 strbuf_addf(&sb, " - %s", ctx.page.title);
1260 ctx.page.title = strbuf_detach(&sb, NULL);