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