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)
10 #include "ui-shared.h"
15 static const char cgit_doctype[] =
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"};
27 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm.tm_wday],
28 tm.tm_mday, month[tm.tm_mon], 1900 + tm.tm_year,
29 tm.tm_hour, tm.tm_min, tm.tm_sec);
32 void cgit_print_error(const char *fmt, ...)
36 cgit_vprint_error(fmt, ap);
40 void cgit_vprint_error(const char *fmt, va_list ap)
43 html("<div class='error'>");
50 const char *cgit_httpscheme(void)
52 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
58 char *cgit_hosturl(void)
60 if (ctx.env.http_host)
61 return xstrdup(ctx.env.http_host);
62 if (!ctx.env.server_name)
64 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
65 return xstrdup(ctx.env.server_name);
66 return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
69 char *cgit_currenturl(void)
71 const char *root = cgit_rooturl();
75 if (root[0] && root[strlen(root) - 1] == '/')
76 return fmtalloc("%s%s", root, ctx.qry.url);
77 return fmtalloc("%s/%s", root, ctx.qry.url);
80 char *cgit_currentfullurl(void)
82 const char *root = cgit_rooturl();
83 const char *orig_query = ctx.env.query_string ? ctx.env.query_string : "";
84 size_t len = strlen(orig_query);
85 char *query = xmalloc(len + 2), *start_url, *ret;
87 /* Remove all url=... parts from query string */
88 memcpy(query + 1, orig_query, len + 1);
91 while ((start_url = strstr(start_url, "url=")) != NULL) {
92 if (start_url[-1] == '?' || start_url[-1] == '&') {
93 const char *end_url = strchr(start_url, '&');
95 memmove(start_url, end_url + 1, strlen(end_url));
105 ret = fmtalloc("%s%s", root, query);
106 else if (root[0] && root[strlen(root) - 1] == '/')
107 ret = fmtalloc("%s%s%s", root, ctx.qry.url, query);
109 ret = fmtalloc("%s/%s%s", root, ctx.qry.url, query);
114 const char *cgit_rooturl(void)
116 if (ctx.cfg.virtual_root)
117 return ctx.cfg.virtual_root;
119 return ctx.cfg.script_name;
122 const char *cgit_loginurl(void)
124 static const char *login_url;
126 login_url = fmtalloc("%s?p=login", cgit_rooturl());
130 char *cgit_repourl(const char *reponame)
132 if (ctx.cfg.virtual_root)
133 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
135 return fmtalloc("?r=%s", reponame);
138 char *cgit_fileurl(const char *reponame, const char *pagename,
139 const char *filename, const char *query)
141 struct strbuf sb = STRBUF_INIT;
144 if (ctx.cfg.virtual_root) {
145 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
146 pagename, (filename ? filename:""));
149 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
150 (filename ? filename : ""));
154 strbuf_addf(&sb, "%s%s", delim, query);
155 return strbuf_detach(&sb, NULL);
158 char *cgit_pageurl(const char *reponame, const char *pagename,
161 return cgit_fileurl(reponame, pagename, NULL, query);
164 const char *cgit_repobasename(const char *reponame)
166 /* I assume we don't need to store more than one repo basename */
167 static char rvbuf[1024];
172 len = strlcpy(rvbuf, reponame, sizeof(rvbuf));
173 if (len >= sizeof(rvbuf))
174 die("cgit_repobasename: truncated repository name '%s'", reponame);
176 /* strip trailing slashes */
177 while (p && rvbuf[p] == '/')
179 /* strip trailing .git */
180 if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
184 /* strip more trailing slashes if any */
185 while (p && rvbuf[p] == '/')
187 /* find last slash in the remaining string */
188 rv = strrchr(rvbuf, '/');
194 const char *cgit_snapshot_prefix(const struct cgit_repo *repo)
196 if (repo->snapshot_prefix)
197 return repo->snapshot_prefix;
199 return cgit_repobasename(repo->url);
202 static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
206 if (always_root || page)
207 html_attr(cgit_rooturl());
209 char *currenturl = cgit_currenturl();
210 html_attr(currenturl);
215 htmlf("?p=%s", page);
232 htmlf("ofs=%d", ofs);
236 static void site_link(const char *page, const char *name, const char *title,
237 const char *class, const char *search, const char *sort, int ofs, int always_root)
251 site_url(page, search, sort, ofs, always_root);
257 void cgit_index_link(const char *name, const char *title, const char *class,
258 const char *pattern, const char *sort, int ofs, int always_root)
260 site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
263 static char *repolink(const char *title, const char *class, const char *page,
264 const char *head, const char *path)
280 if (ctx.cfg.virtual_root) {
281 html_url_path(ctx.cfg.virtual_root);
282 html_url_path(ctx.repo->url);
283 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
292 html_url_path(ctx.cfg.script_name);
294 html_url_arg(ctx.repo->url);
295 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
305 if (head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch)) {
311 return fmt("%s", delim);
314 static void reporevlink(const char *page, const char *name, const char *title,
315 const char *class, const char *head, const char *rev,
320 delim = repolink(title, class, page, head, path);
321 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
331 void cgit_summary_link(const char *name, const char *title, const char *class,
334 reporevlink(NULL, name, title, class, head, NULL, NULL);
337 void cgit_tag_link(const char *name, const char *title, const char *class,
340 reporevlink("tag", name, title, class, tag, NULL, NULL);
343 void cgit_tree_link(const char *name, const char *title, const char *class,
344 const char *head, const char *rev, const char *path)
346 reporevlink("tree", name, title, class, head, rev, path);
349 void cgit_plain_link(const char *name, const char *title, const char *class,
350 const char *head, const char *rev, const char *path)
352 reporevlink("plain", name, title, class, head, rev, path);
355 void cgit_blame_link(const char *name, const char *title, const char *class,
356 const char *head, const char *rev, const char *path)
358 reporevlink("blame", name, title, class, head, rev, path);
361 void cgit_log_link(const char *name, const char *title, const char *class,
362 const char *head, const char *rev, const char *path,
363 int ofs, const char *grep, const char *pattern, int showmsg,
368 delim = repolink(title, class, "log", head, path);
369 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
375 if (grep && pattern) {
382 html_url_arg(pattern);
404 void cgit_commit_link(const char *name, const char *title, const char *class,
405 const char *head, const char *rev, const char *path)
409 delim = repolink(title, class, "commit", head, path);
410 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
416 if (ctx.qry.difftype) {
418 htmlf("dt=%d", ctx.qry.difftype);
421 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
424 htmlf("%d", ctx.qry.context);
427 if (ctx.qry.ignorews) {
432 if (ctx.qry.follow) {
437 if (name[0] != '\0') {
438 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
439 html_ntxt(name, ctx.cfg.max_msg_len - 3);
444 html_txt("(no commit message)");
448 void cgit_refs_link(const char *name, const char *title, const char *class,
449 const char *head, const char *rev, const char *path)
451 reporevlink("refs", name, title, class, head, rev, path);
454 void cgit_snapshot_link(const char *name, const char *title, const char *class,
455 const char *head, const char *rev,
456 const char *archivename)
458 reporevlink("snapshot", name, title, class, head, rev, archivename);
461 void cgit_diff_link(const char *name, const char *title, const char *class,
462 const char *head, const char *new_rev, const char *old_rev,
467 delim = repolink(title, class, "diff", head, path);
468 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
471 html_url_arg(new_rev);
477 html_url_arg(old_rev);
480 if (ctx.qry.difftype) {
482 htmlf("dt=%d", ctx.qry.difftype);
485 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
488 htmlf("%d", ctx.qry.context);
491 if (ctx.qry.ignorews) {
496 if (ctx.qry.follow) {
505 void cgit_patch_link(const char *name, const char *title, const char *class,
506 const char *head, const char *rev, const char *path)
508 reporevlink("patch", name, title, class, head, rev, path);
511 void cgit_stats_link(const char *name, const char *title, const char *class,
512 const char *head, const char *path)
514 reporevlink("stats", name, title, class, head, NULL, path);
517 static void cgit_self_link(char *name, const char *title, const char *class)
519 if (!strcmp(ctx.qry.page, "repolist"))
520 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
522 else if (!strcmp(ctx.qry.page, "summary"))
523 cgit_summary_link(name, title, class, ctx.qry.head);
524 else if (!strcmp(ctx.qry.page, "tag"))
525 cgit_tag_link(name, title, class, ctx.qry.has_oid ?
526 ctx.qry.oid : ctx.qry.head);
527 else if (!strcmp(ctx.qry.page, "tree"))
528 cgit_tree_link(name, title, class, ctx.qry.head,
529 ctx.qry.has_oid ? ctx.qry.oid : NULL,
531 else if (!strcmp(ctx.qry.page, "plain"))
532 cgit_plain_link(name, title, class, ctx.qry.head,
533 ctx.qry.has_oid ? ctx.qry.oid : NULL,
535 else if (!strcmp(ctx.qry.page, "blame"))
536 cgit_blame_link(name, title, class, ctx.qry.head,
537 ctx.qry.has_oid ? ctx.qry.oid : NULL,
539 else if (!strcmp(ctx.qry.page, "log"))
540 cgit_log_link(name, title, class, ctx.qry.head,
541 ctx.qry.has_oid ? ctx.qry.oid : NULL,
542 ctx.qry.path, ctx.qry.ofs,
543 ctx.qry.grep, ctx.qry.search,
544 ctx.qry.showmsg, ctx.qry.follow);
545 else if (!strcmp(ctx.qry.page, "commit"))
546 cgit_commit_link(name, title, class, ctx.qry.head,
547 ctx.qry.has_oid ? ctx.qry.oid : NULL,
549 else if (!strcmp(ctx.qry.page, "patch"))
550 cgit_patch_link(name, title, class, ctx.qry.head,
551 ctx.qry.has_oid ? ctx.qry.oid : NULL,
553 else if (!strcmp(ctx.qry.page, "refs"))
554 cgit_refs_link(name, title, class, ctx.qry.head,
555 ctx.qry.has_oid ? ctx.qry.oid : NULL,
557 else if (!strcmp(ctx.qry.page, "snapshot"))
558 cgit_snapshot_link(name, title, class, ctx.qry.head,
559 ctx.qry.has_oid ? ctx.qry.oid : NULL,
561 else if (!strcmp(ctx.qry.page, "diff"))
562 cgit_diff_link(name, title, class, ctx.qry.head,
563 ctx.qry.oid, ctx.qry.oid2,
565 else if (!strcmp(ctx.qry.page, "stats"))
566 cgit_stats_link(name, title, class, ctx.qry.head,
569 /* Don't known how to make link for this page */
570 repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
571 html("><!-- cgit_self_link() doesn't know how to make link for page '");
572 html_txt(ctx.qry.page);
579 void cgit_object_link(struct object *obj)
581 char *page, *shortrev, *fullrev, *name;
583 fullrev = oid_to_hex(&obj->oid);
584 shortrev = xstrdup(fullrev);
586 if (obj->type == OBJ_COMMIT) {
587 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
588 ctx.qry.head, fullrev, NULL);
590 } else if (obj->type == OBJ_TREE)
592 else if (obj->type == OBJ_TAG)
596 name = fmt("%s %s...", type_name(obj->type), shortrev);
597 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
600 static struct string_list_item *lookup_path(struct string_list *list,
603 struct string_list_item *item;
605 while (path && path[0]) {
606 if ((item = string_list_lookup(list, path)))
608 if (!(path = strchr(path, '/')))
615 void cgit_submodule_link(const char *class, char *path, const char *rev)
617 struct string_list *list;
618 struct string_list_item *item;
624 list = &ctx.repo->submodules;
625 item = lookup_path(list, path);
628 tail = path[len - 1];
631 item = lookup_path(list, path);
634 if (item || ctx.repo->module_link) {
637 htmlf("class='%s' ", class);
640 html_attrf(item->util, rev);
642 dir = strrchr(path, '/');
647 html_attrf(ctx.repo->module_link, dir, rev);
655 htmlf(" class='%s'", class);
660 html_txtf(" @ %.7s", rev);
662 path[len - 1] = tail;
665 const struct date_mode *cgit_date_mode(enum date_mode_type type)
667 static struct date_mode mode;
669 mode.local = ctx.cfg.local_time;
673 static void print_rel_date(time_t t, int tz, double value,
674 const char *class, const char *suffix)
676 htmlf("<span class='%s' title='", class);
677 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
678 htmlf("'>%.0f %s</span>", value, suffix);
681 void cgit_print_age(time_t t, int tz, time_t max_relative)
692 if (secs > max_relative && max_relative >= 0) {
693 html("<span title='");
694 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
696 html_txt(show_date(t, tz, cgit_date_mode(DATE_SHORT)));
701 if (secs < TM_HOUR * 2) {
702 print_rel_date(t, tz, secs * 1.0 / TM_MIN, "age-mins", "min.");
705 if (secs < TM_DAY * 2) {
706 print_rel_date(t, tz, secs * 1.0 / TM_HOUR, "age-hours", "hours");
709 if (secs < TM_WEEK * 2) {
710 print_rel_date(t, tz, secs * 1.0 / TM_DAY, "age-days", "days");
713 if (secs < TM_MONTH * 2) {
714 print_rel_date(t, tz, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
717 if (secs < TM_YEAR * 2) {
718 print_rel_date(t, tz, secs * 1.0 / TM_MONTH, "age-months", "months");
721 print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years");
724 void cgit_print_http_headers(void)
726 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
730 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
731 if (ctx.page.mimetype && ctx.page.charset)
732 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
734 else if (ctx.page.mimetype)
735 htmlf("Content-Type: %s\n", ctx.page.mimetype);
737 htmlf("Content-Length: %zd\n", ctx.page.size);
738 if (ctx.page.filename) {
739 html("Content-Disposition: inline; filename=\"");
740 html_header_arg_in_quotes(ctx.page.filename);
743 if (!ctx.env.authenticated)
744 html("Cache-Control: no-cache, no-store\n");
745 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
746 htmlf("Expires: %s\n", http_date(ctx.page.expires));
748 htmlf("ETag: \"%s\"\n", ctx.page.etag);
750 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
754 void cgit_redirect(const char *url, bool permanent)
756 htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
762 static void print_rel_vcs_link(const char *url)
764 html("<link rel='vcs-git' href='");
767 html_attr(ctx.repo->name);
768 html(" Git repository'/>\n");
771 void cgit_print_docstart(void)
773 char *host = cgit_hosturl();
775 if (ctx.cfg.embedded) {
777 html_include(ctx.cfg.header);
782 html("<html lang='en'>\n");
785 html_txt(ctx.page.title);
787 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
788 if (ctx.cfg.robots && *ctx.cfg.robots)
789 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
790 html("<link rel='stylesheet' type='text/css' href='");
791 html_attr(ctx.cfg.css);
793 if (ctx.cfg.favicon) {
794 html("<link rel='shortcut icon' href='");
795 html_attr(ctx.cfg.favicon);
798 if (host && ctx.repo && ctx.qry.head) {
800 struct strbuf sb = STRBUF_INIT;
801 strbuf_addf(&sb, "h=%s", ctx.qry.head);
803 html("<link rel='alternate' title='Atom feed' href='");
804 html(cgit_httpscheme());
806 fileurl = cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
809 html("' type='application/atom+xml'/>\n");
814 cgit_add_clone_urls(print_rel_vcs_link);
815 if (ctx.cfg.head_include)
816 html_include(ctx.cfg.head_include);
817 if (ctx.repo && ctx.repo->extra_head_content)
818 html(ctx.repo->extra_head_content);
822 html_include(ctx.cfg.header);
826 void cgit_print_docend(void)
828 html("</div> <!-- class=content -->\n");
829 if (ctx.cfg.embedded) {
830 html("</div> <!-- id=cgit -->\n");
832 html_include(ctx.cfg.footer);
836 html_include(ctx.cfg.footer);
838 htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
839 "(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version, git_version_string);
840 html_txt(show_date(time(NULL), 0, cgit_date_mode(DATE_ISO8601)));
843 html("</div> <!-- id=cgit -->\n");
844 html("</body>\n</html>\n");
847 void cgit_print_error_page(int code, const char *msg, const char *fmt, ...)
850 ctx.page.expires = ctx.cfg.cache_dynamic_ttl;
851 ctx.page.status = code;
852 ctx.page.statusmsg = msg;
853 cgit_print_layout_start();
855 cgit_vprint_error(fmt, ap);
857 cgit_print_layout_end();
860 void cgit_print_layout_start(void)
862 cgit_print_http_headers();
863 cgit_print_docstart();
864 cgit_print_pageheader();
867 void cgit_print_layout_end(void)
872 static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
874 struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
877 for (i = 0; url_list[i]; i++) {
878 strbuf_rtrim(url_list[i]);
879 if (url_list[i]->len == 0)
881 if (suffix && *suffix)
882 strbuf_addf(url_list[i], "/%s", suffix);
883 fn(url_list[i]->buf);
886 strbuf_list_free(url_list);
889 void cgit_add_clone_urls(void (*fn)(const char *))
891 if (ctx.repo->clone_url)
892 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
893 else if (ctx.cfg.clone_prefix)
894 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
897 static int print_branch_option(const char *refname, const struct object_id *oid,
898 int flags, void *cb_data)
900 char *name = (char *)refname;
901 html_option(name, name, ctx.qry.head);
905 void cgit_add_hidden_formfields(int incl_head, int incl_search,
908 if (!ctx.cfg.virtual_root) {
909 struct strbuf url = STRBUF_INIT;
911 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
913 strbuf_addf(&url, "/%s", ctx.qry.vpath);
914 html_hidden("url", url.buf);
915 strbuf_release(&url);
918 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
919 strcmp(ctx.qry.head, ctx.repo->defbranch))
920 html_hidden("h", ctx.qry.head);
923 html_hidden("id", ctx.qry.oid);
925 html_hidden("id2", ctx.qry.oid2);
927 html_hidden("showmsg", "1");
931 html_hidden("qt", ctx.qry.grep);
933 html_hidden("q", ctx.qry.search);
937 static const char *hc(const char *page)
942 return strcmp(ctx.qry.page, page) ? NULL : "active";
945 static void cgit_print_path_crumbs(char *path)
947 char *old_path = ctx.qry.path;
948 char *p = path, *q, *end = path + strlen(path);
952 cgit_self_link("root", NULL, NULL);
953 ctx.qry.path = p = path;
955 if (!(q = strchr(p, '/')) || levels > 15)
959 cgit_self_link(p, NULL, NULL);
965 ctx.qry.path = old_path;
968 static void print_header(void)
970 char *logo = NULL, *logo_link = NULL;
972 html("<table id='header'>\n");
975 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
976 logo = ctx.repo->logo;
979 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
980 logo_link = ctx.repo->logo_link;
982 logo_link = ctx.cfg.logo_link;
984 html("<td class='logo' rowspan='2'><a href='");
985 if (logo_link && *logo_link)
986 html_attr(logo_link);
988 html_attr(cgit_rooturl());
989 html("'><img src='");
991 html("' alt='cgit logo'/></a></td>\n");
994 html("<td class='main'>");
996 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
998 cgit_summary_link(ctx.repo->name, NULL, NULL, NULL);
999 if (ctx.env.authenticated) {
1000 html("</td><td class='form'>");
1001 html("<form method='get'>\n");
1002 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
1003 html("<select name='h' onchange='this.form.submit();'>\n");
1004 for_each_branch_ref(print_branch_option, ctx.qry.head);
1005 if (ctx.repo->enable_remote_branches)
1006 for_each_remote_ref(print_branch_option, ctx.qry.head);
1008 html("<input type='submit' value='switch'/>");
1012 html_txt(ctx.cfg.root_title);
1013 html("</td></tr>\n");
1015 html("<tr><td class='sub'>");
1017 html_txt(ctx.repo->desc);
1018 html("</td><td class='sub right'>");
1019 if (ctx.repo->owner_filter) {
1020 cgit_open_filter(ctx.repo->owner_filter);
1021 html_txt(ctx.repo->owner);
1022 cgit_close_filter(ctx.repo->owner_filter);
1024 html_txt(ctx.repo->owner);
1027 if (ctx.cfg.root_desc)
1028 html_txt(ctx.cfg.root_desc);
1030 html("</td></tr></table>\n");
1033 void cgit_print_pageheader(void)
1035 html("<div id='cgit'>");
1036 if (!ctx.env.authenticated || !ctx.cfg.noheader)
1039 html("<table class='tabs'><tr><td>\n");
1040 if (ctx.env.authenticated && ctx.repo) {
1041 if (ctx.repo->readme.nr)
1042 reporevlink("about", "about", NULL,
1043 hc("about"), ctx.qry.head, NULL,
1045 cgit_summary_link("summary", NULL, hc("summary"),
1047 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
1049 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
1050 NULL, ctx.qry.vpath, 0, NULL, NULL,
1051 ctx.qry.showmsg, ctx.qry.follow);
1052 if (ctx.qry.page && !strcmp(ctx.qry.page, "blame"))
1053 cgit_blame_link("blame", NULL, hc("blame"), ctx.qry.head,
1054 ctx.qry.oid, ctx.qry.vpath);
1056 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
1057 ctx.qry.oid, ctx.qry.vpath);
1058 cgit_commit_link("commit", NULL, hc("commit"),
1059 ctx.qry.head, ctx.qry.oid, ctx.qry.vpath);
1060 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
1061 ctx.qry.oid, ctx.qry.oid2, ctx.qry.vpath);
1062 if (ctx.repo->max_stats)
1063 cgit_stats_link("stats", NULL, hc("stats"),
1064 ctx.qry.head, ctx.qry.vpath);
1065 if (ctx.repo->homepage) {
1067 html_attr(ctx.repo->homepage);
1068 html("'>homepage</a>");
1070 html("</td><td class='form'>");
1071 html("<form class='right' method='get' action='");
1072 if (ctx.cfg.virtual_root) {
1073 char *fileurl = cgit_fileurl(ctx.qry.repo, "log",
1074 ctx.qry.vpath, NULL);
1075 html_url_path(fileurl);
1079 cgit_add_hidden_formfields(1, 0, "log");
1080 html("<select name='qt'>\n");
1081 html_option("grep", "log msg", ctx.qry.grep);
1082 html_option("author", "author", ctx.qry.grep);
1083 html_option("committer", "committer", ctx.qry.grep);
1084 html_option("range", "range", ctx.qry.grep);
1085 html("</select>\n");
1086 html("<input class='txt' type='search' size='10' name='q' value='");
1087 html_attr(ctx.qry.search);
1089 html("<input type='submit' value='search'/>\n");
1091 } else if (ctx.env.authenticated) {
1092 char *currenturl = cgit_currenturl();
1093 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
1094 if (ctx.cfg.root_readme)
1095 site_link("about", "about", NULL, hc("about"),
1097 html("</td><td class='form'>");
1098 html("<form method='get' action='");
1099 html_attr(currenturl);
1101 html("<input type='search' name='q' size='10' value='");
1102 html_attr(ctx.qry.search);
1104 html("<input type='submit' value='search'/>\n");
1108 html("</td></tr></table>\n");
1109 if (ctx.env.authenticated && ctx.repo && ctx.qry.vpath) {
1110 html("<div class='path'>");
1112 cgit_print_path_crumbs(ctx.qry.vpath);
1113 if (ctx.cfg.enable_follow_links && !strcmp(ctx.qry.page, "log")) {
1115 ctx.qry.follow = !ctx.qry.follow;
1116 cgit_self_link(ctx.qry.follow ? "follow" : "unfollow",
1118 ctx.qry.follow = !ctx.qry.follow;
1123 html("<div class='content'>");
1126 void cgit_print_filemode(unsigned short mode)
1130 else if (S_ISLNK(mode))
1132 else if (S_ISGITLINK(mode))
1136 html_fileperm(mode >> 6);
1137 html_fileperm(mode >> 3);
1138 html_fileperm(mode);
1141 void cgit_compose_snapshot_prefix(struct strbuf *filename, const char *base,
1144 struct object_id oid;
1147 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1148 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1149 * i.e. each stripped tag can be inverted without ambiguities.
1151 if (get_oid(fmt("refs/tags/%s", ref), &oid) == 0 &&
1152 (ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]) &&
1153 ((get_oid(fmt("refs/tags/%s", ref + 1), &oid) == 0) +
1154 (get_oid(fmt("refs/tags/v%s", ref + 1), &oid) == 0) +
1155 (get_oid(fmt("refs/tags/V%s", ref + 1), &oid) == 0) == 1))
1158 strbuf_addf(filename, "%s-%s", base, ref);
1161 void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
1162 const char *separator)
1164 const struct cgit_snapshot_format *f;
1165 struct strbuf filename = STRBUF_INIT;
1166 const char *basename;
1169 basename = cgit_snapshot_prefix(repo);
1170 if (starts_with(ref, basename))
1171 strbuf_addstr(&filename, ref);
1173 cgit_compose_snapshot_prefix(&filename, basename, ref);
1175 prefixlen = filename.len;
1176 for (f = cgit_snapshot_formats; f->suffix; f++) {
1177 if (!(repo->snapshots & cgit_snapshot_format_bit(f)))
1179 strbuf_setlen(&filename, prefixlen);
1180 strbuf_addstr(&filename, f->suffix);
1181 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1183 if (cgit_snapshot_get_sig(ref, f)) {
1184 strbuf_addstr(&filename, ".asc");
1186 cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1189 } else if (starts_with(f->suffix, ".tar") && cgit_snapshot_get_sig(ref, &cgit_snapshot_formats[0])) {
1190 strbuf_setlen(&filename, strlen(filename.buf) - strlen(f->suffix));
1191 strbuf_addstr(&filename, ".tar.asc");
1193 cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1199 strbuf_release(&filename);
1202 void cgit_set_title_from_path(const char *path)
1204 struct strbuf sb = STRBUF_INIT;
1205 const char *slash, *last_slash;
1210 for (last_slash = path + strlen(path); (slash = memrchr(path, '/', last_slash - path)) != NULL; last_slash = slash) {
1211 strbuf_add(&sb, slash + 1, last_slash - slash - 1);
1212 strbuf_addstr(&sb, " \xc2\xab ");
1214 strbuf_add(&sb, path, last_slash - path);
1215 strbuf_addf(&sb, " - %s", ctx.page.title);
1216 ctx.page.title = strbuf_detach(&sb, NULL);