blob: a402758fab53bc2c099e7b803fa8deee68226250 [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* cgit.c: cgi for the git scm
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli0d169ad2006-12-09 15:18:17 +01009#include "cgit.h"
Lars Hjemliee4056b2008-03-27 09:22:13 +010010#include "cache.h"
Lars Hjemlif3c1a182008-03-24 00:51:19 +010011#include "cmd.h"
Lars Hjemli20a33542008-03-28 00:09:11 +010012#include "configfile.h"
Lars Hjemlie87e8962008-04-08 21:11:36 +020013#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010014#include "ui-shared.h"
Lars Hjemli0d169ad2006-12-09 15:18:17 +010015
Lars Hjemli92908af2008-03-24 23:10:59 +010016const char *cgit_version = CGIT_VERSION;
17
Lars Hjemli163037e2008-03-24 17:26:08 +010018void config_cb(const char *name, const char *value)
19{
20 if (!strcmp(name, "root-title"))
21 ctx.cfg.root_title = xstrdup(value);
Lars Hjemli4c991602008-04-29 00:55:34 +020022 else if (!strcmp(name, "root-desc"))
23 ctx.cfg.root_desc = xstrdup(value);
Lars Hjemlic6431a72008-04-29 01:06:30 +020024 else if (!strcmp(name, "root-readme"))
25 ctx.cfg.root_readme = xstrdup(value);
Lars Hjemli163037e2008-03-24 17:26:08 +010026 else if (!strcmp(name, "css"))
27 ctx.cfg.css = xstrdup(value);
28 else if (!strcmp(name, "logo"))
29 ctx.cfg.logo = xstrdup(value);
30 else if (!strcmp(name, "index-header"))
31 ctx.cfg.index_header = xstrdup(value);
32 else if (!strcmp(name, "index-info"))
33 ctx.cfg.index_info = xstrdup(value);
34 else if (!strcmp(name, "logo-link"))
35 ctx.cfg.logo_link = xstrdup(value);
36 else if (!strcmp(name, "module-link"))
37 ctx.cfg.module_link = xstrdup(value);
38 else if (!strcmp(name, "virtual-root")) {
39 ctx.cfg.virtual_root = trim_end(value, '/');
40 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
41 ctx.cfg.virtual_root = "";
42 } else if (!strcmp(name, "nocache"))
43 ctx.cfg.nocache = atoi(value);
44 else if (!strcmp(name, "snapshots"))
45 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
46 else if (!strcmp(name, "enable-index-links"))
47 ctx.cfg.enable_index_links = atoi(value);
48 else if (!strcmp(name, "enable-log-filecount"))
49 ctx.cfg.enable_log_filecount = atoi(value);
50 else if (!strcmp(name, "enable-log-linecount"))
51 ctx.cfg.enable_log_linecount = atoi(value);
52 else if (!strcmp(name, "cache-root"))
53 ctx.cfg.cache_root = xstrdup(value);
54 else if (!strcmp(name, "cache-root-ttl"))
55 ctx.cfg.cache_root_ttl = atoi(value);
56 else if (!strcmp(name, "cache-repo-ttl"))
57 ctx.cfg.cache_repo_ttl = atoi(value);
58 else if (!strcmp(name, "cache-static-ttl"))
59 ctx.cfg.cache_static_ttl = atoi(value);
60 else if (!strcmp(name, "cache-dynamic-ttl"))
61 ctx.cfg.cache_dynamic_ttl = atoi(value);
62 else if (!strcmp(name, "max-message-length"))
63 ctx.cfg.max_msg_len = atoi(value);
64 else if (!strcmp(name, "max-repodesc-length"))
65 ctx.cfg.max_repodesc_len = atoi(value);
66 else if (!strcmp(name, "max-commit-count"))
67 ctx.cfg.max_commit_count = atoi(value);
68 else if (!strcmp(name, "summary-log"))
69 ctx.cfg.summary_log = atoi(value);
70 else if (!strcmp(name, "summary-branches"))
71 ctx.cfg.summary_branches = atoi(value);
72 else if (!strcmp(name, "summary-tags"))
73 ctx.cfg.summary_tags = atoi(value);
74 else if (!strcmp(name, "agefile"))
75 ctx.cfg.agefile = xstrdup(value);
76 else if (!strcmp(name, "renamelimit"))
77 ctx.cfg.renamelimit = atoi(value);
78 else if (!strcmp(name, "robots"))
79 ctx.cfg.robots = xstrdup(value);
80 else if (!strcmp(name, "clone-prefix"))
81 ctx.cfg.clone_prefix = xstrdup(value);
82 else if (!strcmp(name, "repo.group"))
83 ctx.cfg.repo_group = xstrdup(value);
84 else if (!strcmp(name, "repo.url"))
85 ctx.repo = cgit_add_repo(value);
86 else if (!strcmp(name, "repo.name"))
87 ctx.repo->name = xstrdup(value);
88 else if (ctx.repo && !strcmp(name, "repo.path"))
89 ctx.repo->path = trim_end(value, '/');
90 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
91 ctx.repo->clone_url = xstrdup(value);
92 else if (ctx.repo && !strcmp(name, "repo.desc"))
93 ctx.repo->desc = xstrdup(value);
94 else if (ctx.repo && !strcmp(name, "repo.owner"))
95 ctx.repo->owner = xstrdup(value);
96 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
97 ctx.repo->defbranch = xstrdup(value);
98 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
99 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
100 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
101 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
102 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
103 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
104 else if (ctx.repo && !strcmp(name, "repo.module-link"))
105 ctx.repo->module_link= xstrdup(value);
106 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
107 if (*value == '/')
108 ctx.repo->readme = xstrdup(value);
109 else
110 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
111 } else if (!strcmp(name, "include"))
Lars Hjemli20a33542008-03-28 00:09:11 +0100112 parse_configfile(value, config_cb);
Lars Hjemli163037e2008-03-24 17:26:08 +0100113}
114
115static void querystring_cb(const char *name, const char *value)
116{
117 if (!strcmp(name,"r")) {
118 ctx.qry.repo = xstrdup(value);
119 ctx.repo = cgit_get_repoinfo(value);
120 } else if (!strcmp(name, "p")) {
121 ctx.qry.page = xstrdup(value);
122 } else if (!strcmp(name, "url")) {
123 cgit_parse_url(value);
124 } else if (!strcmp(name, "qt")) {
125 ctx.qry.grep = xstrdup(value);
126 } else if (!strcmp(name, "q")) {
127 ctx.qry.search = xstrdup(value);
128 } else if (!strcmp(name, "h")) {
129 ctx.qry.head = xstrdup(value);
130 ctx.qry.has_symref = 1;
131 } else if (!strcmp(name, "id")) {
132 ctx.qry.sha1 = xstrdup(value);
133 ctx.qry.has_sha1 = 1;
134 } else if (!strcmp(name, "id2")) {
135 ctx.qry.sha2 = xstrdup(value);
136 ctx.qry.has_sha1 = 1;
137 } else if (!strcmp(name, "ofs")) {
138 ctx.qry.ofs = atoi(value);
139 } else if (!strcmp(name, "path")) {
140 ctx.qry.path = trim_end(value, '/');
141 } else if (!strcmp(name, "name")) {
142 ctx.qry.name = xstrdup(value);
143 }
144}
145
146static void prepare_context(struct cgit_context *ctx)
147{
148 memset(ctx, 0, sizeof(ctx));
149 ctx->cfg.agefile = "info/web/last-modified";
150 ctx->cfg.cache_dynamic_ttl = 5;
151 ctx->cfg.cache_max_create_time = 5;
152 ctx->cfg.cache_repo_ttl = 5;
153 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
154 ctx->cfg.cache_root_ttl = 5;
155 ctx->cfg.cache_static_ttl = -1;
156 ctx->cfg.css = "/cgit.css";
157 ctx->cfg.logo = "/git-logo.png";
158 ctx->cfg.max_commit_count = 50;
159 ctx->cfg.max_lock_attempts = 5;
160 ctx->cfg.max_msg_len = 60;
161 ctx->cfg.max_repodesc_len = 60;
162 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
163 ctx->cfg.renamelimit = -1;
164 ctx->cfg.robots = "index, nofollow";
165 ctx->cfg.root_title = "Git repository browser";
Lars Hjemli4c991602008-04-29 00:55:34 +0200166 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
Lars Hjemli163037e2008-03-24 17:26:08 +0100167 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
168 ctx->page.mimetype = "text/html";
169 ctx->page.charset = PAGE_ENCODING;
170 ctx->page.filename = NULL;
171}
172
Lars Hjemlice1c7332007-02-03 15:02:55 +0100173static int cgit_prepare_cache(struct cacheitem *item)
Lars Hjemli83a5f352007-01-12 00:00:15 +0100174{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100175 if (!ctx.repo && ctx.qry.repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100176 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
177 "Bad request");
178 cgit_print_http_headers(&ctx);
179 cgit_print_docstart(&ctx);
180 cgit_print_pageheader(&ctx);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100181 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +0100182 cgit_print_docend();
183 return 0;
184 }
185
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100186 if (!ctx.repo) {
Lars Hjemli78031f92008-04-14 23:07:52 +0200187 item->name = xstrdup(fmt("%s/index.%s.html",
188 ctx.cfg.cache_root,
189 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100190 item->ttl = ctx.cfg.cache_root_ttl;
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200191 return 1;
192 }
193
Lars Hjemlidc3282f2008-03-25 02:00:09 +0100194 if (!ctx.qry.page) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100195 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100196 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100197 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100198 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100199 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100200 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100201 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100202 ctx.qry.page,
203 cache_safe_filename(ctx.qry.raw)));
204 if (ctx.qry.has_symref)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100205 item->ttl = ctx.cfg.cache_dynamic_ttl;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100206 else if (ctx.qry.has_sha1)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100207 item->ttl = ctx.cfg.cache_static_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100208 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100209 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100210 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100211 return 1;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100212}
213
Lars Hjemlif80ff372008-01-04 13:43:40 +0100214struct refmatch {
215 char *req_ref;
216 char *first_ref;
217 int match;
218};
219
220int find_current_ref(const char *refname, const unsigned char *sha1,
221 int flags, void *cb_data)
222{
223 struct refmatch *info;
224
225 info = (struct refmatch *)cb_data;
226 if (!strcmp(refname, info->req_ref))
227 info->match = 1;
228 if (!info->first_ref)
229 info->first_ref = xstrdup(refname);
230 return info->match;
231}
232
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100233char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +0100234{
235 struct refmatch info;
236
237 info.req_ref = repo->defbranch;
238 info.first_ref = NULL;
239 info.match = 0;
240 for_each_branch_ref(find_current_ref, &info);
241 if (info.match)
242 return info.req_ref;
243 else
244 return info.first_ref;
245}
246
Lars Hjemlie0e44782008-03-24 01:09:39 +0100247static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100248{
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100249 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100250 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +0100251 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +0200252
Lars Hjemlie0e44782008-03-24 01:09:39 +0100253 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100254 setup_git_directory_gently(&nongit);
255 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +0100256 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
257 "config error");
258 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
259 ctx->repo = NULL;
260 cgit_print_http_headers(ctx);
261 cgit_print_docstart(ctx);
262 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100263 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100264 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100265 return 1;
266 }
267 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
268
269 if (!ctx->qry.head) {
270 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
271 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100272 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100273
Lars Hjemlie0e44782008-03-24 01:09:39 +0100274 if (!ctx->qry.head) {
275 cgit_print_http_headers(ctx);
276 cgit_print_docstart(ctx);
277 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100278 cgit_print_error("Repository seems to be empty");
279 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100280 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100281 }
282
Lars Hjemlie0e44782008-03-24 01:09:39 +0100283 if (get_sha1(ctx->qry.head, sha1)) {
284 tmp = xstrdup(ctx->qry.head);
285 ctx->qry.head = ctx->repo->defbranch;
286 cgit_print_http_headers(ctx);
287 cgit_print_docstart(ctx);
288 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100289 cgit_print_error(fmt("Invalid branch: %s", tmp));
290 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100291 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100292 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100293 return 0;
294}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100295
Lars Hjemlie0e44782008-03-24 01:09:39 +0100296static void process_request(struct cgit_context *ctx)
297{
298 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100299
Lars Hjemlie0e44782008-03-24 01:09:39 +0100300 cmd = cgit_get_cmd(ctx);
301 if (!cmd) {
302 ctx->page.title = "cgit error";
303 ctx->repo = NULL;
304 cgit_print_http_headers(ctx);
305 cgit_print_docstart(ctx);
306 cgit_print_pageheader(ctx);
307 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100308 cgit_print_docend();
309 return;
310 }
311
Lars Hjemli651ef792008-04-29 01:01:30 +0200312 if (cmd->want_repo && !ctx->repo) {
313 cgit_print_http_headers(ctx);
314 cgit_print_docstart(ctx);
315 cgit_print_pageheader(ctx);
316 cgit_print_error(fmt("No repository selected"));
317 cgit_print_docend();
318 return;
319 }
320
321 if (ctx->repo && prepare_repo_cmd(ctx))
Lars Hjemlie0e44782008-03-24 01:09:39 +0100322 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100323
Lars Hjemlie0e44782008-03-24 01:09:39 +0100324 if (cmd->want_layout) {
325 cgit_print_http_headers(ctx);
326 cgit_print_docstart(ctx);
327 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100328 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100329
330 cmd->fn(ctx);
331
332 if (cmd->want_layout)
333 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100334}
335
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100336static long ttl_seconds(long ttl)
337{
338 if (ttl<0)
339 return 60 * 60 * 24 * 365;
340 else
341 return ttl * 60;
342}
343
Lars Hjemliab2ab952007-02-08 13:53:13 +0100344static void cgit_fill_cache(struct cacheitem *item, int use_cache)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100345{
Lars Hjemliab2ab952007-02-08 13:53:13 +0100346 int stdout2;
Lars Hjemli7c849d92006-12-16 13:55:58 +0100347
Lars Hjemliab2ab952007-02-08 13:53:13 +0100348 if (use_cache) {
Lars Hjemli5b94c962007-05-14 22:58:01 +0200349 stdout2 = chk_positive(dup(STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100350 "Preserving STDOUT");
351 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
352 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
353 }
354
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100355 ctx.page.modified = time(NULL);
356 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100357 process_request(&ctx);
Lars Hjemliab2ab952007-02-08 13:53:13 +0100358
359 if (use_cache) {
360 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
Lars Hjemli5b94c962007-05-14 22:58:01 +0200361 chk_positive(dup2(stdout2, STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100362 "Restoring original STDOUT");
363 chk_zero(close(stdout2), "Closing temporary STDOUT");
364 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100365}
366
Lars Hjemli44923f82006-12-11 17:25:41 +0100367static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +0100368{
Lars Hjemli318d1062006-12-11 12:10:12 +0100369 int i = 0;
370
Lars Hjemli25105d72006-12-10 22:31:36 +0100371 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100372 if (++i > ctx.cfg.max_lock_attempts) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100373 die("cgit_refresh_cache: unable to lock %s: %s",
374 item->name, strerror(errno));
375 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100376 if (!cache_exist(item)) {
377 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100378 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100379 goto top;
380 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100381 if (!cache_exist(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100382 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100383 cache_unlock(item);
384 } else {
385 cache_cancel_lock(item);
386 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100387 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100388 if (cache_expired(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100389 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100390 cache_unlock(item);
391 } else {
392 cache_cancel_lock(item);
393 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100394 }
395}
396
397static void cgit_print_cache(struct cacheitem *item)
398{
399 static char buf[4096];
400 ssize_t i;
401
402 int fd = open(item->name, O_RDONLY);
403 if (fd<0)
404 die("Unable to open cached file %s", item->name);
405
406 while((i=read(fd, buf, sizeof(buf))) > 0)
407 write(STDOUT_FILENO, buf, i);
408
409 close(fd);
410}
411
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100412static void cgit_parse_args(int argc, const char **argv)
413{
414 int i;
415
416 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100417 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100418 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100419 }
420 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100421 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100422 }
423 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100424 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100425 }
426 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100427 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100428 }
429 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100430 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100431 }
432 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100433 ctx.qry.head = xstrdup(argv[i]+7);
434 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100435 }
436 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100437 ctx.qry.sha1 = xstrdup(argv[i]+7);
438 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100439 }
440 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100441 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100442 }
443 }
444}
445
Lars Hjemli25105d72006-12-10 22:31:36 +0100446int main(int argc, const char **argv)
447{
Lars Hjemli44923f82006-12-11 17:25:41 +0100448 struct cacheitem item;
Michael Krelin51f65472007-07-02 02:29:12 +0200449 const char *cgit_config_env = getenv("CGIT_CONFIG");
Lars Hjemli44923f82006-12-11 17:25:41 +0100450
Lars Hjemli163037e2008-03-24 17:26:08 +0100451 prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100452 item.st.st_mtime = time(NULL);
453 cgit_repolist.length = 0;
454 cgit_repolist.count = 0;
455 cgit_repolist.repos = NULL;
456
Lars Hjemli20a33542008-03-28 00:09:11 +0100457 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
Lars Hjemli163037e2008-03-24 17:26:08 +0100458 config_cb);
Lars Hjemlic6f74762008-04-08 21:27:12 +0200459 ctx.repo = NULL;
Lars Hjemliea2831f2007-05-15 00:48:31 +0200460 if (getenv("SCRIPT_NAME"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100461 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100462 if (getenv("QUERY_STRING"))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100463 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100464 cgit_parse_args(argc, argv);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200465 http_parse_querystring(ctx.qry.raw, querystring_cb);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100466 if (!cgit_prepare_cache(&item))
467 return 0;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100468 if (ctx.cfg.nocache) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100469 cgit_fill_cache(&item, 0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100470 } else {
471 cgit_check_cache(&item);
472 cgit_print_cache(&item);
473 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100474 return 0;
475}