blob: b3dd1199c87baa72c5eec2de450f60bc8a90837e [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 Hjemlif3c1a182008-03-24 00:51:19 +010010#include "cmd.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010011#include "ui-shared.h"
Lars Hjemli0d169ad2006-12-09 15:18:17 +010012
Lars Hjemli163037e2008-03-24 17:26:08 +010013void config_cb(const char *name, const char *value)
14{
15 if (!strcmp(name, "root-title"))
16 ctx.cfg.root_title = xstrdup(value);
17 else if (!strcmp(name, "css"))
18 ctx.cfg.css = xstrdup(value);
19 else if (!strcmp(name, "logo"))
20 ctx.cfg.logo = xstrdup(value);
21 else if (!strcmp(name, "index-header"))
22 ctx.cfg.index_header = xstrdup(value);
23 else if (!strcmp(name, "index-info"))
24 ctx.cfg.index_info = xstrdup(value);
25 else if (!strcmp(name, "logo-link"))
26 ctx.cfg.logo_link = xstrdup(value);
27 else if (!strcmp(name, "module-link"))
28 ctx.cfg.module_link = xstrdup(value);
29 else if (!strcmp(name, "virtual-root")) {
30 ctx.cfg.virtual_root = trim_end(value, '/');
31 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
32 ctx.cfg.virtual_root = "";
33 } else if (!strcmp(name, "nocache"))
34 ctx.cfg.nocache = atoi(value);
35 else if (!strcmp(name, "snapshots"))
36 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
37 else if (!strcmp(name, "enable-index-links"))
38 ctx.cfg.enable_index_links = atoi(value);
39 else if (!strcmp(name, "enable-log-filecount"))
40 ctx.cfg.enable_log_filecount = atoi(value);
41 else if (!strcmp(name, "enable-log-linecount"))
42 ctx.cfg.enable_log_linecount = atoi(value);
43 else if (!strcmp(name, "cache-root"))
44 ctx.cfg.cache_root = xstrdup(value);
45 else if (!strcmp(name, "cache-root-ttl"))
46 ctx.cfg.cache_root_ttl = atoi(value);
47 else if (!strcmp(name, "cache-repo-ttl"))
48 ctx.cfg.cache_repo_ttl = atoi(value);
49 else if (!strcmp(name, "cache-static-ttl"))
50 ctx.cfg.cache_static_ttl = atoi(value);
51 else if (!strcmp(name, "cache-dynamic-ttl"))
52 ctx.cfg.cache_dynamic_ttl = atoi(value);
53 else if (!strcmp(name, "max-message-length"))
54 ctx.cfg.max_msg_len = atoi(value);
55 else if (!strcmp(name, "max-repodesc-length"))
56 ctx.cfg.max_repodesc_len = atoi(value);
57 else if (!strcmp(name, "max-commit-count"))
58 ctx.cfg.max_commit_count = atoi(value);
59 else if (!strcmp(name, "summary-log"))
60 ctx.cfg.summary_log = atoi(value);
61 else if (!strcmp(name, "summary-branches"))
62 ctx.cfg.summary_branches = atoi(value);
63 else if (!strcmp(name, "summary-tags"))
64 ctx.cfg.summary_tags = atoi(value);
65 else if (!strcmp(name, "agefile"))
66 ctx.cfg.agefile = xstrdup(value);
67 else if (!strcmp(name, "renamelimit"))
68 ctx.cfg.renamelimit = atoi(value);
69 else if (!strcmp(name, "robots"))
70 ctx.cfg.robots = xstrdup(value);
71 else if (!strcmp(name, "clone-prefix"))
72 ctx.cfg.clone_prefix = xstrdup(value);
73 else if (!strcmp(name, "repo.group"))
74 ctx.cfg.repo_group = xstrdup(value);
75 else if (!strcmp(name, "repo.url"))
76 ctx.repo = cgit_add_repo(value);
77 else if (!strcmp(name, "repo.name"))
78 ctx.repo->name = xstrdup(value);
79 else if (ctx.repo && !strcmp(name, "repo.path"))
80 ctx.repo->path = trim_end(value, '/');
81 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
82 ctx.repo->clone_url = xstrdup(value);
83 else if (ctx.repo && !strcmp(name, "repo.desc"))
84 ctx.repo->desc = xstrdup(value);
85 else if (ctx.repo && !strcmp(name, "repo.owner"))
86 ctx.repo->owner = xstrdup(value);
87 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
88 ctx.repo->defbranch = xstrdup(value);
89 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
90 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
91 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
92 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
93 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
94 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
95 else if (ctx.repo && !strcmp(name, "repo.module-link"))
96 ctx.repo->module_link= xstrdup(value);
97 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
98 if (*value == '/')
99 ctx.repo->readme = xstrdup(value);
100 else
101 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
102 } else if (!strcmp(name, "include"))
103 cgit_read_config(value, config_cb);
104}
105
106static void querystring_cb(const char *name, const char *value)
107{
108 if (!strcmp(name,"r")) {
109 ctx.qry.repo = xstrdup(value);
110 ctx.repo = cgit_get_repoinfo(value);
111 } else if (!strcmp(name, "p")) {
112 ctx.qry.page = xstrdup(value);
113 } else if (!strcmp(name, "url")) {
114 cgit_parse_url(value);
115 } else if (!strcmp(name, "qt")) {
116 ctx.qry.grep = xstrdup(value);
117 } else if (!strcmp(name, "q")) {
118 ctx.qry.search = xstrdup(value);
119 } else if (!strcmp(name, "h")) {
120 ctx.qry.head = xstrdup(value);
121 ctx.qry.has_symref = 1;
122 } else if (!strcmp(name, "id")) {
123 ctx.qry.sha1 = xstrdup(value);
124 ctx.qry.has_sha1 = 1;
125 } else if (!strcmp(name, "id2")) {
126 ctx.qry.sha2 = xstrdup(value);
127 ctx.qry.has_sha1 = 1;
128 } else if (!strcmp(name, "ofs")) {
129 ctx.qry.ofs = atoi(value);
130 } else if (!strcmp(name, "path")) {
131 ctx.qry.path = trim_end(value, '/');
132 } else if (!strcmp(name, "name")) {
133 ctx.qry.name = xstrdup(value);
134 }
135}
136
137static void prepare_context(struct cgit_context *ctx)
138{
139 memset(ctx, 0, sizeof(ctx));
140 ctx->cfg.agefile = "info/web/last-modified";
141 ctx->cfg.cache_dynamic_ttl = 5;
142 ctx->cfg.cache_max_create_time = 5;
143 ctx->cfg.cache_repo_ttl = 5;
144 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
145 ctx->cfg.cache_root_ttl = 5;
146 ctx->cfg.cache_static_ttl = -1;
147 ctx->cfg.css = "/cgit.css";
148 ctx->cfg.logo = "/git-logo.png";
149 ctx->cfg.max_commit_count = 50;
150 ctx->cfg.max_lock_attempts = 5;
151 ctx->cfg.max_msg_len = 60;
152 ctx->cfg.max_repodesc_len = 60;
153 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
154 ctx->cfg.renamelimit = -1;
155 ctx->cfg.robots = "index, nofollow";
156 ctx->cfg.root_title = "Git repository browser";
157 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
158 ctx->page.mimetype = "text/html";
159 ctx->page.charset = PAGE_ENCODING;
160 ctx->page.filename = NULL;
161}
162
Lars Hjemlice1c7332007-02-03 15:02:55 +0100163static int cgit_prepare_cache(struct cacheitem *item)
Lars Hjemli83a5f352007-01-12 00:00:15 +0100164{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100165 if (!ctx.repo && ctx.qry.repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100166 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
167 "Bad request");
168 cgit_print_http_headers(&ctx);
169 cgit_print_docstart(&ctx);
170 cgit_print_pageheader(&ctx);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100171 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +0100172 cgit_print_docend();
173 return 0;
174 }
175
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100176 if (!ctx.repo) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100177 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
178 item->ttl = ctx.cfg.cache_root_ttl;
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200179 return 1;
180 }
181
182 if (!cgit_cmd) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100183 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100184 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100185 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100186 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100187 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100188 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100189 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100190 ctx.qry.page,
191 cache_safe_filename(ctx.qry.raw)));
192 if (ctx.qry.has_symref)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100193 item->ttl = ctx.cfg.cache_dynamic_ttl;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100194 else if (ctx.qry.has_sha1)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100195 item->ttl = ctx.cfg.cache_static_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100196 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100197 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100198 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100199 return 1;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100200}
201
Lars Hjemlif80ff372008-01-04 13:43:40 +0100202struct refmatch {
203 char *req_ref;
204 char *first_ref;
205 int match;
206};
207
208int find_current_ref(const char *refname, const unsigned char *sha1,
209 int flags, void *cb_data)
210{
211 struct refmatch *info;
212
213 info = (struct refmatch *)cb_data;
214 if (!strcmp(refname, info->req_ref))
215 info->match = 1;
216 if (!info->first_ref)
217 info->first_ref = xstrdup(refname);
218 return info->match;
219}
220
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100221char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +0100222{
223 struct refmatch info;
224
225 info.req_ref = repo->defbranch;
226 info.first_ref = NULL;
227 info.match = 0;
228 for_each_branch_ref(find_current_ref, &info);
229 if (info.match)
230 return info.req_ref;
231 else
232 return info.first_ref;
233}
234
Lars Hjemlie0e44782008-03-24 01:09:39 +0100235static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100236{
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100237 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100238 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +0100239 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +0200240
Lars Hjemlie0e44782008-03-24 01:09:39 +0100241 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100242 setup_git_directory_gently(&nongit);
243 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +0100244 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
245 "config error");
246 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
247 ctx->repo = NULL;
248 cgit_print_http_headers(ctx);
249 cgit_print_docstart(ctx);
250 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100251 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100252 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100253 return 1;
254 }
255 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
256
257 if (!ctx->qry.head) {
258 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
259 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100260 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100261
Lars Hjemlie0e44782008-03-24 01:09:39 +0100262 if (!ctx->qry.head) {
263 cgit_print_http_headers(ctx);
264 cgit_print_docstart(ctx);
265 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100266 cgit_print_error("Repository seems to be empty");
267 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100268 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100269 }
270
Lars Hjemlie0e44782008-03-24 01:09:39 +0100271 if (get_sha1(ctx->qry.head, sha1)) {
272 tmp = xstrdup(ctx->qry.head);
273 ctx->qry.head = ctx->repo->defbranch;
274 cgit_print_http_headers(ctx);
275 cgit_print_docstart(ctx);
276 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100277 cgit_print_error(fmt("Invalid branch: %s", tmp));
278 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100279 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100280 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100281 return 0;
282}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100283
Lars Hjemlie0e44782008-03-24 01:09:39 +0100284static void process_request(struct cgit_context *ctx)
285{
286 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100287
Lars Hjemlie0e44782008-03-24 01:09:39 +0100288 cmd = cgit_get_cmd(ctx);
289 if (!cmd) {
290 ctx->page.title = "cgit error";
291 ctx->repo = NULL;
292 cgit_print_http_headers(ctx);
293 cgit_print_docstart(ctx);
294 cgit_print_pageheader(ctx);
295 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100296 cgit_print_docend();
297 return;
298 }
299
Lars Hjemlie0e44782008-03-24 01:09:39 +0100300 if (cmd->want_repo && prepare_repo_cmd(ctx))
301 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100302
Lars Hjemlie0e44782008-03-24 01:09:39 +0100303 if (cmd->want_layout) {
304 cgit_print_http_headers(ctx);
305 cgit_print_docstart(ctx);
306 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100307 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100308
309 cmd->fn(ctx);
310
311 if (cmd->want_layout)
312 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100313}
314
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100315static long ttl_seconds(long ttl)
316{
317 if (ttl<0)
318 return 60 * 60 * 24 * 365;
319 else
320 return ttl * 60;
321}
322
Lars Hjemliab2ab952007-02-08 13:53:13 +0100323static void cgit_fill_cache(struct cacheitem *item, int use_cache)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100324{
Lars Hjemliab2ab952007-02-08 13:53:13 +0100325 int stdout2;
Lars Hjemli7c849d92006-12-16 13:55:58 +0100326
Lars Hjemliab2ab952007-02-08 13:53:13 +0100327 if (use_cache) {
Lars Hjemli5b94c962007-05-14 22:58:01 +0200328 stdout2 = chk_positive(dup(STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100329 "Preserving STDOUT");
330 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
331 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
332 }
333
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100334 ctx.page.modified = time(NULL);
335 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100336 process_request(&ctx);
Lars Hjemliab2ab952007-02-08 13:53:13 +0100337
338 if (use_cache) {
339 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
Lars Hjemli5b94c962007-05-14 22:58:01 +0200340 chk_positive(dup2(stdout2, STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100341 "Restoring original STDOUT");
342 chk_zero(close(stdout2), "Closing temporary STDOUT");
343 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100344}
345
Lars Hjemli44923f82006-12-11 17:25:41 +0100346static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +0100347{
Lars Hjemli318d1062006-12-11 12:10:12 +0100348 int i = 0;
349
Lars Hjemli25105d72006-12-10 22:31:36 +0100350 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100351 if (++i > ctx.cfg.max_lock_attempts) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100352 die("cgit_refresh_cache: unable to lock %s: %s",
353 item->name, strerror(errno));
354 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100355 if (!cache_exist(item)) {
356 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100357 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100358 goto top;
359 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100360 if (!cache_exist(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100361 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100362 cache_unlock(item);
363 } else {
364 cache_cancel_lock(item);
365 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100366 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100367 if (cache_expired(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100368 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100369 cache_unlock(item);
370 } else {
371 cache_cancel_lock(item);
372 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100373 }
374}
375
376static void cgit_print_cache(struct cacheitem *item)
377{
378 static char buf[4096];
379 ssize_t i;
380
381 int fd = open(item->name, O_RDONLY);
382 if (fd<0)
383 die("Unable to open cached file %s", item->name);
384
385 while((i=read(fd, buf, sizeof(buf))) > 0)
386 write(STDOUT_FILENO, buf, i);
387
388 close(fd);
389}
390
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100391static void cgit_parse_args(int argc, const char **argv)
392{
393 int i;
394
395 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100396 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100397 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100398 }
399 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100400 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100401 }
402 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100403 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100404 }
405 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100406 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100407 }
408 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100409 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100410 }
411 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100412 ctx.qry.head = xstrdup(argv[i]+7);
413 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100414 }
415 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100416 ctx.qry.sha1 = xstrdup(argv[i]+7);
417 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100418 }
419 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100420 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100421 }
422 }
423}
424
Lars Hjemli25105d72006-12-10 22:31:36 +0100425int main(int argc, const char **argv)
426{
Lars Hjemli44923f82006-12-11 17:25:41 +0100427 struct cacheitem item;
Michael Krelin51f65472007-07-02 02:29:12 +0200428 const char *cgit_config_env = getenv("CGIT_CONFIG");
Lars Hjemli44923f82006-12-11 17:25:41 +0100429
Lars Hjemli163037e2008-03-24 17:26:08 +0100430 prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100431 item.st.st_mtime = time(NULL);
432 cgit_repolist.length = 0;
433 cgit_repolist.count = 0;
434 cgit_repolist.repos = NULL;
435
Michael Krelin51f65472007-07-02 02:29:12 +0200436 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
Lars Hjemli163037e2008-03-24 17:26:08 +0100437 config_cb);
Lars Hjemliea2831f2007-05-15 00:48:31 +0200438 if (getenv("SCRIPT_NAME"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100439 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100440 if (getenv("QUERY_STRING"))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100441 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100442 cgit_parse_args(argc, argv);
Lars Hjemli163037e2008-03-24 17:26:08 +0100443 cgit_parse_query(ctx.qry.raw, querystring_cb);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100444 if (!cgit_prepare_cache(&item))
445 return 0;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100446 if (ctx.cfg.nocache) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100447 cgit_fill_cache(&item, 0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100448 } else {
449 cgit_check_cache(&item);
450 cgit_print_cache(&item);
451 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100452 return 0;
453}