]>
git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
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];
138 len
= strlcpy(rvbuf
, reponame
, sizeof(rvbuf
));
139 if (len
>= sizeof(rvbuf
))
140 die("cgit_repobasename: truncated repository name '%s'", reponame
);
142 /* strip trailing slashes */
143 while (p
&& rvbuf
[p
] == '/')
145 /* strip trailing .git */
146 if (p
>= 3 && starts_with(&rvbuf
[p
-3], ".git")) {
150 /* strip more trailing slashes if any */
151 while (p
&& rvbuf
[p
] == '/')
153 /* find last slash in the remaining string */
154 rv
= strrchr(rvbuf
, '/');
160 const char *cgit_snapshot_prefix(const struct cgit_repo
*repo
)
162 if (repo
->snapshot_prefix
)
163 return repo
->snapshot_prefix
;
165 return cgit_repobasename(repo
->url
);
168 static void site_url(const char *page
, const char *search
, const char *sort
, int ofs
, int always_root
)
172 if (always_root
|| page
)
173 html_attr(cgit_rooturl());
175 char *currenturl
= cgit_currenturl();
176 html_attr(currenturl
);
181 htmlf("?p=%s", page
);
198 htmlf("ofs=%d", ofs
);
202 static void site_link(const char *page
, const char *name
, const char *title
,
203 const char *class, const char *search
, const char *sort
, int ofs
, int always_root
)
217 site_url(page
, search
, sort
, ofs
, always_root
);
223 void cgit_index_link(const char *name
, const char *title
, const char *class,
224 const char *pattern
, const char *sort
, int ofs
, int always_root
)
226 site_link(NULL
, name
, title
, class, pattern
, sort
, ofs
, always_root
);
229 static char *repolink(const char *title
, const char *class, const char *page
,
230 const char *head
, const char *path
)
246 if (ctx
.cfg
.virtual_root
) {
247 html_url_path(ctx
.cfg
.virtual_root
);
248 html_url_path(ctx
.repo
->url
);
249 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
258 html_url_path(ctx
.cfg
.script_name
);
260 html_url_arg(ctx
.repo
->url
);
261 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
271 if (head
&& ctx
.repo
->defbranch
&& strcmp(head
, ctx
.repo
->defbranch
)) {
277 return fmt("%s", delim
);
280 static void reporevlink(const char *page
, const char *name
, const char *title
,
281 const char *class, const char *head
, const char *rev
,
286 delim
= repolink(title
, class, page
, head
, path
);
287 if (rev
&& ctx
.qry
.head
!= NULL
&& strcmp(rev
, ctx
.qry
.head
)) {
297 void cgit_summary_link(const char *name
, const char *title
, const char *class,
300 reporevlink(NULL
, name
, title
, class, head
, NULL
, NULL
);
303 void cgit_tag_link(const char *name
, const char *title
, const char *class,
306 reporevlink("tag", name
, title
, class, tag
, NULL
, NULL
);
309 void cgit_tree_link(const char *name
, const char *title
, const char *class,
310 const char *head
, const char *rev
, const char *path
)
312 reporevlink("tree", name
, title
, class, head
, rev
, path
);
315 void cgit_plain_link(const char *name
, const char *title
, const char *class,
316 const char *head
, const char *rev
, const char *path
)
318 reporevlink("plain", name
, title
, class, head
, rev
, path
);
321 void cgit_blame_link(const char *name
, const char *title
, const char *class,
322 const char *head
, const char *rev
, const char *path
)
324 reporevlink("blame", name
, title
, class, head
, rev
, path
);
327 void cgit_log_link(const char *name
, const char *title
, const char *class,
328 const char *head
, const char *rev
, const char *path
,
329 int ofs
, const char *grep
, const char *pattern
, int showmsg
,
334 delim
= repolink(title
, class, "log", head
, path
);
335 if (rev
&& ctx
.qry
.head
&& strcmp(rev
, ctx
.qry
.head
)) {
341 if (grep
&& pattern
) {
348 html_url_arg(pattern
);
370 void cgit_commit_link(const char *name
, const char *title
, const char *class,
371 const char *head
, const char *rev
, const char *path
)
375 delim
= repolink(title
, class, "commit", head
, path
);
376 if (rev
&& ctx
.qry
.head
&& strcmp(rev
, ctx
.qry
.head
)) {
382 if (ctx
.qry
.difftype
) {
384 htmlf("dt=%d", ctx
.qry
.difftype
);
387 if (ctx
.qry
.context
> 0 && ctx
.qry
.context
!= 3) {
390 htmlf("%d", ctx
.qry
.context
);
393 if (ctx
.qry
.ignorews
) {
398 if (ctx
.qry
.follow
) {
403 if (name
[0] != '\0') {
404 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
405 html_ntxt(name
, ctx
.cfg
.max_msg_len
- 3);
410 html_txt("(no commit message)");
414 void cgit_refs_link(const char *name
, const char *title
, const char *class,
415 const char *head
, const char *rev
, const char *path
)
417 reporevlink("refs", name
, title
, class, head
, rev
, path
);
420 void cgit_snapshot_link(const char *name
, const char *title
, const char *class,
421 const char *head
, const char *rev
,
422 const char *archivename
)
424 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
427 void cgit_diff_link(const char *name
, const char *title
, const char *class,
428 const char *head
, const char *new_rev
, const char *old_rev
,
433 delim
= repolink(title
, class, "diff", head
, path
);
434 if (new_rev
&& ctx
.qry
.head
!= NULL
&& strcmp(new_rev
, ctx
.qry
.head
)) {
437 html_url_arg(new_rev
);
443 html_url_arg(old_rev
);
446 if (ctx
.qry
.difftype
) {
448 htmlf("dt=%d", ctx
.qry
.difftype
);
451 if (ctx
.qry
.context
> 0 && ctx
.qry
.context
!= 3) {
454 htmlf("%d", ctx
.qry
.context
);
457 if (ctx
.qry
.ignorews
) {
462 if (ctx
.qry
.follow
) {
471 void cgit_patch_link(const char *name
, const char *title
, const char *class,
472 const char *head
, const char *rev
, const char *path
)
474 reporevlink("patch", name
, title
, class, head
, rev
, path
);
477 void cgit_stats_link(const char *name
, const char *title
, const char *class,
478 const char *head
, const char *path
)
480 reporevlink("stats", name
, title
, class, head
, NULL
, path
);
483 static void cgit_self_link(char *name
, const char *title
, const char *class)
485 if (!strcmp(ctx
.qry
.page
, "repolist"))
486 cgit_index_link(name
, title
, class, ctx
.qry
.search
, ctx
.qry
.sort
,
488 else if (!strcmp(ctx
.qry
.page
, "summary"))
489 cgit_summary_link(name
, title
, class, ctx
.qry
.head
);
490 else if (!strcmp(ctx
.qry
.page
, "tag"))
491 cgit_tag_link(name
, title
, class, ctx
.qry
.has_sha1
?
492 ctx
.qry
.sha1
: ctx
.qry
.head
);
493 else if (!strcmp(ctx
.qry
.page
, "tree"))
494 cgit_tree_link(name
, title
, class, ctx
.qry
.head
,
495 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
497 else if (!strcmp(ctx
.qry
.page
, "plain"))
498 cgit_plain_link(name
, title
, class, ctx
.qry
.head
,
499 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
501 else if (!strcmp(ctx
.qry
.page
, "blame"))
502 cgit_blame_link(name
, title
, class, ctx
.qry
.head
,
503 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
505 else if (!strcmp(ctx
.qry
.page
, "log"))
506 cgit_log_link(name
, title
, class, ctx
.qry
.head
,
507 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
508 ctx
.qry
.path
, ctx
.qry
.ofs
,
509 ctx
.qry
.grep
, ctx
.qry
.search
,
510 ctx
.qry
.showmsg
, ctx
.qry
.follow
);
511 else if (!strcmp(ctx
.qry
.page
, "commit"))
512 cgit_commit_link(name
, title
, class, ctx
.qry
.head
,
513 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
515 else if (!strcmp(ctx
.qry
.page
, "patch"))
516 cgit_patch_link(name
, title
, class, ctx
.qry
.head
,
517 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
519 else if (!strcmp(ctx
.qry
.page
, "refs"))
520 cgit_refs_link(name
, title
, class, ctx
.qry
.head
,
521 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
523 else if (!strcmp(ctx
.qry
.page
, "snapshot"))
524 cgit_snapshot_link(name
, title
, class, ctx
.qry
.head
,
525 ctx
.qry
.has_sha1
? ctx
.qry
.sha1
: NULL
,
527 else if (!strcmp(ctx
.qry
.page
, "diff"))
528 cgit_diff_link(name
, title
, class, ctx
.qry
.head
,
529 ctx
.qry
.sha1
, ctx
.qry
.sha2
,
531 else if (!strcmp(ctx
.qry
.page
, "stats"))
532 cgit_stats_link(name
, title
, class, ctx
.qry
.head
,
535 /* Don't known how to make link for this page */
536 repolink(title
, class, ctx
.qry
.page
, ctx
.qry
.head
, ctx
.qry
.path
);
537 html("><!-- cgit_self_link() doesn't know how to make link for page '");
538 html_txt(ctx
.qry
.page
);
545 void cgit_object_link(struct object
*obj
)
547 char *page
, *shortrev
, *fullrev
, *name
;
549 fullrev
= oid_to_hex(&obj
->oid
);
550 shortrev
= xstrdup(fullrev
);
552 if (obj
->type
== OBJ_COMMIT
) {
553 cgit_commit_link(fmt("commit %s...", shortrev
), NULL
, NULL
,
554 ctx
.qry
.head
, fullrev
, NULL
);
556 } else if (obj
->type
== OBJ_TREE
)
558 else if (obj
->type
== OBJ_TAG
)
562 name
= fmt("%s %s...", type_name(obj
->type
), shortrev
);
563 reporevlink(page
, name
, NULL
, NULL
, ctx
.qry
.head
, fullrev
, NULL
);
566 static struct string_list_item
*lookup_path(struct string_list
*list
,
569 struct string_list_item
*item
;
571 while (path
&& path
[0]) {
572 if ((item
= string_list_lookup(list
, path
)))
574 if (!(path
= strchr(path
, '/')))
581 void cgit_submodule_link(const char *class, char *path
, const char *rev
)
583 struct string_list
*list
;
584 struct string_list_item
*item
;
590 list
= &ctx
.repo
->submodules
;
591 item
= lookup_path(list
, path
);
594 tail
= path
[len
- 1];
597 item
= lookup_path(list
, path
);
600 if (item
|| ctx
.repo
->module_link
) {
603 htmlf("class='%s' ", class);
606 html_attrf(item
->util
, rev
);
608 dir
= strrchr(path
, '/');
613 html_attrf(ctx
.repo
->module_link
, dir
, rev
);
621 htmlf(" class='%s'", class);
626 html_txtf(" @ %.7s", rev
);
628 path
[len
- 1] = tail
;
631 const struct date_mode
*cgit_date_mode(enum date_mode_type type
)
633 static struct date_mode mode
;
635 mode
.local
= ctx
.cfg
.local_time
;
639 static void print_rel_date(time_t t
, int tz
, double value
,
640 const char *class, const char *suffix
)
642 htmlf("<span class='%s' title='", class);
643 html_attr(show_date(t
, tz
, cgit_date_mode(DATE_ISO8601
)));
644 htmlf("'>%.0f %s</span>", value
, suffix
);
647 void cgit_print_age(time_t t
, int tz
, time_t max_relative
)
658 if (secs
> max_relative
&& max_relative
>= 0) {
659 html("<span title='");
660 html_attr(show_date(t
, tz
, cgit_date_mode(DATE_ISO8601
)));
662 html_txt(show_date(t
, tz
, cgit_date_mode(DATE_SHORT
)));
667 if (secs
< TM_HOUR
* 2) {
668 print_rel_date(t
, tz
, secs
* 1.0 / TM_MIN
, "age-mins", "min.");
671 if (secs
< TM_DAY
* 2) {
672 print_rel_date(t
, tz
, secs
* 1.0 / TM_HOUR
, "age-hours", "hours");
675 if (secs
< TM_WEEK
* 2) {
676 print_rel_date(t
, tz
, secs
* 1.0 / TM_DAY
, "age-days", "days");
679 if (secs
< TM_MONTH
* 2) {
680 print_rel_date(t
, tz
, secs
* 1.0 / TM_WEEK
, "age-weeks", "weeks");
683 if (secs
< TM_YEAR
* 2) {
684 print_rel_date(t
, tz
, secs
* 1.0 / TM_MONTH
, "age-months", "months");
687 print_rel_date(t
, tz
, secs
* 1.0 / TM_YEAR
, "age-years", "years");
690 void cgit_print_http_headers(void)
692 if (ctx
.env
.no_http
&& !strcmp(ctx
.env
.no_http
, "1"))
696 htmlf("Status: %d %s\n", ctx
.page
.status
, ctx
.page
.statusmsg
);
697 if (ctx
.page
.mimetype
&& ctx
.page
.charset
)
698 htmlf("Content-Type: %s; charset=%s\n", ctx
.page
.mimetype
,
700 else if (ctx
.page
.mimetype
)
701 htmlf("Content-Type: %s\n", ctx
.page
.mimetype
);
703 htmlf("Content-Length: %zd\n", ctx
.page
.size
);
704 if (ctx
.page
.filename
) {
705 html("Content-Disposition: inline; filename=\"");
706 html_header_arg_in_quotes(ctx
.page
.filename
);
709 if (!ctx
.env
.authenticated
)
710 html("Cache-Control: no-cache, no-store\n");
711 htmlf("Last-Modified: %s\n", http_date(ctx
.page
.modified
));
712 htmlf("Expires: %s\n", http_date(ctx
.page
.expires
));
714 htmlf("ETag: \"%s\"\n", ctx
.page
.etag
);
716 if (ctx
.env
.request_method
&& !strcmp(ctx
.env
.request_method
, "HEAD"))
720 void cgit_redirect(const char *url
, bool permanent
)
722 htmlf("Status: %d %s\n", permanent
? 301 : 302, permanent
? "Moved" : "Found");
728 static void print_rel_vcs_link(const char *url
)
730 html("<link rel='vcs-git' href='");
733 html_attr(ctx
.repo
->name
);
734 html(" Git repository'/>\n");
737 void cgit_print_docstart(void)
739 char *host
= cgit_hosturl();
741 if (ctx
.cfg
.embedded
) {
743 html_include(ctx
.cfg
.header
);
748 html("<html lang='en'>\n");
751 html_txt(ctx
.page
.title
);
753 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
754 if (ctx
.cfg
.robots
&& *ctx
.cfg
.robots
)
755 htmlf("<meta name='robots' content='%s'/>\n", ctx
.cfg
.robots
);
756 html("<link rel='stylesheet' type='text/css' href='");
757 html_attr(ctx
.cfg
.css
);
759 if (ctx
.cfg
.favicon
) {
760 html("<link rel='shortcut icon' href='");
761 html_attr(ctx
.cfg
.favicon
);
764 if (host
&& ctx
.repo
&& ctx
.qry
.head
) {
766 struct strbuf sb
= STRBUF_INIT
;
767 strbuf_addf(&sb
, "h=%s", ctx
.qry
.head
);
769 html("<link rel='alternate' title='Atom feed' href='");
770 html(cgit_httpscheme());
772 fileurl
= cgit_fileurl(ctx
.repo
->url
, "atom", ctx
.qry
.vpath
,
775 html("' type='application/atom+xml'/>\n");
780 cgit_add_clone_urls(print_rel_vcs_link
);
781 if (ctx
.cfg
.head_include
)
782 html_include(ctx
.cfg
.head_include
);
783 if (ctx
.repo
&& ctx
.repo
->extra_head_content
)
784 html(ctx
.repo
->extra_head_content
);
788 html_include(ctx
.cfg
.header
);
792 void cgit_print_docend(void)
794 html("</div> <!-- class=content -->\n");
795 if (ctx
.cfg
.embedded
) {
796 html("</div> <!-- id=cgit -->\n");
798 html_include(ctx
.cfg
.footer
);
802 html_include(ctx
.cfg
.footer
);
804 htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
805 "(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version
, git_version_string
);
806 html_txt(show_date(time(NULL
), 0, cgit_date_mode(DATE_ISO8601
)));
809 html("</div> <!-- id=cgit -->\n");
810 html("</body>\n</html>\n");
813 void cgit_print_error_page(int code
, const char *msg
, const char *fmt
, ...)
816 ctx
.page
.expires
= ctx
.cfg
.cache_dynamic_ttl
;
817 ctx
.page
.status
= code
;
818 ctx
.page
.statusmsg
= msg
;
819 cgit_print_layout_start();
821 cgit_vprint_error(fmt
, ap
);
823 cgit_print_layout_end();
826 void cgit_print_layout_start(void)
828 cgit_print_http_headers();
829 cgit_print_docstart();
830 cgit_print_pageheader();
833 void cgit_print_layout_end(void)
838 static void add_clone_urls(void (*fn
)(const char *), char *txt
, char *suffix
)
840 struct strbuf
**url_list
= strbuf_split_str(txt
, ' ', 0);
843 for (i
= 0; url_list
[i
]; i
++) {
844 strbuf_rtrim(url_list
[i
]);
845 if (url_list
[i
]->len
== 0)
847 if (suffix
&& *suffix
)
848 strbuf_addf(url_list
[i
], "/%s", suffix
);
849 fn(url_list
[i
]->buf
);
852 strbuf_list_free(url_list
);
855 void cgit_add_clone_urls(void (*fn
)(const char *))
857 if (ctx
.repo
->clone_url
)
858 add_clone_urls(fn
, expand_macros(ctx
.repo
->clone_url
), NULL
);
859 else if (ctx
.cfg
.clone_prefix
)
860 add_clone_urls(fn
, ctx
.cfg
.clone_prefix
, ctx
.repo
->url
);
863 static int print_branch_option(const char *refname
, const struct object_id
*oid
,
864 int flags
, void *cb_data
)
866 char *name
= (char *)refname
;
867 html_option(name
, name
, ctx
.qry
.head
);
871 void cgit_add_hidden_formfields(int incl_head
, int incl_search
,
874 if (!ctx
.cfg
.virtual_root
) {
875 struct strbuf url
= STRBUF_INIT
;
877 strbuf_addf(&url
, "%s/%s", ctx
.qry
.repo
, page
);
879 strbuf_addf(&url
, "/%s", ctx
.qry
.vpath
);
880 html_hidden("url", url
.buf
);
881 strbuf_release(&url
);
884 if (incl_head
&& ctx
.qry
.head
&& ctx
.repo
->defbranch
&&
885 strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
886 html_hidden("h", ctx
.qry
.head
);
889 html_hidden("id", ctx
.qry
.sha1
);
891 html_hidden("id2", ctx
.qry
.sha2
);
893 html_hidden("showmsg", "1");
897 html_hidden("qt", ctx
.qry
.grep
);
899 html_hidden("q", ctx
.qry
.search
);
903 static const char *hc(const char *page
)
908 return strcmp(ctx
.qry
.page
, page
) ? NULL
: "active";
911 static void cgit_print_path_crumbs(char *path
)
913 char *old_path
= ctx
.qry
.path
;
914 char *p
= path
, *q
, *end
= path
+ strlen(path
);
917 cgit_self_link("root", NULL
, NULL
);
918 ctx
.qry
.path
= p
= path
;
920 if (!(q
= strchr(p
, '/')))
924 cgit_self_link(p
, NULL
, NULL
);
929 ctx
.qry
.path
= old_path
;
932 static void print_header(void)
934 char *logo
= NULL
, *logo_link
= NULL
;
936 html("<table id='header'>\n");
939 if (ctx
.repo
&& ctx
.repo
->logo
&& *ctx
.repo
->logo
)
940 logo
= ctx
.repo
->logo
;
943 if (ctx
.repo
&& ctx
.repo
->logo_link
&& *ctx
.repo
->logo_link
)
944 logo_link
= ctx
.repo
->logo_link
;
946 logo_link
= ctx
.cfg
.logo_link
;
948 html("<td class='logo' rowspan='2'><a href='");
949 if (logo_link
&& *logo_link
)
950 html_attr(logo_link
);
952 html_attr(cgit_rooturl());
953 html("'><img src='");
955 html("' alt='cgit logo'/></a></td>\n");
958 html("<td class='main'>");
960 cgit_index_link("index", NULL
, NULL
, NULL
, NULL
, 0, 1);
962 cgit_summary_link(ctx
.repo
->name
, ctx
.repo
->name
, NULL
, NULL
);
963 if (ctx
.env
.authenticated
) {
964 html("</td><td class='form'>");
965 html("<form method='get'>\n");
966 cgit_add_hidden_formfields(0, 1, ctx
.qry
.page
);
967 html("<select name='h' onchange='this.form.submit();'>\n");
968 for_each_branch_ref(print_branch_option
, ctx
.qry
.head
);
969 if (ctx
.repo
->enable_remote_branches
)
970 for_each_remote_ref(print_branch_option
, ctx
.qry
.head
);
972 html("<input type='submit' value='switch'/>");
976 html_txt(ctx
.cfg
.root_title
);
977 html("</td></tr>\n");
979 html("<tr><td class='sub'>");
981 html_txt(ctx
.repo
->desc
);
982 html("</td><td class='sub right'>");
983 html_txt(ctx
.repo
->owner
);
985 if (ctx
.cfg
.root_desc
)
986 html_txt(ctx
.cfg
.root_desc
);
988 html("</td></tr></table>\n");
991 void cgit_print_pageheader(void)
993 html("<div id='cgit'>");
994 if (!ctx
.env
.authenticated
|| !ctx
.cfg
.noheader
)
997 html("<table class='tabs'><tr><td>\n");
998 if (ctx
.env
.authenticated
&& ctx
.repo
) {
999 if (ctx
.repo
->readme
.nr
)
1000 reporevlink("about", "about", NULL
,
1001 hc("about"), ctx
.qry
.head
, NULL
,
1003 cgit_summary_link("summary", NULL
, hc("summary"),
1005 cgit_refs_link("refs", NULL
, hc("refs"), ctx
.qry
.head
,
1006 ctx
.qry
.sha1
, NULL
);
1007 cgit_log_link("log", NULL
, hc("log"), ctx
.qry
.head
,
1008 NULL
, ctx
.qry
.vpath
, 0, NULL
, NULL
,
1009 ctx
.qry
.showmsg
, ctx
.qry
.follow
);
1010 if (ctx
.qry
.page
&& !strcmp(ctx
.qry
.page
, "blame"))
1011 cgit_blame_link("blame", NULL
, hc("blame"), ctx
.qry
.head
,
1012 ctx
.qry
.sha1
, ctx
.qry
.vpath
);
1014 cgit_tree_link("tree", NULL
, hc("tree"), ctx
.qry
.head
,
1015 ctx
.qry
.sha1
, ctx
.qry
.vpath
);
1016 cgit_commit_link("commit", NULL
, hc("commit"),
1017 ctx
.qry
.head
, ctx
.qry
.sha1
, ctx
.qry
.vpath
);
1018 cgit_diff_link("diff", NULL
, hc("diff"), ctx
.qry
.head
,
1019 ctx
.qry
.sha1
, ctx
.qry
.sha2
, ctx
.qry
.vpath
);
1020 if (ctx
.repo
->max_stats
)
1021 cgit_stats_link("stats", NULL
, hc("stats"),
1022 ctx
.qry
.head
, ctx
.qry
.vpath
);
1023 if (ctx
.repo
->homepage
) {
1025 html_attr(ctx
.repo
->homepage
);
1026 html("'>homepage</a>");
1028 html("</td><td class='form'>");
1029 html("<form class='right' method='get' action='");
1030 if (ctx
.cfg
.virtual_root
) {
1031 char *fileurl
= cgit_fileurl(ctx
.qry
.repo
, "log",
1032 ctx
.qry
.vpath
, NULL
);
1033 html_url_path(fileurl
);
1037 cgit_add_hidden_formfields(1, 0, "log");
1038 html("<select name='qt'>\n");
1039 html_option("grep", "log msg", ctx
.qry
.grep
);
1040 html_option("author", "author", ctx
.qry
.grep
);
1041 html_option("committer", "committer", ctx
.qry
.grep
);
1042 html_option("range", "range", ctx
.qry
.grep
);
1043 html("</select>\n");
1044 html("<input class='txt' type='search' size='10' name='q' value='");
1045 html_attr(ctx
.qry
.search
);
1047 html("<input type='submit' value='search'/>\n");
1049 } else if (ctx
.env
.authenticated
) {
1050 char *currenturl
= cgit_currenturl();
1051 site_link(NULL
, "index", NULL
, hc("repolist"), NULL
, NULL
, 0, 1);
1052 if (ctx
.cfg
.root_readme
)
1053 site_link("about", "about", NULL
, hc("about"),
1055 html("</td><td class='form'>");
1056 html("<form method='get' action='");
1057 html_attr(currenturl
);
1059 html("<input type='search' name='q' size='10' value='");
1060 html_attr(ctx
.qry
.search
);
1062 html("<input type='submit' value='search'/>\n");
1066 html("</td></tr></table>\n");
1067 if (ctx
.env
.authenticated
&& ctx
.repo
&& ctx
.qry
.vpath
) {
1068 html("<div class='path'>");
1070 cgit_print_path_crumbs(ctx
.qry
.vpath
);
1071 if (ctx
.cfg
.enable_follow_links
&& !strcmp(ctx
.qry
.page
, "log")) {
1073 ctx
.qry
.follow
= !ctx
.qry
.follow
;
1074 cgit_self_link(ctx
.qry
.follow
? "follow" : "unfollow",
1076 ctx
.qry
.follow
= !ctx
.qry
.follow
;
1081 html("<div class='content'>");
1084 void cgit_print_filemode(unsigned short mode
)
1088 else if (S_ISLNK(mode
))
1090 else if (S_ISGITLINK(mode
))
1094 html_fileperm(mode
>> 6);
1095 html_fileperm(mode
>> 3);
1096 html_fileperm(mode
);
1099 void cgit_compose_snapshot_prefix(struct strbuf
*filename
, const char *base
,
1102 struct object_id oid
;
1105 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1106 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1107 * i.e. each stripped tag can be inverted without ambiguities.
1109 if (get_oid(fmt("refs/tags/%s", ref
), &oid
) == 0 &&
1110 (ref
[0] == 'v' || ref
[0] == 'V') && isdigit(ref
[1]) &&
1111 ((get_oid(fmt("refs/tags/%s", ref
+ 1), &oid
) == 0) +
1112 (get_oid(fmt("refs/tags/v%s", ref
+ 1), &oid
) == 0) +
1113 (get_oid(fmt("refs/tags/V%s", ref
+ 1), &oid
) == 0) == 1))
1116 strbuf_addf(filename
, "%s-%s", base
, ref
);
1119 void cgit_print_snapshot_links(const struct cgit_repo
*repo
, const char *ref
,
1120 const char *separator
)
1122 const struct cgit_snapshot_format
*f
;
1123 struct strbuf filename
= STRBUF_INIT
;
1124 const char *basename
;
1127 basename
= cgit_snapshot_prefix(repo
);
1128 if (starts_with(ref
, basename
))
1129 strbuf_addstr(&filename
, ref
);
1131 cgit_compose_snapshot_prefix(&filename
, basename
, ref
);
1133 prefixlen
= filename
.len
;
1134 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
1135 if (!(repo
->snapshots
& cgit_snapshot_format_bit(f
)))
1137 strbuf_setlen(&filename
, prefixlen
);
1138 strbuf_addstr(&filename
, f
->suffix
);
1139 cgit_snapshot_link(filename
.buf
, NULL
, NULL
, NULL
, NULL
,
1141 if (cgit_snapshot_get_sig(ref
, f
)) {
1142 strbuf_addstr(&filename
, ".asc");
1144 cgit_snapshot_link("sig", NULL
, NULL
, NULL
, NULL
,
1147 } else if (starts_with(f
->suffix
, ".tar") && cgit_snapshot_get_sig(ref
, &cgit_snapshot_formats
[0])) {
1148 strbuf_setlen(&filename
, strlen(filename
.buf
) - strlen(f
->suffix
));
1149 strbuf_addstr(&filename
, ".tar.asc");
1151 cgit_snapshot_link("sig", NULL
, NULL
, NULL
, NULL
,
1157 strbuf_release(&filename
);
1160 void cgit_set_title_from_path(const char *path
)
1162 size_t path_len
, path_index
, path_last_end
;
1168 path_len
= strlen(path
);
1169 new_title
= xmalloc(path_len
+ 3 + strlen(ctx
.page
.title
) + 1);
1170 new_title
[0] = '\0';
1172 for (path_index
= path_len
, path_last_end
= path_len
; path_index
-- > 0;) {
1173 if (path
[path_index
] == '/') {
1174 if (path_index
== path_len
- 1) {
1175 path_last_end
= path_index
- 1;
1178 strncat(new_title
, &path
[path_index
+ 1], path_last_end
- path_index
- 1);
1179 strcat(new_title
, "\\");
1180 path_last_end
= path_index
;
1184 strncat(new_title
, path
, path_last_end
);
1186 strcat(new_title
, " - ");
1187 strcat(new_title
, ctx
.page
.title
);
1188 ctx
.page
.title
= new_title
;