]>
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
)
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
);
139 static void site_link(char *page
, char *name
, char *title
, char *class,
154 site_url(page
, search
);
160 static char *repolink(char *title
, char *class, char *page
, char *head
,
177 if (ctx
.cfg
.virtual_root
) {
178 html_attr(ctx
.cfg
.virtual_root
);
179 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
181 html_attr(ctx
.repo
->url
);
182 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
191 html(ctx
.cfg
.script_name
);
193 html_attr(ctx
.repo
->url
);
194 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
204 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
210 return fmt("%s", delim
);
213 static void reporevlink(char *page
, char *name
, char *title
, char *class,
214 char *head
, char *rev
, char *path
)
218 delim
= repolink(title
, class, page
, head
, path
);
219 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
229 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
230 char *rev
, char *path
)
232 reporevlink("tree", name
, title
, class, head
, rev
, path
);
235 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
236 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
)
240 delim
= repolink(title
, class, "log", head
, path
);
241 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
247 if (grep
&& pattern
) {
266 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
269 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
270 name
[ctx
.cfg
.max_msg_len
] = '\0';
271 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
272 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
273 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
275 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
278 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
279 char *rev
, char *path
)
281 reporevlink("refs", name
, title
, class, head
, rev
, path
);
284 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
285 char *rev
, char *archivename
)
287 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
290 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
291 char *new_rev
, char *old_rev
, char *path
)
295 delim
= repolink(title
, class, "diff", head
, path
);
296 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
312 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
315 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
318 void cgit_object_link(struct object
*obj
)
320 char *page
, *arg
, *url
;
322 if (obj
->type
== OBJ_COMMIT
) {
323 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj
->sha1
)), NULL
, NULL
,
324 ctx
.qry
.head
, sha1_to_hex(obj
->sha1
));
326 } else if (obj
->type
== OBJ_TREE
) {
329 } else if (obj
->type
== OBJ_TAG
) {
337 url
= cgit_pageurl(ctx
.qry
.repo
, page
,
338 fmt("%s=%s", arg
, sha1_to_hex(obj
->sha1
)));
339 html_link_open(url
, NULL
, NULL
);
340 htmlf("%s %s", typename(obj
->type
),
341 sha1_to_hex(obj
->sha1
));
345 void cgit_print_date(time_t secs
, char *format
)
352 time
= gmtime(&secs
);
353 strftime(buf
, sizeof(buf
)-1, format
, time
);
357 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
366 if (secs
> max_relative
&& max_relative
>= 0) {
367 cgit_print_date(t
, format
);
371 if (secs
< TM_HOUR
* 2) {
372 htmlf("<span class='age-mins'>%.0f min.</span>",
373 secs
* 1.0 / TM_MIN
);
376 if (secs
< TM_DAY
* 2) {
377 htmlf("<span class='age-hours'>%.0f hours</span>",
378 secs
* 1.0 / TM_HOUR
);
381 if (secs
< TM_WEEK
* 2) {
382 htmlf("<span class='age-days'>%.0f days</span>",
383 secs
* 1.0 / TM_DAY
);
386 if (secs
< TM_MONTH
* 2) {
387 htmlf("<span class='age-weeks'>%.0f weeks</span>",
388 secs
* 1.0 / TM_WEEK
);
391 if (secs
< TM_YEAR
* 2) {
392 htmlf("<span class='age-months'>%.0f months</span>",
393 secs
* 1.0 / TM_MONTH
);
396 htmlf("<span class='age-years'>%.0f years</span>",
397 secs
* 1.0 / TM_YEAR
);
400 void cgit_print_http_headers(struct cgit_context
*ctx
)
402 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
403 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
405 else if (ctx
->page
.mimetype
)
406 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
407 if (ctx
->page
.filename
)
408 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
410 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
411 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
415 void cgit_print_docstart(struct cgit_context
*ctx
)
418 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
421 html_txt(ctx
->page
.title
);
423 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
424 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
425 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
426 html("<link rel='stylesheet' type='text/css' href='");
427 html_attr(ctx
->cfg
.css
);
433 void cgit_print_docend()
435 html("</div>\n</body>\n</html>\n");
438 int print_branch_option(const char *refname
, const unsigned char *sha1
,
439 int flags
, void *cb_data
)
441 char *name
= (char *)refname
;
442 html_option(name
, name
, ctx
.qry
.head
);
446 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
447 int flags
, void *cb_data
)
450 struct taginfo
*info
;
453 unsigned char fileid
[20];
454 int *header
= (int *)cb_data
;
456 if (prefixcmp(refname
, "refs/archives"))
458 strncpy(buf
, refname
+14, sizeof(buf
));
459 obj
= parse_object(sha1
);
462 if (obj
->type
== OBJ_TAG
) {
463 tag
= lookup_tag(sha1
);
464 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
466 hashcpy(fileid
, tag
->tagged
->sha1
);
467 } else if (obj
->type
!= OBJ_BLOB
) {
470 hashcpy(fileid
, sha1
);
473 html("<h1>download</h1>\n");
476 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
477 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
479 html_link_open(url
, NULL
, "menu");
480 html_txt(strlpart(buf
, 20));
485 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
489 if (!ctx
.cfg
.virtual_root
) {
490 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
492 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
493 html_hidden("url", url
);
496 if (incl_head
&& strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
497 html_hidden("h", ctx
.qry
.head
);
500 html_hidden("id", ctx
.qry
.sha1
);
502 html_hidden("id2", ctx
.qry
.sha2
);
506 html_hidden("qt", ctx
.qry
.grep
);
508 html_hidden("q", ctx
.qry
.search
);
512 char *hc(struct cgit_cmd
*cmd
, const char *page
)
514 return (strcmp(cmd
->name
, page
) ? NULL
: "active");
517 void cgit_print_pageheader(struct cgit_context
*ctx
)
519 struct cgit_cmd
*cmd
= cgit_get_cmd(ctx
);
521 html("<table id='header'>\n");
523 html("<td class='logo' rowspan='2'><a href='");
524 if (ctx
->cfg
.logo_link
)
525 html_attr(ctx
->cfg
.logo_link
);
527 html_attr(cgit_rooturl());
528 html("'><img src='");
529 html_attr(ctx
->cfg
.logo
);
530 html("' alt='cgit logo'/></a></td>\n");
532 html("<td class='main'>");
534 reporevlink(NULL
, ctx
->repo
->name
, NULL
, hc(cmd
, "summary"),
535 ctx
->qry
.head
, NULL
, NULL
);
537 html_txt(ctx
->qry
.page
);
538 html("</td><td class='form'>");
539 html("<form method='get' action=''>\n");
540 add_hidden_formfields(0, 1, ctx
->qry
.page
);
541 html("<select name='h' onchange='this.form.submit();'>\n");
542 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
544 html("<input type='submit' name='' value='switch'/>");
547 html_txt(ctx
->cfg
.root_title
);
548 html("</td></tr>\n");
550 html("<tr><td class='sub'");
552 html(" colspan='2'>");
553 html_txt(ctx
->repo
->desc
);
556 if (ctx
->cfg
.root_desc
)
557 html_txt(ctx
->cfg
.root_desc
);
558 else if (ctx
->cfg
.index_info
)
559 html_include(ctx
->cfg
.index_info
);
561 html("</td></tr></table>\n");
563 html("<table class='tabs'><tr><td>\n");
565 reporevlink(NULL
, "summary", NULL
, hc(cmd
, "summary"),
566 ctx
->qry
.head
, NULL
, NULL
);
567 cgit_refs_link("refs", NULL
, hc(cmd
, "refs"), ctx
->qry
.head
,
568 ctx
->qry
.sha1
, NULL
);
569 cgit_log_link("log", NULL
, hc(cmd
, "log"), ctx
->qry
.head
,
570 NULL
, NULL
, 0, NULL
, NULL
);
571 cgit_tree_link("tree", NULL
, hc(cmd
, "tree"), ctx
->qry
.head
,
572 ctx
->qry
.sha1
, NULL
);
573 cgit_commit_link("commit", NULL
, hc(cmd
, "commit"),
574 ctx
->qry
.head
, ctx
->qry
.sha1
);
575 cgit_diff_link("diff", NULL
, hc(cmd
, "diff"), ctx
->qry
.head
,
576 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
577 if (ctx
->repo
->readme
)
578 reporevlink("about", "about", NULL
,
579 hc(cmd
, "about"), ctx
->qry
.head
, NULL
,
581 html("</td><td class='form'>");
582 html("<form class='right' method='get' action='");
583 if (ctx
->cfg
.virtual_root
)
584 html_attr(cgit_fileurl(ctx
->qry
.repo
, "log",
585 ctx
->qry
.path
, NULL
));
587 add_hidden_formfields(1, 0, "log");
588 html("<select name='qt'>\n");
589 html_option("grep", "log msg", ctx
->qry
.grep
);
590 html_option("author", "author", ctx
->qry
.grep
);
591 html_option("committer", "committer", ctx
->qry
.grep
);
593 html("<input class='txt' type='text' size='10' name='q' value='");
594 html_attr(ctx
->qry
.search
);
596 html("<input type='submit' value='search'/>\n");
599 site_link(NULL
, "index", NULL
, hc(cmd
, "repolist"), NULL
);
600 if (ctx
->cfg
.root_readme
)
601 site_link("about", "about", NULL
, hc(cmd
, "about"), NULL
);
602 html("</td><td class='form'>");
603 html("<form method='get' action='");
604 html_attr(cgit_rooturl());
606 html("<input type='text' name='q' size='10' value='");
607 html_attr(ctx
->qry
.search
);
609 html("<input type='submit' value='search'/>\n");
612 html("</td></tr></table>\n");
613 html("<div class='content'>");
616 void cgit_print_filemode(unsigned short mode
)
620 else if (S_ISLNK(mode
))
622 else if (S_ISGITLINK(mode
))
626 html_fileperm(mode
>> 6);
627 html_fileperm(mode
>> 3);
631 void cgit_print_snapshot_links(const char *repo
, const char *head
,
632 const char *hex
, int snapshots
)
634 const struct cgit_snapshot_format
* f
;
637 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
638 if (!(snapshots
& f
->bit
))
640 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
642 cgit_snapshot_link(filename
, NULL
, NULL
, (char *)head
,
643 (char *)hex
, filename
);