summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c33
1 files changed, 17 insertions, 16 deletions
diff --git a/cgit.c b/cgit.c
index 8a30c3c..f0907a1 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,112 +1,112 @@
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
11static int cgit_prepare_cache(struct cacheitem *item) 11static int cgit_prepare_cache(struct cacheitem *item)
12{ 12{
13 if (!cgit_repo && ctx.qry.repo) { 13 if (!cgit_repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 14 char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request");
15 cgit_print_docstart(title, item); 15 cgit_print_docstart(title, item);
16 cgit_print_pageheader(title, 0); 16 cgit_print_pageheader(title, 0);
17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); 17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 18 cgit_print_docend();
19 return 0; 19 return 0;
20 } 20 }
21 21
22 if (!cgit_repo) { 22 if (!cgit_repo) {
23 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); 23 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
24 item->ttl = cgit_cache_root_ttl; 24 item->ttl = ctx.cfg.cache_root_ttl;
25 return 1; 25 return 1;
26 } 26 }
27 27
28 if (!cgit_cmd) { 28 if (!cgit_cmd) {
29 item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, 29 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
30 cache_safe_filename(cgit_repo->url), 30 cache_safe_filename(cgit_repo->url),
31 cache_safe_filename(ctx.qry.raw))); 31 cache_safe_filename(ctx.qry.raw)));
32 item->ttl = cgit_cache_repo_ttl; 32 item->ttl = ctx.cfg.cache_repo_ttl;
33 } else { 33 } else {
34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
35 cache_safe_filename(cgit_repo->url), 35 cache_safe_filename(cgit_repo->url),
36 ctx.qry.page, 36 ctx.qry.page,
37 cache_safe_filename(ctx.qry.raw))); 37 cache_safe_filename(ctx.qry.raw)));
38 if (ctx.qry.has_symref) 38 if (ctx.qry.has_symref)
39 item->ttl = cgit_cache_dynamic_ttl; 39 item->ttl = ctx.cfg.cache_dynamic_ttl;
40 else if (ctx.qry.has_sha1) 40 else if (ctx.qry.has_sha1)
41 item->ttl = cgit_cache_static_ttl; 41 item->ttl = ctx.cfg.cache_static_ttl;
42 else 42 else
43 item->ttl = cgit_cache_repo_ttl; 43 item->ttl = ctx.cfg.cache_repo_ttl;
44 } 44 }
45 return 1; 45 return 1;
46} 46}
47 47
48struct refmatch { 48struct refmatch {
49 char *req_ref; 49 char *req_ref;
50 char *first_ref; 50 char *first_ref;
51 int match; 51 int match;
52}; 52};
53 53
54int find_current_ref(const char *refname, const unsigned char *sha1, 54int find_current_ref(const char *refname, const unsigned char *sha1,
55 int flags, void *cb_data) 55 int flags, void *cb_data)
56{ 56{
57 struct refmatch *info; 57 struct refmatch *info;
58 58
59 info = (struct refmatch *)cb_data; 59 info = (struct refmatch *)cb_data;
60 if (!strcmp(refname, info->req_ref)) 60 if (!strcmp(refname, info->req_ref))
61 info->match = 1; 61 info->match = 1;
62 if (!info->first_ref) 62 if (!info->first_ref)
63 info->first_ref = xstrdup(refname); 63 info->first_ref = xstrdup(refname);
64 return info->match; 64 return info->match;
65} 65}
66 66
67char *find_default_branch(struct repoinfo *repo) 67char *find_default_branch(struct repoinfo *repo)
68{ 68{
69 struct refmatch info; 69 struct refmatch info;
70 70
71 info.req_ref = repo->defbranch; 71 info.req_ref = repo->defbranch;
72 info.first_ref = NULL; 72 info.first_ref = NULL;
73 info.match = 0; 73 info.match = 0;
74 for_each_branch_ref(find_current_ref, &info); 74 for_each_branch_ref(find_current_ref, &info);
75 if (info.match) 75 if (info.match)
76 return info.req_ref; 76 return info.req_ref;
77 else 77 else
78 return info.first_ref; 78 return info.first_ref;
79} 79}
80 80
81static void cgit_print_repo_page(struct cacheitem *item) 81static void cgit_print_repo_page(struct cacheitem *item)
82{ 82{
83 char *title, *tmp; 83 char *title, *tmp;
84 int show_search; 84 int show_search;
85 unsigned char sha1[20]; 85 unsigned char sha1[20];
86 86
87 if (chdir(cgit_repo->path)) { 87 if (chdir(cgit_repo->path)) {
88 title = fmt("%s - %s", cgit_root_title, "Bad request"); 88 title = fmt("%s - %s", ctx.cfg.root_title, "Bad request");
89 cgit_print_docstart(title, item); 89 cgit_print_docstart(title, item);
90 cgit_print_pageheader(title, 0); 90 cgit_print_pageheader(title, 0);
91 cgit_print_error(fmt("Unable to scan repository: %s", 91 cgit_print_error(fmt("Unable to scan repository: %s",
92 strerror(errno))); 92 strerror(errno)));
93 cgit_print_docend(); 93 cgit_print_docend();
94 return; 94 return;
95 } 95 }
96 96
97 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 97 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc);
98 show_search = 0; 98 show_search = 0;
99 setenv("GIT_DIR", cgit_repo->path, 1); 99 setenv("GIT_DIR", cgit_repo->path, 1);
100 100
101 if (!ctx.qry.head) { 101 if (!ctx.qry.head) {
102 ctx.qry.head = xstrdup(find_default_branch(cgit_repo)); 102 ctx.qry.head = xstrdup(find_default_branch(cgit_repo));
103 cgit_repo->defbranch = ctx.qry.head; 103 cgit_repo->defbranch = ctx.qry.head;
104 } 104 }
105 105
106 if (!ctx.qry.head) { 106 if (!ctx.qry.head) {
107 cgit_print_docstart(title, item); 107 cgit_print_docstart(title, item);
108 cgit_print_pageheader(title, 0); 108 cgit_print_pageheader(title, 0);
109 cgit_print_error("Repository seems to be empty"); 109 cgit_print_error("Repository seems to be empty");
110 cgit_print_docend(); 110 cgit_print_docend();
111 return; 111 return;
112 } 112 }
@@ -132,49 +132,49 @@ static void cgit_print_repo_page(struct cacheitem *item)
132 if (cgit_cmd == CMD_PATCH) { 132 if (cgit_cmd == CMD_PATCH) {
133 cgit_print_patch(ctx.qry.sha1, item); 133 cgit_print_patch(ctx.qry.sha1, item);
134 return; 134 return;
135 } 135 }
136 136
137 if (cgit_cmd == CMD_BLOB) { 137 if (cgit_cmd == CMD_BLOB) {
138 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); 138 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path);
139 return; 139 return;
140 } 140 }
141 141
142 show_search = (cgit_cmd == CMD_LOG); 142 show_search = (cgit_cmd == CMD_LOG);
143 cgit_print_docstart(title, item); 143 cgit_print_docstart(title, item);
144 if (!cgit_cmd) { 144 if (!cgit_cmd) {
145 cgit_print_pageheader("summary", show_search); 145 cgit_print_pageheader("summary", show_search);
146 cgit_print_summary(); 146 cgit_print_summary();
147 cgit_print_docend(); 147 cgit_print_docend();
148 return; 148 return;
149 } 149 }
150 150
151 cgit_print_pageheader(ctx.qry.page, show_search); 151 cgit_print_pageheader(ctx.qry.page, show_search);
152 152
153 switch(cgit_cmd) { 153 switch(cgit_cmd) {
154 case CMD_LOG: 154 case CMD_LOG:
155 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, 155 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs,
156 cgit_max_commit_count, ctx.qry.grep, ctx.qry.search, 156 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search,
157 ctx.qry.path, 1); 157 ctx.qry.path, 1);
158 break; 158 break;
159 case CMD_TREE: 159 case CMD_TREE:
160 cgit_print_tree(ctx.qry.sha1, ctx.qry.path); 160 cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
161 break; 161 break;
162 case CMD_COMMIT: 162 case CMD_COMMIT:
163 cgit_print_commit(ctx.qry.sha1); 163 cgit_print_commit(ctx.qry.sha1);
164 break; 164 break;
165 case CMD_REFS: 165 case CMD_REFS:
166 cgit_print_refs(); 166 cgit_print_refs();
167 break; 167 break;
168 case CMD_TAG: 168 case CMD_TAG:
169 cgit_print_tag(ctx.qry.sha1); 169 cgit_print_tag(ctx.qry.sha1);
170 break; 170 break;
171 case CMD_DIFF: 171 case CMD_DIFF:
172 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); 172 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path);
173 break; 173 break;
174 default: 174 default:
175 cgit_print_error("Invalid request"); 175 cgit_print_error("Invalid request");
176 } 176 }
177 cgit_print_docend(); 177 cgit_print_docend();
178} 178}
179 179
180static void cgit_fill_cache(struct cacheitem *item, int use_cache) 180static void cgit_fill_cache(struct cacheitem *item, int use_cache)
@@ -191,128 +191,129 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
193 } 193 }
194 194
195 if (cgit_repo) 195 if (cgit_repo)
196 cgit_print_repo_page(item); 196 cgit_print_repo_page(item);
197 else 197 else
198 cgit_print_repolist(item); 198 cgit_print_repolist(item);
199 199
200 if (use_cache) { 200 if (use_cache) {
201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
202 chk_positive(dup2(stdout2, STDOUT_FILENO), 202 chk_positive(dup2(stdout2, STDOUT_FILENO),
203 "Restoring original STDOUT"); 203 "Restoring original STDOUT");
204 chk_zero(close(stdout2), "Closing temporary STDOUT"); 204 chk_zero(close(stdout2), "Closing temporary STDOUT");
205 } 205 }
206 206
207 chdir(buf); 207 chdir(buf);
208} 208}
209 209
210static void cgit_check_cache(struct cacheitem *item) 210static void cgit_check_cache(struct cacheitem *item)
211{ 211{
212 int i = 0; 212 int i = 0;
213 213
214 top: 214 top:
215 if (++i > cgit_max_lock_attempts) { 215 if (++i > ctx.cfg.max_lock_attempts) {
216 die("cgit_refresh_cache: unable to lock %s: %s", 216 die("cgit_refresh_cache: unable to lock %s: %s",
217 item->name, strerror(errno)); 217 item->name, strerror(errno));
218 } 218 }
219 if (!cache_exist(item)) { 219 if (!cache_exist(item)) {
220 if (!cache_lock(item)) { 220 if (!cache_lock(item)) {
221 sleep(1); 221 sleep(1);
222 goto top; 222 goto top;
223 } 223 }
224 if (!cache_exist(item)) { 224 if (!cache_exist(item)) {
225 cgit_fill_cache(item, 1); 225 cgit_fill_cache(item, 1);
226 cache_unlock(item); 226 cache_unlock(item);
227 } else { 227 } else {
228 cache_cancel_lock(item); 228 cache_cancel_lock(item);
229 } 229 }
230 } else if (cache_expired(item) && cache_lock(item)) { 230 } else if (cache_expired(item) && cache_lock(item)) {
231 if (cache_expired(item)) { 231 if (cache_expired(item)) {
232 cgit_fill_cache(item, 1); 232 cgit_fill_cache(item, 1);
233 cache_unlock(item); 233 cache_unlock(item);
234 } else { 234 } else {
235 cache_cancel_lock(item); 235 cache_cancel_lock(item);
236 } 236 }
237 } 237 }
238} 238}
239 239
240static void cgit_print_cache(struct cacheitem *item) 240static void cgit_print_cache(struct cacheitem *item)
241{ 241{
242 static char buf[4096]; 242 static char buf[4096];
243 ssize_t i; 243 ssize_t i;
244 244
245 int fd = open(item->name, O_RDONLY); 245 int fd = open(item->name, O_RDONLY);
246 if (fd<0) 246 if (fd<0)
247 die("Unable to open cached file %s", item->name); 247 die("Unable to open cached file %s", item->name);
248 248
249 while((i=read(fd, buf, sizeof(buf))) > 0) 249 while((i=read(fd, buf, sizeof(buf))) > 0)
250 write(STDOUT_FILENO, buf, i); 250 write(STDOUT_FILENO, buf, i);
251 251
252 close(fd); 252 close(fd);
253} 253}
254 254
255static void cgit_parse_args(int argc, const char **argv) 255static void cgit_parse_args(int argc, const char **argv)
256{ 256{
257 int i; 257 int i;
258 258
259 for (i = 1; i < argc; i++) { 259 for (i = 1; i < argc; i++) {
260 if (!strncmp(argv[i], "--cache=", 8)) { 260 if (!strncmp(argv[i], "--cache=", 8)) {
261 cgit_cache_root = xstrdup(argv[i]+8); 261 ctx.cfg.cache_root = xstrdup(argv[i]+8);
262 } 262 }
263 if (!strcmp(argv[i], "--nocache")) { 263 if (!strcmp(argv[i], "--nocache")) {
264 cgit_nocache = 1; 264 ctx.cfg.nocache = 1;
265 } 265 }
266 if (!strncmp(argv[i], "--query=", 8)) { 266 if (!strncmp(argv[i], "--query=", 8)) {
267 ctx.qry.raw = xstrdup(argv[i]+8); 267 ctx.qry.raw = xstrdup(argv[i]+8);
268 } 268 }
269 if (!strncmp(argv[i], "--repo=", 7)) { 269 if (!strncmp(argv[i], "--repo=", 7)) {
270 ctx.qry.repo = xstrdup(argv[i]+7); 270 ctx.qry.repo = xstrdup(argv[i]+7);
271 } 271 }
272 if (!strncmp(argv[i], "--page=", 7)) { 272 if (!strncmp(argv[i], "--page=", 7)) {
273 ctx.qry.page = xstrdup(argv[i]+7); 273 ctx.qry.page = xstrdup(argv[i]+7);
274 } 274 }
275 if (!strncmp(argv[i], "--head=", 7)) { 275 if (!strncmp(argv[i], "--head=", 7)) {
276 ctx.qry.head = xstrdup(argv[i]+7); 276 ctx.qry.head = xstrdup(argv[i]+7);
277 ctx.qry.has_symref = 1; 277 ctx.qry.has_symref = 1;
278 } 278 }
279 if (!strncmp(argv[i], "--sha1=", 7)) { 279 if (!strncmp(argv[i], "--sha1=", 7)) {
280 ctx.qry.sha1 = xstrdup(argv[i]+7); 280 ctx.qry.sha1 = xstrdup(argv[i]+7);
281 ctx.qry.has_sha1 = 1; 281 ctx.qry.has_sha1 = 1;
282 } 282 }
283 if (!strncmp(argv[i], "--ofs=", 6)) { 283 if (!strncmp(argv[i], "--ofs=", 6)) {
284 ctx.qry.ofs = atoi(argv[i]+6); 284 ctx.qry.ofs = atoi(argv[i]+6);
285 } 285 }
286 } 286 }
287} 287}
288 288
289int main(int argc, const char **argv) 289int main(int argc, const char **argv)
290{ 290{
291 struct cacheitem item; 291 struct cacheitem item;
292 const char *cgit_config_env = getenv("CGIT_CONFIG"); 292 const char *cgit_config_env = getenv("CGIT_CONFIG");
293 293
294 cgit_prepare_context(&ctx);
294 htmlfd = STDOUT_FILENO; 295 htmlfd = STDOUT_FILENO;
295 item.st.st_mtime = time(NULL); 296 item.st.st_mtime = time(NULL);
296 cgit_repolist.length = 0; 297 cgit_repolist.length = 0;
297 cgit_repolist.count = 0; 298 cgit_repolist.count = 0;
298 cgit_repolist.repos = NULL; 299 cgit_repolist.repos = NULL;
299 300
300 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 301 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
301 cgit_global_config_cb); 302 cgit_global_config_cb);
302 cgit_repo = NULL; 303 cgit_repo = NULL;
303 if (getenv("SCRIPT_NAME")) 304 if (getenv("SCRIPT_NAME"))
304 cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); 305 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
305 if (getenv("QUERY_STRING")) 306 if (getenv("QUERY_STRING"))
306 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 307 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
307 cgit_parse_args(argc, argv); 308 cgit_parse_args(argc, argv);
308 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); 309 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
309 if (!cgit_prepare_cache(&item)) 310 if (!cgit_prepare_cache(&item))
310 return 0; 311 return 0;
311 if (cgit_nocache) { 312 if (ctx.cfg.nocache) {
312 cgit_fill_cache(&item, 0); 313 cgit_fill_cache(&item, 0);
313 } else { 314 } else {
314 cgit_check_cache(&item); 315 cgit_check_cache(&item);
315 cgit_print_cache(&item); 316 cgit_print_cache(&item);
316 } 317 }
317 return 0; 318 return 0;
318} 319}