]>
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_stats_link(char *name
, char *title
, char *class, char *head
,
375 reporevlink("stats", name
, title
, class, head
, NULL
, path
);
378 void cgit_object_link(struct object
*obj
)
380 char *page
, *shortrev
, *fullrev
, *name
;
382 fullrev
= sha1_to_hex(obj
->sha1
);
383 shortrev
= xstrdup(fullrev
);
385 if (obj
->type
== OBJ_COMMIT
) {
386 cgit_commit_link(fmt("commit %s...", shortrev
), NULL
, NULL
,
387 ctx
.qry
.head
, fullrev
);
389 } else if (obj
->type
== OBJ_TREE
)
391 else if (obj
->type
== OBJ_TAG
)
395 name
= fmt("%s %s...", typename(obj
->type
), shortrev
);
396 reporevlink(page
, name
, NULL
, NULL
, ctx
.qry
.head
, fullrev
, NULL
);
399 void cgit_print_date(time_t secs
, char *format
, int local_time
)
407 time
= localtime(&secs
);
409 time
= gmtime(&secs
);
410 strftime(buf
, sizeof(buf
)-1, format
, time
);
414 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
423 if (secs
> max_relative
&& max_relative
>= 0) {
424 cgit_print_date(t
, format
, ctx
.cfg
.local_time
);
428 if (secs
< TM_HOUR
* 2) {
429 htmlf("<span class='age-mins'>%.0f min.</span>",
430 secs
* 1.0 / TM_MIN
);
433 if (secs
< TM_DAY
* 2) {
434 htmlf("<span class='age-hours'>%.0f hours</span>",
435 secs
* 1.0 / TM_HOUR
);
438 if (secs
< TM_WEEK
* 2) {
439 htmlf("<span class='age-days'>%.0f days</span>",
440 secs
* 1.0 / TM_DAY
);
443 if (secs
< TM_MONTH
* 2) {
444 htmlf("<span class='age-weeks'>%.0f weeks</span>",
445 secs
* 1.0 / TM_WEEK
);
448 if (secs
< TM_YEAR
* 2) {
449 htmlf("<span class='age-months'>%.0f months</span>",
450 secs
* 1.0 / TM_MONTH
);
453 htmlf("<span class='age-years'>%.0f years</span>",
454 secs
* 1.0 / TM_YEAR
);
457 void cgit_print_http_headers(struct cgit_context
*ctx
)
459 if (ctx
->cfg
.embedded
)
462 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
463 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
465 else if (ctx
->page
.mimetype
)
466 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
468 htmlf("Content-Length: %ld\n", ctx
->page
.size
);
469 if (ctx
->page
.filename
)
470 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
472 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
473 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
477 void cgit_print_docstart(struct cgit_context
*ctx
)
479 if (ctx
->cfg
.embedded
)
482 char *host
= cgit_hosturl();
484 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
487 html_txt(ctx
->page
.title
);
489 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
490 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
491 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
492 html("<link rel='stylesheet' type='text/css' href='");
493 html_attr(ctx
->cfg
.css
);
495 if (ctx
->cfg
.favicon
) {
496 html("<link rel='shortcut icon' href='");
497 html_attr(ctx
->cfg
.favicon
);
500 if (host
&& ctx
->repo
) {
501 html("<link rel='alternate' title='Atom feed' href='http://");
502 html_attr(cgit_hosturl());
503 html_attr(cgit_fileurl(ctx
->repo
->url
, "atom", ctx
->qry
.path
,
504 fmt("h=%s", ctx
->qry
.head
)));
505 html("' type='application/atom+xml'/>");
510 html_include(ctx
->cfg
.header
);
513 void cgit_print_docend()
517 html_include(ctx
.cfg
.footer
);
519 htmlf("<div class='footer'>generated by cgit %s at ",
521 cgit_print_date(time(NULL
), FMT_LONGDATE
, ctx
.cfg
.local_time
);
525 if (ctx
.cfg
.embedded
)
527 html("</body>\n</html>\n");
530 int print_branch_option(const char *refname
, const unsigned char *sha1
,
531 int flags
, void *cb_data
)
533 char *name
= (char *)refname
;
534 html_option(name
, name
, ctx
.qry
.head
);
538 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
539 int flags
, void *cb_data
)
542 struct taginfo
*info
;
545 unsigned char fileid
[20];
546 int *header
= (int *)cb_data
;
548 if (prefixcmp(refname
, "refs/archives"))
550 strncpy(buf
, refname
+14, sizeof(buf
));
551 obj
= parse_object(sha1
);
554 if (obj
->type
== OBJ_TAG
) {
555 tag
= lookup_tag(sha1
);
556 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
558 hashcpy(fileid
, tag
->tagged
->sha1
);
559 } else if (obj
->type
!= OBJ_BLOB
) {
562 hashcpy(fileid
, sha1
);
565 html("<h1>download</h1>\n");
568 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
569 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
571 html_link_open(url
, NULL
, "menu");
572 html_txt(strlpart(buf
, 20));
577 void cgit_add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
581 if (!ctx
.cfg
.virtual_root
) {
582 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
584 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
585 html_hidden("url", url
);
588 if (incl_head
&& ctx
.qry
.head
&& ctx
.repo
->defbranch
&&
589 strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
590 html_hidden("h", ctx
.qry
.head
);
593 html_hidden("id", ctx
.qry
.sha1
);
595 html_hidden("id2", ctx
.qry
.sha2
);
597 html_hidden("showmsg", "1");
601 html_hidden("qt", ctx
.qry
.grep
);
603 html_hidden("q", ctx
.qry
.search
);
607 const char *fallback_cmd
= "repolist";
609 char *hc(struct cgit_cmd
*cmd
, const char *page
)
611 return (strcmp(cmd
? cmd
->name
: fallback_cmd
, page
) ? NULL
: "active");
614 void cgit_print_pageheader(struct cgit_context
*ctx
)
616 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
618 if (!cmd
&& ctx
->repo
)
619 fallback_cmd
= "summary";
621 html("<div id='cgit'>");
622 html("<table id='header'>\n");
624 html("<td class='logo' rowspan='2'><a href='");
625 if (ctx
->cfg
.logo_link
)
626 html_attr(ctx
->cfg
.logo_link
);
628 html_attr(cgit_rooturl());
629 html("'><img src='");
630 html_attr(ctx
->cfg
.logo
);
631 html("' alt='cgit logo'/></a></td>\n");
633 html("<td class='main'>");
635 cgit_index_link("index", NULL
, NULL
, NULL
, 0);
637 cgit_summary_link(ctx
->repo
->name
, ctx
->repo
->name
, NULL
, NULL
);
638 html("</td><td class='form'>");
639 html("<form method='get' action=''>\n");
640 cgit_add_hidden_formfields(0, 1, ctx
->qry
.page
);
641 html("<select name='h' onchange='this.form.submit();'>\n");
642 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
644 html("<input type='submit' name='' value='switch'/>");
647 html_txt(ctx
->cfg
.root_title
);
648 html("</td></tr>\n");
650 html("<tr><td class='sub'>");
652 html_txt(ctx
->repo
->desc
);
653 html("</td><td class='sub right'>");
654 html_txt(ctx
->repo
->owner
);
656 if (ctx
->cfg
.root_desc
)
657 html_txt(ctx
->cfg
.root_desc
);
658 else if (ctx
->cfg
.index_info
)
659 html_include(ctx
->cfg
.index_info
);
661 html("</td></tr></table>\n");
663 html("<table class='tabs'><tr><td>\n");
665 cgit_summary_link("summary", NULL
, hc(cmd
, "summary"),
667 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
668 ctx
->qry
.sha1
, NULL
);
669 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
670 NULL
, NULL
, 0, NULL
, NULL
, ctx
->qry
.showmsg
);
671 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
672 ctx
->qry
.sha1
, NULL
);
673 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
674 ctx
->qry
.head
, ctx
->qry
.sha1
);
675 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
676 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
677 if (ctx
->repo
->max_stats
)
678 cgit_stats_link("stats", NULL
, hc(cmd
, "stats"),
679 ctx
->qry
.head
, NULL
);
680 if (ctx
->repo
->readme
)
681 reporevlink("about", "about", NULL
,
682 hc(cmd
, "about"), ctx
->qry
.head
, NULL
,
684 html("</td><td class='form'>");
685 html("<form class='right' method='get' action='");
686 if (ctx
->cfg
.virtual_root
)
687 html_url_path(cgit_fileurl(ctx
->qry
.repo
, "log",
688 ctx
->qry
.path
, NULL
));
690 cgit_add_hidden_formfields(1, 0, "log");
691 html("<select name='qt'>\n");
692 html_option("grep", "log msg", ctx
->qry
.grep
);
693 html_option("author", "author", ctx
->qry
.grep
);
694 html_option("committer", "committer", ctx
->qry
.grep
);
696 html("<input class='txt' type='text' size='10' name='q' value='");
697 html_attr(ctx
->qry
.search
);
699 html("<input type='submit' value='search'/>\n");
702 site_link(NULL
, "index", NULL
, hc(cmd
, "repolist"), NULL
, 0);
703 if (ctx
->cfg
.root_readme
)
704 site_link("about", "about", NULL
, hc(cmd
, "about"),
706 html("</td><td class='form'>");
707 html("<form method='get' action='");
708 html_attr(cgit_rooturl());
710 html("<input type='text' name='q' size='10' value='");
711 html_attr(ctx
->qry
.search
);
713 html("<input type='submit' value='search'/>\n");
716 html("</td></tr></table>\n");
717 html("<div class='content'>");
720 void cgit_print_filemode(unsigned short mode
)
724 else if (S_ISLNK(mode
))
726 else if (S_ISGITLINK(mode
))
730 html_fileperm(mode
>> 6);
731 html_fileperm(mode
>> 3);
735 void cgit_print_snapshot_links(const char *repo
, const char *head
,
736 const char *hex
, int snapshots
)
738 const struct cgit_snapshot_format
* f
;
741 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
742 if (!(snapshots
& f
->bit
))
744 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
746 cgit_snapshot_link(filename
, NULL
, NULL
, NULL
, NULL
, filename
);