summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c391
1 files changed, 266 insertions, 125 deletions
diff --git a/cgit.c b/cgit.c
index e8acc03..6ec763f 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,318 +1,459 @@
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#include "cache.h"
11#include "cmd.h"
12#include "configfile.h"
13#include "html.h"
14#include "ui-shared.h"
15
16const char *cgit_version = CGIT_VERSION;
17
18void config_cb(const char *name, const char *value)
19{
20 if (!strcmp(name, "root-title"))
21 ctx.cfg.root_title = xstrdup(value);
22 else if (!strcmp(name, "css"))
23 ctx.cfg.css = xstrdup(value);
24 else if (!strcmp(name, "logo"))
25 ctx.cfg.logo = xstrdup(value);
26 else if (!strcmp(name, "index-header"))
27 ctx.cfg.index_header = xstrdup(value);
28 else if (!strcmp(name, "index-info"))
29 ctx.cfg.index_info = xstrdup(value);
30 else if (!strcmp(name, "logo-link"))
31 ctx.cfg.logo_link = xstrdup(value);
32 else if (!strcmp(name, "module-link"))
33 ctx.cfg.module_link = xstrdup(value);
34 else if (!strcmp(name, "virtual-root")) {
35 ctx.cfg.virtual_root = trim_end(value, '/');
36 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
37 ctx.cfg.virtual_root = "";
38 } else if (!strcmp(name, "nocache"))
39 ctx.cfg.nocache = atoi(value);
40 else if (!strcmp(name, "snapshots"))
41 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
42 else if (!strcmp(name, "enable-index-links"))
43 ctx.cfg.enable_index_links = atoi(value);
44 else if (!strcmp(name, "enable-log-filecount"))
45 ctx.cfg.enable_log_filecount = atoi(value);
46 else if (!strcmp(name, "enable-log-linecount"))
47 ctx.cfg.enable_log_linecount = atoi(value);
48 else if (!strcmp(name, "cache-root"))
49 ctx.cfg.cache_root = xstrdup(value);
50 else if (!strcmp(name, "cache-root-ttl"))
51 ctx.cfg.cache_root_ttl = atoi(value);
52 else if (!strcmp(name, "cache-repo-ttl"))
53 ctx.cfg.cache_repo_ttl = atoi(value);
54 else if (!strcmp(name, "cache-static-ttl"))
55 ctx.cfg.cache_static_ttl = atoi(value);
56 else if (!strcmp(name, "cache-dynamic-ttl"))
57 ctx.cfg.cache_dynamic_ttl = atoi(value);
58 else if (!strcmp(name, "max-message-length"))
59 ctx.cfg.max_msg_len = atoi(value);
60 else if (!strcmp(name, "max-repodesc-length"))
61 ctx.cfg.max_repodesc_len = atoi(value);
62 else if (!strcmp(name, "max-commit-count"))
63 ctx.cfg.max_commit_count = atoi(value);
64 else if (!strcmp(name, "summary-log"))
65 ctx.cfg.summary_log = atoi(value);
66 else if (!strcmp(name, "summary-branches"))
67 ctx.cfg.summary_branches = atoi(value);
68 else if (!strcmp(name, "summary-tags"))
69 ctx.cfg.summary_tags = atoi(value);
70 else if (!strcmp(name, "agefile"))
71 ctx.cfg.agefile = xstrdup(value);
72 else if (!strcmp(name, "renamelimit"))
73 ctx.cfg.renamelimit = atoi(value);
74 else if (!strcmp(name, "robots"))
75 ctx.cfg.robots = xstrdup(value);
76 else if (!strcmp(name, "clone-prefix"))
77 ctx.cfg.clone_prefix = xstrdup(value);
78 else if (!strcmp(name, "repo.group"))
79 ctx.cfg.repo_group = xstrdup(value);
80 else if (!strcmp(name, "repo.url"))
81 ctx.repo = cgit_add_repo(value);
82 else if (!strcmp(name, "repo.name"))
83 ctx.repo->name = xstrdup(value);
84 else if (ctx.repo && !strcmp(name, "repo.path"))
85 ctx.repo->path = trim_end(value, '/');
86 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
87 ctx.repo->clone_url = xstrdup(value);
88 else if (ctx.repo && !strcmp(name, "repo.desc"))
89 ctx.repo->desc = xstrdup(value);
90 else if (ctx.repo && !strcmp(name, "repo.owner"))
91 ctx.repo->owner = xstrdup(value);
92 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
93 ctx.repo->defbranch = xstrdup(value);
94 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
95 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
96 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
97 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
98 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
99 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
100 else if (ctx.repo && !strcmp(name, "repo.module-link"))
101 ctx.repo->module_link= xstrdup(value);
102 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
103 if (*value == '/')
104 ctx.repo->readme = xstrdup(value);
105 else
106 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
107 } else if (!strcmp(name, "include"))
108 parse_configfile(value, config_cb);
109}
110
111static void querystring_cb(const char *name, const char *value)
112{
113 if (!strcmp(name,"r")) {
114 ctx.qry.repo = xstrdup(value);
115 ctx.repo = cgit_get_repoinfo(value);
116 } else if (!strcmp(name, "p")) {
117 ctx.qry.page = xstrdup(value);
118 } else if (!strcmp(name, "url")) {
119 cgit_parse_url(value);
120 } else if (!strcmp(name, "qt")) {
121 ctx.qry.grep = xstrdup(value);
122 } else if (!strcmp(name, "q")) {
123 ctx.qry.search = xstrdup(value);
124 } else if (!strcmp(name, "h")) {
125 ctx.qry.head = xstrdup(value);
126 ctx.qry.has_symref = 1;
127 } else if (!strcmp(name, "id")) {
128 ctx.qry.sha1 = xstrdup(value);
129 ctx.qry.has_sha1 = 1;
130 } else if (!strcmp(name, "id2")) {
131 ctx.qry.sha2 = xstrdup(value);
132 ctx.qry.has_sha1 = 1;
133 } else if (!strcmp(name, "ofs")) {
134 ctx.qry.ofs = atoi(value);
135 } else if (!strcmp(name, "path")) {
136 ctx.qry.path = trim_end(value, '/');
137 } else if (!strcmp(name, "name")) {
138 ctx.qry.name = xstrdup(value);
139 }
140}
141
142static void prepare_context(struct cgit_context *ctx)
143{
144 memset(ctx, 0, sizeof(ctx));
145 ctx->cfg.agefile = "info/web/last-modified";
146 ctx->cfg.cache_dynamic_ttl = 5;
147 ctx->cfg.cache_max_create_time = 5;
148 ctx->cfg.cache_repo_ttl = 5;
149 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
150 ctx->cfg.cache_root_ttl = 5;
151 ctx->cfg.cache_static_ttl = -1;
152 ctx->cfg.css = "/cgit.css";
153 ctx->cfg.logo = "/git-logo.png";
154 ctx->cfg.max_commit_count = 50;
155 ctx->cfg.max_lock_attempts = 5;
156 ctx->cfg.max_msg_len = 60;
157 ctx->cfg.max_repodesc_len = 60;
158 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
159 ctx->cfg.renamelimit = -1;
160 ctx->cfg.robots = "index, nofollow";
161 ctx->cfg.root_title = "Git repository browser";
162 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
163 ctx->page.mimetype = "text/html";
164 ctx->page.charset = PAGE_ENCODING;
165 ctx->page.filename = NULL;
166}
10 167
11static int cgit_prepare_cache(struct cacheitem *item) 168static int cgit_prepare_cache(struct cacheitem *item)
12{ 169{
13 if (!cgit_repo && cgit_query_repo) { 170 if (!ctx.repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 171 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
15 cgit_print_docstart(title, item); 172 "Bad request");
16 cgit_print_pageheader(title, 0); 173 cgit_print_http_headers(&ctx);
17 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); 174 cgit_print_docstart(&ctx);
175 cgit_print_pageheader(&ctx);
176 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 177 cgit_print_docend();
19 return 0; 178 return 0;
20 } 179 }
21 180
22 if (!cgit_repo) { 181 if (!ctx.repo) {
23 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); 182 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
24 item->ttl = cgit_cache_root_ttl; 183 item->ttl = ctx.cfg.cache_root_ttl;
25 return 1; 184 return 1;
26 } 185 }
27 186
28 if (!cgit_cmd) { 187 if (!ctx.qry.page) {
29 item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, 188 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
30 cache_safe_filename(cgit_repo->url), 189 cache_safe_filename(ctx.repo->url),
31 cache_safe_filename(cgit_querystring))); 190 cache_safe_filename(ctx.qry.raw)));
32 item->ttl = cgit_cache_repo_ttl; 191 item->ttl = ctx.cfg.cache_repo_ttl;
33 } else { 192 } else {
34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 193 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
35 cache_safe_filename(cgit_repo->url), 194 cache_safe_filename(ctx.repo->url),
36 cgit_query_page, 195 ctx.qry.page,
37 cache_safe_filename(cgit_querystring))); 196 cache_safe_filename(ctx.qry.raw)));
38 if (cgit_query_has_symref) 197 if (ctx.qry.has_symref)
39 item->ttl = cgit_cache_dynamic_ttl; 198 item->ttl = ctx.cfg.cache_dynamic_ttl;
40 else if (cgit_query_has_sha1) 199 else if (ctx.qry.has_sha1)
41 item->ttl = cgit_cache_static_ttl; 200 item->ttl = ctx.cfg.cache_static_ttl;
42 else 201 else
43 item->ttl = cgit_cache_repo_ttl; 202 item->ttl = ctx.cfg.cache_repo_ttl;
44 } 203 }
45 return 1; 204 return 1;
46} 205}
47 206
48struct refmatch { 207struct refmatch {
49 char *req_ref; 208 char *req_ref;
50 char *first_ref; 209 char *first_ref;
51 int match; 210 int match;
52}; 211};
53 212
54int find_current_ref(const char *refname, const unsigned char *sha1, 213int find_current_ref(const char *refname, const unsigned char *sha1,
55 int flags, void *cb_data) 214 int flags, void *cb_data)
56{ 215{
57 struct refmatch *info; 216 struct refmatch *info;
58 217
59 info = (struct refmatch *)cb_data; 218 info = (struct refmatch *)cb_data;
60 if (!strcmp(refname, info->req_ref)) 219 if (!strcmp(refname, info->req_ref))
61 info->match = 1; 220 info->match = 1;
62 if (!info->first_ref) 221 if (!info->first_ref)
63 info->first_ref = xstrdup(refname); 222 info->first_ref = xstrdup(refname);
64 return info->match; 223 return info->match;
65} 224}
66 225
67char *find_default_branch(struct repoinfo *repo) 226char *find_default_branch(struct cgit_repo *repo)
68{ 227{
69 struct refmatch info; 228 struct refmatch info;
70 229
71 info.req_ref = repo->defbranch; 230 info.req_ref = repo->defbranch;
72 info.first_ref = NULL; 231 info.first_ref = NULL;
73 info.match = 0; 232 info.match = 0;
74 for_each_branch_ref(find_current_ref, &info); 233 for_each_branch_ref(find_current_ref, &info);
75 if (info.match) 234 if (info.match)
76 return info.req_ref; 235 return info.req_ref;
77 else 236 else
78 return info.first_ref; 237 return info.first_ref;
79} 238}
80 239
81static void cgit_print_repo_page(struct cacheitem *item) 240static int prepare_repo_cmd(struct cgit_context *ctx)
82{ 241{
83 char *title, *tmp; 242 char *tmp;
84 int show_search;
85 unsigned char sha1[20]; 243 unsigned char sha1[20];
86 244 int nongit = 0;
87 if (chdir(cgit_repo->path)) { 245
88 title = fmt("%s - %s", cgit_root_title, "Bad request"); 246 setenv("GIT_DIR", ctx->repo->path, 1);
89 cgit_print_docstart(title, item); 247 setup_git_directory_gently(&nongit);
90 cgit_print_pageheader(title, 0); 248 if (nongit) {
91 cgit_print_error(fmt("Unable to scan repository: %s", 249 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
92 strerror(errno))); 250 "config error");
251 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
252 ctx->repo = NULL;
253 cgit_print_http_headers(ctx);
254 cgit_print_docstart(ctx);
255 cgit_print_pageheader(ctx);
256 cgit_print_error(tmp);
93 cgit_print_docend(); 257 cgit_print_docend();
94 return; 258 return 1;
95 } 259 }
260 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
96 261
97 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 262 if (!ctx->qry.head) {
98 show_search = 0; 263 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
99 setenv("GIT_DIR", cgit_repo->path, 1); 264 ctx->repo->defbranch = ctx->qry.head;
100
101 if (!cgit_query_head) {
102 cgit_query_head = xstrdup(find_default_branch(cgit_repo));
103 cgit_repo->defbranch = cgit_query_head;
104 } 265 }
105 266
106 if (!cgit_query_head) { 267 if (!ctx->qry.head) {
107 cgit_print_docstart(title, item); 268 cgit_print_http_headers(ctx);
108 cgit_print_pageheader(title, 0); 269 cgit_print_docstart(ctx);
270 cgit_print_pageheader(ctx);
109 cgit_print_error("Repository seems to be empty"); 271 cgit_print_error("Repository seems to be empty");
110 cgit_print_docend(); 272 cgit_print_docend();
111 return; 273 return 1;
112 } 274 }
113 275
114 if (get_sha1(cgit_query_head, sha1)) { 276 if (get_sha1(ctx->qry.head, sha1)) {
115 tmp = xstrdup(cgit_query_head); 277 tmp = xstrdup(ctx->qry.head);
116 cgit_query_head = cgit_repo->defbranch; 278 ctx->qry.head = ctx->repo->defbranch;
117 cgit_print_docstart(title, item); 279 cgit_print_http_headers(ctx);
118 cgit_print_pageheader(title, 0); 280 cgit_print_docstart(ctx);
281 cgit_print_pageheader(ctx);
119 cgit_print_error(fmt("Invalid branch: %s", tmp)); 282 cgit_print_error(fmt("Invalid branch: %s", tmp));
120 cgit_print_docend(); 283 cgit_print_docend();
121 return; 284 return 1;
122 } 285 }
286 return 0;
287}
123 288
124 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { 289static void process_request(struct cgit_context *ctx)
125 cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, 290{
126 cgit_repobasename(cgit_repo->url), 291 struct cgit_cmd *cmd;
127 cgit_query_path, 292
128 cgit_repo->snapshots ); 293 cmd = cgit_get_cmd(ctx);
294 if (!cmd) {
295 ctx->page.title = "cgit error";
296 ctx->repo = NULL;
297 cgit_print_http_headers(ctx);
298 cgit_print_docstart(ctx);
299 cgit_print_pageheader(ctx);
300 cgit_print_error("Invalid request");
301 cgit_print_docend();
129 return; 302 return;
130 } 303 }
131 304
132 if (cgit_cmd == CMD_PATCH) { 305 if (cmd->want_repo && prepare_repo_cmd(ctx))
133 cgit_print_patch(cgit_query_sha1, item);
134 return; 306 return;
135 }
136 307
137 if (cgit_cmd == CMD_BLOB) { 308 if (cmd->want_layout) {
138 cgit_print_blob(item, cgit_query_sha1, cgit_query_path); 309 cgit_print_http_headers(ctx);
139 return; 310 cgit_print_docstart(ctx);
311 cgit_print_pageheader(ctx);
140 } 312 }
141 313
142 show_search = (cgit_cmd == CMD_LOG); 314 cmd->fn(ctx);
143 cgit_print_docstart(title, item); 315
144 if (!cgit_cmd) { 316 if (cmd->want_layout)
145 cgit_print_pageheader("summary", show_search);
146 cgit_print_summary();
147 cgit_print_docend(); 317 cgit_print_docend();
148 return; 318}
149 }
150 319
151 cgit_print_pageheader(cgit_query_page, show_search); 320static long ttl_seconds(long ttl)
152 321{
153 switch(cgit_cmd) { 322 if (ttl<0)
154 case CMD_LOG: 323 return 60 * 60 * 24 * 365;
155 cgit_print_log(cgit_query_sha1, cgit_query_ofs, 324 else
156 cgit_max_commit_count, cgit_query_grep, cgit_query_search, 325 return ttl * 60;
157 cgit_query_path, 1);
158 break;
159 case CMD_TREE:
160 cgit_print_tree(cgit_query_sha1, cgit_query_path);
161 break;
162 case CMD_COMMIT:
163 cgit_print_commit(cgit_query_sha1);
164 break;
165 case CMD_REFS:
166 cgit_print_refs();
167 break;
168 case CMD_TAG:
169 cgit_print_tag(cgit_query_sha1);
170 break;
171 case CMD_DIFF:
172 cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path);
173 break;
174 default:
175 cgit_print_error("Invalid request");
176 }
177 cgit_print_docend();
178} 326}
179 327
180static void cgit_fill_cache(struct cacheitem *item, int use_cache) 328static void cgit_fill_cache(struct cacheitem *item, int use_cache)
181{ 329{
182 static char buf[PATH_MAX];
183 int stdout2; 330 int stdout2;
184 331
185 getcwd(buf, sizeof(buf));
186 item->st.st_mtime = time(NULL);
187
188 if (use_cache) { 332 if (use_cache) {
189 stdout2 = chk_positive(dup(STDOUT_FILENO), 333 stdout2 = chk_positive(dup(STDOUT_FILENO),
190 "Preserving STDOUT"); 334 "Preserving STDOUT");
191 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 335 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 336 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
193 } 337 }
194 338
195 if (cgit_repo) 339 ctx.page.modified = time(NULL);
196 cgit_print_repo_page(item); 340 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
197 else 341 process_request(&ctx);
198 cgit_print_repolist(item);
199 342
200 if (use_cache) { 343 if (use_cache) {
201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 344 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
202 chk_positive(dup2(stdout2, STDOUT_FILENO), 345 chk_positive(dup2(stdout2, STDOUT_FILENO),
203 "Restoring original STDOUT"); 346 "Restoring original STDOUT");
204 chk_zero(close(stdout2), "Closing temporary STDOUT"); 347 chk_zero(close(stdout2), "Closing temporary STDOUT");
205 } 348 }
206
207 chdir(buf);
208} 349}
209 350
210static void cgit_check_cache(struct cacheitem *item) 351static void cgit_check_cache(struct cacheitem *item)
211{ 352{
212 int i = 0; 353 int i = 0;
213 354
214 top: 355 top:
215 if (++i > cgit_max_lock_attempts) { 356 if (++i > ctx.cfg.max_lock_attempts) {
216 die("cgit_refresh_cache: unable to lock %s: %s", 357 die("cgit_refresh_cache: unable to lock %s: %s",
217 item->name, strerror(errno)); 358 item->name, strerror(errno));
218 } 359 }
219 if (!cache_exist(item)) { 360 if (!cache_exist(item)) {
220 if (!cache_lock(item)) { 361 if (!cache_lock(item)) {
221 sleep(1); 362 sleep(1);
222 goto top; 363 goto top;
223 } 364 }
224 if (!cache_exist(item)) { 365 if (!cache_exist(item)) {
225 cgit_fill_cache(item, 1); 366 cgit_fill_cache(item, 1);
226 cache_unlock(item); 367 cache_unlock(item);
227 } else { 368 } else {
228 cache_cancel_lock(item); 369 cache_cancel_lock(item);
229 } 370 }
230 } else if (cache_expired(item) && cache_lock(item)) { 371 } else if (cache_expired(item) && cache_lock(item)) {
231 if (cache_expired(item)) { 372 if (cache_expired(item)) {
232 cgit_fill_cache(item, 1); 373 cgit_fill_cache(item, 1);
233 cache_unlock(item); 374 cache_unlock(item);
234 } else { 375 } else {
235 cache_cancel_lock(item); 376 cache_cancel_lock(item);
236 } 377 }
237 } 378 }
238} 379}
239 380
240static void cgit_print_cache(struct cacheitem *item) 381static void cgit_print_cache(struct cacheitem *item)
241{ 382{
242 static char buf[4096]; 383 static char buf[4096];
243 ssize_t i; 384 ssize_t i;
244 385
245 int fd = open(item->name, O_RDONLY); 386 int fd = open(item->name, O_RDONLY);
246 if (fd<0) 387 if (fd<0)
247 die("Unable to open cached file %s", item->name); 388 die("Unable to open cached file %s", item->name);
248 389
249 while((i=read(fd, buf, sizeof(buf))) > 0) 390 while((i=read(fd, buf, sizeof(buf))) > 0)
250 write(STDOUT_FILENO, buf, i); 391 write(STDOUT_FILENO, buf, i);
251 392
252 close(fd); 393 close(fd);
253} 394}
254 395
255static void cgit_parse_args(int argc, const char **argv) 396static void cgit_parse_args(int argc, const char **argv)
256{ 397{
257 int i; 398 int i;
258 399
259 for (i = 1; i < argc; i++) { 400 for (i = 1; i < argc; i++) {
260 if (!strncmp(argv[i], "--cache=", 8)) { 401 if (!strncmp(argv[i], "--cache=", 8)) {
261 cgit_cache_root = xstrdup(argv[i]+8); 402 ctx.cfg.cache_root = xstrdup(argv[i]+8);
262 } 403 }
263 if (!strcmp(argv[i], "--nocache")) { 404 if (!strcmp(argv[i], "--nocache")) {
264 cgit_nocache = 1; 405 ctx.cfg.nocache = 1;
265 } 406 }
266 if (!strncmp(argv[i], "--query=", 8)) { 407 if (!strncmp(argv[i], "--query=", 8)) {
267 cgit_querystring = xstrdup(argv[i]+8); 408 ctx.qry.raw = xstrdup(argv[i]+8);
268 } 409 }
269 if (!strncmp(argv[i], "--repo=", 7)) { 410 if (!strncmp(argv[i], "--repo=", 7)) {
270 cgit_query_repo = xstrdup(argv[i]+7); 411 ctx.qry.repo = xstrdup(argv[i]+7);
271 } 412 }
272 if (!strncmp(argv[i], "--page=", 7)) { 413 if (!strncmp(argv[i], "--page=", 7)) {
273 cgit_query_page = xstrdup(argv[i]+7); 414 ctx.qry.page = xstrdup(argv[i]+7);
274 } 415 }
275 if (!strncmp(argv[i], "--head=", 7)) { 416 if (!strncmp(argv[i], "--head=", 7)) {
276 cgit_query_head = xstrdup(argv[i]+7); 417 ctx.qry.head = xstrdup(argv[i]+7);
277 cgit_query_has_symref = 1; 418 ctx.qry.has_symref = 1;
278 } 419 }
279 if (!strncmp(argv[i], "--sha1=", 7)) { 420 if (!strncmp(argv[i], "--sha1=", 7)) {
280 cgit_query_sha1 = xstrdup(argv[i]+7); 421 ctx.qry.sha1 = xstrdup(argv[i]+7);
281 cgit_query_has_sha1 = 1; 422 ctx.qry.has_sha1 = 1;
282 } 423 }
283 if (!strncmp(argv[i], "--ofs=", 6)) { 424 if (!strncmp(argv[i], "--ofs=", 6)) {
284 cgit_query_ofs = atoi(argv[i]+6); 425 ctx.qry.ofs = atoi(argv[i]+6);
285 } 426 }
286 } 427 }
287} 428}
288 429
289int main(int argc, const char **argv) 430int main(int argc, const char **argv)
290{ 431{
291 struct cacheitem item; 432 struct cacheitem item;
292 const char *cgit_config_env = getenv("CGIT_CONFIG"); 433 const char *cgit_config_env = getenv("CGIT_CONFIG");
293 434
294 htmlfd = STDOUT_FILENO; 435 prepare_context(&ctx);
295 item.st.st_mtime = time(NULL); 436 item.st.st_mtime = time(NULL);
296 cgit_repolist.length = 0; 437 cgit_repolist.length = 0;
297 cgit_repolist.count = 0; 438 cgit_repolist.count = 0;
298 cgit_repolist.repos = NULL; 439 cgit_repolist.repos = NULL;
299 440
300 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 441 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
301 cgit_global_config_cb); 442 config_cb);
302 cgit_repo = NULL; 443 ctx.repo = NULL;
303 if (getenv("SCRIPT_NAME")) 444 if (getenv("SCRIPT_NAME"))
304 cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); 445 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
305 if (getenv("QUERY_STRING")) 446 if (getenv("QUERY_STRING"))
306 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 447 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
307 cgit_parse_args(argc, argv); 448 cgit_parse_args(argc, argv);
308 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 449 http_parse_querystring(ctx.qry.raw, querystring_cb);
309 if (!cgit_prepare_cache(&item)) 450 if (!cgit_prepare_cache(&item))
310 return 0; 451 return 0;
311 if (cgit_nocache) { 452 if (ctx.cfg.nocache) {
312 cgit_fill_cache(&item, 0); 453 cgit_fill_cache(&item, 0);
313 } else { 454 } else {
314 cgit_check_cache(&item); 455 cgit_check_cache(&item);
315 cgit_print_cache(&item); 456 cgit_print_cache(&item);
316 } 457 }
317 return 0; 458 return 0;
318} 459}