blob: 3943a0f8ab3ed143d8492f6ee7ba8b3450ec4ee1 [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 Hjemli92908af2008-03-24 23:10:59 +010013const char *cgit_version = CGIT_VERSION;
14
Lars Hjemli163037e2008-03-24 17:26:08 +010015void config_cb(const char *name, const char *value)
16{
17 if (!strcmp(name, "root-title"))
18 ctx.cfg.root_title = xstrdup(value);
19 else if (!strcmp(name, "css"))
20 ctx.cfg.css = xstrdup(value);
21 else if (!strcmp(name, "logo"))
22 ctx.cfg.logo = xstrdup(value);
23 else if (!strcmp(name, "index-header"))
24 ctx.cfg.index_header = xstrdup(value);
25 else if (!strcmp(name, "index-info"))
26 ctx.cfg.index_info = xstrdup(value);
27 else if (!strcmp(name, "logo-link"))
28 ctx.cfg.logo_link = xstrdup(value);
29 else if (!strcmp(name, "module-link"))
30 ctx.cfg.module_link = xstrdup(value);
31 else if (!strcmp(name, "virtual-root")) {
32 ctx.cfg.virtual_root = trim_end(value, '/');
33 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
34 ctx.cfg.virtual_root = "";
35 } else if (!strcmp(name, "nocache"))
36 ctx.cfg.nocache = atoi(value);
37 else if (!strcmp(name, "snapshots"))
38 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
39 else if (!strcmp(name, "enable-index-links"))
40 ctx.cfg.enable_index_links = atoi(value);
41 else if (!strcmp(name, "enable-log-filecount"))
42 ctx.cfg.enable_log_filecount = atoi(value);
43 else if (!strcmp(name, "enable-log-linecount"))
44 ctx.cfg.enable_log_linecount = atoi(value);
45 else if (!strcmp(name, "cache-root"))
46 ctx.cfg.cache_root = xstrdup(value);
47 else if (!strcmp(name, "cache-root-ttl"))
48 ctx.cfg.cache_root_ttl = atoi(value);
49 else if (!strcmp(name, "cache-repo-ttl"))
50 ctx.cfg.cache_repo_ttl = atoi(value);
51 else if (!strcmp(name, "cache-static-ttl"))
52 ctx.cfg.cache_static_ttl = atoi(value);
53 else if (!strcmp(name, "cache-dynamic-ttl"))
54 ctx.cfg.cache_dynamic_ttl = atoi(value);
55 else if (!strcmp(name, "max-message-length"))
56 ctx.cfg.max_msg_len = atoi(value);
57 else if (!strcmp(name, "max-repodesc-length"))
58 ctx.cfg.max_repodesc_len = atoi(value);
59 else if (!strcmp(name, "max-commit-count"))
60 ctx.cfg.max_commit_count = atoi(value);
61 else if (!strcmp(name, "summary-log"))
62 ctx.cfg.summary_log = atoi(value);
63 else if (!strcmp(name, "summary-branches"))
64 ctx.cfg.summary_branches = atoi(value);
65 else if (!strcmp(name, "summary-tags"))
66 ctx.cfg.summary_tags = atoi(value);
67 else if (!strcmp(name, "agefile"))
68 ctx.cfg.agefile = xstrdup(value);
69 else if (!strcmp(name, "renamelimit"))
70 ctx.cfg.renamelimit = atoi(value);
71 else if (!strcmp(name, "robots"))
72 ctx.cfg.robots = xstrdup(value);
73 else if (!strcmp(name, "clone-prefix"))
74 ctx.cfg.clone_prefix = xstrdup(value);
75 else if (!strcmp(name, "repo.group"))
76 ctx.cfg.repo_group = xstrdup(value);
77 else if (!strcmp(name, "repo.url"))
78 ctx.repo = cgit_add_repo(value);
79 else if (!strcmp(name, "repo.name"))
80 ctx.repo->name = xstrdup(value);
81 else if (ctx.repo && !strcmp(name, "repo.path"))
82 ctx.repo->path = trim_end(value, '/');
83 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
84 ctx.repo->clone_url = xstrdup(value);
85 else if (ctx.repo && !strcmp(name, "repo.desc"))
86 ctx.repo->desc = xstrdup(value);
87 else if (ctx.repo && !strcmp(name, "repo.owner"))
88 ctx.repo->owner = xstrdup(value);
89 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
90 ctx.repo->defbranch = xstrdup(value);
91 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
92 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
93 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
94 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
95 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
96 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
97 else if (ctx.repo && !strcmp(name, "repo.module-link"))
98 ctx.repo->module_link= xstrdup(value);
99 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
100 if (*value == '/')
101 ctx.repo->readme = xstrdup(value);
102 else
103 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
104 } else if (!strcmp(name, "include"))
105 cgit_read_config(value, config_cb);
106}
107
108static void querystring_cb(const char *name, const char *value)
109{
110 if (!strcmp(name,"r")) {
111 ctx.qry.repo = xstrdup(value);
112 ctx.repo = cgit_get_repoinfo(value);
113 } else if (!strcmp(name, "p")) {
114 ctx.qry.page = xstrdup(value);
115 } else if (!strcmp(name, "url")) {
116 cgit_parse_url(value);
117 } else if (!strcmp(name, "qt")) {
118 ctx.qry.grep = xstrdup(value);
119 } else if (!strcmp(name, "q")) {
120 ctx.qry.search = xstrdup(value);
121 } else if (!strcmp(name, "h")) {
122 ctx.qry.head = xstrdup(value);
123 ctx.qry.has_symref = 1;
124 } else if (!strcmp(name, "id")) {
125 ctx.qry.sha1 = xstrdup(value);
126 ctx.qry.has_sha1 = 1;
127 } else if (!strcmp(name, "id2")) {
128 ctx.qry.sha2 = xstrdup(value);
129 ctx.qry.has_sha1 = 1;
130 } else if (!strcmp(name, "ofs")) {
131 ctx.qry.ofs = atoi(value);
132 } else if (!strcmp(name, "path")) {
133 ctx.qry.path = trim_end(value, '/');
134 } else if (!strcmp(name, "name")) {
135 ctx.qry.name = xstrdup(value);
136 }
137}
138
139static void prepare_context(struct cgit_context *ctx)
140{
141 memset(ctx, 0, sizeof(ctx));
142 ctx->cfg.agefile = "info/web/last-modified";
143 ctx->cfg.cache_dynamic_ttl = 5;
144 ctx->cfg.cache_max_create_time = 5;
145 ctx->cfg.cache_repo_ttl = 5;
146 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
147 ctx->cfg.cache_root_ttl = 5;
148 ctx->cfg.cache_static_ttl = -1;
149 ctx->cfg.css = "/cgit.css";
150 ctx->cfg.logo = "/git-logo.png";
151 ctx->cfg.max_commit_count = 50;
152 ctx->cfg.max_lock_attempts = 5;
153 ctx->cfg.max_msg_len = 60;
154 ctx->cfg.max_repodesc_len = 60;
155 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
156 ctx->cfg.renamelimit = -1;
157 ctx->cfg.robots = "index, nofollow";
158 ctx->cfg.root_title = "Git repository browser";
159 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
160 ctx->page.mimetype = "text/html";
161 ctx->page.charset = PAGE_ENCODING;
162 ctx->page.filename = NULL;
163}
164
Lars Hjemlice1c7332007-02-03 15:02:55 +0100165static int cgit_prepare_cache(struct cacheitem *item)
Lars Hjemli83a5f352007-01-12 00:00:15 +0100166{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100167 if (!ctx.repo && ctx.qry.repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100168 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
169 "Bad request");
170 cgit_print_http_headers(&ctx);
171 cgit_print_docstart(&ctx);
172 cgit_print_pageheader(&ctx);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100173 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +0100174 cgit_print_docend();
175 return 0;
176 }
177
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100178 if (!ctx.repo) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100179 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
180 item->ttl = ctx.cfg.cache_root_ttl;
Lars Hjemli30ccdca2007-05-18 03:00:54 +0200181 return 1;
182 }
183
184 if (!cgit_cmd) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100185 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100186 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100187 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100188 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100189 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100190 item->name = xstrdup(fmt("%s/%s/%s/%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 ctx.qry.page,
193 cache_safe_filename(ctx.qry.raw)));
194 if (ctx.qry.has_symref)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100195 item->ttl = ctx.cfg.cache_dynamic_ttl;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100196 else if (ctx.qry.has_sha1)
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100197 item->ttl = ctx.cfg.cache_static_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100198 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100199 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100200 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100201 return 1;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100202}
203
Lars Hjemlif80ff372008-01-04 13:43:40 +0100204struct refmatch {
205 char *req_ref;
206 char *first_ref;
207 int match;
208};
209
210int find_current_ref(const char *refname, const unsigned char *sha1,
211 int flags, void *cb_data)
212{
213 struct refmatch *info;
214
215 info = (struct refmatch *)cb_data;
216 if (!strcmp(refname, info->req_ref))
217 info->match = 1;
218 if (!info->first_ref)
219 info->first_ref = xstrdup(refname);
220 return info->match;
221}
222
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100223char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +0100224{
225 struct refmatch info;
226
227 info.req_ref = repo->defbranch;
228 info.first_ref = NULL;
229 info.match = 0;
230 for_each_branch_ref(find_current_ref, &info);
231 if (info.match)
232 return info.req_ref;
233 else
234 return info.first_ref;
235}
236
Lars Hjemlie0e44782008-03-24 01:09:39 +0100237static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100238{
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100239 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100240 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +0100241 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +0200242
Lars Hjemlie0e44782008-03-24 01:09:39 +0100243 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100244 setup_git_directory_gently(&nongit);
245 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +0100246 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
247 "config error");
248 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
249 ctx->repo = NULL;
250 cgit_print_http_headers(ctx);
251 cgit_print_docstart(ctx);
252 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100253 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100254 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100255 return 1;
256 }
257 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
258
259 if (!ctx->qry.head) {
260 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
261 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100262 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100263
Lars Hjemlie0e44782008-03-24 01:09:39 +0100264 if (!ctx->qry.head) {
265 cgit_print_http_headers(ctx);
266 cgit_print_docstart(ctx);
267 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100268 cgit_print_error("Repository seems to be empty");
269 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100270 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100271 }
272
Lars Hjemlie0e44782008-03-24 01:09:39 +0100273 if (get_sha1(ctx->qry.head, sha1)) {
274 tmp = xstrdup(ctx->qry.head);
275 ctx->qry.head = ctx->repo->defbranch;
276 cgit_print_http_headers(ctx);
277 cgit_print_docstart(ctx);
278 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100279 cgit_print_error(fmt("Invalid branch: %s", tmp));
280 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100281 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100282 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100283 return 0;
284}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100285
Lars Hjemlie0e44782008-03-24 01:09:39 +0100286static void process_request(struct cgit_context *ctx)
287{
288 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100289
Lars Hjemlie0e44782008-03-24 01:09:39 +0100290 cmd = cgit_get_cmd(ctx);
291 if (!cmd) {
292 ctx->page.title = "cgit error";
293 ctx->repo = NULL;
294 cgit_print_http_headers(ctx);
295 cgit_print_docstart(ctx);
296 cgit_print_pageheader(ctx);
297 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100298 cgit_print_docend();
299 return;
300 }
301
Lars Hjemlie0e44782008-03-24 01:09:39 +0100302 if (cmd->want_repo && prepare_repo_cmd(ctx))
303 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100304
Lars Hjemlie0e44782008-03-24 01:09:39 +0100305 if (cmd->want_layout) {
306 cgit_print_http_headers(ctx);
307 cgit_print_docstart(ctx);
308 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100309 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100310
311 cmd->fn(ctx);
312
313 if (cmd->want_layout)
314 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100315}
316
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100317static long ttl_seconds(long ttl)
318{
319 if (ttl<0)
320 return 60 * 60 * 24 * 365;
321 else
322 return ttl * 60;
323}
324
Lars Hjemliab2ab952007-02-08 13:53:13 +0100325static void cgit_fill_cache(struct cacheitem *item, int use_cache)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100326{
Lars Hjemliab2ab952007-02-08 13:53:13 +0100327 int stdout2;
Lars Hjemli7c849d92006-12-16 13:55:58 +0100328
Lars Hjemliab2ab952007-02-08 13:53:13 +0100329 if (use_cache) {
Lars Hjemli5b94c962007-05-14 22:58:01 +0200330 stdout2 = chk_positive(dup(STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100331 "Preserving STDOUT");
332 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
333 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
334 }
335
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100336 ctx.page.modified = time(NULL);
337 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100338 process_request(&ctx);
Lars Hjemliab2ab952007-02-08 13:53:13 +0100339
340 if (use_cache) {
341 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
Lars Hjemli5b94c962007-05-14 22:58:01 +0200342 chk_positive(dup2(stdout2, STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100343 "Restoring original STDOUT");
344 chk_zero(close(stdout2), "Closing temporary STDOUT");
345 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100346}
347
Lars Hjemli44923f82006-12-11 17:25:41 +0100348static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +0100349{
Lars Hjemli318d1062006-12-11 12:10:12 +0100350 int i = 0;
351
Lars Hjemli25105d72006-12-10 22:31:36 +0100352 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100353 if (++i > ctx.cfg.max_lock_attempts) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100354 die("cgit_refresh_cache: unable to lock %s: %s",
355 item->name, strerror(errno));
356 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100357 if (!cache_exist(item)) {
358 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100359 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100360 goto top;
361 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100362 if (!cache_exist(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100363 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100364 cache_unlock(item);
365 } else {
366 cache_cancel_lock(item);
367 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100368 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100369 if (cache_expired(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100370 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100371 cache_unlock(item);
372 } else {
373 cache_cancel_lock(item);
374 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100375 }
376}
377
378static void cgit_print_cache(struct cacheitem *item)
379{
380 static char buf[4096];
381 ssize_t i;
382
383 int fd = open(item->name, O_RDONLY);
384 if (fd<0)
385 die("Unable to open cached file %s", item->name);
386
387 while((i=read(fd, buf, sizeof(buf))) > 0)
388 write(STDOUT_FILENO, buf, i);
389
390 close(fd);
391}
392
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100393static void cgit_parse_args(int argc, const char **argv)
394{
395 int i;
396
397 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100398 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100399 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100400 }
401 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100402 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100403 }
404 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100405 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100406 }
407 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100408 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100409 }
410 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100411 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100412 }
413 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100414 ctx.qry.head = xstrdup(argv[i]+7);
415 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100416 }
417 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100418 ctx.qry.sha1 = xstrdup(argv[i]+7);
419 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100420 }
421 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100422 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100423 }
424 }
425}
426
Lars Hjemli25105d72006-12-10 22:31:36 +0100427int main(int argc, const char **argv)
428{
Lars Hjemli44923f82006-12-11 17:25:41 +0100429 struct cacheitem item;
Michael Krelin51f65472007-07-02 02:29:12 +0200430 const char *cgit_config_env = getenv("CGIT_CONFIG");
Lars Hjemli44923f82006-12-11 17:25:41 +0100431
Lars Hjemli163037e2008-03-24 17:26:08 +0100432 prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100433 item.st.st_mtime = time(NULL);
434 cgit_repolist.length = 0;
435 cgit_repolist.count = 0;
436 cgit_repolist.repos = NULL;
437
Michael Krelin51f65472007-07-02 02:29:12 +0200438 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
Lars Hjemli163037e2008-03-24 17:26:08 +0100439 config_cb);
Lars Hjemliea2831f2007-05-15 00:48:31 +0200440 if (getenv("SCRIPT_NAME"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100441 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100442 if (getenv("QUERY_STRING"))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100443 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100444 cgit_parse_args(argc, argv);
Lars Hjemli163037e2008-03-24 17:26:08 +0100445 cgit_parse_query(ctx.qry.raw, querystring_cb);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100446 if (!cgit_prepare_cache(&item))
447 return 0;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100448 if (ctx.cfg.nocache) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100449 cgit_fill_cache(&item, 0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100450 } else {
451 cgit_check_cache(&item);
452 cgit_print_cache(&item);
453 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100454 return 0;
455}