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"};
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(void)
51 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
57 char *cgit_hosturl(void)
59 if (ctx.env.http_host)
60 return xstrdup(ctx.env.http_host);
61 if (!ctx.env.server_name)
63 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
64 return xstrdup(ctx.env.server_name);
65 return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
68 char *cgit_currenturl(void)
70 const char *root = cgit_rooturl();
71 size_t len = strlen(root);
75 if (len && root[len - 1] == '/')
76 return fmtalloc("%s%s", root, ctx.qry.url);
77 return fmtalloc("%s/%s", root, ctx.qry.url);
80 const char *cgit_rooturl(void)
82 if (ctx.cfg.virtual_root)
83 return ctx.cfg.virtual_root;
85 return ctx.cfg.script_name;
88 const char *cgit_loginurl(void)
90 static const char *login_url;
92 login_url = fmtalloc("%s?p=login", cgit_rooturl());
96 char *cgit_repourl(const char *reponame)
98 if (ctx.cfg.virtual_root)
99 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
101 return fmtalloc("?r=%s", reponame);
104 char *cgit_fileurl(const char *reponame, const char *pagename,
105 const char *filename, const char *query)
107 struct strbuf sb = STRBUF_INIT;
110 if (ctx.cfg.virtual_root) {
111 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
112 pagename, (filename ? filename:""));
115 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
116 (filename ? filename : ""));
120 strbuf_addf(&sb, "%s%s", delim, query);
121 return strbuf_detach(&sb, NULL);
124 char *cgit_pageurl(const char *reponame, const char *pagename,
127 return cgit_fileurl(reponame, pagename, NULL, query);
130 const char *cgit_repobasename(const char *reponame)
132 /* I assume we don't need to store more than one repo basename */
133 static char rvbuf[1024];
136 strncpy(rvbuf, reponame, sizeof(rvbuf));
137 if (rvbuf[sizeof(rvbuf)-1])
138 die("cgit_repobasename: truncated repository name '%s'", reponame);
140 /* strip trailing slashes */
141 while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
142 /* strip trailing .git */
143 if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
144 p -= 3; rvbuf[p--] = 0;
146 /* strip more trailing slashes if any */
147 while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
148 /* find last slash in the remaining string */
149 rv = strrchr(rvbuf,'/');
155 const char *cgit_snapshot_prefix(const struct cgit_repo *repo)
157 if (repo->snapshot_prefix)
158 return repo->snapshot_prefix;
160 return cgit_repobasename(repo->url);
163 static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
167 if (always_root || page)
168 html_attr(cgit_rooturl());
170 char *currenturl = cgit_currenturl();
171 html_attr(currenturl);
176 htmlf("?p=%s", page);
193 htmlf("ofs=%d", ofs);
197 static void site_link(const char *page, const char *name, const char *title,
198 const char *class, const char *search, const char *sort, int ofs, int always_root)
212 site_url(page, search, sort, ofs, always_root);
218 void cgit_index_link(const char *name, const char *title, const char *class,
219 const char *pattern, const char *sort, int ofs, int always_root)
221 site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
224 static char *repolink(const char *title, const char *class, const char *page,
225 const char *head, const char *path)
241 if (ctx.cfg.virtual_root) {
242 html_url_path(ctx.cfg.virtual_root);
243 html_url_path(ctx.repo->url);
244 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
253 html_url_path(ctx.cfg.script_name);
255 html_url_arg(ctx.repo->url);
256 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
266 if (head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch)) {
272 return fmt("%s", delim);
275 static void reporevlink(const char *page, const char *name, const char *title,
276 const char *class, const char *head, const char *rev,
281 delim = repolink(title, class, page, head, path);
282 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
292 void cgit_summary_link(const char *name, const char *title, const char *class,
295 reporevlink(NULL, name, title, class, head, NULL, NULL);
298 void cgit_tag_link(const char *name, const char *title, const char *class,
301 reporevlink("tag", name, title, class, tag, NULL, NULL);
304 void cgit_tree_link(const char *name, const char *title, const char *class,
305 const char *head, const char *rev, const char *path)
307 reporevlink("tree", name, title, class, head, rev, path);
310 void cgit_plain_link(const char *name, const char *title, const char *class,
311 const char *head, const char *rev, const char *path)
313 reporevlink("plain", name, title, class, head, rev, path);
316 void cgit_blame_link(const char *name, const char *title, const char *class,
317 const char *head, const char *rev, const char *path)
319 reporevlink("blame", name, title, class, head, rev, path);
322 void cgit_log_link(const char *name, const char *title, const char *class,
323 const char *head, const char *rev, const char *path,
324 int ofs, const char *grep, const char *pattern, int showmsg,
329 delim = repolink(title, class, "log", head, path);
330 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
336 if (grep && pattern) {
343 html_url_arg(pattern);
365 void cgit_commit_link(const char *name, const char *title, const char *class,
366 const char *head, const char *rev, const char *path)
370 delim = repolink(title, class, "commit", head, path);
371 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
377 if (ctx.qry.difftype) {
379 htmlf("dt=%d", ctx.qry.difftype);
382 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
385 htmlf("%d", ctx.qry.context);
388 if (ctx.qry.ignorews) {
393 if (ctx.qry.follow) {
398 if (name[0] != '\0') {
399 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
400 html_ntxt(name, ctx.cfg.max_msg_len - 3);
405 html_txt("(no commit message)");
409 void cgit_refs_link(const char *name, const char *title, const char *class,
410 const char *head, const char *rev, const char *path)
412 reporevlink("refs", name, title, class, head, rev, path);
415 void cgit_snapshot_link(const char *name, const char *title, const char *class,
416 const char *head, const char *rev,
417 const char *archivename)
419 reporevlink("snapshot", name, title, class, head, rev, archivename);
422 void cgit_diff_link(const char *name, const char *title, const char *class,
423 const char *head, const char *new_rev, const char *old_rev,
428 delim = repolink(title, class, "diff", head, path);
429 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
432 html_url_arg(new_rev);
438 html_url_arg(old_rev);
441 if (ctx.qry.difftype) {
443 htmlf("dt=%d", ctx.qry.difftype);
446 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
449 htmlf("%d", ctx.qry.context);
452 if (ctx.qry.ignorews) {
457 if (ctx.qry.follow) {
466 void cgit_patch_link(const char *name, const char *title, const char *class,
467 const char *head, const char *rev, const char *path)
469 reporevlink("patch", name, title, class, head, rev, path);
472 void cgit_stats_link(const char *name, const char *title, const char *class,
473 const char *head, const char *path)
475 reporevlink("stats", name, title, class, head, NULL, path);
478 static void cgit_self_link(char *name, const char *title, const char *class)
480 if (!strcmp(ctx.qry.page, "repolist"))
481 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
483 else if (!strcmp(ctx.qry.page, "summary"))
484 cgit_summary_link(name, title, class, ctx.qry.head);
485 else if (!strcmp(ctx.qry.page, "tag"))
486 cgit_tag_link(name, title, class, ctx.qry.has_sha1 ?
487 ctx.qry.sha1 : ctx.qry.head);
488 else if (!strcmp(ctx.qry.page, "tree"))
489 cgit_tree_link(name, title, class, ctx.qry.head,
490 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
492 else if (!strcmp(ctx.qry.page, "plain"))
493 cgit_plain_link(name, title, class, ctx.qry.head,
494 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
496 else if (!strcmp(ctx.qry.page, "blame"))
497 cgit_blame_link(name, title, class, ctx.qry.head,
498 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
500 else if (!strcmp(ctx.qry.page, "log"))
501 cgit_log_link(name, title, class, ctx.qry.head,
502 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
503 ctx.qry.path, ctx.qry.ofs,
504 ctx.qry.grep, ctx.qry.search,
505 ctx.qry.showmsg, ctx.qry.follow);
506 else if (!strcmp(ctx.qry.page, "commit"))
507 cgit_commit_link(name, title, class, ctx.qry.head,
508 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
510 else if (!strcmp(ctx.qry.page, "patch"))
511 cgit_patch_link(name, title, class, ctx.qry.head,
512 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
514 else if (!strcmp(ctx.qry.page, "refs"))
515 cgit_refs_link(name, title, class, ctx.qry.head,
516 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
518 else if (!strcmp(ctx.qry.page, "snapshot"))
519 cgit_snapshot_link(name, title, class, ctx.qry.head,
520 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
522 else if (!strcmp(ctx.qry.page, "diff"))
523 cgit_diff_link(name, title, class, ctx.qry.head,
524 ctx.qry.sha1, ctx.qry.sha2,
526 else if (!strcmp(ctx.qry.page, "stats"))
527 cgit_stats_link(name, title, class, ctx.qry.head,
530 /* Don't known how to make link for this page */
531 repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
532 html("><!-- cgit_self_link() doesn't know how to make link for page '");
533 html_txt(ctx.qry.page);
540 void cgit_object_link(struct object *obj)
542 char *page, *shortrev, *fullrev, *name;
544 fullrev = oid_to_hex(&obj->oid);
545 shortrev = xstrdup(fullrev);
547 if (obj->type == OBJ_COMMIT) {
548 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
549 ctx.qry.head, fullrev, NULL);
551 } else if (obj->type == OBJ_TREE)
553 else if (obj->type == OBJ_TAG)
557 name = fmt("%s %s...", type_name(obj->type), shortrev);
558 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
561 static struct string_list_item *lookup_path(struct string_list *list,
564 struct string_list_item *item;
566 while (path && path[0]) {
567 if ((item = string_list_lookup(list, path)))
569 if (!(path = strchr(path, '/')))
576 void cgit_submodule_link(const char *class, char *path, const char *rev)
578 struct string_list *list;
579 struct string_list_item *item;
585 list = &ctx.repo->submodules;
586 item = lookup_path(list, path);
589 tail = path[len - 1];
592 item = lookup_path(list, path);
595 if (item || ctx.repo->module_link) {
598 htmlf("class='%s' ", class);
601 html_attrf(item->util, rev);
603 dir = strrchr(path, '/');
608 html_attrf(ctx.repo->module_link, dir, rev);
616 htmlf(" class='%s'", class);
621 html_txtf(" @ %.7s", rev);
623 path[len - 1] = tail;
626 const struct date_mode *cgit_date_mode(enum date_mode_type type)
628 static struct date_mode mode;
630 mode.local = ctx.cfg.local_time;
634 static void print_rel_date(time_t t, int tz, double value,
635 const char *class, const char *suffix)
637 htmlf("<span class='%s' title='", class);
638 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
639 htmlf("'>%.0f %s</span>", value, suffix);
642 void cgit_print_age(time_t t, int tz, time_t max_relative)
653 if (secs > max_relative && max_relative >= 0) {
654 html("<span title='");
655 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
657 html_txt(show_date(t, tz, cgit_date_mode(DATE_SHORT)));
662 if (secs < TM_HOUR * 2) {
663 print_rel_date(t, tz, secs * 1.0 / TM_MIN, "age-mins", "min.");
666 if (secs < TM_DAY * 2) {
667 print_rel_date(t, tz, secs * 1.0 / TM_HOUR, "age-hours", "hours");
670 if (secs < TM_WEEK * 2) {
671 print_rel_date(t, tz, secs * 1.0 / TM_DAY, "age-days", "days");
674 if (secs < TM_MONTH * 2) {
675 print_rel_date(t, tz, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
678 if (secs < TM_YEAR * 2) {
679 print_rel_date(t, tz, secs * 1.0 / TM_MONTH, "age-months", "months");
682 print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years");
685 void cgit_print_http_headers(void)
687 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
691 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
692 if (ctx.page.mimetype && ctx.page.charset)
693 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
695 else if (ctx.page.mimetype)
696 htmlf("Content-Type: %s\n", ctx.page.mimetype);
698 htmlf("Content-Length: %zd\n", ctx.page.size);
699 if (ctx.page.filename) {
700 html("Content-Disposition: inline; filename=\"");
701 html_header_arg_in_quotes(ctx.page.filename);
704 if (!ctx.env.authenticated)
705 html("Cache-Control: no-cache, no-store\n");
706 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
707 htmlf("Expires: %s\n", http_date(ctx.page.expires));
709 htmlf("ETag: \"%s\"\n", ctx.page.etag);
711 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
715 void cgit_redirect(const char *url, bool permanent)
717 htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
723 static void print_rel_vcs_link(const char *url)
725 html("<link rel='vcs-git' href='");
728 html_attr(ctx.repo->name);
729 html(" Git repository'/>\n");
732 void cgit_print_docstart(void)
734 char *host = cgit_hosturl();
736 if (ctx.cfg.embedded) {
738 html_include(ctx.cfg.header);
743 html("<html lang='en'>\n");
746 html_txt(ctx.page.title);
748 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
749 if (ctx.cfg.robots && *ctx.cfg.robots)
750 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
751 html("<link rel='stylesheet' type='text/css' href='");
752 html_attr(ctx.cfg.css);
754 if (ctx.cfg.favicon) {
755 html("<link rel='shortcut icon' href='");
756 html_attr(ctx.cfg.favicon);
759 if (host && ctx.repo && ctx.qry.head) {
761 struct strbuf sb = STRBUF_INIT;
762 strbuf_addf(&sb, "h=%s", ctx.qry.head);
764 html("<link rel='alternate' title='Atom feed' href='");
765 html(cgit_httpscheme());
767 fileurl = cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
770 html("' type='application/atom+xml'/>\n");
775 cgit_add_clone_urls(print_rel_vcs_link);
776 if (ctx.cfg.head_include)
777 html_include(ctx.cfg.head_include);
778 if (ctx.repo && ctx.repo->extra_head_content)
779 html(ctx.repo->extra_head_content);
783 html_include(ctx.cfg.header);
787 void cgit_print_docend(void)
789 html("</div> <!-- class=content -->\n");
790 if (ctx.cfg.embedded) {
791 html("</div> <!-- id=cgit -->\n");
793 html_include(ctx.cfg.footer);
797 html_include(ctx.cfg.footer);
799 htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
800 "(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version, git_version_string);
801 html_txt(show_date(time(NULL), 0, cgit_date_mode(DATE_ISO8601)));
804 html("</div> <!-- id=cgit -->\n");
805 html("</body>\n</html>\n");
808 void cgit_print_error_page(int code, const char *msg, const char *fmt, ...)
811 ctx.page.expires = ctx.cfg.cache_dynamic_ttl;
812 ctx.page.status = code;
813 ctx.page.statusmsg = msg;
814 cgit_print_layout_start();
816 cgit_vprint_error(fmt, ap);
818 cgit_print_layout_end();
821 void cgit_print_layout_start(void)
823 cgit_print_http_headers();
824 cgit_print_docstart();
825 cgit_print_pageheader();
828 void cgit_print_layout_end(void)
833 static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
835 struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
838 for (i = 0; url_list[i]; i++) {
839 strbuf_rtrim(url_list[i]);
840 if (url_list[i]->len == 0)
842 if (suffix && *suffix)
843 strbuf_addf(url_list[i], "/%s", suffix);
844 fn(url_list[i]->buf);
847 strbuf_list_free(url_list);
850 void cgit_add_clone_urls(void (*fn)(const char *))
852 if (ctx.repo->clone_url)
853 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
854 else if (ctx.cfg.clone_prefix)
855 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
858 static int print_branch_option(const char *refname, const struct object_id *oid,
859 int flags, void *cb_data)
861 char *name = (char *)refname;
862 html_option(name, name, ctx.qry.head);
866 void cgit_add_hidden_formfields(int incl_head, int incl_search,
869 if (!ctx.cfg.virtual_root) {
870 struct strbuf url = STRBUF_INIT;
872 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
874 strbuf_addf(&url, "/%s", ctx.qry.vpath);
875 html_hidden("url", url.buf);
876 strbuf_release(&url);
879 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
880 strcmp(ctx.qry.head, ctx.repo->defbranch))
881 html_hidden("h", ctx.qry.head);
884 html_hidden("id", ctx.qry.sha1);
886 html_hidden("id2", ctx.qry.sha2);
888 html_hidden("showmsg", "1");
892 html_hidden("qt", ctx.qry.grep);
894 html_hidden("q", ctx.qry.search);
898 static const char *hc(const char *page)
903 return strcmp(ctx.qry.page, page) ? NULL : "active";
906 static void cgit_print_path_crumbs(char *path)
908 char *old_path = ctx.qry.path;
909 char *p = path, *q, *end = path + strlen(path);
912 cgit_self_link("root", NULL, NULL);
913 ctx.qry.path = p = path;
915 if (!(q = strchr(p, '/')))
919 cgit_self_link(p, NULL, NULL);
924 ctx.qry.path = old_path;
927 static void print_header(void)
929 char *logo = NULL, *logo_link = NULL;
931 html("<table id='header'>\n");
934 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
935 logo = ctx.repo->logo;
938 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
939 logo_link = ctx.repo->logo_link;
941 logo_link = ctx.cfg.logo_link;
943 html("<td class='logo' rowspan='2'><a href='");
944 if (logo_link && *logo_link)
945 html_attr(logo_link);
947 html_attr(cgit_rooturl());
948 html("'><img src='");
950 html("' alt='cgit logo'/></a></td>\n");
953 html("<td class='main'>");
955 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
957 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
958 if (ctx.env.authenticated) {
959 html("</td><td class='form'>");
960 html("<form method='get'>\n");
961 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
962 html("<select name='h' onchange='this.form.submit();'>\n");
963 for_each_branch_ref(print_branch_option, ctx.qry.head);
964 if (ctx.repo->enable_remote_branches)
965 for_each_remote_ref(print_branch_option, ctx.qry.head);
967 html("<input type='submit' value='switch'/>");
971 html_txt(ctx.cfg.root_title);
972 html("</td></tr>\n");
974 html("<tr><td class='sub'>");
976 html_txt(ctx.repo->desc);
977 html("</td><td class='sub right'>");
978 html_txt(ctx.repo->owner);
980 if (ctx.cfg.root_desc)
981 html_txt(ctx.cfg.root_desc);
983 html("</td></tr></table>\n");
986 void cgit_print_pageheader(void)
988 html("<div id='cgit'>");
989 if (!ctx.env.authenticated || !ctx.cfg.noheader)
992 html("<table class='tabs'><tr><td>\n");
993 if (ctx.env.authenticated && ctx.repo) {
994 if (ctx.repo->readme.nr)
995 reporevlink("about", "about", NULL,
996 hc("about"), ctx.qry.head, NULL,
998 cgit_summary_link("summary", NULL, hc("summary"),
1000 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
1001 ctx.qry.sha1, NULL);
1002 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
1003 NULL, ctx.qry.vpath, 0, NULL, NULL,
1004 ctx.qry.showmsg, ctx.qry.follow);
1005 if (ctx.qry.page && !strcmp(ctx.qry.page, "blame"))
1006 cgit_blame_link("blame", NULL, hc("blame"), ctx.qry.head,
1007 ctx.qry.sha1, ctx.qry.vpath);
1009 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
1010 ctx.qry.sha1, ctx.qry.vpath);
1011 cgit_commit_link("commit", NULL, hc("commit"),
1012 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath);
1013 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
1014 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath);
1015 if (ctx.repo->max_stats)
1016 cgit_stats_link("stats", NULL, hc("stats"),
1017 ctx.qry.head, ctx.qry.vpath);
1018 if (ctx.repo->homepage) {
1020 html_attr(ctx.repo->homepage);
1021 html("'>homepage</a>");
1023 html("</td><td class='form'>");
1024 html("<form class='right' method='get' action='");
1025 if (ctx.cfg.virtual_root) {
1026 char *fileurl = cgit_fileurl(ctx.qry.repo, "log",
1027 ctx.qry.vpath, NULL);
1028 html_url_path(fileurl);
1032 cgit_add_hidden_formfields(1, 0, "log");
1033 html("<select name='qt'>\n");
1034 html_option("grep", "log msg", ctx.qry.grep);
1035 html_option("author", "author", ctx.qry.grep);
1036 html_option("committer", "committer", ctx.qry.grep);
1037 html_option("range", "range", ctx.qry.grep);
1038 html("</select>\n");
1039 html("<input class='txt' type='search' size='10' name='q' value='");
1040 html_attr(ctx.qry.search);
1042 html("<input type='submit' value='search'/>\n");
1044 } else if (ctx.env.authenticated) {
1045 char *currenturl = cgit_currenturl();
1046 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
1047 if (ctx.cfg.root_readme)
1048 site_link("about", "about", NULL, hc("about"),
1050 html("</td><td class='form'>");
1051 html("<form method='get' action='");
1052 html_attr(currenturl);
1054 html("<input type='search' name='q' size='10' value='");
1055 html_attr(ctx.qry.search);
1057 html("<input type='submit' value='search'/>\n");
1061 html("</td></tr></table>\n");
1062 if (ctx.env.authenticated && ctx.repo && ctx.qry.vpath) {
1063 html("<div class='path'>");
1065 cgit_print_path_crumbs(ctx.qry.vpath);
1066 if (ctx.cfg.enable_follow_links && !strcmp(ctx.qry.page, "log")) {
1068 ctx.qry.follow = !ctx.qry.follow;
1069 cgit_self_link(ctx.qry.follow ? "follow" : "unfollow",
1071 ctx.qry.follow = !ctx.qry.follow;
1076 html("<div class='content'>");
1079 void cgit_print_filemode(unsigned short mode)
1083 else if (S_ISLNK(mode))
1085 else if (S_ISGITLINK(mode))
1089 html_fileperm(mode >> 6);
1090 html_fileperm(mode >> 3);
1091 html_fileperm(mode);
1094 void cgit_compose_snapshot_prefix(struct strbuf *filename, const char *base,
1097 struct object_id oid;
1100 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1101 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1102 * i.e. each stripped tag can be inverted without ambiguities.
1104 if (get_oid(fmt("refs/tags/%s", ref), &oid) == 0 &&
1105 (ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]) &&
1106 ((get_oid(fmt("refs/tags/%s", ref + 1), &oid) == 0) +
1107 (get_oid(fmt("refs/tags/v%s", ref + 1), &oid) == 0) +
1108 (get_oid(fmt("refs/tags/V%s", ref + 1), &oid) == 0) == 1))
1111 strbuf_addf(filename, "%s-%s", base, ref);
1114 void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *ref,
1115 const char *separator)
1117 const struct cgit_snapshot_format* f;
1118 struct strbuf filename = STRBUF_INIT;
1119 const char *basename;
1122 basename = cgit_snapshot_prefix(repo);
1123 if (starts_with(ref, basename))
1124 strbuf_addstr(&filename, ref);
1126 cgit_compose_snapshot_prefix(&filename, basename, ref);
1128 prefixlen = filename.len;
1129 for (f = cgit_snapshot_formats; f->suffix; f++) {
1130 if (!(repo->snapshots & cgit_snapshot_format_bit(f)))
1132 strbuf_setlen(&filename, prefixlen);
1133 strbuf_addstr(&filename, f->suffix);
1134 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1136 if (cgit_snapshot_get_sig(ref, f)) {
1137 strbuf_addstr(&filename, ".asc");
1139 cgit_snapshot_link("sig", NULL, NULL, NULL, NULL,
1145 strbuf_release(&filename);
1148 void cgit_set_title_from_path(const char *path)
1150 size_t path_len, path_index, path_last_end;
1156 path_len = strlen(path);
1157 new_title = xmalloc(path_len + 3 + strlen(ctx.page.title) + 1);
1158 new_title[0] = '\0';
1160 for (path_index = path_len, path_last_end = path_len; path_index-- > 0;) {
1161 if (path[path_index] == '/') {
1162 if (path_index == path_len - 1) {
1163 path_last_end = path_index - 1;
1166 strncat(new_title, &path[path_index + 1], path_last_end - path_index - 1);
1167 strcat(new_title, "\\");
1168 path_last_end = path_index;
1172 strncat(new_title, path, path_last_end);
1174 strcat(new_title, " - ");
1175 strcat(new_title, ctx.page.title);
1176 ctx.page.title = new_title;