]> git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
a03661ac987d72d799e8e5cdfb94a19ac8864f8b
[cgit.git] / ui-shared.c
1 /* ui-shared.c: common web output functions
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10
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";
14
15 static char *http_date(time_t t)
16 {
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);
26 }
27
28 static long ttl_seconds(long ttl)
29 {
30 if (ttl<0)
31 return 60 * 60 * 24 * 365;
32 else
33 return ttl * 60;
34 }
35
36 void cgit_print_error(char *msg)
37 {
38 html("<div class='error'>");
39 html_txt(msg);
40 html("</div>\n");
41 }
42
43 char *cgit_rooturl()
44 {
45 if (cgit_virtual_root)
46 return fmt("%s/", cgit_virtual_root);
47 else
48 return cgit_script_name;
49 }
50
51 char *cgit_repourl(const char *reponame)
52 {
53 if (cgit_virtual_root) {
54 return fmt("%s/%s/", cgit_virtual_root, reponame);
55 } else {
56 return fmt("?r=%s", reponame);
57 }
58 }
59
60 char *cgit_fileurl(const char *reponame, const char *pagename,
61 const char *filename, const char *query)
62 {
63 if (cgit_virtual_root) {
64 if (query)
65 return fmt("%s/%s/%s/%s?%s", cgit_virtual_root, reponame,
66 pagename, filename?filename:"", query);
67 else
68 return fmt("%s/%s/%s/", cgit_virtual_root, reponame,
69 pagename);
70 } else {
71 if (query)
72 return fmt("?r=%s&amp;p=%s&amp;%s", reponame, pagename, query);
73 else
74 return fmt("?r=%s&amp;p=%s", reponame, pagename);
75 }
76 }
77
78 char *cgit_pageurl(const char *reponame, const char *pagename,
79 const char *query)
80 {
81 return cgit_fileurl(reponame,pagename,0,query);
82 }
83
84 const char *cgit_repobasename(const char *reponame)
85 {
86 /* I assume we don't need to store more than one repo basename */
87 static char rvbuf[1024];
88 int p;
89 const char *rv;
90 strncpy(rvbuf,reponame,sizeof(rvbuf));
91 if(rvbuf[sizeof(rvbuf)-1])
92 die("cgit_repobasename: truncated repository name '%s'", reponame);
93 p = strlen(rvbuf)-1;
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;
99 }
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,'/');
104 if(rv)
105 return ++rv;
106 return rvbuf;
107 }
108
109 char *cgit_currurl()
110 {
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);
117 else
118 return fmt("%s/", cgit_virtual_root);
119 }
120
121 static char *repolink(char *title, char *class, char *page, char *head,
122 char *path)
123 {
124 char *delim = "?";
125
126 html("<a");
127 if (title) {
128 html(" title='");
129 html_attr(title);
130 html("'");
131 }
132 if (class) {
133 html(" class='");
134 html_attr(class);
135 html("'");
136 }
137 html(" href='");
138 if (cgit_virtual_root) {
139 html_attr(cgit_virtual_root);
140 if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/')
141 html("/");
142 html_attr(cgit_repo->url);
143 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
144 html("/");
145 if (page) {
146 html(page);
147 html("/");
148 if (path)
149 html_attr(path);
150 }
151 } else {
152 html(cgit_script_name);
153 html("?url=");
154 html_attr(cgit_repo->url);
155 if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/')
156 html("/");
157 if (page) {
158 html(page);
159 html("/");
160 if (path)
161 html_attr(path);
162 }
163 delim = "&amp;";
164 }
165 if (head && strcmp(head, cgit_repo->defbranch)) {
166 html(delim);
167 html("h=");
168 html_attr(head);
169 delim = "&amp;";
170 }
171 return fmt("%s", delim);
172 }
173
174 static void reporevlink(char *page, char *name, char *title, char *class,
175 char *head, char *rev, char *path)
176 {
177 char *delim;
178
179 delim = repolink(title, class, page, head, path);
180 if (rev && strcmp(rev, cgit_query_head)) {
181 html(delim);
182 html("id=");
183 html_attr(rev);
184 }
185 html("'>");
186 html_txt(name);
187 html("</a>");
188 }
189
190 void cgit_tree_link(char *name, char *title, char *class, char *head,
191 char *rev, char *path)
192 {
193 reporevlink("tree", name, title, class, head, rev, path);
194 }
195
196 void cgit_log_link(char *name, char *title, char *class, char *head,
197 char *rev, char *path, int ofs, char *grep, char *pattern)
198 {
199 char *delim;
200
201 delim = repolink(title, class, "log", head, path);
202 if (rev && strcmp(rev, cgit_query_head)) {
203 html(delim);
204 html("id=");
205 html_attr(rev);
206 delim = "&";
207 }
208 if (grep && pattern) {
209 html(delim);
210 html("qt=");
211 html_attr(grep);
212 delim = "&";
213 html(delim);
214 html("q=");
215 html_attr(pattern);
216 }
217 if (ofs > 0) {
218 html(delim);
219 html("ofs=");
220 htmlf("%d", ofs);
221 }
222 html("'>");
223 html_txt(name);
224 html("</a>");
225 }
226
227 void cgit_commit_link(char *name, char *title, char *class, char *head,
228 char *rev)
229 {
230 if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) {
231 name[cgit_max_msg_len] = '\0';
232 name[cgit_max_msg_len - 1] = '.';
233 name[cgit_max_msg_len - 2] = '.';
234 name[cgit_max_msg_len - 3] = '.';
235 }
236 reporevlink("commit", name, title, class, head, rev, NULL);
237 }
238
239 void cgit_refs_link(char *name, char *title, char *class, char *head,
240 char *rev, char *path)
241 {
242 reporevlink("refs", name, title, class, head, rev, path);
243 }
244
245 void cgit_snapshot_link(char *name, char *title, char *class, char *head,
246 char *rev, char *archivename)
247 {
248 reporevlink("snapshot", name, title, class, head, rev, archivename);
249 }
250
251 void cgit_diff_link(char *name, char *title, char *class, char *head,
252 char *new_rev, char *old_rev, char *path)
253 {
254 char *delim;
255
256 delim = repolink(title, class, "diff", head, path);
257 if (new_rev && strcmp(new_rev, cgit_query_head)) {
258 html(delim);
259 html("id=");
260 html_attr(new_rev);
261 delim = "&amp;";
262 }
263 if (old_rev) {
264 html(delim);
265 html("id2=");
266 html_attr(old_rev);
267 }
268 html("'>");
269 html_txt(name);
270 html("</a>");
271 }
272
273 void cgit_object_link(struct object *obj)
274 {
275 char *page, *arg, *url;
276
277 if (obj->type == OBJ_COMMIT) {
278 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
279 cgit_query_head, sha1_to_hex(obj->sha1));
280 return;
281 } else if (obj->type == OBJ_TREE) {
282 page = "tree";
283 arg = "id";
284 } else if (obj->type == OBJ_TAG) {
285 page = "tag";
286 arg = "id";
287 } else {
288 page = "blob";
289 arg = "id";
290 }
291
292 url = cgit_pageurl(cgit_query_repo, page,
293 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
294 html_link_open(url, NULL, NULL);
295 htmlf("%s %s", typename(obj->type),
296 sha1_to_hex(obj->sha1));
297 html_link_close();
298 }
299
300 void cgit_print_date(time_t secs, char *format)
301 {
302 char buf[64];
303 struct tm *time;
304
305 time = gmtime(&secs);
306 strftime(buf, sizeof(buf)-1, format, time);
307 html_txt(buf);
308 }
309
310 void cgit_print_age(time_t t, time_t max_relative, char *format)
311 {
312 time_t now, secs;
313
314 time(&now);
315 secs = now - t;
316
317 if (secs > max_relative && max_relative >= 0) {
318 cgit_print_date(t, format);
319 return;
320 }
321
322 if (secs < TM_HOUR * 2) {
323 htmlf("<span class='age-mins'>%.0f min.</span>",
324 secs * 1.0 / TM_MIN);
325 return;
326 }
327 if (secs < TM_DAY * 2) {
328 htmlf("<span class='age-hours'>%.0f hours</span>",
329 secs * 1.0 / TM_HOUR);
330 return;
331 }
332 if (secs < TM_WEEK * 2) {
333 htmlf("<span class='age-days'>%.0f days</span>",
334 secs * 1.0 / TM_DAY);
335 return;
336 }
337 if (secs < TM_MONTH * 2) {
338 htmlf("<span class='age-weeks'>%.0f weeks</span>",
339 secs * 1.0 / TM_WEEK);
340 return;
341 }
342 if (secs < TM_YEAR * 2) {
343 htmlf("<span class='age-months'>%.0f months</span>",
344 secs * 1.0 / TM_MONTH);
345 return;
346 }
347 htmlf("<span class='age-years'>%.0f years</span>",
348 secs * 1.0 / TM_YEAR);
349 }
350
351 void cgit_print_docstart(char *title, struct cacheitem *item)
352 {
353 html("Content-Type: text/html; charset=utf-8\n");
354 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
355 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
356 ttl_seconds(item->ttl)));
357 html("\n");
358 html(cgit_doctype);
359 html("<html>\n");
360 html("<head>\n");
361 html("<title>");
362 html_txt(title);
363 html("</title>\n");
364 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
365 html("<link rel='stylesheet' type='text/css' href='");
366 html_attr(cgit_css);
367 html("'/>\n");
368 html("</head>\n");
369 html("<body>\n");
370 }
371
372 void cgit_print_docend()
373 {
374 html("</td>\n</tr>\n<table>\n</body>\n</html>\n");
375 }
376
377 int print_branch_option(const char *refname, const unsigned char *sha1,
378 int flags, void *cb_data)
379 {
380 char *name = (char *)refname;
381 html_option(name, name, cgit_query_head);
382 return 0;
383 }
384
385 int print_archive_ref(const char *refname, const unsigned char *sha1,
386 int flags, void *cb_data)
387 {
388 struct tag *tag;
389 struct taginfo *info;
390 struct object *obj;
391 char buf[256], *url;
392 unsigned char fileid[20];
393 int *header = (int *)cb_data;
394
395 if (prefixcmp(refname, "refs/archives"))
396 return 0;
397 strncpy(buf, refname+14, sizeof(buf));
398 obj = parse_object(sha1);
399 if (!obj)
400 return 1;
401 if (obj->type == OBJ_TAG) {
402 tag = lookup_tag(sha1);
403 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
404 return 0;
405 hashcpy(fileid, tag->tagged->sha1);
406 } else if (obj->type != OBJ_BLOB) {
407 return 0;
408 } else {
409 hashcpy(fileid, sha1);
410 }
411 if (!*header) {
412 html("<p><h1>download</h1>");
413 *header = 1;
414 }
415 url = cgit_pageurl(cgit_query_repo, "blob",
416 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
417 buf));
418 html_link_open(url, NULL, "menu");
419 html_txt(strlpart(buf, 20));
420 html_link_close();
421 return 0;
422 }
423
424 void add_hidden_formfields(int incl_head, int incl_search)
425 {
426 if (!cgit_virtual_root) {
427 if (cgit_query_repo)
428 html_hidden("r", cgit_query_repo);
429 if (cgit_query_page)
430 html_hidden("p", cgit_query_page);
431 }
432
433 if (incl_head && strcmp(cgit_query_head, cgit_repo->defbranch))
434 html_hidden("h", cgit_query_head);
435
436 if (cgit_query_sha1)
437 html_hidden("id", cgit_query_sha1);
438 if (cgit_query_sha2)
439 html_hidden("id2", cgit_query_sha2);
440
441 if (incl_search) {
442 if (cgit_query_grep)
443 html_hidden("qt", cgit_query_grep);
444 if (cgit_query_search)
445 html_hidden("q", cgit_query_search);
446 }
447 }
448
449 void cgit_print_pageheader(char *title, int show_search)
450 {
451 static const char *default_info = "This is cgit, a fast webinterface for git repositories";
452 int header = 0;
453
454 html("<div id='sidebar'>\n");
455 html("<a href='");
456 html_attr(cgit_rooturl());
457 htmlf("'><div id='logo'><img src='%s' alt='cgit'/></div></a>\n",
458 cgit_logo);
459 html("<div class='infobox'>");
460 if (cgit_query_repo) {
461 html("<h1>");
462 html_txt(strrpart(cgit_repo->name, 20));
463 html("</h1>\n");
464 html_txt(cgit_repo->desc);
465 if (cgit_repo->owner) {
466 html("<p>\n<h1>owner</h1>\n");
467 html_txt(cgit_repo->owner);
468 }
469 html("<p>\n<h1>navigate</h1>\n");
470 reporevlink(NULL, "summary", NULL, "menu", cgit_query_head,
471 NULL, NULL);
472 cgit_log_link("log", NULL, "menu", cgit_query_head,
473 cgit_query_sha1, cgit_query_path, 0, NULL, NULL);
474 cgit_tree_link("tree", NULL, "menu", cgit_query_head,
475 cgit_query_sha1, NULL);
476 cgit_commit_link("commit", NULL, "menu", cgit_query_head,
477 cgit_query_sha1);
478 cgit_diff_link("diff", NULL, "menu", cgit_query_head,
479 cgit_query_sha1, cgit_query_sha2,
480 cgit_query_path);
481
482 for_each_ref(print_archive_ref, &header);
483
484 html("<p>\n<h1>branch</h1>\n");
485 html("<form method='get' action=''>\n");
486 add_hidden_formfields(0, 1);
487 html("<select name='h' onchange='this.form.submit();'>\n");
488 for_each_branch_ref(print_branch_option, cgit_query_head);
489 html("</select>\n");
490 html("</form>\n");
491
492 html("<p>\n<h1>search</h1>\n");
493 html("<form method='get' action='");
494 html_attr(cgit_pageurl(cgit_query_repo, "log", NULL));
495 html("'>\n");
496 add_hidden_formfields(1, 0);
497 html("<select name='qt'>\n");
498 html_option("grep", "log msg", cgit_query_grep);
499 html_option("author", "author", cgit_query_grep);
500 html_option("committer", "committer", cgit_query_grep);
501 html("</select>\n");
502 html("<input class='txt' type='text' name='q' value='");
503 html_attr(cgit_query_search);
504 html("'/>\n");
505 html("</form>\n");
506 } else {
507 if (!cgit_index_info || html_include(cgit_index_info))
508 html(default_info);
509 }
510
511 html("</div>\n");
512
513 html("</div>\n<table class='grid'><tr><td id='content'>\n");
514 }
515
516
517 void cgit_print_snapshot_start(const char *mimetype, const char *filename,
518 struct cacheitem *item)
519 {
520 htmlf("Content-Type: %s\n", mimetype);
521 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
522 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
523 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
524 ttl_seconds(item->ttl)));
525 html("\n");
526 }
527
528 /* vim:set sw=8: */