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)
11 const char cgit_doctype[] =
12 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
13 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
15 static char *http_date(time_t t)
17 static char day[][4] =
18 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
19 static char month[][4] =
20 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
21 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
22 struct tm *tm = gmtime(&t);
23 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
24 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
25 tm->tm_hour, tm->tm_min, tm->tm_sec);
28 static long ttl_seconds(long ttl)
31 return 60 * 60 * 24 * 365;
36 void cgit_print_error(char *msg)
38 html("<div class='error'>");
45 if (cgit_virtual_root)
46 return fmt("%s/", cgit_virtual_root);
48 return cgit_script_name;
51 char *cgit_repourl(const char *reponame)
53 if (cgit_virtual_root) {
54 return fmt("%s/%s/", cgit_virtual_root, reponame);
56 return fmt("?r=%s", reponame);
60 char *cgit_fileurl(const char *reponame, const char *pagename,
61 const char *filename, const char *query)
63 if (cgit_virtual_root) {
65 return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame,
66 pagename, filename?filename:"", query);
68 return fmt("%s/%s/%s/", cgit_virtual_root, reponame,
72 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
74 return fmt("?r=%s&p=%s", reponame, pagename);
78 char *cgit_pageurl(const char *reponame, const char *pagename,
81 return cgit_fileurl(reponame,pagename,0,query);
84 const char *cgit_repobasename(const char *reponame)
86 /* I assume we don't need to store more than one repo basename */
87 static char rvbuf[1024];
90 strncpy(rvbuf,reponame,sizeof(rvbuf));
91 if(rvbuf[sizeof(rvbuf)-1])
92 die("cgit_repobasename: truncated repository name '%s'", reponame);
94 /* strip trailing slashes */
95 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
96 /* strip trailing .git */
97 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
98 p -= 3; rvbuf[p--] = 0;
100 /* strip more trailing slashes if any */
101 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
102 /* find last slash in the remaining string */
103 rv = strrchr(rvbuf,'/');
111 if (!cgit_virtual_root)
112 return cgit_script_name;
113 else if (cgit_query_page)
114 return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page);
115 else if (cgit_query_repo)
116 return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo);
118 return fmt("%s/", cgit_virtual_root);
121 static char *repolink(char *title, char *class, char *page, char *head,
138 if (cgit_virtual_root) {
139 html_attr(cgit_virtual_root);
140 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/')
142 html_attr(cgit_repo->url);
143 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
152 html(cgit_script_name);
154 html_attr(cgit_repo->url);
155 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
165 if (head && strcmp(head, cgit_repo->defbranch)) {
171 return fmt("%s", delim);
174 static void reporevlink(char *page, char *name, char *title, char *class,
175 char *head, char *rev, char *path)
179 delim = repolink(title, class, page, head, path);
180 if (rev && strcmp(rev, cgit_query_head)) {
190 void cgit_tree_link(char *name, char *title, char *class, char *head,
191 char *rev, char *path)
193 reporevlink("tree", name, title, class, head, rev, path);
196 void cgit_log_link(char *name, char *title, char *class, char *head,
197 char *rev, char *path, int ofs)
201 delim = repolink(title, class, "log", head, path);
202 if (rev && strcmp(rev, cgit_query_head)) {
218 void cgit_commit_link(char *name, char *title, char *class, char *head,
221 if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) {
222 name[cgit_max_msg_len] = '\0';
223 name[cgit_max_msg_len - 1] = '.';
224 name[cgit_max_msg_len - 2] = '.';
225 name[cgit_max_msg_len - 3] = '.';
227 reporevlink("commit", name, title, class, head, rev, NULL);
230 void cgit_refs_link(char *name, char *title, char *class, char *head,
231 char *rev, char *path)
233 reporevlink("refs", name, title, class, head, rev, path);
236 void cgit_snapshot_link(char *name, char *title, char *class, char *head,
237 char *rev, char *archivename)
239 reporevlink("snapshot", name, title, class, head, rev, archivename);
242 void cgit_diff_link(char *name, char *title, char *class, char *head,
243 char *new_rev, char *old_rev, char *path)
247 delim = repolink(title, class, "diff", head, path);
248 if (new_rev && strcmp(new_rev, cgit_query_head)) {
264 void cgit_object_link(struct object *obj)
266 char *page, *arg, *url;
268 if (obj->type == OBJ_COMMIT) {
269 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
270 cgit_query_head, sha1_to_hex(obj->sha1));
272 } else if (obj->type == OBJ_TREE) {
275 } else if (obj->type == OBJ_TAG) {
283 url = cgit_pageurl(cgit_query_repo, page,
284 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
285 html_link_open(url, NULL, NULL);
286 htmlf("%s %s", typename(obj->type),
287 sha1_to_hex(obj->sha1));
291 void cgit_print_date(time_t secs, char *format)
296 time = gmtime(&secs);
297 strftime(buf, sizeof(buf)-1, format, time);
301 void cgit_print_age(time_t t, time_t max_relative, char *format)
308 if (secs > max_relative && max_relative >= 0) {
309 cgit_print_date(t, format);
313 if (secs < TM_HOUR * 2) {
314 htmlf("<span class='age-mins'>%.0f min.</span>",
315 secs * 1.0 / TM_MIN);
318 if (secs < TM_DAY * 2) {
319 htmlf("<span class='age-hours'>%.0f hours</span>",
320 secs * 1.0 / TM_HOUR);
323 if (secs < TM_WEEK * 2) {
324 htmlf("<span class='age-days'>%.0f days</span>",
325 secs * 1.0 / TM_DAY);
328 if (secs < TM_MONTH * 2) {
329 htmlf("<span class='age-weeks'>%.0f weeks</span>",
330 secs * 1.0 / TM_WEEK);
333 if (secs < TM_YEAR * 2) {
334 htmlf("<span class='age-months'>%.0f months</span>",
335 secs * 1.0 / TM_MONTH);
338 htmlf("<span class='age-years'>%.0f years</span>",
339 secs * 1.0 / TM_YEAR);
342 void cgit_print_docstart(char *title, struct cacheitem *item)
344 html("Content-Type: text/html; charset=utf-8\n");
345 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
346 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
347 ttl_seconds(item->ttl)));
355 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
356 html("<link rel='stylesheet' type='text/css' href='");
363 void cgit_print_docend()
365 html("</td>\n</tr>\n<table>\n</body>\n</html>\n");
368 int print_branch_option(const char *refname, const unsigned char *sha1,
369 int flags, void *cb_data)
371 char *name = (char *)refname;
372 html_option(name, name, cgit_query_head);
376 int print_archive_ref(const char *refname, const unsigned char *sha1,
377 int flags, void *cb_data)
380 struct taginfo *info;
383 unsigned char fileid[20];
384 int *header = (int *)cb_data;
386 if (prefixcmp(refname, "refs/archives"))
388 strncpy(buf, refname+14, sizeof(buf));
389 obj = parse_object(sha1);
392 if (obj->type == OBJ_TAG) {
393 tag = lookup_tag(sha1);
394 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
396 hashcpy(fileid, tag->tagged->sha1);
397 } else if (obj->type != OBJ_BLOB) {
400 hashcpy(fileid, sha1);
403 html("<p><h1>download</h1>");
406 url = cgit_pageurl(cgit_query_repo, "blob",
407 fmt("id=%s&path=%s", sha1_to_hex(fileid),
409 html_link_open(url, NULL, "menu");
410 html_txt(strlpart(buf, 20));
415 void add_hidden_formfields(int incl_head, int incl_search)
417 if (!cgit_virtual_root) {
419 html_hidden("r", cgit_query_repo);
421 html_hidden("p", cgit_query_page);
424 if (incl_head && strcmp(cgit_query_head, cgit_repo->defbranch))
425 html_hidden("h", cgit_query_head);
428 html_hidden("id", cgit_query_sha1);
430 html_hidden("id2", cgit_query_sha2);
434 html_hidden("qt", cgit_query_grep);
435 if (cgit_query_search)
436 html_hidden("q", cgit_query_search);
440 void cgit_print_pageheader(char *title, int show_search)
442 static const char *default_info = "This is cgit, a fast webinterface for git repositories";
445 html("<div id='sidebar'>\n");
447 html_attr(cgit_rooturl());
448 htmlf("'><div id='logo'><img src='%s' alt='cgit'/></div></a>\n",
450 html("<div class='infobox'>");
451 if (cgit_query_repo) {
453 html_txt(strrpart(cgit_repo->name, 20));
455 html_txt(cgit_repo->desc);
456 if (cgit_repo->owner) {
457 html("<p>\n<h1>owner</h1>\n");
458 html_txt(cgit_repo->owner);
460 html("<p>\n<h1>navigate</h1>\n");
461 reporevlink(NULL, "summary", NULL, "menu", cgit_query_head,
463 cgit_log_link("log", NULL, "menu", cgit_query_head,
464 cgit_query_sha1, cgit_query_path, 0);
465 cgit_tree_link("tree", NULL, "menu", cgit_query_head,
466 cgit_query_sha1, NULL);
467 cgit_commit_link("commit", NULL, "menu", cgit_query_head,
469 cgit_diff_link("diff", NULL, "menu", cgit_query_head,
470 cgit_query_sha1, cgit_query_sha2,
473 for_each_ref(print_archive_ref, &header);
475 html("<p>\n<h1>branch</h1>\n");
476 html("<form method='get' action=''>\n");
477 add_hidden_formfields(0, 1);
478 html("<select name='h' onchange='this.form.submit();'>\n");
479 for_each_branch_ref(print_branch_option, cgit_query_head);
483 html("<p>\n<h1>search</h1>\n");
484 html("<form method='get' action='");
485 html_attr(cgit_pageurl(cgit_query_repo, "log", NULL));
487 add_hidden_formfields(1, 0);
488 html("<select name='qt'>\n");
489 html_option("grep", "log msg", cgit_query_grep);
490 html_option("author", "author", cgit_query_grep);
491 html_option("committer", "committer", cgit_query_grep);
493 html("<input class='txt' type='text' name='q' value='");
494 html_attr(cgit_query_search);
498 if (!cgit_index_info || html_include(cgit_index_info))
504 html("</div>\n<table class='grid'><tr><td id='content'>\n");
508 void cgit_print_snapshot_start(const char *mimetype, const char *filename,
509 struct cacheitem *item)
511 htmlf("Content-Type: %s\n", mimetype);
512 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
513 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
514 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
515 ttl_seconds(item->ttl)));