]>
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("SERVER_NAME");
44 port
= getenv("SERVER_PORT");
45 if (port
&& atoi(port
) != 80)
46 host
= xstrdup(fmt("%s:%d", host
, atoi(port
)));
54 if (ctx
.cfg
.virtual_root
)
55 return fmt("%s/", ctx
.cfg
.virtual_root
);
57 return ctx
.cfg
.script_name
;
60 char *cgit_repourl(const char *reponame
)
62 if (ctx
.cfg
.virtual_root
) {
63 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, reponame
);
65 return fmt("?r=%s", reponame
);
69 char *cgit_fileurl(const char *reponame
, const char *pagename
,
70 const char *filename
, const char *query
)
75 if (ctx
.cfg
.virtual_root
) {
76 tmp
= fmt("%s/%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
77 pagename
, (filename
? filename
:""));
80 tmp
= fmt("?url=%s/%s/%s", reponame
, pagename
,
81 (filename
? filename
: ""));
85 tmp
= fmt("%s%s%s", tmp
, delim
, query
);
89 char *cgit_pageurl(const char *reponame
, const char *pagename
,
92 return cgit_fileurl(reponame
,pagename
,0,query
);
95 const char *cgit_repobasename(const char *reponame
)
97 /* I assume we don't need to store more than one repo basename */
98 static char rvbuf
[1024];
101 strncpy(rvbuf
,reponame
,sizeof(rvbuf
));
102 if(rvbuf
[sizeof(rvbuf
)-1])
103 die("cgit_repobasename: truncated repository name '%s'", reponame
);
105 /* strip trailing slashes */
106 while(p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
107 /* strip trailing .git */
108 if(p
>=3 && !strncmp(&rvbuf
[p
-3],".git",4)) {
109 p
-= 3; rvbuf
[p
--] = 0;
111 /* strip more trailing slashes if any */
112 while( p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
113 /* find last slash in the remaining string */
114 rv
= strrchr(rvbuf
,'/');
122 if (!ctx
.cfg
.virtual_root
)
123 return ctx
.cfg
.script_name
;
124 else if (ctx
.qry
.page
)
125 return fmt("%s/%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
, ctx
.qry
.page
);
126 else if (ctx
.qry
.repo
)
127 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
);
129 return fmt("%s/", ctx
.cfg
.virtual_root
);
132 static void site_url(char *page
, char *search
, int ofs
)
136 if (ctx
.cfg
.virtual_root
) {
137 html_attr(ctx
.cfg
.virtual_root
);
138 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
141 html(ctx
.cfg
.script_name
);
144 htmlf("?p=%s", page
);
155 htmlf("ofs=%d", ofs
);
159 static void site_link(char *page
, char *name
, char *title
, char *class,
160 char *search
, int ofs
)
174 site_url(page
, search
, ofs
);
180 void cgit_index_link(char *name
, char *title
, char *class, char *pattern
,
183 site_link(NULL
, name
, title
, class, pattern
, ofs
);
186 static char *repolink(char *title
, char *class, char *page
, char *head
,
203 if (ctx
.cfg
.virtual_root
) {
204 html_attr(ctx
.cfg
.virtual_root
);
205 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
207 html_attr(ctx
.repo
->url
);
208 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
217 html(ctx
.cfg
.script_name
);
219 html_attr(ctx
.repo
->url
);
220 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
230 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
236 return fmt("%s", delim
);
239 static void reporevlink(char *page
, char *name
, char *title
, char *class,
240 char *head
, char *rev
, char *path
)
244 delim
= repolink(title
, class, page
, head
, path
);
245 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
255 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
256 char *rev
, char *path
)
258 reporevlink("tree", name
, title
, class, head
, rev
, path
);
261 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
262 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
)
266 delim
= repolink(title
, class, "log", head
, path
);
267 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
273 if (grep
&& pattern
) {
292 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
295 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
296 name
[ctx
.cfg
.max_msg_len
] = '\0';
297 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
298 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
299 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
301 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
304 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
305 char *rev
, char *path
)
307 reporevlink("refs", name
, title
, class, head
, rev
, path
);
310 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
311 char *rev
, char *archivename
)
313 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
316 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
317 char *new_rev
, char *old_rev
, char *path
)
321 delim
= repolink(title
, class, "diff", head
, path
);
322 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
338 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
341 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
344 void cgit_object_link(struct object
*obj
)
346 char *page
, *arg
, *url
;
348 if (obj
->type
== OBJ_COMMIT
) {
349 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj
->sha1
)), NULL
, NULL
,
350 ctx
.qry
.head
, sha1_to_hex(obj
->sha1
));
352 } else if (obj
->type
== OBJ_TREE
) {
355 } else if (obj
->type
== OBJ_TAG
) {
363 url
= cgit_pageurl(ctx
.qry
.repo
, page
,
364 fmt("%s=%s", arg
, sha1_to_hex(obj
->sha1
)));
365 html_link_open(url
, NULL
, NULL
);
366 htmlf("%s %s", typename(obj
->type
),
367 sha1_to_hex(obj
->sha1
));
371 void cgit_print_date(time_t secs
, char *format
, int local_time
)
379 time
= localtime(&secs
);
381 time
= gmtime(&secs
);
382 strftime(buf
, sizeof(buf
)-1, format
, time
);
386 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
395 if (secs
> max_relative
&& max_relative
>= 0) {
396 cgit_print_date(t
, format
, ctx
.cfg
.local_time
);
400 if (secs
< TM_HOUR
* 2) {
401 htmlf("<span class='age-mins'>%.0f min.</span>",
402 secs
* 1.0 / TM_MIN
);
405 if (secs
< TM_DAY
* 2) {
406 htmlf("<span class='age-hours'>%.0f hours</span>",
407 secs
* 1.0 / TM_HOUR
);
410 if (secs
< TM_WEEK
* 2) {
411 htmlf("<span class='age-days'>%.0f days</span>",
412 secs
* 1.0 / TM_DAY
);
415 if (secs
< TM_MONTH
* 2) {
416 htmlf("<span class='age-weeks'>%.0f weeks</span>",
417 secs
* 1.0 / TM_WEEK
);
420 if (secs
< TM_YEAR
* 2) {
421 htmlf("<span class='age-months'>%.0f months</span>",
422 secs
* 1.0 / TM_MONTH
);
425 htmlf("<span class='age-years'>%.0f years</span>",
426 secs
* 1.0 / TM_YEAR
);
429 void cgit_print_http_headers(struct cgit_context
*ctx
)
431 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
432 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
434 else if (ctx
->page
.mimetype
)
435 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
436 if (ctx
->page
.filename
)
437 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
439 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
440 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
444 void cgit_print_docstart(struct cgit_context
*ctx
)
446 char *host
= cgit_hosturl();
448 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
451 html_txt(ctx
->page
.title
);
453 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
454 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
455 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
456 html("<link rel='stylesheet' type='text/css' href='");
457 html_attr(ctx
->cfg
.css
);
459 if (ctx
->cfg
.favicon
) {
460 html("<link rel='shortcut icon' href='");
461 html_attr(ctx
->cfg
.favicon
);
464 if (host
&& ctx
->repo
) {
465 html("<link rel='alternate' title='Atom feed' href='http://");
466 html_attr(cgit_hosturl());
467 html_attr(cgit_fileurl(ctx
->repo
->url
, "atom", ctx
->qry
.path
,
468 fmt("h=%s", ctx
->qry
.head
)));
469 html("' type='application/atom+xml'/>");
475 void cgit_print_docend()
479 html_include(ctx
.cfg
.footer
);
481 html("<div class='footer'>generated ");
482 cgit_print_date(time(NULL
), FMT_LONGDATE
, ctx
.cfg
.local_time
);
483 htmlf(" by cgit %s", cgit_version
);
486 html("</body>\n</html>\n");
489 int print_branch_option(const char *refname
, const unsigned char *sha1
,
490 int flags
, void *cb_data
)
492 char *name
= (char *)refname
;
493 html_option(name
, name
, ctx
.qry
.head
);
497 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
498 int flags
, void *cb_data
)
501 struct taginfo
*info
;
504 unsigned char fileid
[20];
505 int *header
= (int *)cb_data
;
507 if (prefixcmp(refname
, "refs/archives"))
509 strncpy(buf
, refname
+14, sizeof(buf
));
510 obj
= parse_object(sha1
);
513 if (obj
->type
== OBJ_TAG
) {
514 tag
= lookup_tag(sha1
);
515 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
517 hashcpy(fileid
, tag
->tagged
->sha1
);
518 } else if (obj
->type
!= OBJ_BLOB
) {
521 hashcpy(fileid
, sha1
);
524 html("<h1>download</h1>\n");
527 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
528 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
530 html_link_open(url
, NULL
, "menu");
531 html_txt(strlpart(buf
, 20));
536 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
540 if (!ctx
.cfg
.virtual_root
) {
541 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
543 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
544 html_hidden("url", url
);
547 if (incl_head
&& ctx
.qry
.head
&& ctx
.repo
->defbranch
&&
548 strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
549 html_hidden("h", ctx
.qry
.head
);
552 html_hidden("id", ctx
.qry
.sha1
);
554 html_hidden("id2", ctx
.qry
.sha2
);
558 html_hidden("qt", ctx
.qry
.grep
);
560 html_hidden("q", ctx
.qry
.search
);
564 char *hc(struct cgit_cmd
*cmd
, const char *page
)
566 return (strcmp(cmd
->name
, page
) ? NULL
: "active");
569 void cgit_print_pageheader(struct cgit_context
*ctx
)
571 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
573 html("<table id='header'>\n");
575 html("<td class='logo' rowspan='2'><a href='");
576 if (ctx
->cfg
.logo_link
)
577 html_attr(ctx
->cfg
.logo_link
);
579 html_attr(cgit_rooturl());
580 html("'><img src='");
581 html_attr(ctx
->cfg
.logo
);
582 html("' alt='cgit logo'/></a></td>\n");
584 html("<td class='main'>");
586 cgit_index_link("index", NULL
, NULL
, NULL
, 0);
588 reporevlink(NULL
, ctx
->repo
->name
, NULL
, hc(cmd
, "summary"),
589 ctx
->qry
.head
, NULL
, NULL
);
590 html("</td><td class='form'>");
591 html("<form method='get' action=''>\n");
592 add_hidden_formfields(0, 1, ctx
->qry
.page
);
593 html("<select name='h' onchange='this.form.submit();'>\n");
594 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
596 html("<input type='submit' name='' value='switch'/>");
599 html_txt(ctx
->cfg
.root_title
);
600 html("</td></tr>\n");
602 html("<tr><td class='sub'>");
604 html_txt(ctx
->repo
->desc
);
605 html("</td><td class='sub right'>");
606 html_txt(ctx
->repo
->owner
);
608 if (ctx
->cfg
.root_desc
)
609 html_txt(ctx
->cfg
.root_desc
);
610 else if (ctx
->cfg
.index_info
)
611 html_include(ctx
->cfg
.index_info
);
613 html("</td></tr></table>\n");
615 html("<table class='tabs'><tr><td>\n");
617 reporevlink(NULL
, "summary", NULL
, hc(cmd
, "summary"),
618 ctx
->qry
.head
, NULL
, NULL
);
619 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
620 ctx
->qry
.sha1
, NULL
);
621 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
622 NULL
, NULL
, 0, NULL
, NULL
);
623 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
624 ctx
->qry
.sha1
, NULL
);
625 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
626 ctx
->qry
.head
, ctx
->qry
.sha1
);
627 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
628 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
629 if (ctx
->repo
->readme
)
630 reporevlink("about", "about", NULL
,
631 hc(cmd
, "about"), ctx
->qry
.head
, NULL
,
633 html("</td><td class='form'>");
634 html("<form class='right' method='get' action='");
635 if (ctx
->cfg
.virtual_root
)
636 html_attr(cgit_fileurl(ctx
->qry
.repo
, "log",
637 ctx
->qry
.path
, NULL
));
639 add_hidden_formfields(1, 0, "log");
640 html("<select name='qt'>\n");
641 html_option("grep", "log msg", ctx
->qry
.grep
);
642 html_option("author", "author", ctx
->qry
.grep
);
643 html_option("committer", "committer", ctx
->qry
.grep
);
645 html("<input class='txt' type='text' size='10' name='q' value='");
646 html_attr(ctx
->qry
.search
);
648 html("<input type='submit' value='search'/>\n");
651 site_link(NULL
, "index", NULL
, hc(cmd
, "repolist"), NULL
, 0);
652 if (ctx
->cfg
.root_readme
)
653 site_link("about", "about", NULL
, hc(cmd
, "about"),
655 html("</td><td class='form'>");
656 html("<form method='get' action='");
657 html_attr(cgit_rooturl());
659 html("<input type='text' name='q' size='10' value='");
660 html_attr(ctx
->qry
.search
);
662 html("<input type='submit' value='search'/>\n");
665 html("</td></tr></table>\n");
666 html("<div class='content'>");
669 void cgit_print_filemode(unsigned short mode
)
673 else if (S_ISLNK(mode
))
675 else if (S_ISGITLINK(mode
))
679 html_fileperm(mode
>> 6);
680 html_fileperm(mode
>> 3);
684 void cgit_print_snapshot_links(const char *repo
, const char *head
,
685 const char *hex
, int snapshots
)
687 const struct cgit_snapshot_format
* f
;
690 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
691 if (!(snapshots
& f
->bit
))
693 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
695 cgit_snapshot_link(filename
, NULL
, NULL
, (char *)head
,
696 (char *)hex
, filename
);