]>
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
);
444 void cgit_print_docend()
446 html("</div>\n</body>\n</html>\n");
449 int print_branch_option(const char *refname
, const unsigned char *sha1
,
450 int flags
, void *cb_data
)
452 char *name
= (char *)refname
;
453 html_option(name
, name
, ctx
.qry
.head
);
457 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
458 int flags
, void *cb_data
)
461 struct taginfo
*info
;
464 unsigned char fileid
[20];
465 int *header
= (int *)cb_data
;
467 if (prefixcmp(refname
, "refs/archives"))
469 strncpy(buf
, refname
+14, sizeof(buf
));
470 obj
= parse_object(sha1
);
473 if (obj
->type
== OBJ_TAG
) {
474 tag
= lookup_tag(sha1
);
475 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
477 hashcpy(fileid
, tag
->tagged
->sha1
);
478 } else if (obj
->type
!= OBJ_BLOB
) {
481 hashcpy(fileid
, sha1
);
484 html("<h1>download</h1>\n");
487 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
488 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
490 html_link_open(url
, NULL
, "menu");
491 html_txt(strlpart(buf
, 20));
496 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
500 if (!ctx
.cfg
.virtual_root
) {
501 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
503 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
504 html_hidden("url", url
);
507 if (incl_head
&& strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
508 html_hidden("h", ctx
.qry
.head
);
511 html_hidden("id", ctx
.qry
.sha1
);
513 html_hidden("id2", ctx
.qry
.sha2
);
517 html_hidden("qt", ctx
.qry
.grep
);
519 html_hidden("q", ctx
.qry
.search
);
523 char *hc(struct cgit_cmd
*cmd
, const char *page
)
525 return (strcmp(cmd
->name
, page
) ? NULL
: "active");
528 void cgit_print_pageheader(struct cgit_context
*ctx
)
530 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
532 html("<table id='header'>\n");
534 html("<td class='logo' rowspan='2'><a href='");
535 if (ctx
->cfg
.logo_link
)
536 html_attr(ctx
->cfg
.logo_link
);
538 html_attr(cgit_rooturl());
539 html("'><img src='");
540 html_attr(ctx
->cfg
.logo
);
541 html("' alt='cgit logo'/></a></td>\n");
543 html("<td class='main'>");
545 reporevlink(NULL
, ctx
->repo
->name
, NULL
, hc(cmd
, "summary"),
546 ctx
->qry
.head
, NULL
, NULL
);
548 html_txt(ctx
->qry
.page
);
549 html("</td><td class='form'>");
550 html("<form method='get' action=''>\n");
551 add_hidden_formfields(0, 1, ctx
->qry
.page
);
552 html("<select name='h' onchange='this.form.submit();'>\n");
553 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
555 html("<input type='submit' name='' value='switch'/>");
558 html_txt(ctx
->cfg
.root_title
);
559 html("</td></tr>\n");
561 html("<tr><td class='sub'");
563 html(" colspan='2'>");
564 html_txt(ctx
->repo
->desc
);
567 if (ctx
->cfg
.root_desc
)
568 html_txt(ctx
->cfg
.root_desc
);
569 else if (ctx
->cfg
.index_info
)
570 html_include(ctx
->cfg
.index_info
);
572 html("</td></tr></table>\n");
574 html("<table class='tabs'><tr><td>\n");
576 reporevlink(NULL
, "summary", NULL
, hc(cmd
, "summary"),
577 ctx
->qry
.head
, NULL
, NULL
);
578 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
579 ctx
->qry
.sha1
, NULL
);
580 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
581 NULL
, NULL
, 0, NULL
, NULL
);
582 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
583 ctx
->qry
.sha1
, NULL
);
584 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
585 ctx
->qry
.head
, ctx
->qry
.sha1
);
586 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
587 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
588 if (ctx
->repo
->readme
)
589 reporevlink("about", "about", NULL
,
590 hc(cmd
, "about"), ctx
->qry
.head
, NULL
,
592 html("</td><td class='form'>");
593 html("<form class='right' method='get' action='");
594 if (ctx
->cfg
.virtual_root
)
595 html_attr(cgit_fileurl(ctx
->qry
.repo
, "log",
596 ctx
->qry
.path
, NULL
));
598 add_hidden_formfields(1, 0, "log");
599 html("<select name='qt'>\n");
600 html_option("grep", "log msg", ctx
->qry
.grep
);
601 html_option("author", "author", ctx
->qry
.grep
);
602 html_option("committer", "committer", ctx
->qry
.grep
);
604 html("<input class='txt' type='text' size='10' name='q' value='");
605 html_attr(ctx
->qry
.search
);
607 html("<input type='submit' value='search'/>\n");
610 site_link(NULL
, "index", NULL
, hc(cmd
, "repolist"), NULL
, 0);
611 if (ctx
->cfg
.root_readme
)
612 site_link("about", "about", NULL
, hc(cmd
, "about"),
614 html("</td><td class='form'>");
615 html("<form method='get' action='");
616 html_attr(cgit_rooturl());
618 html("<input type='text' name='q' size='10' value='");
619 html_attr(ctx
->qry
.search
);
621 html("<input type='submit' value='search'/>\n");
624 html("</td></tr></table>\n");
625 html("<div class='content'>");
628 void cgit_print_filemode(unsigned short mode
)
632 else if (S_ISLNK(mode
))
634 else if (S_ISGITLINK(mode
))
638 html_fileperm(mode
>> 6);
639 html_fileperm(mode
>> 3);
643 void cgit_print_snapshot_links(const char *repo
, const char *head
,
644 const char *hex
, int snapshots
)
646 const struct cgit_snapshot_format
* f
;
649 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
650 if (!(snapshots
& f
->bit
))
652 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
654 cgit_snapshot_link(filename
, NULL
, NULL
, (char *)head
,
655 (char *)hex
, filename
);