]>
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'>");
39 if (ctx
.cfg
.virtual_root
)
40 return fmt("%s/", ctx
.cfg
.virtual_root
);
42 return ctx
.cfg
.script_name
;
45 char *cgit_repourl(const char *reponame
)
47 if (ctx
.cfg
.virtual_root
) {
48 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, reponame
);
50 return fmt("?r=%s", reponame
);
54 char *cgit_fileurl(const char *reponame
, const char *pagename
,
55 const char *filename
, const char *query
)
60 if (ctx
.cfg
.virtual_root
) {
61 tmp
= fmt("%s/%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
62 pagename
, (filename
? filename
:""));
65 tmp
= fmt("?url=%s/%s/%s", reponame
, pagename
,
66 (filename
? filename
: ""));
70 tmp
= fmt("%s%s%s", tmp
, delim
, query
);
74 char *cgit_pageurl(const char *reponame
, const char *pagename
,
77 return cgit_fileurl(reponame
,pagename
,0,query
);
80 const char *cgit_repobasename(const char *reponame
)
82 /* I assume we don't need to store more than one repo basename */
83 static char rvbuf
[1024];
86 strncpy(rvbuf
,reponame
,sizeof(rvbuf
));
87 if(rvbuf
[sizeof(rvbuf
)-1])
88 die("cgit_repobasename: truncated repository name '%s'", reponame
);
90 /* strip trailing slashes */
91 while(p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
92 /* strip trailing .git */
93 if(p
>=3 && !strncmp(&rvbuf
[p
-3],".git",4)) {
94 p
-= 3; rvbuf
[p
--] = 0;
96 /* strip more trailing slashes if any */
97 while( p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
98 /* find last slash in the remaining string */
99 rv
= strrchr(rvbuf
,'/');
107 if (!ctx
.cfg
.virtual_root
)
108 return ctx
.cfg
.script_name
;
109 else if (ctx
.qry
.page
)
110 return fmt("%s/%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
, ctx
.qry
.page
);
111 else if (ctx
.qry
.repo
)
112 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
);
114 return fmt("%s/", ctx
.cfg
.virtual_root
);
117 static void site_url(char *page
, char *search
, int ofs
)
121 if (ctx
.cfg
.virtual_root
) {
122 html_attr(ctx
.cfg
.virtual_root
);
123 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
126 html(ctx
.cfg
.script_name
);
129 htmlf("?p=%s", page
);
140 htmlf("ofs=%d", ofs
);
144 static void site_link(char *page
, char *name
, char *title
, char *class,
145 char *search
, int ofs
)
159 site_url(page
, search
, ofs
);
165 void cgit_index_link(char *name
, char *title
, char *class, char *pattern
,
168 site_link(NULL
, name
, title
, class, pattern
, ofs
);
171 static char *repolink(char *title
, char *class, char *page
, char *head
,
188 if (ctx
.cfg
.virtual_root
) {
189 html_attr(ctx
.cfg
.virtual_root
);
190 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
192 html_attr(ctx
.repo
->url
);
193 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
202 html(ctx
.cfg
.script_name
);
204 html_attr(ctx
.repo
->url
);
205 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
215 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
221 return fmt("%s", delim
);
224 static void reporevlink(char *page
, char *name
, char *title
, char *class,
225 char *head
, char *rev
, char *path
)
229 delim
= repolink(title
, class, page
, head
, path
);
230 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
240 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
241 char *rev
, char *path
)
243 reporevlink("tree", name
, title
, class, head
, rev
, path
);
246 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
247 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
)
251 delim
= repolink(title
, class, "log", head
, path
);
252 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
258 if (grep
&& pattern
) {
277 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
280 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
281 name
[ctx
.cfg
.max_msg_len
] = '\0';
282 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
283 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
284 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
286 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
289 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
290 char *rev
, char *path
)
292 reporevlink("refs", name
, title
, class, head
, rev
, path
);
295 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
296 char *rev
, char *archivename
)
298 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
301 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
302 char *new_rev
, char *old_rev
, char *path
)
306 delim
= repolink(title
, class, "diff", head
, path
);
307 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
323 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
326 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
329 void cgit_object_link(struct object
*obj
)
331 char *page
, *arg
, *url
;
333 if (obj
->type
== OBJ_COMMIT
) {
334 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj
->sha1
)), NULL
, NULL
,
335 ctx
.qry
.head
, sha1_to_hex(obj
->sha1
));
337 } else if (obj
->type
== OBJ_TREE
) {
340 } else if (obj
->type
== OBJ_TAG
) {
348 url
= cgit_pageurl(ctx
.qry
.repo
, page
,
349 fmt("%s=%s", arg
, sha1_to_hex(obj
->sha1
)));
350 html_link_open(url
, NULL
, NULL
);
351 htmlf("%s %s", typename(obj
->type
),
352 sha1_to_hex(obj
->sha1
));
356 void cgit_print_date(time_t secs
, char *format
)
363 time
= gmtime(&secs
);
364 strftime(buf
, sizeof(buf
)-1, format
, time
);
368 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
377 if (secs
> max_relative
&& max_relative
>= 0) {
378 cgit_print_date(t
, format
);
382 if (secs
< TM_HOUR
* 2) {
383 htmlf("<span class='age-mins'>%.0f min.</span>",
384 secs
* 1.0 / TM_MIN
);
387 if (secs
< TM_DAY
* 2) {
388 htmlf("<span class='age-hours'>%.0f hours</span>",
389 secs
* 1.0 / TM_HOUR
);
392 if (secs
< TM_WEEK
* 2) {
393 htmlf("<span class='age-days'>%.0f days</span>",
394 secs
* 1.0 / TM_DAY
);
397 if (secs
< TM_MONTH
* 2) {
398 htmlf("<span class='age-weeks'>%.0f weeks</span>",
399 secs
* 1.0 / TM_WEEK
);
402 if (secs
< TM_YEAR
* 2) {
403 htmlf("<span class='age-months'>%.0f months</span>",
404 secs
* 1.0 / TM_MONTH
);
407 htmlf("<span class='age-years'>%.0f years</span>",
408 secs
* 1.0 / TM_YEAR
);
411 void cgit_print_http_headers(struct cgit_context
*ctx
)
413 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
414 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
416 else if (ctx
->page
.mimetype
)
417 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
418 if (ctx
->page
.filename
)
419 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
421 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
422 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
426 void cgit_print_docstart(struct cgit_context
*ctx
)
429 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
432 html_txt(ctx
->page
.title
);
434 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
435 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
436 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
437 html("<link rel='stylesheet' type='text/css' href='");
438 html_attr(ctx
->cfg
.css
);
440 if (ctx
->cfg
.favicon
) {
441 html("<link rel='shortcut icon' href='");
442 html_attr(ctx
->cfg
.favicon
);
449 void cgit_print_docend()
453 html_include(ctx
.cfg
.footer
);
455 html("<div class='footer'>generated ");
456 cgit_print_date(time(NULL
), FMT_LONGDATE
);
457 htmlf(" by cgit %s", cgit_version
);
460 html("</body>\n</html>\n");
463 int print_branch_option(const char *refname
, const unsigned char *sha1
,
464 int flags
, void *cb_data
)
466 char *name
= (char *)refname
;
467 html_option(name
, name
, ctx
.qry
.head
);
471 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
472 int flags
, void *cb_data
)
475 struct taginfo
*info
;
478 unsigned char fileid
[20];
479 int *header
= (int *)cb_data
;
481 if (prefixcmp(refname
, "refs/archives"))
483 strncpy(buf
, refname
+14, sizeof(buf
));
484 obj
= parse_object(sha1
);
487 if (obj
->type
== OBJ_TAG
) {
488 tag
= lookup_tag(sha1
);
489 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
491 hashcpy(fileid
, tag
->tagged
->sha1
);
492 } else if (obj
->type
!= OBJ_BLOB
) {
495 hashcpy(fileid
, sha1
);
498 html("<h1>download</h1>\n");
501 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
502 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
504 html_link_open(url
, NULL
, "menu");
505 html_txt(strlpart(buf
, 20));
510 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
514 if (!ctx
.cfg
.virtual_root
) {
515 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
517 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
518 html_hidden("url", url
);
521 if (incl_head
&& strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
522 html_hidden("h", ctx
.qry
.head
);
525 html_hidden("id", ctx
.qry
.sha1
);
527 html_hidden("id2", ctx
.qry
.sha2
);
531 html_hidden("qt", ctx
.qry
.grep
);
533 html_hidden("q", ctx
.qry
.search
);
537 char *hc(struct cgit_cmd
*cmd
, const char *page
)
539 return (strcmp(cmd
->name
, page
) ? NULL
: "active");
542 void cgit_print_pageheader(struct cgit_context
*ctx
)
544 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
546 html("<table id='header'>\n");
548 html("<td class='logo' rowspan='2'><a href='");
549 if (ctx
->cfg
.logo_link
)
550 html_attr(ctx
->cfg
.logo_link
);
552 html_attr(cgit_rooturl());
553 html("'><img src='");
554 html_attr(ctx
->cfg
.logo
);
555 html("' alt='cgit logo'/></a></td>\n");
557 html("<td class='main'>");
559 cgit_index_link("index", NULL
, NULL
, NULL
, 0);
561 reporevlink(NULL
, ctx
->repo
->name
, NULL
, hc(cmd
, "summary"),
562 ctx
->qry
.head
, NULL
, NULL
);
563 html("</td><td class='form'>");
564 html("<form method='get' action=''>\n");
565 add_hidden_formfields(0, 1, ctx
->qry
.page
);
566 html("<select name='h' onchange='this.form.submit();'>\n");
567 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
569 html("<input type='submit' name='' value='switch'/>");
572 html_txt(ctx
->cfg
.root_title
);
573 html("</td></tr>\n");
575 html("<tr><td class='sub'");
577 html(" colspan='2'>");
578 html_txt(ctx
->repo
->desc
);
581 if (ctx
->cfg
.root_desc
)
582 html_txt(ctx
->cfg
.root_desc
);
583 else if (ctx
->cfg
.index_info
)
584 html_include(ctx
->cfg
.index_info
);
586 html("</td></tr></table>\n");
588 html("<table class='tabs'><tr><td>\n");
590 reporevlink(NULL
, "summary", NULL
, hc(cmd
, "summary"),
591 ctx
->qry
.head
, NULL
, NULL
);
592 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
593 ctx
->qry
.sha1
, NULL
);
594 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
595 NULL
, NULL
, 0, NULL
, NULL
);
596 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
597 ctx
->qry
.sha1
, NULL
);
598 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
599 ctx
->qry
.head
, ctx
->qry
.sha1
);
600 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
601 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
602 if (ctx
->repo
->readme
)
603 reporevlink("about", "about", NULL
,
604 hc(cmd
, "about"), ctx
->qry
.head
, NULL
,
606 html("</td><td class='form'>");
607 html("<form class='right' method='get' action='");
608 if (ctx
->cfg
.virtual_root
)
609 html_attr(cgit_fileurl(ctx
->qry
.repo
, "log",
610 ctx
->qry
.path
, NULL
));
612 add_hidden_formfields(1, 0, "log");
613 html("<select name='qt'>\n");
614 html_option("grep", "log msg", ctx
->qry
.grep
);
615 html_option("author", "author", ctx
->qry
.grep
);
616 html_option("committer", "committer", ctx
->qry
.grep
);
618 html("<input class='txt' type='text' size='10' name='q' value='");
619 html_attr(ctx
->qry
.search
);
621 html("<input type='submit' value='search'/>\n");
624 site_link(NULL
, "index", NULL
, hc(cmd
, "repolist"), NULL
, 0);
625 if (ctx
->cfg
.root_readme
)
626 site_link("about", "about", NULL
, hc(cmd
, "about"),
628 html("</td><td class='form'>");
629 html("<form method='get' action='");
630 html_attr(cgit_rooturl());
632 html("<input type='text' name='q' size='10' value='");
633 html_attr(ctx
->qry
.search
);
635 html("<input type='submit' value='search'/>\n");
638 html("</td></tr></table>\n");
639 html("<div class='content'>");
642 void cgit_print_filemode(unsigned short mode
)
646 else if (S_ISLNK(mode
))
648 else if (S_ISGITLINK(mode
))
652 html_fileperm(mode
>> 6);
653 html_fileperm(mode
>> 3);
657 void cgit_print_snapshot_links(const char *repo
, const char *head
,
658 const char *hex
, int snapshots
)
660 const struct cgit_snapshot_format
* f
;
663 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
664 if (!(snapshots
& f
->bit
))
666 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
668 cgit_snapshot_link(filename
, NULL
, NULL
, (char *)head
,
669 (char *)hex
, filename
);