]>
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();
74 if (root
[0] && root
[strlen(root
) - 1] == '/')
75 return fmtalloc("%s%s", root
, ctx
.qry
.url
);
76 return fmtalloc("%s/%s", root
, ctx
.qry
.url
);
79 char *cgit_currentfullurl(void)
81 const char *root
= cgit_rooturl();
82 const char *orig_query
= ctx
.env
.query_string
? ctx
.env
.query_string
: "";
83 size_t len
= strlen(orig_query
);
84 char *query
= xmalloc(len
+ 2), *start_url
, *ret
;
86 /* Remove all url=... parts from query string */
87 memcpy(query
+ 1, orig_query
, len
+ 1);
90 while ((start_url
= strstr(start_url
, "url=")) != NULL
) {
91 if (start_url
[-1] == '?' || start_url
[-1] == '&') {
92 const char *end_url
= strchr(start_url
, '&');
94 memmove(start_url
, end_url
+ 1, strlen(end_url
));
104 ret
= fmtalloc("%s%s", root
, query
);
105 else if (root
[0] && root
[strlen(root
) - 1] == '/')
106 ret
= fmtalloc("%s%s%s", root
, ctx
.qry
.url
, query
);
108 ret
= fmtalloc("%s/%s%s", root
, ctx
.qry
.url
, query
);
113 const char *cgit_rooturl(void)
115 if (ctx
.cfg
.virtual_root
)
116 return ctx
.cfg
.virtual_root
;
118 return ctx
.cfg
.script_name
;
121 const char *cgit_loginurl(void)
123 static const char *login_url
;
125 login_url
= fmtalloc("%s?p=login", cgit_rooturl());
129 char *cgit_repourl(const char *reponame
)
131 if (ctx
.cfg
.virtual_root
)
132 return fmtalloc("%s%s/", ctx
.cfg
.virtual_root
, reponame
);
134 return fmtalloc("?r=%s", reponame
);
137 char *cgit_fileurl(const char *reponame
, const char *pagename
,
138 const char *filename
, const char *query
)
140 struct strbuf sb
= STRBUF_INIT
;
143 if (ctx
.cfg
.virtual_root
) {
144 strbuf_addf(&sb
, "%s%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
145 pagename
, (filename
? filename
:""));
148 strbuf_addf(&sb
, "?url=%s/%s/%s", reponame
, pagename
,
149 (filename
? filename
: ""));
153 strbuf_addf(&sb
, "%s%s", delim
, query
);
154 return strbuf_detach(&sb
, NULL
);
157 char *cgit_pageurl(const char *reponame
, const char *pagename
,
160 return cgit_fileurl(reponame
, pagename
, NULL
, query
);
163 const char *cgit_repobasename(const char *reponame
)
165 /* I assume we don't need to store more than one repo basename */
166 static char rvbuf
[1024];
171 len
= strlcpy(rvbuf
, reponame
, sizeof(rvbuf
));
172 if (len
>= sizeof(rvbuf
))
173 die("cgit_repobasename: truncated repository name '%s'", reponame
);
175 /* strip trailing slashes */
176 while (p
&& rvbuf
[p
] == '/')
178 /* strip trailing .git */
179 if (p
>= 3 && starts_with(&rvbuf
[p
-3], ".git")) {
183 /* strip more trailing slashes if any */
184 while (p
&& rvbuf
[p
] == '/')
186 /* find last slash in the remaining string */
187 rv
= strrchr(rvbuf
, '/');
193 const char *cgit_snapshot_prefix(const struct cgit_repo
*repo
)
195 if (repo
->snapshot_prefix
)
196 return repo
->snapshot_prefix
;
198 return cgit_repobasename(repo
->url
);
201 static void site_url(const char *page
, const char *search
, const char *sort
, int ofs
, int always_root
)
205 if (always_root
|| page
)
206 html_attr(cgit_rooturl());
208 char *currenturl
= cgit_currenturl();
209 html_attr(currenturl
);
214 htmlf("?p=%s", page
);
231 htmlf("ofs=%d", ofs
);
235 static void site_link(const char *page
, const char *name
, const char *title
,
236 const char *class, const char *search
, const char *sort
, int ofs
, int always_root
)
250 site_url(page
, search
, sort
, ofs
, always_root
);
256 void cgit_index_link(const char *name
, const char *title
, const char *class,
257 const char *pattern
, const char *sort
, int ofs
, int always_root
)
259 site_link(NULL
, name
, title
, class, pattern
, sort
, ofs
, always_root
);
262 static char *repolink(const char *title
, const char *class, const char *page
,
263 const char *head
, const char *path
)
279 if (ctx
.cfg
.virtual_root
) {
280 html_url_path(ctx
.cfg
.virtual_root
);
281 html_url_path(ctx
.repo
->url
);
282 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
291 html_url_path(ctx
.cfg
.script_name
);
293 html_url_arg(ctx
.repo
->url
);
294 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
304 if (head
&& ctx
.repo
->defbranch
&& strcmp(head
, ctx
.repo
->defbranch
)) {
310 return fmt("%s", delim
);
313 static void reporevlink(const char *page
, const char *name
, const char *title
,
314 const char *class, const char *head
, const char *rev
,
319 delim
= repolink(title
, class, page
, head
, path
);
320 if (rev
&& ctx
.qry
.head
!= NULL
&& strcmp(rev
, ctx
.qry
.head
)) {
330 void cgit_summary_link(const char *name
, const char *title
, const char *class,
333 reporevlink(NULL
, name
, title
, class, head
, NULL
, NULL
);
336 void cgit_tag_link(const char *name
, const char *title
, const char *class,
339 reporevlink("tag", name
, title
, class, tag
, NULL
, NULL
);
342 void cgit_tree_link(const char *name
, const char *title
, const char *class,
343 const char *head
, const char *rev
, const char *path
)
345 reporevlink("tree", name
, title
, class, head
, rev
, path
);
348 void cgit_plain_link(const char *name
, const char *title
, const char *class,
349 const char *head
, const char *rev
, const char *path
)
351 reporevlink("plain", name
, title
, class, head
, rev
, path
);
354 void cgit_blame_link(const char *name
, const char *title
, const char *class,
355 const char *head
, const char *rev
, const char *path
)
357 reporevlink("blame", name
, title
, class, head
, rev
, path
);
360 void cgit_log_link(const char *name
, const char *title
, const char *class,
361 const char *head
, const char *rev
, const char *path
,
362 int ofs
, const char *grep
, const char *pattern
, int showmsg
,
367 delim
= repolink(title
, class, "log", head
, path
);
368 if (rev
&& ctx
.qry
.head
&& strcmp(rev
, ctx
.qry
.head
)) {
374 if (grep
&& pattern
) {
381 html_url_arg(pattern
);
403 void cgit_commit_link(const char *name
, const char *title
, const char *class,
404 const char *head
, const char *rev
, const char *path
)
408 delim
= repolink(title
, class, "commit", head
, path
);
409 if (rev
&& ctx
.qry
.head
&& strcmp(rev
, ctx
.qry
.head
)) {
415 if (ctx
.qry
.difftype
) {
417 htmlf("dt=%d", ctx
.qry
.difftype
);
420 if (ctx
.qry
.context
> 0 && ctx
.qry
.context
!= 3) {
423 htmlf("%d", ctx
.qry
.context
);
426 if (ctx
.qry
.ignorews
) {
431 if (ctx
.qry
.follow
) {
436 if (name
[0] != '\0') {
437 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
438 html_ntxt(name
, ctx
.cfg
.max_msg_len
- 3);
443 html_txt("(no commit message)");
447 void cgit_refs_link(const char *name
, const char *title
, const char *class,
448 const char *head
, const char *rev
, const char *path
)
450 reporevlink("refs", name
, title
, class, head
, rev
, path
);
453 void cgit_snapshot_link(const char *name
, const char *title
, const char *class,
454 const char *head
, const char *rev
,
455 const char *archivename
)
457 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
460 void cgit_diff_link(const char *name
, const char *title
, const char *class,
461 const char *head
, const char *new_rev
, const char *old_rev
,
466 delim
= repolink(title
, class, "diff", head
, path
);
467 if (new_rev
&& ctx
.qry
.head
!= NULL
&& strcmp(new_rev
, ctx
.qry
.head
)) {
470 html_url_arg(new_rev
);
476 html_url_arg(old_rev
);
479 if (ctx
.qry
.difftype
) {
481 htmlf("dt=%d", ctx
.qry
.difftype
);
484 if (ctx
.qry
.context
> 0 && ctx
.qry
.context
!= 3) {
487 htmlf("%d", ctx
.qry
.context
);
490 if (ctx
.qry
.ignorews
) {
495 if (ctx
.qry
.follow
) {
504 void cgit_patch_link(const char *name
, const char *title
, const char *class,
505 const char *head
, const char *rev
, const char *path
)
507 reporevlink("patch", name
, title
, class, head
, rev
, path
);
510 void cgit_stats_link(const char *name
, const char *title
, const char *class,
511 const char *head
, const char *path
)
513 reporevlink("stats", name
, title
, class, head
, NULL
, path
);
516 static void cgit_self_link(char *name
, const char *title
, const char *class)
518 if (!strcmp(ctx
.qry
.page
, "repolist"))
519 cgit_index_link(name
, title
, class, ctx
.qry
.search
, ctx
.qry
.sort
,
521 else if (!strcmp(ctx
.qry
.page
, "summary"))
522 cgit_summary_link(name
, title
, class, ctx
.qry
.head
);
523 else if (!strcmp(ctx
.qry
.page
, "tag"))
524 cgit_tag_link(name
, title
, class, ctx
.qry
.has_oid
?
525 ctx
.qry
.oid
: ctx
.qry
.head
);
526 else if (!strcmp(ctx
.qry
.page
, "tree"))
527 cgit_tree_link(name
, title
, class, ctx
.qry
.head
,
528 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
530 else if (!strcmp(ctx
.qry
.page
, "plain"))
531 cgit_plain_link(name
, title
, class, ctx
.qry
.head
,
532 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
534 else if (!strcmp(ctx
.qry
.page
, "blame"))
535 cgit_blame_link(name
, title
, class, ctx
.qry
.head
,
536 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
538 else if (!strcmp(ctx
.qry
.page
, "log"))
539 cgit_log_link(name
, title
, class, ctx
.qry
.head
,
540 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
541 ctx
.qry
.path
, ctx
.qry
.ofs
,
542 ctx
.qry
.grep
, ctx
.qry
.search
,
543 ctx
.qry
.showmsg
, ctx
.qry
.follow
);
544 else if (!strcmp(ctx
.qry
.page
, "commit"))
545 cgit_commit_link(name
, title
, class, ctx
.qry
.head
,
546 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
548 else if (!strcmp(ctx
.qry
.page
, "patch"))
549 cgit_patch_link(name
, title
, class, ctx
.qry
.head
,
550 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
552 else if (!strcmp(ctx
.qry
.page
, "refs"))
553 cgit_refs_link(name
, title
, class, ctx
.qry
.head
,
554 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
556 else if (!strcmp(ctx
.qry
.page
, "snapshot"))
557 cgit_snapshot_link(name
, title
, class, ctx
.qry
.head
,
558 ctx
.qry
.has_oid
? ctx
.qry
.oid
: NULL
,
560 else if (!strcmp(ctx
.qry
.page
, "diff"))
561 cgit_diff_link(name
, title
, class, ctx
.qry
.head
,
562 ctx
.qry
.oid
, ctx
.qry
.oid2
,
564 else if (!strcmp(ctx
.qry
.page
, "stats"))
565 cgit_stats_link(name
, title
, class, ctx
.qry
.head
,
568 /* Don't known how to make link for this page */
569 repolink(title
, class, ctx
.qry
.page
, ctx
.qry
.head
, ctx
.qry
.path
);
570 html("><!-- cgit_self_link() doesn't know how to make link for page '");
571 html_txt(ctx
.qry
.page
);
578 void cgit_object_link(struct object
*obj
)
580 char *page
, *shortrev
, *fullrev
, *name
;
582 fullrev
= oid_to_hex(&obj
->oid
);
583 shortrev
= xstrdup(fullrev
);
585 if (obj
->type
== OBJ_COMMIT
) {
586 cgit_commit_link(fmt("commit %s...", shortrev
), NULL
, NULL
,
587 ctx
.qry
.head
, fullrev
, NULL
);
589 } else if (obj
->type
== OBJ_TREE
)
591 else if (obj
->type
== OBJ_TAG
)
595 name
= fmt("%s %s...", type_name(obj
->type
), shortrev
);
596 reporevlink(page
, name
, NULL
, NULL
, ctx
.qry
.head
, fullrev
, NULL
);
599 static struct string_list_item
*lookup_path(struct string_list
*list
,
602 struct string_list_item
*item
;
604 while (path
&& path
[0]) {
605 if ((item
= string_list_lookup(list
, path
)))
607 if (!(path
= strchr(path
, '/')))
614 void cgit_submodule_link(const char *class, char *path
, const char *rev
)
616 struct string_list
*list
;
617 struct string_list_item
*item
;
623 list
= &ctx
.repo
->submodules
;
624 item
= lookup_path(list
, path
);
627 tail
= path
[len
- 1];
630 item
= lookup_path(list
, path
);
633 if (item
|| ctx
.repo
->module_link
) {
636 htmlf("class='%s' ", class);
639 html_attrf(item
->util
, rev
);
641 dir
= strrchr(path
, '/');
646 html_attrf(ctx
.repo
->module_link
, dir
, rev
);
654 htmlf(" class='%s'", class);
659 html_txtf(" @ %.7s", rev
);
661 path
[len
- 1] = tail
;
664 const struct date_mode
*cgit_date_mode(enum date_mode_type type
)
666 static struct date_mode mode
;
668 mode
.local
= ctx
.cfg
.local_time
;
672 static void print_rel_date(time_t t
, int tz
, double value
,
673 const char *class, const char *suffix
)
675 htmlf("<span class='%s' title='", class);
676 html_attr(show_date(t
, tz
, cgit_date_mode(DATE_ISO8601
)));
677 htmlf("'>%.0f %s</span>", value
, suffix
);
680 void cgit_print_age(time_t t
, int tz
, time_t max_relative
)
691 if (secs
> max_relative
&& max_relative
>= 0) {
692 html("<span title='");
693 html_attr(show_date(t
, tz
, cgit_date_mode(DATE_ISO8601
)));
695 html_txt(show_date(t
, tz
, cgit_date_mode(DATE_SHORT
)));
700 if (secs
< TM_HOUR
* 2) {
701 print_rel_date(t
, tz
, secs
* 1.0 / TM_MIN
, "age-mins", "min.");
704 if (secs
< TM_DAY
* 2) {
705 print_rel_date(t
, tz
, secs
* 1.0 / TM_HOUR
, "age-hours", "hours");
708 if (secs
< TM_WEEK
* 2) {
709 print_rel_date(t
, tz
, secs
* 1.0 / TM_DAY
, "age-days", "days");
712 if (secs
< TM_MONTH
* 2) {
713 print_rel_date(t
, tz
, secs
* 1.0 / TM_WEEK
, "age-weeks", "weeks");
716 if (secs
< TM_YEAR
* 2) {
717 print_rel_date(t
, tz
, secs
* 1.0 / TM_MONTH
, "age-months", "months");
720 print_rel_date(t
, tz
, secs
* 1.0 / TM_YEAR
, "age-years", "years");
723 void cgit_print_http_headers(void)
725 if (ctx
.env
.no_http
&& !strcmp(ctx
.env
.no_http
, "1"))
729 htmlf("Status: %d %s\n", ctx
.page
.status
, ctx
.page
.statusmsg
);
730 if (ctx
.page
.mimetype
&& ctx
.page
.charset
)
731 htmlf("Content-Type: %s; charset=%s\n", ctx
.page
.mimetype
,
733 else if (ctx
.page
.mimetype
)
734 htmlf("Content-Type: %s\n", ctx
.page
.mimetype
);
736 htmlf("Content-Length: %zd\n", ctx
.page
.size
);
737 if (ctx
.page
.filename
) {
738 html("Content-Disposition: inline; filename=\"");
739 html_header_arg_in_quotes(ctx
.page
.filename
);
742 if (!ctx
.env
.authenticated
)
743 html("Cache-Control: no-cache, no-store\n");
744 htmlf("Last-Modified: %s\n", http_date(ctx
.page
.modified
));
745 htmlf("Expires: %s\n", http_date(ctx
.page
.expires
));
747 htmlf("ETag: \"%s\"\n", ctx
.page
.etag
);
749 if (ctx
.env
.request_method
&& !strcmp(ctx
.env
.request_method
, "HEAD"))
753 void cgit_redirect(const char *url
, bool permanent
)
755 htmlf("Status: %d %s\n", permanent
? 301 : 302, permanent
? "Moved" : "Found");
761 static void print_rel_vcs_link(const char *url
)
763 html("<link rel='vcs-git' href='");
766 html_attr(ctx
.repo
->name
);
767 html(" Git repository'/>\n");
770 void cgit_print_docstart(void)
772 char *host
= cgit_hosturl();
774 if (ctx
.cfg
.embedded
) {
776 html_include(ctx
.cfg
.header
);
781 html("<html lang='en'>\n");
784 html_txt(ctx
.page
.title
);
786 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
787 if (ctx
.cfg
.robots
&& *ctx
.cfg
.robots
)
788 htmlf("<meta name='robots' content='%s'/>\n", ctx
.cfg
.robots
);
789 html("<link rel='stylesheet' type='text/css' href='");
790 html_attr(ctx
.cfg
.css
);
792 if (ctx
.cfg
.favicon
) {
793 html("<link rel='shortcut icon' href='");
794 html_attr(ctx
.cfg
.favicon
);
797 if (host
&& ctx
.repo
&& ctx
.qry
.head
) {
799 struct strbuf sb
= STRBUF_INIT
;
800 strbuf_addf(&sb
, "h=%s", ctx
.qry
.head
);
802 html("<link rel='alternate' title='Atom feed' href='");
803 html(cgit_httpscheme());
805 fileurl
= cgit_fileurl(ctx
.repo
->url
, "atom", ctx
.qry
.vpath
,
808 html("' type='application/atom+xml'/>\n");
813 cgit_add_clone_urls(print_rel_vcs_link
);
814 if (ctx
.cfg
.head_include
)
815 html_include(ctx
.cfg
.head_include
);
816 if (ctx
.repo
&& ctx
.repo
->extra_head_content
)
817 html(ctx
.repo
->extra_head_content
);
821 html_include(ctx
.cfg
.header
);
825 void cgit_print_docend(void)
827 html("</div> <!-- class=content -->\n");
828 if (ctx
.cfg
.embedded
) {
829 html("</div> <!-- id=cgit -->\n");
831 html_include(ctx
.cfg
.footer
);
835 html_include(ctx
.cfg
.footer
);
837 htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
838 "(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version
, git_version_string
);
839 html_txt(show_date(time(NULL
), 0, cgit_date_mode(DATE_ISO8601
)));
842 html("</div> <!-- id=cgit -->\n");
843 html("</body>\n</html>\n");
846 void cgit_print_error_page(int code
, const char *msg
, const char *fmt
, ...)
849 ctx
.page
.expires
= ctx
.cfg
.cache_dynamic_ttl
;
850 ctx
.page
.status
= code
;
851 ctx
.page
.statusmsg
= msg
;
852 cgit_print_layout_start();
854 cgit_vprint_error(fmt
, ap
);
856 cgit_print_layout_end();
859 void cgit_print_layout_start(void)
861 cgit_print_http_headers();
862 cgit_print_docstart();
863 cgit_print_pageheader();
866 void cgit_print_layout_end(void)
871 static void add_clone_urls(void (*fn
)(const char *), char *txt
, char *suffix
)
873 struct strbuf
**url_list
= strbuf_split_str(txt
, ' ', 0);
876 for (i
= 0; url_list
[i
]; i
++) {
877 strbuf_rtrim(url_list
[i
]);
878 if (url_list
[i
]->len
== 0)
880 if (suffix
&& *suffix
)
881 strbuf_addf(url_list
[i
], "/%s", suffix
);
882 fn(url_list
[i
]->buf
);
885 strbuf_list_free(url_list
);
888 void cgit_add_clone_urls(void (*fn
)(const char *))
890 if (ctx
.repo
->clone_url
)
891 add_clone_urls(fn
, expand_macros(ctx
.repo
->clone_url
), NULL
);
892 else if (ctx
.cfg
.clone_prefix
)
893 add_clone_urls(fn
, ctx
.cfg
.clone_prefix
, ctx
.repo
->url
);
896 static int print_branch_option(const char *refname
, const struct object_id
*oid
,
897 int flags
, void *cb_data
)
899 char *name
= (char *)refname
;
900 html_option(name
, name
, ctx
.qry
.head
);
904 void cgit_add_hidden_formfields(int incl_head
, int incl_search
,
907 if (!ctx
.cfg
.virtual_root
) {
908 struct strbuf url
= STRBUF_INIT
;
910 strbuf_addf(&url
, "%s/%s", ctx
.qry
.repo
, page
);
912 strbuf_addf(&url
, "/%s", ctx
.qry
.vpath
);
913 html_hidden("url", url
.buf
);
914 strbuf_release(&url
);
917 if (incl_head
&& ctx
.qry
.head
&& ctx
.repo
->defbranch
&&
918 strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
919 html_hidden("h", ctx
.qry
.head
);
922 html_hidden("id", ctx
.qry
.oid
);
924 html_hidden("id2", ctx
.qry
.oid2
);
926 html_hidden("showmsg", "1");
930 html_hidden("qt", ctx
.qry
.grep
);
932 html_hidden("q", ctx
.qry
.search
);
936 static const char *hc(const char *page
)
941 return strcmp(ctx
.qry
.page
, page
) ? NULL
: "active";
944 static void cgit_print_path_crumbs(char *path
)
946 char *old_path
= ctx
.qry
.path
;
947 char *p
= path
, *q
, *end
= path
+ strlen(path
);
951 cgit_self_link("root", NULL
, NULL
);
952 ctx
.qry
.path
= p
= path
;
954 if (!(q
= strchr(p
, '/')) || levels
> 15)
958 cgit_self_link(p
, NULL
, NULL
);
964 ctx
.qry
.path
= old_path
;
967 static void print_header(void)
969 char *logo
= NULL
, *logo_link
= NULL
;
971 html("<table id='header'>\n");
974 if (ctx
.repo
&& ctx
.repo
->logo
&& *ctx
.repo
->logo
)
975 logo
= ctx
.repo
->logo
;
978 if (ctx
.repo
&& ctx
.repo
->logo_link
&& *ctx
.repo
->logo_link
)
979 logo_link
= ctx
.repo
->logo_link
;
981 logo_link
= ctx
.cfg
.logo_link
;
983 html("<td class='logo' rowspan='2'><a href='");
984 if (logo_link
&& *logo_link
)
985 html_attr(logo_link
);
987 html_attr(cgit_rooturl());
988 html("'><img src='");
990 html("' alt='cgit logo'/></a></td>\n");
993 html("<td class='main'>");
995 cgit_index_link("index", NULL
, NULL
, NULL
, NULL
, 0, 1);
997 cgit_summary_link(ctx
.repo
->name
, ctx
.repo
->name
, NULL
, NULL
);
998 if (ctx
.env
.authenticated
) {
999 html("</td><td class='form'>");
1000 html("<form method='get'>\n");
1001 cgit_add_hidden_formfields(0, 1, ctx
.qry
.page
);
1002 html("<select name='h' onchange='this.form.submit();'>\n");
1003 for_each_branch_ref(print_branch_option
, ctx
.qry
.head
);
1004 if (ctx
.repo
->enable_remote_branches
)
1005 for_each_remote_ref(print_branch_option
, ctx
.qry
.head
);
1007 html("<input type='submit' value='switch'/>");
1011 html_txt(ctx
.cfg
.root_title
);
1012 html("</td></tr>\n");
1014 html("<tr><td class='sub'>");
1016 html_txt(ctx
.repo
->desc
);
1017 html("</td><td class='sub right'>");
1018 html_txt(ctx
.repo
->owner
);
1020 if (ctx
.cfg
.root_desc
)
1021 html_txt(ctx
.cfg
.root_desc
);
1023 html("</td></tr></table>\n");
1026 void cgit_print_pageheader(void)
1028 html("<div id='cgit'>");
1029 if (!ctx
.env
.authenticated
|| !ctx
.cfg
.noheader
)
1032 html("<table class='tabs'><tr><td>\n");
1033 if (ctx
.env
.authenticated
&& ctx
.repo
) {
1034 if (ctx
.repo
->readme
.nr
)
1035 reporevlink("about", "about", NULL
,
1036 hc("about"), ctx
.qry
.head
, NULL
,
1038 cgit_summary_link("summary", NULL
, hc("summary"),
1040 cgit_refs_link("refs", NULL
, hc("refs"), ctx
.qry
.head
,
1042 cgit_log_link("log", NULL
, hc("log"), ctx
.qry
.head
,
1043 NULL
, ctx
.qry
.vpath
, 0, NULL
, NULL
,
1044 ctx
.qry
.showmsg
, ctx
.qry
.follow
);
1045 if (ctx
.qry
.page
&& !strcmp(ctx
.qry
.page
, "blame"))
1046 cgit_blame_link("blame", NULL
, hc("blame"), ctx
.qry
.head
,
1047 ctx
.qry
.oid
, ctx
.qry
.vpath
);
1049 cgit_tree_link("tree", NULL
, hc("tree"), ctx
.qry
.head
,
1050 ctx
.qry
.oid
, ctx
.qry
.vpath
);
1051 cgit_commit_link("commit", NULL
, hc("commit"),
1052 ctx
.qry
.head
, ctx
.qry
.oid
, ctx
.qry
.vpath
);
1053 cgit_diff_link("diff", NULL
, hc("diff"), ctx
.qry
.head
,
1054 ctx
.qry
.oid
, ctx
.qry
.oid2
, ctx
.qry
.vpath
);
1055 if (ctx
.repo
->max_stats
)
1056 cgit_stats_link("stats", NULL
, hc("stats"),
1057 ctx
.qry
.head
, ctx
.qry
.vpath
);
1058 if (ctx
.repo
->homepage
) {
1060 html_attr(ctx
.repo
->homepage
);
1061 html("'>homepage</a>");
1063 html("</td><td class='form'>");
1064 html("<form class='right' method='get' action='");
1065 if (ctx
.cfg
.virtual_root
) {
1066 char *fileurl
= cgit_fileurl(ctx
.qry
.repo
, "log",
1067 ctx
.qry
.vpath
, NULL
);
1068 html_url_path(fileurl
);
1072 cgit_add_hidden_formfields(1, 0, "log");
1073 html("<select name='qt'>\n");
1074 html_option("grep", "log msg", ctx
.qry
.grep
);
1075 html_option("author", "author", ctx
.qry
.grep
);
1076 html_option("committer", "committer", ctx
.qry
.grep
);
1077 html_option("range", "range", ctx
.qry
.grep
);
1078 html("</select>\n");
1079 html("<input class='txt' type='search' size='10' name='q' value='");
1080 html_attr(ctx
.qry
.search
);
1082 html("<input type='submit' value='search'/>\n");
1084 } else if (ctx
.env
.authenticated
) {
1085 char *currenturl
= cgit_currenturl();
1086 site_link(NULL
, "index", NULL
, hc("repolist"), NULL
, NULL
, 0, 1);
1087 if (ctx
.cfg
.root_readme
)
1088 site_link("about", "about", NULL
, hc("about"),
1090 html("</td><td class='form'>");
1091 html("<form method='get' action='");
1092 html_attr(currenturl
);
1094 html("<input type='search' name='q' size='10' value='");
1095 html_attr(ctx
.qry
.search
);
1097 html("<input type='submit' value='search'/>\n");
1101 html("</td></tr></table>\n");
1102 if (ctx
.env
.authenticated
&& ctx
.repo
&& ctx
.qry
.vpath
) {
1103 html("<div class='path'>");
1105 cgit_print_path_crumbs(ctx
.qry
.vpath
);
1106 if (ctx
.cfg
.enable_follow_links
&& !strcmp(ctx
.qry
.page
, "log")) {
1108 ctx
.qry
.follow
= !ctx
.qry
.follow
;
1109 cgit_self_link(ctx
.qry
.follow
? "follow" : "unfollow",
1111 ctx
.qry
.follow
= !ctx
.qry
.follow
;
1116 html("<div class='content'>");
1119 void cgit_print_filemode(unsigned short mode
)
1123 else if (S_ISLNK(mode
))
1125 else if (S_ISGITLINK(mode
))
1129 html_fileperm(mode
>> 6);
1130 html_fileperm(mode
>> 3);
1131 html_fileperm(mode
);
1134 void cgit_compose_snapshot_prefix(struct strbuf
*filename
, const char *base
,
1137 struct object_id oid
;
1140 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1141 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1142 * i.e. each stripped tag can be inverted without ambiguities.
1144 if (get_oid(fmt("refs/tags/%s", ref
), &oid
) == 0 &&
1145 (ref
[0] == 'v' || ref
[0] == 'V') && isdigit(ref
[1]) &&
1146 ((get_oid(fmt("refs/tags/%s", ref
+ 1), &oid
) == 0) +
1147 (get_oid(fmt("refs/tags/v%s", ref
+ 1), &oid
) == 0) +
1148 (get_oid(fmt("refs/tags/V%s", ref
+ 1), &oid
) == 0) == 1))
1151 strbuf_addf(filename
, "%s-%s", base
, ref
);
1154 void cgit_print_snapshot_links(const struct cgit_repo
*repo
, const char *ref
,
1155 const char *separator
)
1157 const struct cgit_snapshot_format
*f
;
1158 struct strbuf filename
= STRBUF_INIT
;
1159 const char *basename
;
1162 basename
= cgit_snapshot_prefix(repo
);
1163 if (starts_with(ref
, basename
))
1164 strbuf_addstr(&filename
, ref
);
1166 cgit_compose_snapshot_prefix(&filename
, basename
, ref
);
1168 prefixlen
= filename
.len
;
1169 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
1170 if (!(repo
->snapshots
& cgit_snapshot_format_bit(f
)))
1172 strbuf_setlen(&filename
, prefixlen
);
1173 strbuf_addstr(&filename
, f
->suffix
);
1174 cgit_snapshot_link(filename
.buf
, NULL
, NULL
, NULL
, NULL
,
1176 if (cgit_snapshot_get_sig(ref
, f
)) {
1177 strbuf_addstr(&filename
, ".asc");
1179 cgit_snapshot_link("sig", NULL
, NULL
, NULL
, NULL
,
1182 } else if (starts_with(f
->suffix
, ".tar") && cgit_snapshot_get_sig(ref
, &cgit_snapshot_formats
[0])) {
1183 strbuf_setlen(&filename
, strlen(filename
.buf
) - strlen(f
->suffix
));
1184 strbuf_addstr(&filename
, ".tar.asc");
1186 cgit_snapshot_link("sig", NULL
, NULL
, NULL
, NULL
,
1192 strbuf_release(&filename
);
1195 void cgit_set_title_from_path(const char *path
)
1197 struct strbuf sb
= STRBUF_INIT
;
1198 const char *slash
, *last_slash
;
1203 for (last_slash
= path
+ strlen(path
); (slash
= memrchr(path
, '/', last_slash
- path
)) != NULL
; last_slash
= slash
) {
1204 strbuf_add(&sb
, slash
+ 1, last_slash
- slash
- 1);
1205 strbuf_addstr(&sb
, " \xc2\xab ");
1207 strbuf_add(&sb
, path
, last_slash
- path
);
1208 strbuf_addf(&sb
, " - %s", ctx
.page
.title
);
1209 ctx
.page
.title
= strbuf_detach(&sb
, NULL
);