]>
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 void cgit_print_error(char *msg
)
31 html("<div class='error'>");
38 if (ctx
.cfg
.virtual_root
)
39 return fmt("%s/", ctx
.cfg
.virtual_root
);
41 return ctx
.cfg
.script_name
;
44 char *cgit_repourl(const char *reponame
)
46 if (ctx
.cfg
.virtual_root
) {
47 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, reponame
);
49 return fmt("?r=%s", reponame
);
53 char *cgit_fileurl(const char *reponame
, const char *pagename
,
54 const char *filename
, const char *query
)
59 if (ctx
.cfg
.virtual_root
) {
60 tmp
= fmt("%s/%s/%s/%s", ctx
.cfg
.virtual_root
, reponame
,
61 pagename
, (filename
? filename
:""));
64 tmp
= fmt("?url=%s/%s/%s", reponame
, pagename
,
65 (filename
? filename
: ""));
69 tmp
= fmt("%s%s%s", tmp
, delim
, query
);
73 char *cgit_pageurl(const char *reponame
, const char *pagename
,
76 return cgit_fileurl(reponame
,pagename
,0,query
);
79 const char *cgit_repobasename(const char *reponame
)
81 /* I assume we don't need to store more than one repo basename */
82 static char rvbuf
[1024];
85 strncpy(rvbuf
,reponame
,sizeof(rvbuf
));
86 if(rvbuf
[sizeof(rvbuf
)-1])
87 die("cgit_repobasename: truncated repository name '%s'", reponame
);
89 /* strip trailing slashes */
90 while(p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
91 /* strip trailing .git */
92 if(p
>=3 && !strncmp(&rvbuf
[p
-3],".git",4)) {
93 p
-= 3; rvbuf
[p
--] = 0;
95 /* strip more trailing slashes if any */
96 while( p
&& rvbuf
[p
]=='/') rvbuf
[p
--]=0;
97 /* find last slash in the remaining string */
98 rv
= strrchr(rvbuf
,'/');
106 if (!ctx
.cfg
.virtual_root
)
107 return ctx
.cfg
.script_name
;
108 else if (ctx
.qry
.page
)
109 return fmt("%s/%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
, ctx
.qry
.page
);
110 else if (ctx
.qry
.repo
)
111 return fmt("%s/%s/", ctx
.cfg
.virtual_root
, ctx
.qry
.repo
);
113 return fmt("%s/", ctx
.cfg
.virtual_root
);
116 static char *repolink(char *title
, char *class, char *page
, char *head
,
133 if (ctx
.cfg
.virtual_root
) {
134 html_attr(ctx
.cfg
.virtual_root
);
135 if (ctx
.cfg
.virtual_root
[strlen(ctx
.cfg
.virtual_root
) - 1] != '/')
137 html_attr(ctx
.repo
->url
);
138 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
147 html(ctx
.cfg
.script_name
);
149 html_attr(ctx
.repo
->url
);
150 if (ctx
.repo
->url
[strlen(ctx
.repo
->url
) - 1] != '/')
160 if (head
&& strcmp(head
, ctx
.repo
->defbranch
)) {
166 return fmt("%s", delim
);
169 static void reporevlink(char *page
, char *name
, char *title
, char *class,
170 char *head
, char *rev
, char *path
)
174 delim
= repolink(title
, class, page
, head
, path
);
175 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
185 void cgit_tree_link(char *name
, char *title
, char *class, char *head
,
186 char *rev
, char *path
)
188 reporevlink("tree", name
, title
, class, head
, rev
, path
);
191 void cgit_log_link(char *name
, char *title
, char *class, char *head
,
192 char *rev
, char *path
, int ofs
, char *grep
, char *pattern
)
196 delim
= repolink(title
, class, "log", head
, path
);
197 if (rev
&& strcmp(rev
, ctx
.qry
.head
)) {
203 if (grep
&& pattern
) {
222 void cgit_commit_link(char *name
, char *title
, char *class, char *head
,
225 if (strlen(name
) > ctx
.cfg
.max_msg_len
&& ctx
.cfg
.max_msg_len
>= 15) {
226 name
[ctx
.cfg
.max_msg_len
] = '\0';
227 name
[ctx
.cfg
.max_msg_len
- 1] = '.';
228 name
[ctx
.cfg
.max_msg_len
- 2] = '.';
229 name
[ctx
.cfg
.max_msg_len
- 3] = '.';
231 reporevlink("commit", name
, title
, class, head
, rev
, NULL
);
234 void cgit_refs_link(char *name
, char *title
, char *class, char *head
,
235 char *rev
, char *path
)
237 reporevlink("refs", name
, title
, class, head
, rev
, path
);
240 void cgit_snapshot_link(char *name
, char *title
, char *class, char *head
,
241 char *rev
, char *archivename
)
243 reporevlink("snapshot", name
, title
, class, head
, rev
, archivename
);
246 void cgit_diff_link(char *name
, char *title
, char *class, char *head
,
247 char *new_rev
, char *old_rev
, char *path
)
251 delim
= repolink(title
, class, "diff", head
, path
);
252 if (new_rev
&& strcmp(new_rev
, ctx
.qry
.head
)) {
268 void cgit_patch_link(char *name
, char *title
, char *class, char *head
,
271 reporevlink("patch", name
, title
, class, head
, rev
, NULL
);
274 void cgit_object_link(struct object
*obj
)
276 char *page
, *arg
, *url
;
278 if (obj
->type
== OBJ_COMMIT
) {
279 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj
->sha1
)), NULL
, NULL
,
280 ctx
.qry
.head
, sha1_to_hex(obj
->sha1
));
282 } else if (obj
->type
== OBJ_TREE
) {
285 } else if (obj
->type
== OBJ_TAG
) {
293 url
= cgit_pageurl(ctx
.qry
.repo
, page
,
294 fmt("%s=%s", arg
, sha1_to_hex(obj
->sha1
)));
295 html_link_open(url
, NULL
, NULL
);
296 htmlf("%s %s", typename(obj
->type
),
297 sha1_to_hex(obj
->sha1
));
301 void cgit_print_date(time_t secs
, char *format
)
308 time
= gmtime(&secs
);
309 strftime(buf
, sizeof(buf
)-1, format
, time
);
313 void cgit_print_age(time_t t
, time_t max_relative
, char *format
)
322 if (secs
> max_relative
&& max_relative
>= 0) {
323 cgit_print_date(t
, format
);
327 if (secs
< TM_HOUR
* 2) {
328 htmlf("<span class='age-mins'>%.0f min.</span>",
329 secs
* 1.0 / TM_MIN
);
332 if (secs
< TM_DAY
* 2) {
333 htmlf("<span class='age-hours'>%.0f hours</span>",
334 secs
* 1.0 / TM_HOUR
);
337 if (secs
< TM_WEEK
* 2) {
338 htmlf("<span class='age-days'>%.0f days</span>",
339 secs
* 1.0 / TM_DAY
);
342 if (secs
< TM_MONTH
* 2) {
343 htmlf("<span class='age-weeks'>%.0f weeks</span>",
344 secs
* 1.0 / TM_WEEK
);
347 if (secs
< TM_YEAR
* 2) {
348 htmlf("<span class='age-months'>%.0f months</span>",
349 secs
* 1.0 / TM_MONTH
);
352 htmlf("<span class='age-years'>%.0f years</span>",
353 secs
* 1.0 / TM_YEAR
);
356 void cgit_print_http_headers(struct cgit_context
*ctx
)
358 if (ctx
->page
.mimetype
&& ctx
->page
.charset
)
359 htmlf("Content-Type: %s; charset=%s\n", ctx
->page
.mimetype
,
361 else if (ctx
->page
.mimetype
)
362 htmlf("Content-Type: %s\n", ctx
->page
.mimetype
);
363 if (ctx
->page
.filename
)
364 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
366 htmlf("Last-Modified: %s\n", http_date(ctx
->page
.modified
));
367 htmlf("Expires: %s\n", http_date(ctx
->page
.expires
));
371 void cgit_print_docstart(struct cgit_context
*ctx
)
374 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
377 html_txt(ctx
->page
.title
);
379 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version
);
380 if (ctx
->cfg
.robots
&& *ctx
->cfg
.robots
)
381 htmlf("<meta name='robots' content='%s'/>\n", ctx
->cfg
.robots
);
382 html("<link rel='stylesheet' type='text/css' href='");
383 html_attr(ctx
->cfg
.css
);
389 void cgit_print_docend()
391 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
394 int print_branch_option(const char *refname
, const unsigned char *sha1
,
395 int flags
, void *cb_data
)
397 char *name
= (char *)refname
;
398 html_option(name
, name
, ctx
.qry
.head
);
402 int print_archive_ref(const char *refname
, const unsigned char *sha1
,
403 int flags
, void *cb_data
)
406 struct taginfo
*info
;
409 unsigned char fileid
[20];
410 int *header
= (int *)cb_data
;
412 if (prefixcmp(refname
, "refs/archives"))
414 strncpy(buf
, refname
+14, sizeof(buf
));
415 obj
= parse_object(sha1
);
418 if (obj
->type
== OBJ_TAG
) {
419 tag
= lookup_tag(sha1
);
420 if (!tag
|| parse_tag(tag
) || !(info
= cgit_parse_tag(tag
)))
422 hashcpy(fileid
, tag
->tagged
->sha1
);
423 } else if (obj
->type
!= OBJ_BLOB
) {
426 hashcpy(fileid
, sha1
);
429 html("<h1>download</h1>\n");
432 url
= cgit_pageurl(ctx
.qry
.repo
, "blob",
433 fmt("id=%s&path=%s", sha1_to_hex(fileid
),
435 html_link_open(url
, NULL
, "menu");
436 html_txt(strlpart(buf
, 20));
441 void add_hidden_formfields(int incl_head
, int incl_search
, char *page
)
445 if (!ctx
.cfg
.virtual_root
) {
446 url
= fmt("%s/%s", ctx
.qry
.repo
, page
);
448 url
= fmt("%s/%s", url
, ctx
.qry
.path
);
449 html_hidden("url", url
);
452 if (incl_head
&& strcmp(ctx
.qry
.head
, ctx
.repo
->defbranch
))
453 html_hidden("h", ctx
.qry
.head
);
456 html_hidden("id", ctx
.qry
.sha1
);
458 html_hidden("id2", ctx
.qry
.sha2
);
462 html_hidden("qt", ctx
.qry
.grep
);
464 html_hidden("q", ctx
.qry
.search
);
468 void cgit_print_pageheader(struct cgit_context
*ctx
)
470 static const char *default_info
= "This is cgit, a fast webinterface for git repositories";
474 html("<table id='layout' summary=''>\n");
475 html("<tr><td id='sidebar'>\n");
476 html("<table class='sidebar' cellspacing='0' summary=''>\n");
477 html("<tr><td class='sidebar'>\n<a href='");
478 html_attr(cgit_rooturl());
479 htmlf("'><img src='%s' alt='cgit'/></a>\n",
481 html("</td></tr>\n<tr><td class='sidebar'>\n");
483 html("<h1 class='first'>");
484 html_txt(strrpart(ctx
->repo
->name
, 20));
486 html_txt(ctx
->repo
->desc
);
487 if (ctx
->repo
->owner
) {
488 html("<h1>owner</h1>\n");
489 html_txt(ctx
->repo
->owner
);
491 html("<h1>navigate</h1>\n");
492 reporevlink(NULL
, "summary", NULL
, "menu", ctx
->qry
.head
,
494 cgit_log_link("log", NULL
, "menu", ctx
->qry
.head
, NULL
, NULL
,
496 cgit_tree_link("tree", NULL
, "menu", ctx
->qry
.head
,
497 ctx
->qry
.sha1
, NULL
);
498 cgit_commit_link("commit", NULL
, "menu", ctx
->qry
.head
,
500 cgit_diff_link("diff", NULL
, "menu", ctx
->qry
.head
,
501 ctx
->qry
.sha1
, ctx
->qry
.sha2
, NULL
);
502 cgit_patch_link("patch", NULL
, "menu", ctx
->qry
.head
,
505 for_each_ref(print_archive_ref
, &header
);
507 if (ctx
->repo
->clone_url
|| ctx
->cfg
.clone_prefix
) {
508 html("<h1>clone</h1>\n");
509 if (ctx
->repo
->clone_url
)
510 url
= ctx
->repo
->clone_url
;
512 url
= fmt("%s%s", ctx
->cfg
.clone_prefix
,
514 html("<a class='menu' href='");
519 html_txt(strrpart(url
, 20));
523 html("<h1>branch</h1>\n");
524 html("<form method='get' action=''>\n");
525 add_hidden_formfields(0, 1, ctx
->qry
.page
);
526 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
527 html("<select name='h' onchange='this.form.submit();'>\n");
528 for_each_branch_ref(print_branch_option
, ctx
->qry
.head
);
530 // html("</td><td>");
531 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
532 // html("</td></tr></table>");
535 html("<h1>search</h1>\n");
536 html("<form method='get' action='");
537 if (ctx
->cfg
.virtual_root
)
538 html_attr(cgit_fileurl(ctx
->qry
.repo
, "log",
539 ctx
->qry
.path
, NULL
));
541 add_hidden_formfields(1, 0, "log");
542 html("<select name='qt'>\n");
543 html_option("grep", "log msg", ctx
->qry
.grep
);
544 html_option("author", "author", ctx
->qry
.grep
);
545 html_option("committer", "committer", ctx
->qry
.grep
);
547 html("<input class='txt' type='text' name='q' value='");
548 html_attr(ctx
->qry
.search
);
552 if (!ctx
->cfg
.index_info
|| html_include(ctx
->cfg
.index_info
))
556 html("</td></tr></table></td>\n");
558 html("<td id='content'>\n");
561 void cgit_print_filemode(unsigned short mode
)
565 else if (S_ISLNK(mode
))
567 else if (S_ISGITLINK(mode
))
571 html_fileperm(mode
>> 6);
572 html_fileperm(mode
>> 3);
576 void cgit_print_snapshot_links(const char *repo
, const char *head
,
577 const char *hex
, int snapshots
)
579 const struct cgit_snapshot_format
* f
;
582 for (f
= cgit_snapshot_formats
; f
->suffix
; f
++) {
583 if (!(snapshots
& f
->bit
))
585 filename
= fmt("%s-%s%s", cgit_repobasename(repo
), hex
,
587 cgit_snapshot_link(filename
, NULL
, NULL
, (char *)head
,
588 (char *)hex
, filename
);