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