blob: 73b1f028e43d61ece95342f72f89e822516a5d28 [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 Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemli0d169ad2006-12-09 15:18:17 +010013
Lars Hjemli92908af2008-03-24 23:10:59 +010014const char *cgit_version = CGIT_VERSION;
15
Lars Hjemli163037e2008-03-24 17:26:08 +010016void config_cb(const char *name, const char *value)
17{
18 if (!strcmp(name, "root-title"))
19 ctx.cfg.root_title = xstrdup(value);
20 else if (!strcmp(name, "css"))
21 ctx.cfg.css = xstrdup(value);
22 else if (!strcmp(name, "logo"))
23 ctx.cfg.logo = xstrdup(value);
24 else if (!strcmp(name, "index-header"))
25 ctx.cfg.index_header = xstrdup(value);
26 else if (!strcmp(name, "index-info"))
27 ctx.cfg.index_info = xstrdup(value);
28 else if (!strcmp(name, "logo-link"))
29 ctx.cfg.logo_link = xstrdup(value);
30 else if (!strcmp(name, "module-link"))
31 ctx.cfg.module_link = xstrdup(value);
32 else if (!strcmp(name, "virtual-root")) {
33 ctx.cfg.virtual_root = trim_end(value, '/');
34 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
35 ctx.cfg.virtual_root = "";
36 } else if (!strcmp(name, "nocache"))
37 ctx.cfg.nocache = atoi(value);
38 else if (!strcmp(name, "snapshots"))
39 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
40 else if (!strcmp(name, "enable-index-links"))
41 ctx.cfg.enable_index_links = atoi(value);
42 else if (!strcmp(name, "enable-log-filecount"))
43 ctx.cfg.enable_log_filecount = atoi(value);
44 else if (!strcmp(name, "enable-log-linecount"))
45 ctx.cfg.enable_log_linecount = atoi(value);
46 else if (!strcmp(name, "cache-root"))
47 ctx.cfg.cache_root = xstrdup(value);
48 else if (!strcmp(name, "cache-root-ttl"))
49 ctx.cfg.cache_root_ttl = atoi(value);
50 else if (!strcmp(name, "cache-repo-ttl"))
51 ctx.cfg.cache_repo_ttl = atoi(value);
52 else if (!strcmp(name, "cache-static-ttl"))
53 ctx.cfg.cache_static_ttl = atoi(value);
54 else if (!strcmp(name, "cache-dynamic-ttl"))
55 ctx.cfg.cache_dynamic_ttl = atoi(value);
56 else if (!strcmp(name, "max-message-length"))
57 ctx.cfg.max_msg_len = atoi(value);
58 else if (!strcmp(name, "max-repodesc-length"))
59 ctx.cfg.max_repodesc_len = atoi(value);
60 else if (!strcmp(name, "max-commit-count"))
61 ctx.cfg.max_commit_count = atoi(value);
62 else if (!strcmp(name, "summary-log"))
63 ctx.cfg.summary_log = atoi(value);
64 else if (!strcmp(name, "summary-branches"))
65 ctx.cfg.summary_branches = atoi(value);
66 else if (!strcmp(name, "summary-tags"))
67 ctx.cfg.summary_tags = atoi(value);
68 else if (!strcmp(name, "agefile"))
69 ctx.cfg.agefile = xstrdup(value);
70 else if (!strcmp(name, "renamelimit"))
71 ctx.cfg.renamelimit = atoi(value);
72 else if (!strcmp(name, "robots"))
73 ctx.cfg.robots = xstrdup(value);
74 else if (!strcmp(name, "clone-prefix"))
75 ctx.cfg.clone_prefix = xstrdup(value);
76 else if (!strcmp(name, "repo.group"))
77 ctx.cfg.repo_group = xstrdup(value);
78 else if (!strcmp(name, "repo.url"))
79 ctx.repo = cgit_add_repo(value);
80 else if (!strcmp(name, "repo.name"))
81 ctx.repo->name = xstrdup(value);
82 else if (ctx.repo && !strcmp(name, "repo.path"))
83 ctx.repo->path = trim_end(value, '/');
84 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
85 ctx.repo->clone_url = xstrdup(value);
86 else if (ctx.repo && !strcmp(name, "repo.desc"))
87 ctx.repo->desc = xstrdup(value);
88 else if (ctx.repo && !strcmp(name, "repo.owner"))
89 ctx.repo->owner = xstrdup(value);
90 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
91 ctx.repo->defbranch = xstrdup(value);
92 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
93 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
94 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
95 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
96 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
97 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
98 else if (ctx.repo && !strcmp(name, "repo.module-link"))
99 ctx.repo->module_link= xstrdup(value);
100 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
101 if (*value == '/')
102 ctx.repo->readme = xstrdup(value);
103 else
104 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
105 } else if (!strcmp(name, "include"))
106 cgit_read_config(value, config_cb);
107}
108
109static void querystring_cb(const char *name, const char *value)
110{
111 if (!strcmp(name,"r")) {
112 ctx.qry.repo = xstrdup(value);
113 ctx.repo = cgit_get_repoinfo(value);
114 } else if (!strcmp(name, "p")) {
115 ctx.qry.page = xstrdup(value);
116 } else if (!strcmp(name, "url")) {
117 cgit_parse_url(value);
118 } else if (!strcmp(name, "qt")) {
119 ctx.qry.grep = xstrdup(value);
120 } else if (!strcmp(name, "q")) {
121 ctx.qry.search = xstrdup(value);
122 } else if (!strcmp(name, "h")) {
123 ctx.qry.head = xstrdup(value);
124 ctx.qry.has_symref = 1;
125 } else if (!strcmp(name, "id")) {
126 ctx.qry.sha1 = xstrdup(value);
127 ctx.qry.has_sha1 = 1;
128 } else if (!strcmp(name, "id2")) {
129 ctx.qry.sha2 = xstrdup(value);
130 ctx.qry.has_sha1 = 1;
131 } else if (!strcmp(name, "ofs")) {
132 ctx.qry.ofs = atoi(value);
133 } else if (!strcmp(name, "path")) {
134 ctx.qry.path = trim_end(value, '/');
135 } else if (!strcmp(name, "name")) {
136 ctx.qry.name = xstrdup(value);
137 }
138}
139
140static void prepare_context(struct cgit_context *ctx)
141{
142 memset(ctx, 0, sizeof(ctx));
143 ctx->cfg.agefile = "info/web/last-modified";
144 ctx->cfg.cache_dynamic_ttl = 5;
145 ctx->cfg.cache_max_create_time = 5;
146 ctx->cfg.cache_repo_ttl = 5;
147 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
148 ctx->cfg.cache_root_ttl = 5;
149 ctx->cfg.cache_static_ttl = -1;
150 ctx->cfg.css = "/cgit.css";
151 ctx->cfg.logo = "/git-logo.png";
152 ctx->cfg.max_commit_count = 50;
153 ctx->cfg.max_lock_attempts = 5;
154 ctx->cfg.max_msg_len = 60;
155 ctx->cfg.max_repodesc_len = 60;
156 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
157 ctx->cfg.renamelimit = -1;
158 ctx->cfg.robots = "index, nofollow";
159 ctx->cfg.root_title = "Git repository browser";
160 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
161 ctx->page.mimetype = "text/html";
162 ctx->page.charset = PAGE_ENCODING;
163 ctx->page.filename = NULL;
164}
165
Lars Hjemlice1c7332007-02-03 15:02:55 +0100166static int cgit_prepare_cache(struct cacheitem *item)
Lars Hjemli83a5f352007-01-12 00:00:15 +0100167{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100168 if (!ctx.repo && ctx.qry.repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100169 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
170 "Bad request");
171 cgit_print_http_headers(&ctx);
172 cgit_print_docstart(&ctx);
173 cgit_print_pageheader(&ctx);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100174 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +0100175 cgit_print_docend();
176 return 0;
177 }
178
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100179 if (!ctx.repo) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100180 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
181 item->ttl = ctx.cfg.cache_root_ttl;
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200182 return 1;
183 }
184
Lars Hjemlidc3282f2008-03-25 02:00:09 +0100185 if (!ctx.qry.page) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100186 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100187 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100188 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100189 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100190 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100191 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100192 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100193 ctx.qry.page,
194 cache_safe_filename(ctx.qry.raw)));
195 if (ctx.qry.has_symref)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100196 item->ttl = ctx.cfg.cache_dynamic_ttl;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100197 else if (ctx.qry.has_sha1)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100198 item->ttl = ctx.cfg.cache_static_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100199 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100200 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100201 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100202 return 1;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100203}
204
Lars Hjemlif80ff372008-01-04 13:43:40 +0100205struct refmatch {
206 char *req_ref;
207 char *first_ref;
208 int match;
209};
210
211int find_current_ref(const char *refname, const unsigned char *sha1,
212 int flags, void *cb_data)
213{
214 struct refmatch *info;
215
216 info = (struct refmatch *)cb_data;
217 if (!strcmp(refname, info->req_ref))
218 info->match = 1;
219 if (!info->first_ref)
220 info->first_ref = xstrdup(refname);
221 return info->match;
222}
223
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100224char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +0100225{
226 struct refmatch info;
227
228 info.req_ref = repo->defbranch;
229 info.first_ref = NULL;
230 info.match = 0;
231 for_each_branch_ref(find_current_ref, &info);
232 if (info.match)
233 return info.req_ref;
234 else
235 return info.first_ref;
236}
237
Lars Hjemlie0e44782008-03-24 01:09:39 +0100238static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100239{
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100240 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100241 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +0100242 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +0200243
Lars Hjemlie0e44782008-03-24 01:09:39 +0100244 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100245 setup_git_directory_gently(&nongit);
246 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +0100247 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
248 "config error");
249 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
250 ctx->repo = NULL;
251 cgit_print_http_headers(ctx);
252 cgit_print_docstart(ctx);
253 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100254 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100255 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100256 return 1;
257 }
258 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
259
260 if (!ctx->qry.head) {
261 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
262 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100263 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100264
Lars Hjemlie0e44782008-03-24 01:09:39 +0100265 if (!ctx->qry.head) {
266 cgit_print_http_headers(ctx);
267 cgit_print_docstart(ctx);
268 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100269 cgit_print_error("Repository seems to be empty");
270 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100271 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100272 }
273
Lars Hjemlie0e44782008-03-24 01:09:39 +0100274 if (get_sha1(ctx->qry.head, sha1)) {
275 tmp = xstrdup(ctx->qry.head);
276 ctx->qry.head = ctx->repo->defbranch;
277 cgit_print_http_headers(ctx);
278 cgit_print_docstart(ctx);
279 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100280 cgit_print_error(fmt("Invalid branch: %s", tmp));
281 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100282 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100283 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100284 return 0;
285}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100286
Lars Hjemlie0e44782008-03-24 01:09:39 +0100287static void process_request(struct cgit_context *ctx)
288{
289 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100290
Lars Hjemlie0e44782008-03-24 01:09:39 +0100291 cmd = cgit_get_cmd(ctx);
292 if (!cmd) {
293 ctx->page.title = "cgit error";
294 ctx->repo = NULL;
295 cgit_print_http_headers(ctx);
296 cgit_print_docstart(ctx);
297 cgit_print_pageheader(ctx);
298 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100299 cgit_print_docend();
300 return;
301 }
302
Lars Hjemlie0e44782008-03-24 01:09:39 +0100303 if (cmd->want_repo && prepare_repo_cmd(ctx))
304 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100305
Lars Hjemlie0e44782008-03-24 01:09:39 +0100306 if (cmd->want_layout) {
307 cgit_print_http_headers(ctx);
308 cgit_print_docstart(ctx);
309 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100310 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100311
312 cmd->fn(ctx);
313
314 if (cmd->want_layout)
315 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100316}
317
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100318static long ttl_seconds(long ttl)
319{
320 if (ttl<0)
321 return 60 * 60 * 24 * 365;
322 else
323 return ttl * 60;
324}
325
Lars Hjemliab2ab952007-02-08 13:53:13 +0100326static void cgit_fill_cache(struct cacheitem *item, int use_cache)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100327{
Lars Hjemliab2ab952007-02-08 13:53:13 +0100328 int stdout2;
Lars Hjemli7c849d92006-12-16 13:55:58 +0100329
Lars Hjemliab2ab952007-02-08 13:53:13 +0100330 if (use_cache) {
Lars Hjemli5b94c962007-05-14 22:58:01 +0200331 stdout2 = chk_positive(dup(STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100332 "Preserving STDOUT");
333 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
334 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
335 }
336
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100337 ctx.page.modified = time(NULL);
338 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100339 process_request(&ctx);
Lars Hjemliab2ab952007-02-08 13:53:13 +0100340
341 if (use_cache) {
342 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
Lars Hjemli5b94c962007-05-14 22:58:01 +0200343 chk_positive(dup2(stdout2, STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100344 "Restoring original STDOUT");
345 chk_zero(close(stdout2), "Closing temporary STDOUT");
346 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100347}
348
Lars Hjemli44923f82006-12-11 17:25:41 +0100349static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +0100350{
Lars Hjemli318d1062006-12-11 12:10:12 +0100351 int i = 0;
352
Lars Hjemli25105d72006-12-10 22:31:36 +0100353 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100354 if (++i > ctx.cfg.max_lock_attempts) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100355 die("cgit_refresh_cache: unable to lock %s: %s",
356 item->name, strerror(errno));
357 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100358 if (!cache_exist(item)) {
359 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100360 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100361 goto top;
362 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100363 if (!cache_exist(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100364 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100365 cache_unlock(item);
366 } else {
367 cache_cancel_lock(item);
368 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100369 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100370 if (cache_expired(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100371 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100372 cache_unlock(item);
373 } else {
374 cache_cancel_lock(item);
375 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100376 }
377}
378
379static void cgit_print_cache(struct cacheitem *item)
380{
381 static char buf[4096];
382 ssize_t i;
383
384 int fd = open(item->name, O_RDONLY);
385 if (fd<0)
386 die("Unable to open cached file %s", item->name);
387
388 while((i=read(fd, buf, sizeof(buf))) > 0)
389 write(STDOUT_FILENO, buf, i);
390
391 close(fd);
392}
393
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100394static void cgit_parse_args(int argc, const char **argv)
395{
396 int i;
397
398 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100399 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100400 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100401 }
402 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100403 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100404 }
405 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100406 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100407 }
408 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100409 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100410 }
411 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100412 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100413 }
414 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100415 ctx.qry.head = xstrdup(argv[i]+7);
416 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100417 }
418 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100419 ctx.qry.sha1 = xstrdup(argv[i]+7);
420 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100421 }
422 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100423 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100424 }
425 }
426}
427
Lars Hjemli25105d72006-12-10 22:31:36 +0100428int main(int argc, const char **argv)
429{
Lars Hjemli44923f82006-12-11 17:25:41 +0100430 struct cacheitem item;
Michael Krelin51f65472007-07-02 02:29:12 +0200431 const char *cgit_config_env = getenv("CGIT_CONFIG");
Lars Hjemli44923f82006-12-11 17:25:41 +0100432
Lars Hjemli163037e2008-03-24 17:26:08 +0100433 prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100434 item.st.st_mtime = time(NULL);
435 cgit_repolist.length = 0;
436 cgit_repolist.count = 0;
437 cgit_repolist.repos = NULL;
438
Michael Krelin51f65472007-07-02 02:29:12 +0200439 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
Lars Hjemli163037e2008-03-24 17:26:08 +0100440 config_cb);
Lars Hjemliea2831f2007-05-15 00:48:31 +0200441 if (getenv("SCRIPT_NAME"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100442 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100443 if (getenv("QUERY_STRING"))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100444 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100445 cgit_parse_args(argc, argv);
Lars Hjemli163037e2008-03-24 17:26:08 +0100446 cgit_parse_query(ctx.qry.raw, querystring_cb);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100447 if (!cgit_prepare_cache(&item))
448 return 0;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100449 if (ctx.cfg.nocache) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100450 cgit_fill_cache(&item, 0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100451 } else {
452 cgit_check_cache(&item);
453 cgit_print_cache(&item);
454 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100455 return 0;
456}