]>
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)
12 const char cgit_doctype
[] =
13 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
14 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16 static char *http_date(time_t t
)
18 static char day
[][4] =
19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
20 static char month
[][4] =
21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
23 struct tm
*tm
= gmtime(&t
);
24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day
[tm
->tm_wday
],
25 tm
->tm_mday
, month
[tm
->tm_mon
], 1900+tm
->tm_year
,
26 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
29 static long ttl_seconds(long ttl
)
32 return 60 * 60 * 24 * 365;
37 void cgit_print_error(char *msg
)
39 html("<div class='error'>");
46 if (ctx
.cfg
.virtual_root
)
47 return fmt("%s/", ctx
.cfg
.virtual_root
);
49 return ctx
.cfg
.script_name
;
52 char *cgit_repourl(const char *reponame
)
54 if (ctx
.cfg
.virtual_root
) {
55 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, reponame
);
57 return fmt("?r=%s", reponame
);
61 char *cgit_fileurl(const char *reponame
, const char *pagename
,
62 const char *filename
, const char *query
)
67 if (ctx
.cfg
.virtual_root
) {
68 tmp
= fmt("%s/%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
69 pagename
, (filename
? filename
:""));
72 tmp
= fmt("?url=%s/%s/%s", reponame
, pagename
,
73 (filename
? filename
: ""));
77 tmp
= fmt("%s%s%s", tmp
, delim
, query
);
81 char *cgit_pageurl(const char *reponame
, const char *pagename
,
84 return cgit_fileurl(reponame
,pagename
,0,query
);
87 const char *cgit_repobasename(const char *reponame
)
89 /* I assume we don't need to store more than one repo basename */
90 static char rvbuf
[1024];
93 strncpy(rvbuf
,reponame
,sizeof(rvbuf
));
94 if(rvbuf
[sizeof(rvbuf
)-1])
95 die("cgit_repobasename: truncated repository name '%s'", reponame
);
97 /* strip trailing slashes */
98 while(p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
99 /* strip trailing .git */
100 if(p
>=3 && !strncmp(&rvbuf
[p
-3],".git",4)) {
101 p
-= 3; rvbuf
[p
--] = 0;
103 /* strip more trailing slashes if any */
104 while( p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
105 /* find last slash in the remaining string */
106 rv
= strrchr(rvbuf
,'/');
114 if (!ctx
.cfg
.virtual_root
)
115 return ctx
.cfg
.script_name
;
116 else if (ctx
.qry
.page
)
117 return fmt("%s/%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
, ctx
.qry
.page
);
118 else if (ctx
.qry
.repo
)
119 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
);
121 return fmt("%s/", ctx
.cfg
.virtual_root
);
124 static char *repolink(char *title
, char *class, char *page
, char *head
,
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] != '/')
145 html_attr(ctx
.repo
->url
);
146 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
155 html(ctx
.cfg
.script_name
);
157 html_attr(ctx
.repo
->url
);
158 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
168 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
174 return fmt("%s", delim
);
177 static void reporevlink(char *page
, char *name
, char *title
, char *class,
178 char *head
, char *rev
, char *path
)
182 delim
= repolink(title
, class, page
, head
, path
);
183 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
193 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
194 char *rev
, char *path
)
196 reporevlink("tree", name
, title
, class, head
, rev
, path
);
199 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
200 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
)
204 delim
= repolink(title
, class, "log", head
, path
);
205 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
211 if (grep
&& pattern
) {
230 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
233 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
234 name
[ctx
.cfg
.max_msg_len
] = '\0';
235 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
236 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
237 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
239 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
242 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
243 char *rev
, char *path
)
245 reporevlink("refs", name
, title
, class, head
, rev
, path
);
248 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
249 char *rev
, char *archivename
)
251 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
254 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
255 char *new_rev
, char *old_rev
, char *path
)
259 delim
= repolink(title
, class, "diff", head
, path
);
260 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
276 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
279 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
282 void cgit_object_link(struct object
*obj
)
284 char *page
, *arg
, *url
;
286 if (obj
->type
== OBJ_COMMIT
) {
287 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj
->sha1
)), NULL
, NULL
,
288 ctx
.qry
.head
, sha1_to_hex(obj
->sha1
));
290 } else if (obj
->type
== OBJ_TREE
) {
293 } else if (obj
->type
== OBJ_TAG
) {
301 url
= cgit_pageurl(ctx
.qry
.repo
, page
,
302 fmt("%s=%s", arg
, sha1_to_hex(obj
->sha1
)));
303 html_link_open(url
, NULL
, NULL
);
304 htmlf("%s %s", typename(obj
->type
),
305 sha1_to_hex(obj
->sha1
));
309 void cgit_print_date(time_t secs
, char *format
)
316 time
= gmtime(&secs
);
317 strftime(buf
, sizeof(buf
)-1, format
, time
);
321 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
330 if (secs
> max_relative
&& max_relative
>= 0) {
331 cgit_print_date(t
, format
);
335 if (secs
< TM_HOUR
* 2) {
336 htmlf("<span class='age-mins'>%.0f min.</span>",
337 secs
* 1.0 / TM_MIN
);
340 if (secs
< TM_DAY
* 2) {
341 htmlf("<span class='age-hours'>%.0f hours</span>",
342 secs
* 1.0 / TM_HOUR
);
345 if (secs
< TM_WEEK
* 2) {
346 htmlf("<span class='age-days'>%.0f days</span>",
347 secs
* 1.0 / TM_DAY
);
350 if (secs
< TM_MONTH
* 2) {
351 htmlf("<span class='age-weeks'>%.0f weeks</span>",
352 secs
* 1.0 / TM_WEEK
);
355 if (secs
< TM_YEAR
* 2) {
356 htmlf("<span class='age-months'>%.0f months</span>",
357 secs
* 1.0 / TM_MONTH
);
360 htmlf("<span class='age-years'>%.0f years</span>",
361 secs
* 1.0 / TM_YEAR
);
364 void cgit_print_docstart(char *title
, struct cacheitem
*item
)
366 html("Content-Type: text/html; charset=" PAGE_ENCODING
"\n");
367 htmlf("Last-Modified: %s\n", http_date(item
->st
.st_mtime
));
368 htmlf("Expires: %s\n", http_date(item
->st
.st_mtime
+
369 ttl_seconds(item
->ttl
)));
372 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
377 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
378 if (ctx
.cfg
.robots
&& *ctx
.cfg
.robots
)
379 htmlf("<meta name='robots' content='%s'/>\n", ctx
.cfg
.robots
);
380 html("<link rel='stylesheet' type='text/css' href='");
381 html_attr(ctx
.cfg
.css
);
387 void cgit_print_docend()
389 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
392 int print_branch_option(const char *refname
, const unsigned char *sha1
,
393 int flags
, void *cb_data
)
395 char *name
= (char *)refname
;
396 html_option(name
, name
, ctx
.qry
.head
);
400 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
401 int flags
, void *cb_data
)
404 struct taginfo
*info
;
407 unsigned char fileid
[20];
408 int *header
= (int *)cb_data
;
410 if (prefixcmp(refname
, "refs/archives"))
412 strncpy(buf
, refname
+14, sizeof(buf
));
413 obj
= parse_object(sha1
);
416 if (obj
->type
== OBJ_TAG
) {
417 tag
= lookup_tag(sha1
);
418 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
420 hashcpy(fileid
, tag
->tagged
->sha1
);
421 } else if (obj
->type
!= OBJ_BLOB
) {
424 hashcpy(fileid
, sha1
);
427 html("<h1>download</h1>\n");
430 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
431 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
433 html_link_open(url
, NULL
, "menu");
434 html_txt(strlpart(buf
, 20));
439 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
443 if (!ctx
.cfg
.virtual_root
) {
444 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
446 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
447 html_hidden("url", url
);
450 if (incl_head
&& strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
451 html_hidden("h", ctx
.qry
.head
);
454 html_hidden("id", ctx
.qry
.sha1
);
456 html_hidden("id2", ctx
.qry
.sha2
);
460 html_hidden("qt", ctx
.qry
.grep
);
462 html_hidden("q", ctx
.qry
.search
);
466 void cgit_print_pageheader(char *title
, int show_search
)
468 static const char *default_info
= "This is cgit, a fast webinterface for git repositories";
472 html("<table id='layout' summary=''>\n");
473 html("<tr><td id='sidebar'>\n");
474 html("<table class='sidebar' cellspacing='0' summary=''>\n");
475 html("<tr><td class='sidebar'>\n<a href='");
476 html_attr(cgit_rooturl());
477 htmlf("'><img src='%s' alt='cgit'/></a>\n",
479 html("</td></tr>\n<tr><td class='sidebar'>\n");
481 html("<h1 class='first'>");
482 html_txt(strrpart(ctx
.repo
->name
, 20));
484 html_txt(ctx
.repo
->desc
);
485 if (ctx
.repo
->owner
) {
486 html("<h1>owner</h1>\n");
487 html_txt(ctx
.repo
->owner
);
489 html("<h1>navigate</h1>\n");
490 reporevlink(NULL
, "summary", NULL
, "menu", ctx
.qry
.head
,
492 cgit_log_link("log", NULL
, "menu", ctx
.qry
.head
, NULL
, NULL
,
494 cgit_tree_link("tree", NULL
, "menu", ctx
.qry
.head
,
496 cgit_commit_link("commit", NULL
, "menu", ctx
.qry
.head
,
498 cgit_diff_link("diff", NULL
, "menu", ctx
.qry
.head
,
499 ctx
.qry
.sha1
, ctx
.qry
.sha2
, NULL
);
500 cgit_patch_link("patch", NULL
, "menu", ctx
.qry
.head
,
503 for_each_ref(print_archive_ref
, &header
);
505 if (ctx
.repo
->clone_url
|| ctx
.cfg
.clone_prefix
) {
506 html("<h1>clone</h1>\n");
507 if (ctx
.repo
->clone_url
)
508 url
= ctx
.repo
->clone_url
;
510 url
= fmt("%s%s", ctx
.cfg
.clone_prefix
,
512 html("<a class='menu' href='");
517 html_txt(strrpart(url
, 20));
521 html("<h1>branch</h1>\n");
522 html("<form method='get' action=''>\n");
523 add_hidden_formfields(0, 1, ctx
.qry
.page
);
524 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
525 html("<select name='h' onchange='this.form.submit();'>\n");
526 for_each_branch_ref(print_branch_option
, ctx
.qry
.head
);
528 // html("</td><td>");
529 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
530 // html("</td></tr></table>");
533 html("<h1>search</h1>\n");
534 html("<form method='get' action='");
535 if (ctx
.cfg
.virtual_root
)
536 html_attr(cgit_fileurl(ctx
.qry
.repo
, "log",
537 ctx
.qry
.path
, NULL
));
539 add_hidden_formfields(1, 0, "log");
540 html("<select name='qt'>\n");
541 html_option("grep", "log msg", ctx
.qry
.grep
);
542 html_option("author", "author", ctx
.qry
.grep
);
543 html_option("committer", "committer", ctx
.qry
.grep
);
545 html("<input class='txt' type='text' name='q' value='");
546 html_attr(ctx
.qry
.search
);
550 if (!ctx
.cfg
.index_info
|| html_include(ctx
.cfg
.index_info
))
554 html("</td></tr></table></td>\n");
556 html("<td id='content'>\n");
560 void cgit_print_snapshot_start(const char *mimetype
, const char *filename
,
561 struct cacheitem
*item
)
563 htmlf("Content-Type: %s\n", mimetype
);
564 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename
);
565 htmlf("Last-Modified: %s\n", http_date(item
->st
.st_mtime
));
566 htmlf("Expires: %s\n", http_date(item
->st
.st_mtime
+
567 ttl_seconds(item
->ttl
)));
571 void cgit_print_filemode(unsigned short mode
)
575 else if (S_ISLNK(mode
))
577 else if (S_ISGITLINK(mode
))
581 html_fileperm(mode
>> 6);
582 html_fileperm(mode
>> 3);