cgit.c (view raw)
1/* cgit.c: cgi for the git scm
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
11const char cgit_version[] = CGIT_VERSION;
12
13const 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";
16
17const char cgit_error[] =
18"<div class='error'>%s</div>";
19
20const char cgit_lib_error[] =
21"<div class='error'>%s: %s</div>";
22
23int htmlfd = 0;
24
25char *cgit_root = "/usr/src/git";
26char *cgit_root_title = "Git repository browser";
27char *cgit_css = "/cgit.css";
28char *cgit_logo = "/git-logo.png";
29char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/";
30char *cgit_virtual_root = NULL;
31
32char *cgit_cache_root = "/var/cache/cgit";
33
34int cgit_max_lock_attempts = 5;
35int cgit_cache_root_ttl = 5;
36int cgit_cache_repo_ttl = 5;
37int cgit_cache_dynamic_ttl = 5;
38int cgit_cache_static_ttl = -1;
39int cgit_cache_max_create_time = 5;
40
41char *cgit_repo_name = NULL;
42char *cgit_repo_desc = NULL;
43char *cgit_repo_owner = NULL;
44
45int cgit_query_has_symref = 0;
46int cgit_query_has_sha1 = 0;
47
48char *cgit_querystring = NULL;
49char *cgit_query_repo = NULL;
50char *cgit_query_page = NULL;
51char *cgit_query_head = NULL;
52char *cgit_query_sha1 = NULL;
53
54struct cacheitem cacheitem;
55
56void cgit_global_config_cb(const char *name, const char *value)
57{
58 if (!strcmp(name, "root"))
59 cgit_root = xstrdup(value);
60 else if (!strcmp(name, "root-title"))
61 cgit_root_title = xstrdup(value);
62 else if (!strcmp(name, "css"))
63 cgit_css = xstrdup(value);
64 else if (!strcmp(name, "logo"))
65 cgit_logo = xstrdup(value);
66 else if (!strcmp(name, "logo-link"))
67 cgit_logo_link = xstrdup(value);
68 else if (!strcmp(name, "virtual-root"))
69 cgit_virtual_root = xstrdup(value);
70}
71
72void cgit_repo_config_cb(const char *name, const char *value)
73{
74 if (!strcmp(name, "name"))
75 cgit_repo_name = xstrdup(value);
76 else if (!strcmp(name, "desc"))
77 cgit_repo_desc = xstrdup(value);
78 else if (!strcmp(name, "owner"))
79 cgit_repo_owner = xstrdup(value);
80}
81
82void cgit_querystring_cb(const char *name, const char *value)
83{
84 if (!strcmp(name,"r"))
85 cgit_query_repo = xstrdup(value);
86 else if (!strcmp(name, "p"))
87 cgit_query_page = xstrdup(value);
88 else if (!strcmp(name, "h")) {
89 cgit_query_head = xstrdup(value);
90 cgit_query_has_symref = 1;
91 } else if (!strcmp(name, "id")) {
92 cgit_query_sha1 = xstrdup(value);
93 cgit_query_has_sha1 = 1;
94 }
95}
96
97char *cgit_repourl(const char *reponame)
98{
99 if (cgit_virtual_root) {
100 return fmt("%s/%s/", cgit_virtual_root, reponame);
101 } else {
102 return fmt("?r=%s", reponame);
103 }
104}
105
106char *cgit_pageurl(const char *reponame, const char *pagename,
107 const char *query)
108{
109 if (cgit_virtual_root) {
110 return fmt("%s/%s/%s/?%s", cgit_virtual_root, reponame,
111 pagename, query);
112 } else {
113 return fmt("?r=%s&p=%s&%s", reponame, pagename, query);
114 }
115}
116
117static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1,
118 int flags, void *cb_data)
119{
120 struct commit *commit;
121 char buf[256], *url;
122
123 commit = lookup_commit(sha1);
124 if (commit && !parse_commit(commit)){
125 html("<tr><td>");
126 url = cgit_pageurl(cgit_query_repo, "log",
127 fmt("h=%s", refname));
128 html_link_open(url, NULL, NULL);
129 strncpy(buf, refname, sizeof(buf));
130 html_txt(buf);
131 html_link_close();
132 html("</td><td>");
133 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0, buf,
134 sizeof(buf), 0, NULL, NULL, 0);
135 html_txt(buf);
136 html("</td></tr>\n");
137 } else {
138 html("<tr><td>");
139 html_txt(buf);
140 html("</td><td>");
141 htmlf("*** bad ref %s", sha1_to_hex(sha1));
142 html("</td></tr>\n");
143 }
144 return 0;
145}
146
147/* Sun, 06 Nov 1994 08:49:37 GMT */
148static char *http_date(time_t t)
149{
150 static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
151 static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
152 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
153 struct tm *tm = gmtime(&t);
154 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
155 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
156 tm->tm_hour, tm->tm_min, tm->tm_sec);
157}
158
159static int ttl_seconds(int ttl)
160{
161 if (ttl<0)
162 return 60 * 60 * 24 * 365;
163 else
164 return ttl * 60;
165}
166
167static void cgit_print_docstart(char *title)
168{
169 html("Content-Type: text/html; charset=utf-8\n");
170 htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime));
171 htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime +
172 ttl_seconds(cacheitem.ttl)));
173 html("\n");
174 html(cgit_doctype);
175 html("<html>\n");
176 html("<head>\n");
177 html("<title>");
178 html_txt(title);
179 html("</title>\n");
180 htmlf("<meta name='generator' content='cgit v%s'/>\n", cgit_version);
181 html("<link rel='stylesheet' type='text/css' href='");
182 html_attr(cgit_css);
183 html("'/>\n");
184 html("</head>\n");
185 html("<body>\n");
186}
187
188static void cgit_print_docend()
189{
190 html("</body>\n</html>\n");
191}
192
193static void cgit_print_pageheader(char *title)
194{
195 html("<div id='header'>");
196 htmlf("<a href='%s'>", cgit_logo_link);
197 htmlf("<img id='logo' src='%s'/>\n", cgit_logo);
198 htmlf("</a>");
199 html_txt(title);
200 html("</div>");
201}
202
203static void cgit_print_repolist()
204{
205 DIR *d;
206 struct dirent *de;
207 struct stat st;
208 char *name;
209
210 chdir(cgit_root);
211 cgit_print_docstart(cgit_root_title);
212 cgit_print_pageheader(cgit_root_title);
213
214 if (!(d = opendir("."))) {
215 htmlf(cgit_lib_error, "Unable to scan repository directory",
216 strerror(errno));
217 cgit_print_docend();
218 return;
219 }
220
221 html("<h2>Repositories</h2>\n");
222 html("<table class='list'>");
223 html("<tr><th>Name</th><th>Description</th><th>Owner</th></tr>\n");
224 while ((de = readdir(d)) != NULL) {
225 if (de->d_name[0] == '.')
226 continue;
227 if (stat(de->d_name, &st) < 0)
228 continue;
229 if (!S_ISDIR(st.st_mode))
230 continue;
231
232 cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL;
233 name = fmt("%s/info/cgit", de->d_name);
234 if (cgit_read_config(name, cgit_repo_config_cb))
235 continue;
236
237 html("<tr><td>");
238 html_link_open(cgit_repourl(de->d_name), NULL, NULL);
239 html_txt(cgit_repo_name);
240 html_link_close();
241 html("</td><td>");
242 html_txt(cgit_repo_desc);
243 html("</td><td>");
244 html_txt(cgit_repo_owner);
245 html("</td></tr>\n");
246 }
247 closedir(d);
248 html("</table>");
249 cgit_print_docend();
250}
251
252static void cgit_print_branches()
253{
254 html("<table class='list'>");
255 html("<tr><th>Branch name</th><th>Head commit</th></tr>\n");
256 for_each_branch_ref(cgit_print_branch_cb, NULL);
257 html("</table>");
258}
259
260static int get_one_line(char *txt)
261{
262 char *t;
263
264 for(t=txt; *t != '\n' && t != '\0'; t++)
265 ;
266 *t = '\0';
267 return t-txt-1;
268}
269
270static void cgit_print_commit_shortlog(struct commit *commit)
271{
272 char *h, *t, *p;
273 char *tree = NULL, *author = NULL, *subject = NULL;
274 int len;
275 time_t sec;
276 struct tm *time;
277 char buf[32];
278
279 h = t = commit->buffer;
280
281 if (strncmp(h, "tree ", 5))
282 die("Bad commit format: %s",
283 sha1_to_hex(commit->object.sha1));
284
285 len = get_one_line(h);
286 tree = h+5;
287 h += len + 2;
288
289 while (!strncmp(h, "parent ", 7))
290 h += get_one_line(h) + 2;
291
292 if (!strncmp(h, "author ", 7)) {
293 author = h+7;
294 h += get_one_line(h) + 2;
295 t = author;
296 while(t!=h && *t!='<')
297 t++;
298 *t='\0';
299 p = t;
300 while(--t!=author && *t==' ')
301 *t='\0';
302 while(++p!=h && *p!='>')
303 ;
304 while(++p!=h && !isdigit(*p))
305 ;
306
307 t = p;
308 while(++p && isdigit(*p))
309 ;
310 *p = '\0';
311 sec = atoi(t);
312 time = gmtime(&sec);
313 }
314
315 while((len = get_one_line(h)) > 0)
316 h += len+2;
317
318 h++;
319 len = get_one_line(h);
320
321 subject = h;
322
323 html("<tr><td>");
324 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
325 html_txt(buf);
326 html("</td><td>");
327 char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1));
328 char *url = cgit_pageurl(cgit_query_repo, "view", qry);
329 html_link_open(url, NULL, NULL);
330 html_txt(subject);
331 html_link_close();
332 html("</td><td>");
333 html_txt(author);
334 html("</td></tr>\n");
335}
336
337static void cgit_print_log(const char *tip, int ofs, int cnt)
338{
339 struct rev_info rev;
340 struct commit *commit;
341 const char *argv[2] = {NULL, tip};
342 int n = 0;
343
344 init_revisions(&rev, NULL);
345 rev.abbrev = DEFAULT_ABBREV;
346 rev.commit_format = CMIT_FMT_DEFAULT;
347 rev.verbose_header = 1;
348 rev.show_root_diff = 0;
349 setup_revisions(2, argv, &rev, NULL);
350 prepare_revision_walk(&rev);
351
352 html("<h2>Log</h2>");
353 html("<table class='list'>");
354 html("<tr><th>Date</th><th>Message</th><th>Author</th></tr>\n");
355 while ((commit = get_revision(&rev)) != NULL && n++ < 100) {
356 cgit_print_commit_shortlog(commit);
357 free(commit->buffer);
358 commit->buffer = NULL;
359 free_commit_list(commit->parents);
360 commit->parents = NULL;
361 }
362 html("</table>\n");
363}
364
365static void cgit_print_repo_summary()
366{
367 html("<h2>");
368 html_txt("Repo summary page");
369 html("</h2>");
370 cgit_print_branches();
371}
372
373static void cgit_print_object(char *hex)
374{
375 unsigned char sha1[20];
376 //struct object *object;
377 char type[20];
378 unsigned char *buf;
379 unsigned long size;
380
381 if (get_sha1_hex(hex, sha1)){
382 htmlf(cgit_error, "Bad hex value");
383 return;
384 }
385
386 if (sha1_object_info(sha1, type, NULL)){
387 htmlf(cgit_error, "Bad object name");
388 return;
389 }
390
391 buf = read_sha1_file(sha1, type, &size);
392 if (!buf) {
393 htmlf(cgit_error, "Error reading object");
394 return;
395 }
396
397 buf[size] = '\0';
398 html("<h2>Object view</h2>");
399 htmlf("sha1=%s<br/>type=%s<br/>size=%i<br/>", hex, type, size);
400 html("<pre>");
401 html_txt(buf);
402 html("</pre>");
403}
404
405static void cgit_print_repo_page()
406{
407 if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) ||
408 cgit_read_config("info/cgit", cgit_repo_config_cb)) {
409 char *title = fmt("%s - %s", cgit_root_title, "Bad request");
410 cgit_print_docstart(title);
411 cgit_print_pageheader(title);
412 htmlf(cgit_lib_error, "Unable to scan repository",
413 strerror(errno));
414 cgit_print_docend();
415 return;
416 }
417 setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1);
418 char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc);
419 cgit_print_docstart(title);
420 cgit_print_pageheader(title);
421 if (!cgit_query_page)
422 cgit_print_repo_summary();
423 else if (!strcmp(cgit_query_page, "log")) {
424 cgit_print_log(cgit_query_head, 0, 100);
425 } else if (!strcmp(cgit_query_page, "view")) {
426 cgit_print_object(cgit_query_sha1);
427 }
428 cgit_print_docend();
429}
430
431static void cgit_fill_cache(struct cacheitem *item)
432{
433 htmlfd = item->fd;
434 item->st.st_mtime = time(NULL);
435 if (cgit_query_repo)
436 cgit_print_repo_page();
437 else
438 cgit_print_repolist();
439}
440
441static void cgit_refresh_cache(struct cacheitem *item)
442{
443 int i = 0;
444
445 cache_prepare(item);
446 top:
447 if (++i > cgit_max_lock_attempts) {
448 die("cgit_refresh_cache: unable to lock %s: %s",
449 item->name, strerror(errno));
450 }
451 if (!cache_exist(item)) {
452 if (!cache_lock(item)) {
453 sleep(1);
454 goto top;
455 }
456 if (!cache_exist(item))
457 cgit_fill_cache(item);
458 cache_unlock(item);
459 } else if (cache_expired(item) && cache_lock(item)) {
460 if (cache_expired(item))
461 cgit_fill_cache(item);
462 cache_unlock(item);
463 }
464}
465
466static void cgit_print_cache(struct cacheitem *item)
467{
468 static char buf[4096];
469 ssize_t i;
470
471 int fd = open(item->name, O_RDONLY);
472 if (fd<0)
473 die("Unable to open cached file %s", item->name);
474
475 while((i=read(fd, buf, sizeof(buf))) > 0)
476 write(STDOUT_FILENO, buf, i);
477
478 close(fd);
479}
480
481int main(int argc, const char **argv)
482{
483 cgit_read_config("/etc/cgitrc", cgit_global_config_cb);
484 cgit_querystring = xstrdup(getenv("QUERY_STRING"));
485 cgit_parse_query(cgit_querystring, cgit_querystring_cb);
486 cgit_refresh_cache(&cacheitem);
487 cgit_print_cache(&cacheitem);
488 return 0;
489}