]>
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'>");
41 host
= getenv("HTTP_HOST");
45 host
= getenv("SERVER_NAME");
48 port
= getenv("SERVER_PORT");
49 if (port
&& atoi(port
) != 80)
50 host
= xstrdup(fmt("%s:%d", host
, atoi(port
)));
59 if (ctx
.cfg
.virtual_root
)
60 return fmt("%s/", ctx
.cfg
.virtual_root
);
62 return ctx
.cfg
.script_name
;
65 char *cgit_repourl(const char *reponame
)
67 if (ctx
.cfg
.virtual_root
) {
68 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, reponame
);
70 return fmt("?r=%s", reponame
);
74 char *cgit_fileurl(const char *reponame
, const char *pagename
,
75 const char *filename
, const char *query
)
80 if (ctx
.cfg
.virtual_root
) {
81 tmp
= fmt("%s/%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
82 pagename
, (filename
? filename
:""));
85 tmp
= fmt("?url=%s/%s/%s", reponame
, pagename
,
86 (filename
? filename
: ""));
90 tmp
= fmt("%s%s%s", tmp
, delim
, query
);
94 char *cgit_pageurl(const char *reponame
, const char *pagename
,
97 return cgit_fileurl(reponame
,pagename
,0,query
);
100 const char *cgit_repobasename(const char *reponame
)
102 /* I assume we don't need to store more than one repo basename */
103 static char rvbuf
[1024];
106 strncpy(rvbuf
,reponame
,sizeof(rvbuf
));
107 if(rvbuf
[sizeof(rvbuf
)-1])
108 die("cgit_repobasename: truncated repository name '%s'", reponame
);
110 /* strip trailing slashes */
111 while(p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
112 /* strip trailing .git */
113 if(p
>=3 && !strncmp(&rvbuf
[p
-3],".git",4)) {
114 p
-= 3; rvbuf
[p
--] = 0;
116 /* strip more trailing slashes if any */
117 while( p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
118 /* find last slash in the remaining string */
119 rv
= strrchr(rvbuf
,'/');
127 if (!ctx
.cfg
.virtual_root
)
128 return ctx
.cfg
.script_name
;
129 else if (ctx
.qry
.page
)
130 return fmt("%s/%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
, ctx
.qry
.page
);
131 else if (ctx
.qry
.repo
)
132 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
);
134 return fmt("%s/", ctx
.cfg
.virtual_root
);
137 static void site_url(char *page
, char *search
, int ofs
)
141 if (ctx
.cfg
.virtual_root
) {
142 html_attr(ctx
.cfg
.virtual_root
);
143 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
146 html(ctx
.cfg
.script_name
);
149 htmlf("?p=%s", page
);
160 htmlf("ofs=%d", ofs
);
164 static void site_link(char *page
, char *name
, char *title
, char *class,
165 char *search
, int ofs
)
179 site_url(page
, search
, ofs
);
185 void cgit_index_link(char *name
, char *title
, char *class, char *pattern
,
188 site_link(NULL
, name
, title
, class, pattern
, ofs
);
191 static char *repolink(char *title
, char *class, char *page
, char *head
,
208 if (ctx
.cfg
.virtual_root
) {
209 html_url_path(ctx
.cfg
.virtual_root
);
210 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
212 html_url_path(ctx
.repo
->url
);
213 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
222 html(ctx
.cfg
.script_name
);
224 html_url_arg(ctx
.repo
->url
);
225 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
235 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
241 return fmt("%s", delim
);
244 static void reporevlink(char *page
, char *name
, char *title
, char *class,
245 char *head
, char *rev
, char *path
)
249 delim
= repolink(title
, class, page
, head
, path
);
250 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
260 void cgit_summary_link(char *name
, char *title
, char *class, char *head
)
262 reporevlink(NULL
, name
, title
, class, head
, NULL
, NULL
);
265 void cgit_tag_link(char *name
, char *title
, char *class, char *head
,
268 reporevlink("tag", name
, title
, class, head
, rev
, NULL
);
271 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
272 char *rev
, char *path
)
274 reporevlink("tree", name
, title
, class, head
, rev
, path
);
277 void cgit_plain_link(char *name
, char *title
, char *class, char *head
,
278 char *rev
, char *path
)
280 reporevlink("plain", name
, title
, class, head
, rev
, path
);
283 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
284 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
,
289 delim
= repolink(title
, class, "log", head
, path
);
290 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
296 if (grep
&& pattern
) {
303 html_url_arg(pattern
);
320 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
323 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
324 name
[ctx
.cfg
.max_msg_len
] = '\0';
325 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
326 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
327 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
329 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
332 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
333 char *rev
, char *path
)
335 reporevlink("refs", name
, title
, class, head
, rev
, path
);
338 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
339 char *rev
, char *archivename
)
341 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
344 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
345 char *new_rev
, char *old_rev
, char *path
)
349 delim
= repolink(title
, class, "diff", head
, path
);
350 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
353 html_url_arg(new_rev
);
359 html_url_arg(old_rev
);
366 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
369 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
372 void cgit_object_link(struct object
*obj
)
374 char *page
, *shortrev
, *fullrev
, *name
;
376 fullrev
= sha1_to_hex(obj
->sha1
);
377 shortrev
= xstrdup(fullrev
);
379 if (obj
->type
== OBJ_COMMIT
) {
380 cgit_commit_link(fmt("commit %s...", shortrev
), NULL
, NULL
,
381 ctx
.qry
.head
, fullrev
);
383 } else if (obj
->type
== OBJ_TREE
)
385 else if (obj
->type
== OBJ_TAG
)
389 name
= fmt("%s %s...", typename(obj
->type
), shortrev
);
390 reporevlink(page
, name
, NULL
, NULL
, ctx
.qry
.head
, fullrev
, NULL
);
393 void cgit_print_date(time_t secs
, char *format
, int local_time
)
401 time
= localtime(&secs
);
403 time
= gmtime(&secs
);
404 strftime(buf
, sizeof(buf
)-1, format
, time
);
408 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
417 if (secs
> max_relative
&& max_relative
>= 0) {
418 cgit_print_date(t
, format
, ctx
.cfg
.local_time
);
422 if (secs
< TM_HOUR
* 2) {
423 htmlf("<span class='age-mins'>%.0f min.</span>",
424 secs
* 1.0 / TM_MIN
);
427 if (secs
< TM_DAY
* 2) {
428 htmlf("<span class='age-hours'>%.0f hours</span>",
429 secs
* 1.0 / TM_HOUR
);
432 if (secs
< TM_WEEK
* 2) {
433 htmlf("<span class='age-days'>%.0f days</span>",
434 secs
* 1.0 / TM_DAY
);
437 if (secs
< TM_MONTH
* 2) {
438 htmlf("<span class='age-weeks'>%.0f weeks</span>",
439 secs
* 1.0 / TM_WEEK
);
442 if (secs
< TM_YEAR
* 2) {
443 htmlf("<span class='age-months'>%.0f months</span>",
444 secs
* 1.0 / TM_MONTH
);
447 htmlf("<span class='age-years'>%.0f years</span>",
448 secs
* 1.0 / TM_YEAR
);
451 void cgit_print_http_headers(struct cgit_context
*ctx
)
453 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
454 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
456 else if (ctx
->page
.mimetype
)
457 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
459 htmlf("Content-Length: %ld\n", ctx
->page
.size
);
460 if (ctx
->page
.filename
)
461 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
463 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
464 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
468 void cgit_print_docstart(struct cgit_context
*ctx
)
470 char *host
= cgit_hosturl();
472 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
475 html_txt(ctx
->page
.title
);
477 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
478 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
479 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
480 html("<link rel='stylesheet' type='text/css' href='");
481 html_attr(ctx
->cfg
.css
);
483 if (ctx
->cfg
.favicon
) {
484 html("<link rel='shortcut icon' href='");
485 html_attr(ctx
->cfg
.favicon
);
488 if (host
&& ctx
->repo
) {
489 html("<link rel='alternate' title='Atom feed' href='http://");
490 html_attr(cgit_hosturl());
491 html_attr(cgit_fileurl(ctx
->repo
->url
, "atom", ctx
->qry
.path
,
492 fmt("h=%s", ctx
->qry
.head
)));
493 html("' type='application/atom+xml'/>");
499 void cgit_print_docend()
503 html_include(ctx
.cfg
.footer
);
505 htmlf("<div class='footer'>generated by cgit %s at ",
507 cgit_print_date(time(NULL
), FMT_LONGDATE
, ctx
.cfg
.local_time
);
510 html("</body>\n</html>\n");
513 int print_branch_option(const char *refname
, const unsigned char *sha1
,
514 int flags
, void *cb_data
)
516 char *name
= (char *)refname
;
517 html_option(name
, name
, ctx
.qry
.head
);
521 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
522 int flags
, void *cb_data
)
525 struct taginfo
*info
;
528 unsigned char fileid
[20];
529 int *header
= (int *)cb_data
;
531 if (prefixcmp(refname
, "refs/archives"))
533 strncpy(buf
, refname
+14, sizeof(buf
));
534 obj
= parse_object(sha1
);
537 if (obj
->type
== OBJ_TAG
) {
538 tag
= lookup_tag(sha1
);
539 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
541 hashcpy(fileid
, tag
->tagged
->sha1
);
542 } else if (obj
->type
!= OBJ_BLOB
) {
545 hashcpy(fileid
, sha1
);
548 html("<h1>download</h1>\n");
551 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
552 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
554 html_link_open(url
, NULL
, "menu");
555 html_txt(strlpart(buf
, 20));
560 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
564 if (!ctx
.cfg
.virtual_root
) {
565 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
567 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
568 html_hidden("url", url
);
571 if (incl_head
&& ctx
.qry
.head
&& ctx
.repo
->defbranch
&&
572 strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
573 html_hidden("h", ctx
.qry
.head
);
576 html_hidden("id", ctx
.qry
.sha1
);
578 html_hidden("id2", ctx
.qry
.sha2
);
580 html_hidden("showmsg", "1");
584 html_hidden("qt", ctx
.qry
.grep
);
586 html_hidden("q", ctx
.qry
.search
);
590 char *hc(struct cgit_cmd
*cmd
, const char *page
)
592 return (strcmp(cmd
->name
, page
) ? NULL
: "active");
595 void cgit_print_pageheader(struct cgit_context
*ctx
)
597 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
599 html("<table id='header'>\n");
601 html("<td class='logo' rowspan='2'><a href='");
602 if (ctx
->cfg
.logo_link
)
603 html_attr(ctx
->cfg
.logo_link
);
605 html_attr(cgit_rooturl());
606 html("'><img src='");
607 html_attr(ctx
->cfg
.logo
);
608 html("' alt='cgit logo'/></a></td>\n");
610 html("<td class='main'>");
612 cgit_index_link("index", NULL
, NULL
, NULL
, 0);
614 cgit_summary_link(ctx
->repo
->name
, ctx
->repo
->name
, NULL
, NULL
);
615 html("</td><td class='form'>");
616 html("<form method='get' action=''>\n");
617 add_hidden_formfields(0, 1, ctx
->qry
.page
);
618 html("<select name='h' onchange='this.form.submit();'>\n");
619 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
621 html("<input type='submit' name='' value='switch'/>");
624 html_txt(ctx
->cfg
.root_title
);
625 html("</td></tr>\n");
627 html("<tr><td class='sub'>");
629 html_txt(ctx
->repo
->desc
);
630 html("</td><td class='sub right'>");
631 html_txt(ctx
->repo
->owner
);
633 if (ctx
->cfg
.root_desc
)
634 html_txt(ctx
->cfg
.root_desc
);
635 else if (ctx
->cfg
.index_info
)
636 html_include(ctx
->cfg
.index_info
);
638 html("</td></tr></table>\n");
640 html("<table class='tabs'><tr><td>\n");
642 cgit_summary_link("summary", NULL
, hc(cmd
, "summary"),
644 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
645 ctx
->qry
.sha1
, NULL
);
646 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
647 NULL
, NULL
, 0, NULL
, NULL
, ctx
->qry
.showmsg
);
648 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
649 ctx
->qry
.sha1
, NULL
);
650 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
651 ctx
->qry
.head
, ctx
->qry
.sha1
);
652 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
653 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
654 if (ctx
->repo
->readme
)
655 reporevlink("about", "about", NULL
,
656 hc(cmd
, "about"), ctx
->qry
.head
, NULL
,
658 html("</td><td class='form'>");
659 html("<form class='right' method='get' action='");
660 if (ctx
->cfg
.virtual_root
)
661 html_url_path(cgit_fileurl(ctx
->qry
.repo
, "log",
662 ctx
->qry
.path
, NULL
));
664 add_hidden_formfields(1, 0, "log");
665 html("<select name='qt'>\n");
666 html_option("grep", "log msg", ctx
->qry
.grep
);
667 html_option("author", "author", ctx
->qry
.grep
);
668 html_option("committer", "committer", ctx
->qry
.grep
);
670 html("<input class='txt' type='text' size='10' name='q' value='");
671 html_attr(ctx
->qry
.search
);
673 html("<input type='submit' value='search'/>\n");
676 site_link(NULL
, "index", NULL
, hc(cmd
, "repolist"), NULL
, 0);
677 if (ctx
->cfg
.root_readme
)
678 site_link("about", "about", NULL
, hc(cmd
, "about"),
680 html("</td><td class='form'>");
681 html("<form method='get' action='");
682 html_attr(cgit_rooturl());
684 html("<input type='text' name='q' size='10' value='");
685 html_attr(ctx
->qry
.search
);
687 html("<input type='submit' value='search'/>\n");
690 html("</td></tr></table>\n");
691 html("<div class='content'>");
694 void cgit_print_filemode(unsigned short mode
)
698 else if (S_ISLNK(mode
))
700 else if (S_ISGITLINK(mode
))
704 html_fileperm(mode
>> 6);
705 html_fileperm(mode
>> 3);
709 void cgit_print_snapshot_links(const char *repo
, const char *head
,
710 const char *hex
, int snapshots
)
712 const struct cgit_snapshot_format
* f
;
715 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
716 if (!(snapshots
& f
->bit
))
718 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
720 cgit_snapshot_link(filename
, NULL
, NULL
, NULL
, NULL
, filename
);