blob: 38b0ba5d0616d29f291a9017ec59cac5522cf4ff [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);
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"))
Lars Hjemli20a33542008-03-28 00:09:11 +0100108 parse_configfile(value, config_cb);
Lars Hjemli163037e2008-03-24 17:26:08 +0100109}
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}
167
Lars Hjemlice1c7332007-02-03 15:02:55 +0100168static int cgit_prepare_cache(struct cacheitem *item)
Lars Hjemli83a5f352007-01-12 00:00:15 +0100169{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100170 if (!ctx.repo && ctx.qry.repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100171 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
172 "Bad request");
173 cgit_print_http_headers(&ctx);
174 cgit_print_docstart(&ctx);
175 cgit_print_pageheader(&ctx);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100176 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +0100177 cgit_print_docend();
178 return 0;
179 }
180
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100181 if (!ctx.repo) {
Lars Hjemli78031f92008-04-14 23:07:52 +0200182 item->name = xstrdup(fmt("%s/index.%s.html",
183 ctx.cfg.cache_root,
184 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100185 item->ttl = ctx.cfg.cache_root_ttl;
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200186 return 1;
187 }
188
Lars Hjemlidc3282f2008-03-25 02:00:09 +0100189 if (!ctx.qry.page) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100190 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100191 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100192 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100193 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100194 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100195 item->name = xstrdup(fmt("%s/%s/%s/%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 ctx.qry.page,
198 cache_safe_filename(ctx.qry.raw)));
199 if (ctx.qry.has_symref)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100200 item->ttl = ctx.cfg.cache_dynamic_ttl;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100201 else if (ctx.qry.has_sha1)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100202 item->ttl = ctx.cfg.cache_static_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100203 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100204 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100205 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100206 return 1;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100207}
208
Lars Hjemlif80ff372008-01-04 13:43:40 +0100209struct refmatch {
210 char *req_ref;
211 char *first_ref;
212 int match;
213};
214
215int find_current_ref(const char *refname, const unsigned char *sha1,
216 int flags, void *cb_data)
217{
218 struct refmatch *info;
219
220 info = (struct refmatch *)cb_data;
221 if (!strcmp(refname, info->req_ref))
222 info->match = 1;
223 if (!info->first_ref)
224 info->first_ref = xstrdup(refname);
225 return info->match;
226}
227
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100228char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +0100229{
230 struct refmatch info;
231
232 info.req_ref = repo->defbranch;
233 info.first_ref = NULL;
234 info.match = 0;
235 for_each_branch_ref(find_current_ref, &info);
236 if (info.match)
237 return info.req_ref;
238 else
239 return info.first_ref;
240}
241
Lars Hjemlie0e44782008-03-24 01:09:39 +0100242static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100243{
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100244 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100245 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +0100246 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +0200247
Lars Hjemlie0e44782008-03-24 01:09:39 +0100248 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100249 setup_git_directory_gently(&nongit);
250 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +0100251 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
252 "config error");
253 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
254 ctx->repo = NULL;
255 cgit_print_http_headers(ctx);
256 cgit_print_docstart(ctx);
257 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100258 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100259 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100260 return 1;
261 }
262 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
263
264 if (!ctx->qry.head) {
265 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
266 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100267 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100268
Lars Hjemlie0e44782008-03-24 01:09:39 +0100269 if (!ctx->qry.head) {
270 cgit_print_http_headers(ctx);
271 cgit_print_docstart(ctx);
272 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100273 cgit_print_error("Repository seems to be empty");
274 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100275 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100276 }
277
Lars Hjemlie0e44782008-03-24 01:09:39 +0100278 if (get_sha1(ctx->qry.head, sha1)) {
279 tmp = xstrdup(ctx->qry.head);
280 ctx->qry.head = ctx->repo->defbranch;
281 cgit_print_http_headers(ctx);
282 cgit_print_docstart(ctx);
283 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100284 cgit_print_error(fmt("Invalid branch: %s", tmp));
285 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100286 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100287 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100288 return 0;
289}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100290
Lars Hjemlie0e44782008-03-24 01:09:39 +0100291static void process_request(struct cgit_context *ctx)
292{
293 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100294
Lars Hjemlie0e44782008-03-24 01:09:39 +0100295 cmd = cgit_get_cmd(ctx);
296 if (!cmd) {
297 ctx->page.title = "cgit error";
298 ctx->repo = NULL;
299 cgit_print_http_headers(ctx);
300 cgit_print_docstart(ctx);
301 cgit_print_pageheader(ctx);
302 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100303 cgit_print_docend();
304 return;
305 }
306
Lars Hjemlie0e44782008-03-24 01:09:39 +0100307 if (cmd->want_repo && prepare_repo_cmd(ctx))
308 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100309
Lars Hjemlie0e44782008-03-24 01:09:39 +0100310 if (cmd->want_layout) {
311 cgit_print_http_headers(ctx);
312 cgit_print_docstart(ctx);
313 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100314 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100315
316 cmd->fn(ctx);
317
318 if (cmd->want_layout)
319 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100320}
321
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100322static long ttl_seconds(long ttl)
323{
324 if (ttl<0)
325 return 60 * 60 * 24 * 365;
326 else
327 return ttl * 60;
328}
329
Lars Hjemliab2ab952007-02-08 13:53:13 +0100330static void cgit_fill_cache(struct cacheitem *item, int use_cache)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100331{
Lars Hjemliab2ab952007-02-08 13:53:13 +0100332 int stdout2;
Lars Hjemli7c849d92006-12-16 13:55:58 +0100333
Lars Hjemliab2ab952007-02-08 13:53:13 +0100334 if (use_cache) {
Lars Hjemli5b94c962007-05-14 22:58:01 +0200335 stdout2 = chk_positive(dup(STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100336 "Preserving STDOUT");
337 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
338 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
339 }
340
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100341 ctx.page.modified = time(NULL);
342 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100343 process_request(&ctx);
Lars Hjemliab2ab952007-02-08 13:53:13 +0100344
345 if (use_cache) {
346 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
Lars Hjemli5b94c962007-05-14 22:58:01 +0200347 chk_positive(dup2(stdout2, STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100348 "Restoring original STDOUT");
349 chk_zero(close(stdout2), "Closing temporary STDOUT");
350 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100351}
352
Lars Hjemli44923f82006-12-11 17:25:41 +0100353static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +0100354{
Lars Hjemli318d1062006-12-11 12:10:12 +0100355 int i = 0;
356
Lars Hjemli25105d72006-12-10 22:31:36 +0100357 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100358 if (++i > ctx.cfg.max_lock_attempts) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100359 die("cgit_refresh_cache: unable to lock %s: %s",
360 item->name, strerror(errno));
361 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100362 if (!cache_exist(item)) {
363 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100364 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100365 goto top;
366 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100367 if (!cache_exist(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 Hjemlif5069d82006-12-11 09:57:58 +0100373 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100374 if (cache_expired(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100375 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100376 cache_unlock(item);
377 } else {
378 cache_cancel_lock(item);
379 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100380 }
381}
382
383static void cgit_print_cache(struct cacheitem *item)
384{
385 static char buf[4096];
386 ssize_t i;
387
388 int fd = open(item->name, O_RDONLY);
389 if (fd<0)
390 die("Unable to open cached file %s", item->name);
391
392 while((i=read(fd, buf, sizeof(buf))) > 0)
393 write(STDOUT_FILENO, buf, i);
394
395 close(fd);
396}
397
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100398static void cgit_parse_args(int argc, const char **argv)
399{
400 int i;
401
402 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100403 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100404 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100405 }
406 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100407 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100408 }
409 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100410 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100411 }
412 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100413 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100414 }
415 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100416 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100417 }
418 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100419 ctx.qry.head = xstrdup(argv[i]+7);
420 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100421 }
422 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100423 ctx.qry.sha1 = xstrdup(argv[i]+7);
424 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100425 }
426 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100427 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100428 }
429 }
430}
431
Lars Hjemli25105d72006-12-10 22:31:36 +0100432int main(int argc, const char **argv)
433{
Lars Hjemli44923f82006-12-11 17:25:41 +0100434 struct cacheitem item;
Michael Krelin51f65472007-07-02 02:29:12 +0200435 const char *cgit_config_env = getenv("CGIT_CONFIG");
Lars Hjemli44923f82006-12-11 17:25:41 +0100436
Lars Hjemli163037e2008-03-24 17:26:08 +0100437 prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100438 item.st.st_mtime = time(NULL);
439 cgit_repolist.length = 0;
440 cgit_repolist.count = 0;
441 cgit_repolist.repos = NULL;
442
Lars Hjemli20a33542008-03-28 00:09:11 +0100443 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
Lars Hjemli163037e2008-03-24 17:26:08 +0100444 config_cb);
Lars Hjemlic6f74762008-04-08 21:27:12 +0200445 ctx.repo = NULL;
Lars Hjemliea2831f2007-05-15 00:48:31 +0200446 if (getenv("SCRIPT_NAME"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100447 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100448 if (getenv("QUERY_STRING"))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100449 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100450 cgit_parse_args(argc, argv);
Lars Hjemlie87e8962008-04-08 21:11:36 +0200451 http_parse_querystring(ctx.qry.raw, querystring_cb);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100452 if (!cgit_prepare_cache(&item))
453 return 0;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100454 if (ctx.cfg.nocache) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100455 cgit_fill_cache(&item, 0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100456 } else {
457 cgit_check_cache(&item);
458 cgit_print_cache(&item);
459 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100460 return 0;
461}