|
diff --git a/cgit.c b/cgit.c index 4c14f77..09c857c 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -9,31 +9,49 @@ static const char cgit_error[] = |
9 | |
9 | |
10 | static const char cgit_lib_error[] = |
10 | static const char cgit_lib_error[] = |
11 | "<div class='error'>%s: %s</div>"; |
11 | "<div class='error'>%s: %s</div>"; |
12 | |
12 | |
| |
13 | int htmlfd = 0; |
13 | |
14 | |
14 | char *cgit_root = "/var/git"; |
15 | char *cgit_root = "/usr/src/git"; |
15 | char *cgit_root_title = "Git repository browser"; |
16 | char *cgit_root_title = "Git repository browser"; |
16 | char *cgit_css = "/cgit.css"; |
17 | char *cgit_css = "/cgit.css"; |
17 | char *cgit_logo = "/git-logo.png"; |
18 | char *cgit_logo = "/git-logo.png"; |
18 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
19 | char *cgit_logo_link = "http://www.kernel.org/pub/software/scm/git/docs/"; |
19 | char *cgit_virtual_root = NULL; |
20 | char *cgit_virtual_root = NULL; |
20 | |
21 | |
| |
22 | char *cgit_cache_root = "/var/cache/cgit"; |
| |
23 | |
| |
24 | int cgit_cache_root_ttl = 5; |
| |
25 | int cgit_cache_repo_ttl = 5; |
| |
26 | int cgit_cache_dynamic_ttl = 5; |
| |
27 | int cgit_cache_static_ttl = -1; |
| |
28 | int cgit_cache_max_create_time = 5; |
| |
29 | |
21 | char *cgit_repo_name = NULL; |
30 | char *cgit_repo_name = NULL; |
22 | char *cgit_repo_desc = NULL; |
31 | char *cgit_repo_desc = NULL; |
23 | char *cgit_repo_owner = NULL; |
32 | char *cgit_repo_owner = NULL; |
24 | |
33 | |
| |
34 | int cgit_query_has_symref = 0; |
| |
35 | int cgit_query_has_sha1 = 0; |
| |
36 | |
| |
37 | char *cgit_querystring = NULL; |
25 | char *cgit_query_repo = NULL; |
38 | char *cgit_query_repo = NULL; |
26 | char *cgit_query_page = NULL; |
39 | char *cgit_query_page = NULL; |
27 | char *cgit_query_head = NULL; |
40 | char *cgit_query_head = NULL; |
| |
41 | char *cgit_query_sha1 = NULL; |
| |
42 | |
| |
43 | struct cacheitem cacheitem; |
28 | |
44 | |
29 | int cgit_parse_query(char *txt, configfn fn) |
45 | int cgit_parse_query(char *txt, configfn fn) |
30 | { |
46 | { |
31 | char *t = txt, *value = NULL, c; |
47 | char *t, *value = NULL, c; |
32 | |
48 | |
33 | if (!txt) |
49 | if (!txt) |
34 | return 0; |
50 | return 0; |
35 | |
51 | |
| |
52 | t = txt = xstrdup(txt); |
| |
53 | |
36 | while((c=*t) != '\0') { |
54 | while((c=*t) != '\0') { |
37 | if (c=='=') { |
55 | if (c=='=') { |
38 | *t = '\0'; |
56 | *t = '\0'; |
39 | value = t+1; |
57 | value = t+1; |
@@ -81,10 +99,15 @@ void cgit_querystring_cb(const char *name, const char *value) |
81 | if (!strcmp(name,"r")) |
99 | if (!strcmp(name,"r")) |
82 | cgit_query_repo = xstrdup(value); |
100 | cgit_query_repo = xstrdup(value); |
83 | else if (!strcmp(name, "p")) |
101 | else if (!strcmp(name, "p")) |
84 | cgit_query_page = xstrdup(value); |
102 | cgit_query_page = xstrdup(value); |
85 | else if (!strcmp(name, "h")) |
103 | else if (!strcmp(name, "h")) { |
86 | cgit_query_head = xstrdup(value); |
104 | cgit_query_head = xstrdup(value); |
| |
105 | cgit_query_has_symref = 1; |
| |
106 | } else if (!strcmp(name, "id")) { |
| |
107 | cgit_query_sha1 = xstrdup(value); |
| |
108 | cgit_query_has_sha1 = 1; |
| |
109 | } |
87 | } |
110 | } |
88 | |
111 | |
89 | char *cgit_repourl(const char *reponame) |
112 | char *cgit_repourl(const char *reponame) |
90 | { |
113 | { |
@@ -135,11 +158,34 @@ static int cgit_print_branch_cb(const char *refname, const unsigned char *sha1, |
135 | } |
158 | } |
136 | return 0; |
159 | return 0; |
137 | } |
160 | } |
138 | |
161 | |
| |
162 | /* Sun, 06 Nov 1994 08:49:37 GMT */ |
| |
163 | static char *http_date(time_t t) |
| |
164 | { |
| |
165 | static char day[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
| |
166 | static char month[][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| |
167 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
| |
168 | struct tm *tm = gmtime(&t); |
| |
169 | return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], |
| |
170 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
| |
171 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| |
172 | } |
| |
173 | |
| |
174 | static int ttl_seconds(int ttl) |
| |
175 | { |
| |
176 | if (ttl<0) |
| |
177 | return 60 * 60 * 24 * 365; |
| |
178 | else |
| |
179 | return ttl * 60; |
| |
180 | } |
| |
181 | |
139 | static void cgit_print_docstart(char *title) |
182 | static void cgit_print_docstart(char *title) |
140 | { |
183 | { |
141 | html("Content-Type: text/html; charset=utf-8\n"); |
184 | html("Content-Type: text/html; charset=utf-8\n"); |
| |
185 | htmlf("Last-Modified: %s\n", http_date(cacheitem.st.st_mtime)); |
| |
186 | htmlf("Expires: %s\n", http_date(cacheitem.st.st_mtime + |
| |
187 | ttl_seconds(cacheitem.ttl))); |
142 | html("\n"); |
188 | html("\n"); |
143 | html(cgit_doctype); |
189 | html(cgit_doctype); |
144 | html("<html>\n"); |
190 | html("<html>\n"); |
145 | html("<head>\n"); |
191 | html("<head>\n"); |
@@ -174,8 +220,9 @@ static void cgit_print_repolist() |
174 | struct dirent *de; |
220 | struct dirent *de; |
175 | struct stat st; |
221 | struct stat st; |
176 | char *name; |
222 | char *name; |
177 | |
223 | |
| |
224 | chdir(cgit_root); |
178 | cgit_print_docstart(cgit_root_title); |
225 | cgit_print_docstart(cgit_root_title); |
179 | cgit_print_pageheader(cgit_root_title); |
226 | cgit_print_pageheader(cgit_root_title); |
180 | |
227 | |
181 | if (!(d = opendir("."))) { |
228 | if (!(d = opendir("."))) { |
@@ -196,9 +243,9 @@ static void cgit_print_repolist() |
196 | if (!S_ISDIR(st.st_mode)) |
243 | if (!S_ISDIR(st.st_mode)) |
197 | continue; |
244 | continue; |
198 | |
245 | |
199 | cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; |
246 | cgit_repo_name = cgit_repo_desc = cgit_repo_owner = NULL; |
200 | name = fmt("%s/.git/info/cgit", de->d_name); |
247 | name = fmt("%s/info/cgit", de->d_name); |
201 | if (cgit_read_config(name, cgit_repo_config_cb)) |
248 | if (cgit_read_config(name, cgit_repo_config_cb)) |
202 | continue; |
249 | continue; |
203 | |
250 | |
204 | html("<tr><td>"); |
251 | html("<tr><td>"); |
@@ -290,9 +337,9 @@ static void cgit_print_commit_shortlog(struct commit *commit) |
290 | html("<tr><td>"); |
337 | html("<tr><td>"); |
291 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); |
338 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time); |
292 | html_txt(buf); |
339 | html_txt(buf); |
293 | html("</td><td>"); |
340 | html("</td><td>"); |
294 | char *qry = fmt("h=%s", sha1_to_hex(commit->object.sha1)); |
341 | char *qry = fmt("id=%s", sha1_to_hex(commit->object.sha1)); |
295 | char *url = cgit_pageurl(cgit_query_repo, "view", qry); |
342 | char *url = cgit_pageurl(cgit_query_repo, "view", qry); |
296 | html_link_open(url, NULL, NULL); |
343 | html_link_open(url, NULL, NULL); |
297 | html_txt(subject); |
344 | html_txt(subject); |
298 | html_link_close(); |
345 | html_link_close(); |
@@ -370,41 +417,81 @@ static void cgit_print_object(char *hex) |
370 | } |
417 | } |
371 | |
418 | |
372 | static void cgit_print_repo_page() |
419 | static void cgit_print_repo_page() |
373 | { |
420 | { |
374 | if (chdir(cgit_query_repo) || |
421 | if (chdir(fmt("%s/%s", cgit_root, cgit_query_repo)) || |
375 | cgit_read_config(".git/info/cgit", cgit_repo_config_cb)) { |
422 | cgit_read_config("info/cgit", cgit_repo_config_cb)) { |
376 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); |
423 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); |
377 | cgit_print_docstart(title); |
424 | cgit_print_docstart(title); |
378 | cgit_print_pageheader(title); |
425 | cgit_print_pageheader(title); |
379 | htmlf(cgit_lib_error, "Unable to scan repository", |
426 | htmlf(cgit_lib_error, "Unable to scan repository", |
380 | strerror(errno)); |
427 | strerror(errno)); |
381 | cgit_print_docend(); |
428 | cgit_print_docend(); |
382 | return; |
429 | return; |
383 | } |
430 | } |
384 | |
431 | setenv("GIT_DIR", fmt("%s/%s", cgit_root, cgit_query_repo), 1); |
385 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); |
432 | char *title = fmt("%s - %s", cgit_repo_name, cgit_repo_desc); |
386 | cgit_print_docstart(title); |
433 | cgit_print_docstart(title); |
387 | cgit_print_pageheader(title); |
434 | cgit_print_pageheader(title); |
388 | if (!cgit_query_page) |
435 | if (!cgit_query_page) |
389 | cgit_print_repo_summary(); |
436 | cgit_print_repo_summary(); |
390 | else if (!strcmp(cgit_query_page, "log")) { |
437 | else if (!strcmp(cgit_query_page, "log")) { |
391 | cgit_print_log(cgit_query_head, 0, 100); |
438 | cgit_print_log(cgit_query_head, 0, 100); |
392 | } else if (!strcmp(cgit_query_page, "view")) { |
439 | } else if (!strcmp(cgit_query_page, "view")) { |
393 | cgit_print_object(cgit_query_head); |
440 | cgit_print_object(cgit_query_sha1); |
394 | } |
441 | } |
395 | cgit_print_docend(); |
442 | cgit_print_docend(); |
396 | } |
443 | } |
397 | |
444 | |
398 | int main(int argc, const char **argv) |
445 | static void cgit_fill_cache(struct cacheitem *item) |
399 | { |
446 | { |
400 | if (cgit_read_config("/etc/cgitrc", cgit_global_config_cb)) |
447 | htmlfd = item->fd; |
401 | die("Error reading config: %d %s", errno, strerror(errno)); |
448 | item->st.st_mtime = time(NULL); |
402 | |
| |
403 | chdir(cgit_root); |
| |
404 | cgit_parse_query(getenv("QUERY_STRING"), cgit_querystring_cb); |
| |
405 | if (cgit_query_repo) |
449 | if (cgit_query_repo) |
406 | cgit_print_repo_page(); |
450 | cgit_print_repo_page(); |
407 | else |
451 | else |
408 | cgit_print_repolist(); |
452 | cgit_print_repolist(); |
| |
453 | } |
| |
454 | |
| |
455 | static void cgit_refresh_cache(struct cacheitem *item) |
| |
456 | { |
| |
457 | top: |
| |
458 | if (!cache_lookup(item)) { |
| |
459 | if (cache_lock(item)) { |
| |
460 | cgit_fill_cache(item); |
| |
461 | cache_unlock(item); |
| |
462 | } else { |
| |
463 | sched_yield(); |
| |
464 | goto top; |
| |
465 | } |
| |
466 | } else if (cache_expired(item)) { |
| |
467 | if (cache_lock(item)) { |
| |
468 | cgit_fill_cache(item); |
| |
469 | cache_unlock(item); |
| |
470 | } |
| |
471 | } |
| |
472 | } |
| |
473 | |
| |
474 | static void cgit_print_cache(struct cacheitem *item) |
| |
475 | { |
| |
476 | static char buf[4096]; |
| |
477 | ssize_t i; |
| |
478 | |
| |
479 | int fd = open(item->name, O_RDONLY); |
| |
480 | if (fd<0) |
| |
481 | die("Unable to open cached file %s", item->name); |
| |
482 | |
| |
483 | while((i=read(fd, buf, sizeof(buf))) > 0) |
| |
484 | write(STDOUT_FILENO, buf, i); |
| |
485 | |
| |
486 | close(fd); |
| |
487 | } |
| |
488 | |
| |
489 | int main(int argc, const char **argv) |
| |
490 | { |
| |
491 | cgit_read_config("/etc/cgitrc", cgit_global_config_cb); |
| |
492 | cgit_querystring = xstrdup(getenv("QUERY_STRING")); |
| |
493 | cgit_parse_query(cgit_querystring, cgit_querystring_cb); |
| |
494 | cgit_refresh_cache(&cacheitem); |
| |
495 | cgit_print_cache(&cacheitem); |
409 | return 0; |
496 | return 0; |
410 | } |
497 | } |
|