]>
git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
1 /* ui-shared.c: common web output functions
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
13 const char cgit_doctype
[] =
14 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
17 static char *http_date(time_t t
)
19 static char day
[][4] =
20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
21 static char month
[][4] =
22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
24 struct tm
*tm
= gmtime(&t
);
25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day
[tm
->tm_wday
],
26 tm
->tm_mday
, month
[tm
->tm_mon
], 1900+tm
->tm_year
,
27 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
30 void cgit_print_error(char *msg
)
32 html("<div class='error'>");
37 char *cgit_httpscheme()
41 https
= getenv("HTTPS");
42 if (https
!= NULL
&& strcmp(https
, "on") == 0)
52 host
= getenv("HTTP_HOST");
56 host
= getenv("SERVER_NAME");
59 port
= getenv("SERVER_PORT");
60 if (port
&& atoi(port
) != 80)
61 host
= xstrdup(fmt("%s:%d", host
, atoi(port
)));
70 if (ctx
.cfg
.virtual_root
)
71 return fmt("%s/", ctx
.cfg
.virtual_root
);
73 return ctx
.cfg
.script_name
;
76 char *cgit_repourl(const char *reponame
)
78 if (ctx
.cfg
.virtual_root
) {
79 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, reponame
);
81 return fmt("?r=%s", reponame
);
85 char *cgit_fileurl(const char *reponame
, const char *pagename
,
86 const char *filename
, const char *query
)
91 if (ctx
.cfg
.virtual_root
) {
92 tmp
= fmt("%s/%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
93 pagename
, (filename
? filename
:""));
96 tmp
= fmt("?url=%s/%s/%s", reponame
, pagename
,
97 (filename
? filename
: ""));
101 tmp
= fmt("%s%s%s", tmp
, delim
, query
);
105 char *cgit_pageurl(const char *reponame
, const char *pagename
,
108 return cgit_fileurl(reponame
,pagename
,0,query
);
111 const char *cgit_repobasename(const char *reponame
)
113 /* I assume we don't need to store more than one repo basename */
114 static char rvbuf
[1024];
117 strncpy(rvbuf
,reponame
,sizeof(rvbuf
));
118 if(rvbuf
[sizeof(rvbuf
)-1])
119 die("cgit_repobasename: truncated repository name '%s'", reponame
);
121 /* strip trailing slashes */
122 while(p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
123 /* strip trailing .git */
124 if(p
>=3 && !strncmp(&rvbuf
[p
-3],".git",4)) {
125 p
-= 3; rvbuf
[p
--] = 0;
127 /* strip more trailing slashes if any */
128 while( p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
129 /* find last slash in the remaining string */
130 rv
= strrchr(rvbuf
,'/');
138 if (!ctx
.cfg
.virtual_root
)
139 return ctx
.cfg
.script_name
;
140 else if (ctx
.qry
.page
)
141 return fmt("%s/%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
, ctx
.qry
.page
);
142 else if (ctx
.qry
.repo
)
143 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
);
145 return fmt("%s/", ctx
.cfg
.virtual_root
);
148 static void site_url(char *page
, char *search
, int ofs
)
152 if (ctx
.cfg
.virtual_root
) {
153 html_attr(ctx
.cfg
.virtual_root
);
154 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
157 html(ctx
.cfg
.script_name
);
160 htmlf("?p=%s", page
);
171 htmlf("ofs=%d", ofs
);
175 static void site_link(char *page
, char *name
, char *title
, char *class,
176 char *search
, int ofs
)
190 site_url(page
, search
, ofs
);
196 void cgit_index_link(char *name
, char *title
, char *class, char *pattern
,
199 site_link(NULL
, name
, title
, class, pattern
, ofs
);
202 static char *repolink(char *title
, char *class, char *page
, char *head
,
219 if (ctx
.cfg
.virtual_root
) {
220 html_url_path(ctx
.cfg
.virtual_root
);
221 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
223 html_url_path(ctx
.repo
->url
);
224 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
233 html(ctx
.cfg
.script_name
);
235 html_url_arg(ctx
.repo
->url
);
236 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
246 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
252 return fmt("%s", delim
);
255 static void reporevlink(char *page
, char *name
, char *title
, char *class,
256 char *head
, char *rev
, char *path
)
260 delim
= repolink(title
, class, page
, head
, path
);
261 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
271 void cgit_summary_link(char *name
, char *title
, char *class, char *head
)
273 reporevlink(NULL
, name
, title
, class, head
, NULL
, NULL
);
276 void cgit_tag_link(char *name
, char *title
, char *class, char *head
,
279 reporevlink("tag", name
, title
, class, head
, rev
, NULL
);
282 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
283 char *rev
, char *path
)
285 reporevlink("tree", name
, title
, class, head
, rev
, path
);
288 void cgit_plain_link(char *name
, char *title
, char *class, char *head
,
289 char *rev
, char *path
)
291 reporevlink("plain", name
, title
, class, head
, rev
, path
);
294 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
295 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
,
300 delim
= repolink(title
, class, "log", head
, path
);
301 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
307 if (grep
&& pattern
) {
314 html_url_arg(pattern
);
331 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
334 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
335 name
[ctx
.cfg
.max_msg_len
] = '\0';
336 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
337 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
338 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
340 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
343 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
344 char *rev
, char *path
)
346 reporevlink("refs", name
, title
, class, head
, rev
, path
);
349 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
350 char *rev
, char *archivename
)
352 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
355 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
356 char *new_rev
, char *old_rev
, char *path
)
360 delim
= repolink(title
, class, "diff", head
, path
);
361 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
364 html_url_arg(new_rev
);
370 html_url_arg(old_rev
);
377 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
380 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
383 void cgit_stats_link(char *name
, char *title
, char *class, char *head
,
386 reporevlink("stats", name
, title
, class, head
, NULL
, path
);
389 void cgit_object_link(struct object
*obj
)
391 char *page
, *shortrev
, *fullrev
, *name
;
393 fullrev
= sha1_to_hex(obj
->sha1
);
394 shortrev
= xstrdup(fullrev
);
396 if (obj
->type
== OBJ_COMMIT
) {
397 cgit_commit_link(fmt("commit %s...", shortrev
), NULL
, NULL
,
398 ctx
.qry
.head
, fullrev
);
400 } else if (obj
->type
== OBJ_TREE
)
402 else if (obj
->type
== OBJ_TAG
)
406 name
= fmt("%s %s...", typename(obj
->type
), shortrev
);
407 reporevlink(page
, name
, NULL
, NULL
, ctx
.qry
.head
, fullrev
, NULL
);
410 void cgit_print_date(time_t secs
, char *format
, int local_time
)
418 time
= localtime(&secs
);
420 time
= gmtime(&secs
);
421 strftime(buf
, sizeof(buf
)-1, format
, time
);
425 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
434 if (secs
> max_relative
&& max_relative
>= 0) {
435 cgit_print_date(t
, format
, ctx
.cfg
.local_time
);
439 if (secs
< TM_HOUR
* 2) {
440 htmlf("<span class='age-mins'>%.0f min.</span>",
441 secs
* 1.0 / TM_MIN
);
444 if (secs
< TM_DAY
* 2) {
445 htmlf("<span class='age-hours'>%.0f hours</span>",
446 secs
* 1.0 / TM_HOUR
);
449 if (secs
< TM_WEEK
* 2) {
450 htmlf("<span class='age-days'>%.0f days</span>",
451 secs
* 1.0 / TM_DAY
);
454 if (secs
< TM_MONTH
* 2) {
455 htmlf("<span class='age-weeks'>%.0f weeks</span>",
456 secs
* 1.0 / TM_WEEK
);
459 if (secs
< TM_YEAR
* 2) {
460 htmlf("<span class='age-months'>%.0f months</span>",
461 secs
* 1.0 / TM_MONTH
);
464 htmlf("<span class='age-years'>%.0f years</span>",
465 secs
* 1.0 / TM_YEAR
);
468 void cgit_print_http_headers(struct cgit_context
*ctx
)
470 const char *method
= getenv("REQUEST_METHOD");
472 if (ctx
->cfg
.embedded
)
475 if (ctx
->page
.status
)
476 htmlf("Status: %d %s\n", ctx
->page
.status
, ctx
->page
.statusmsg
);
477 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
478 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
480 else if (ctx
->page
.mimetype
)
481 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
483 htmlf("Content-Length: %ld\n", ctx
->page
.size
);
484 if (ctx
->page
.filename
)
485 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
487 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
488 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
490 htmlf("ETag: \"%s\"\n", ctx
->page
.etag
);
492 if (method
&& !strcmp(method
, "HEAD"))
496 void cgit_print_docstart(struct cgit_context
*ctx
)
498 if (ctx
->cfg
.embedded
)
501 char *host
= cgit_hosturl();
503 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
506 html_txt(ctx
->page
.title
);
508 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
509 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
510 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
511 html("<link rel='stylesheet' type='text/css' href='");
512 html_attr(ctx
->cfg
.css
);
514 if (ctx
->cfg
.favicon
) {
515 html("<link rel='shortcut icon' href='");
516 html_attr(ctx
->cfg
.favicon
);
519 if (host
&& ctx
->repo
) {
520 html("<link rel='alternate' title='Atom feed' href='");
521 html(cgit_httpscheme());
522 html_attr(cgit_hosturl());
523 html_attr(cgit_fileurl(ctx
->repo
->url
, "atom", ctx
->qry
.path
,
524 fmt("h=%s", ctx
->qry
.head
)));
525 html("' type='application/atom+xml'/>\n");
527 if (ctx
->cfg
.head_include
)
528 html_include(ctx
->cfg
.head_include
);
532 html_include(ctx
->cfg
.header
);
535 void cgit_print_docend()
539 html_include(ctx
.cfg
.footer
);
541 htmlf("<div class='footer'>generated by cgit %s at ",
543 cgit_print_date(time(NULL
), FMT_LONGDATE
, ctx
.cfg
.local_time
);
547 if (ctx
.cfg
.embedded
)
549 html("</body>\n</html>\n");
552 int print_branch_option(const char *refname
, const unsigned char *sha1
,
553 int flags
, void *cb_data
)
555 char *name
= (char *)refname
;
556 html_option(name
, name
, ctx
.qry
.head
);
560 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
561 int flags
, void *cb_data
)
564 struct taginfo
*info
;
567 unsigned char fileid
[20];
568 int *header
= (int *)cb_data
;
570 if (prefixcmp(refname
, "refs/archives"))
572 strncpy(buf
, refname
+14, sizeof(buf
));
573 obj
= parse_object(sha1
);
576 if (obj
->type
== OBJ_TAG
) {
577 tag
= lookup_tag(sha1
);
578 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
580 hashcpy(fileid
, tag
->tagged
->sha1
);
581 } else if (obj
->type
!= OBJ_BLOB
) {
584 hashcpy(fileid
, sha1
);
587 html("<h1>download</h1>\n");
590 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
591 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
593 html_link_open(url
, NULL
, "menu");
594 html_txt(strlpart(buf
, 20));
599 void cgit_add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
603 if (!ctx
.cfg
.virtual_root
) {
604 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
606 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
607 html_hidden("url", url
);
610 if (incl_head
&& ctx
.qry
.head
&& ctx
.repo
->defbranch
&&
611 strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
612 html_hidden("h", ctx
.qry
.head
);
615 html_hidden("id", ctx
.qry
.sha1
);
617 html_hidden("id2", ctx
.qry
.sha2
);
619 html_hidden("showmsg", "1");
623 html_hidden("qt", ctx
.qry
.grep
);
625 html_hidden("q", ctx
.qry
.search
);
629 const char *fallback_cmd
= "repolist";
631 char *hc(struct cgit_cmd
*cmd
, const char *page
)
633 return (strcmp(cmd
? cmd
->name
: fallback_cmd
, page
) ? NULL
: "active");
636 static void print_header(struct cgit_context
*ctx
)
638 html("<table id='header'>\n");
640 html("<td class='logo' rowspan='2'><a href='");
641 if (ctx
->cfg
.logo_link
)
642 html_attr(ctx
->cfg
.logo_link
);
644 html_attr(cgit_rooturl());
645 html("'><img src='");
646 html_attr(ctx
->cfg
.logo
);
647 html("' alt='cgit logo'/></a></td>\n");
649 html("<td class='main'>");
651 cgit_index_link("index", NULL
, NULL
, NULL
, 0);
653 cgit_summary_link(ctx
->repo
->name
, ctx
->repo
->name
, NULL
, NULL
);
654 html("</td><td class='form'>");
655 html("<form method='get' action=''>\n");
656 cgit_add_hidden_formfields(0, 1, ctx
->qry
.page
);
657 html("<select name='h' onchange='this.form.submit();'>\n");
658 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
660 html("<input type='submit' name='' value='switch'/>");
663 html_txt(ctx
->cfg
.root_title
);
664 html("</td></tr>\n");
666 html("<tr><td class='sub'>");
668 html_txt(ctx
->repo
->desc
);
669 html("</td><td class='sub right'>");
670 html_txt(ctx
->repo
->owner
);
672 if (ctx
->cfg
.root_desc
)
673 html_txt(ctx
->cfg
.root_desc
);
674 else if (ctx
->cfg
.index_info
)
675 html_include(ctx
->cfg
.index_info
);
677 html("</td></tr></table>\n");
680 void cgit_print_pageheader(struct cgit_context
*ctx
)
682 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
684 if (!cmd
&& ctx
->repo
)
685 fallback_cmd
= "summary";
687 html("<div id='cgit'>");
688 if (!ctx
->cfg
.noheader
)
691 html("<table class='tabs'><tr><td>\n");
693 cgit_summary_link("summary", NULL
, hc(cmd
, "summary"),
695 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
696 ctx
->qry
.sha1
, NULL
);
697 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
698 NULL
, NULL
, 0, NULL
, NULL
, ctx
->qry
.showmsg
);
699 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
700 ctx
->qry
.sha1
, NULL
);
701 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
702 ctx
->qry
.head
, ctx
->qry
.sha1
);
703 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
704 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
705 if (ctx
->repo
->max_stats
)
706 cgit_stats_link("stats", NULL
, hc(cmd
, "stats"),
707 ctx
->qry
.head
, NULL
);
708 if (ctx
->repo
->readme
)
709 reporevlink("about", "about", NULL
,
710 hc(cmd
, "about"), ctx
->qry
.head
, NULL
,
712 html("</td><td class='form'>");
713 html("<form class='right' method='get' action='");
714 if (ctx
->cfg
.virtual_root
)
715 html_url_path(cgit_fileurl(ctx
->qry
.repo
, "log",
716 ctx
->qry
.path
, NULL
));
718 cgit_add_hidden_formfields(1, 0, "log");
719 html("<select name='qt'>\n");
720 html_option("grep", "log msg", ctx
->qry
.grep
);
721 html_option("author", "author", ctx
->qry
.grep
);
722 html_option("committer", "committer", ctx
->qry
.grep
);
724 html("<input class='txt' type='text' size='10' name='q' value='");
725 html_attr(ctx
->qry
.search
);
727 html("<input type='submit' value='search'/>\n");
730 site_link(NULL
, "index", NULL
, hc(cmd
, "repolist"), NULL
, 0);
731 if (ctx
->cfg
.root_readme
)
732 site_link("about", "about", NULL
, hc(cmd
, "about"),
734 html("</td><td class='form'>");
735 html("<form method='get' action='");
736 html_attr(cgit_rooturl());
738 html("<input type='text' name='q' size='10' value='");
739 html_attr(ctx
->qry
.search
);
741 html("<input type='submit' value='search'/>\n");
744 html("</td></tr></table>\n");
745 html("<div class='content'>");
748 void cgit_print_filemode(unsigned short mode
)
752 else if (S_ISLNK(mode
))
754 else if (S_ISGITLINK(mode
))
758 html_fileperm(mode
>> 6);
759 html_fileperm(mode
>> 3);
763 void cgit_print_snapshot_links(const char *repo
, const char *head
,
764 const char *hex
, int snapshots
)
766 const struct cgit_snapshot_format
* f
;
769 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
770 if (!(snapshots
& f
->bit
))
772 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
774 cgit_snapshot_link(filename
, NULL
, NULL
, NULL
, NULL
, filename
);