]> gitweb.ps.run Git - ps-cgit/blob - cgit.c
CGIT 0.9
[ps-cgit] / cgit.c
1 /* cgit.c: cgi for the git scm
2  *
3  * Copyright (C) 2006 Lars Hjemli
4  * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
5  *
6  * Licensed under GNU General Public License v2
7  *   (see COPYING for full license text)
8  */
9
10 #include "cgit.h"
11 #include "cache.h"
12 #include "cmd.h"
13 #include "configfile.h"
14 #include "html.h"
15 #include "ui-shared.h"
16 #include "ui-stats.h"
17 #include "scan-tree.h"
18
19 const char *cgit_version = CGIT_VERSION;
20
21 void add_mimetype(const char *name, const char *value)
22 {
23         struct string_list_item *item;
24
25         item = string_list_insert(&ctx.cfg.mimetypes, xstrdup(name));
26         item->util = xstrdup(value);
27 }
28
29 struct cgit_filter *new_filter(const char *cmd, int extra_args)
30 {
31         struct cgit_filter *f;
32
33         if (!cmd || !cmd[0])
34                 return NULL;
35
36         f = xmalloc(sizeof(struct cgit_filter));
37         f->cmd = xstrdup(cmd);
38         f->argv = xmalloc((2 + extra_args) * sizeof(char *));
39         f->argv[0] = f->cmd;
40         f->argv[1] = NULL;
41         return f;
42 }
43
44 static void process_cached_repolist(const char *path);
45
46 void repo_config(struct cgit_repo *repo, const char *name, const char *value)
47 {
48         if (!strcmp(name, "name"))
49                 repo->name = xstrdup(value);
50         else if (!strcmp(name, "clone-url"))
51                 repo->clone_url = xstrdup(value);
52         else if (!strcmp(name, "desc"))
53                 repo->desc = xstrdup(value);
54         else if (!strcmp(name, "owner"))
55                 repo->owner = xstrdup(value);
56         else if (!strcmp(name, "defbranch"))
57                 repo->defbranch = xstrdup(value);
58         else if (!strcmp(name, "snapshots"))
59                 repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value);
60         else if (!strcmp(name, "enable-commit-graph"))
61                 repo->enable_commit_graph = ctx.cfg.enable_commit_graph * atoi(value);
62         else if (!strcmp(name, "enable-log-filecount"))
63                 repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
64         else if (!strcmp(name, "enable-log-linecount"))
65                 repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
66         else if (!strcmp(name, "enable-remote-branches"))
67                 repo->enable_remote_branches = atoi(value);
68         else if (!strcmp(name, "enable-subject-links"))
69                 repo->enable_subject_links = atoi(value);
70         else if (!strcmp(name, "max-stats"))
71                 repo->max_stats = cgit_find_stats_period(value, NULL);
72         else if (!strcmp(name, "module-link"))
73                 repo->module_link= xstrdup(value);
74         else if (!strcmp(name, "section"))
75                 repo->section = xstrdup(value);
76         else if (!strcmp(name, "readme") && value != NULL)
77                 repo->readme = xstrdup(value);
78         else if (!strcmp(name, "logo") && value != NULL)
79                 repo->logo = xstrdup(value);
80         else if (!strcmp(name, "logo-link") && value != NULL)
81                 repo->logo_link = xstrdup(value);
82         else if (ctx.cfg.enable_filter_overrides) {
83                 if (!strcmp(name, "about-filter"))
84                         repo->about_filter = new_filter(value, 0);
85                 else if (!strcmp(name, "commit-filter"))
86                         repo->commit_filter = new_filter(value, 0);
87                 else if (!strcmp(name, "source-filter"))
88                         repo->source_filter = new_filter(value, 1);
89         }
90 }
91
92 void config_cb(const char *name, const char *value)
93 {
94         if (!strcmp(name, "section") || !strcmp(name, "repo.group"))
95                 ctx.cfg.section = xstrdup(value);
96         else if (!strcmp(name, "repo.url"))
97                 ctx.repo = cgit_add_repo(value);
98         else if (ctx.repo && !strcmp(name, "repo.path"))
99                 ctx.repo->path = trim_end(value, '/');
100         else if (ctx.repo && !prefixcmp(name, "repo."))
101                 repo_config(ctx.repo, name + 5, value);
102         else if (!strcmp(name, "readme"))
103                 ctx.cfg.readme = xstrdup(value);
104         else if (!strcmp(name, "root-title"))
105                 ctx.cfg.root_title = xstrdup(value);
106         else if (!strcmp(name, "root-desc"))
107                 ctx.cfg.root_desc = xstrdup(value);
108         else if (!strcmp(name, "root-readme"))
109                 ctx.cfg.root_readme = xstrdup(value);
110         else if (!strcmp(name, "css"))
111                 ctx.cfg.css = xstrdup(value);
112         else if (!strcmp(name, "favicon"))
113                 ctx.cfg.favicon = xstrdup(value);
114         else if (!strcmp(name, "footer"))
115                 ctx.cfg.footer = xstrdup(value);
116         else if (!strcmp(name, "head-include"))
117                 ctx.cfg.head_include = xstrdup(value);
118         else if (!strcmp(name, "header"))
119                 ctx.cfg.header = xstrdup(value);
120         else if (!strcmp(name, "logo"))
121                 ctx.cfg.logo = xstrdup(value);
122         else if (!strcmp(name, "index-header"))
123                 ctx.cfg.index_header = xstrdup(value);
124         else if (!strcmp(name, "index-info"))
125                 ctx.cfg.index_info = xstrdup(value);
126         else if (!strcmp(name, "logo-link"))
127                 ctx.cfg.logo_link = xstrdup(value);
128         else if (!strcmp(name, "module-link"))
129                 ctx.cfg.module_link = xstrdup(value);
130         else if (!strcmp(name, "strict-export"))
131                 ctx.cfg.strict_export = xstrdup(value);
132         else if (!strcmp(name, "virtual-root")) {
133                 ctx.cfg.virtual_root = trim_end(value, '/');
134                 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
135                         ctx.cfg.virtual_root = "";
136         } else if (!strcmp(name, "nocache"))
137                 ctx.cfg.nocache = atoi(value);
138         else if (!strcmp(name, "noplainemail"))
139                 ctx.cfg.noplainemail = atoi(value);
140         else if (!strcmp(name, "noheader"))
141                 ctx.cfg.noheader = atoi(value);
142         else if (!strcmp(name, "snapshots"))
143                 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
144         else if (!strcmp(name, "enable-filter-overrides"))
145                 ctx.cfg.enable_filter_overrides = atoi(value);
146         else if (!strcmp(name, "enable-gitweb-owner"))
147                 ctx.cfg.enable_gitweb_owner = atoi(value);
148         else if (!strcmp(name, "enable-index-links"))
149                 ctx.cfg.enable_index_links = atoi(value);
150         else if (!strcmp(name, "enable-commit-graph"))
151                 ctx.cfg.enable_commit_graph = atoi(value);
152         else if (!strcmp(name, "enable-log-filecount"))
153                 ctx.cfg.enable_log_filecount = atoi(value);
154         else if (!strcmp(name, "enable-log-linecount"))
155                 ctx.cfg.enable_log_linecount = atoi(value);
156         else if (!strcmp(name, "enable-remote-branches"))
157                 ctx.cfg.enable_remote_branches = atoi(value);
158         else if (!strcmp(name, "enable-subject-links"))
159                 ctx.cfg.enable_subject_links = atoi(value);
160         else if (!strcmp(name, "enable-tree-linenumbers"))
161                 ctx.cfg.enable_tree_linenumbers = atoi(value);
162         else if (!strcmp(name, "max-stats"))
163                 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
164         else if (!strcmp(name, "cache-size"))
165                 ctx.cfg.cache_size = atoi(value);
166         else if (!strcmp(name, "cache-root"))
167                 ctx.cfg.cache_root = xstrdup(expand_macros(value));
168         else if (!strcmp(name, "cache-root-ttl"))
169                 ctx.cfg.cache_root_ttl = atoi(value);
170         else if (!strcmp(name, "cache-repo-ttl"))
171                 ctx.cfg.cache_repo_ttl = atoi(value);
172         else if (!strcmp(name, "cache-scanrc-ttl"))
173                 ctx.cfg.cache_scanrc_ttl = atoi(value);
174         else if (!strcmp(name, "cache-static-ttl"))
175                 ctx.cfg.cache_static_ttl = atoi(value);
176         else if (!strcmp(name, "cache-dynamic-ttl"))
177                 ctx.cfg.cache_dynamic_ttl = atoi(value);
178         else if (!strcmp(name, "about-filter"))
179                 ctx.cfg.about_filter = new_filter(value, 0);
180         else if (!strcmp(name, "commit-filter"))
181                 ctx.cfg.commit_filter = new_filter(value, 0);
182         else if (!strcmp(name, "embedded"))
183                 ctx.cfg.embedded = atoi(value);
184         else if (!strcmp(name, "max-atom-items"))
185                 ctx.cfg.max_atom_items = atoi(value);
186         else if (!strcmp(name, "max-message-length"))
187                 ctx.cfg.max_msg_len = atoi(value);
188         else if (!strcmp(name, "max-repodesc-length"))
189                 ctx.cfg.max_repodesc_len = atoi(value);
190         else if (!strcmp(name, "max-blob-size"))
191                 ctx.cfg.max_blob_size = atoi(value);
192         else if (!strcmp(name, "max-repo-count"))
193                 ctx.cfg.max_repo_count = atoi(value);
194         else if (!strcmp(name, "max-commit-count"))
195                 ctx.cfg.max_commit_count = atoi(value);
196         else if (!strcmp(name, "project-list"))
197                 ctx.cfg.project_list = xstrdup(expand_macros(value));
198         else if (!strcmp(name, "scan-path"))
199                 if (!ctx.cfg.nocache && ctx.cfg.cache_size)
200                         process_cached_repolist(expand_macros(value));
201                 else if (ctx.cfg.project_list)
202                         scan_projects(expand_macros(value),
203                                       ctx.cfg.project_list, repo_config);
204                 else
205                         scan_tree(expand_macros(value), repo_config);
206         else if (!strcmp(name, "scan-hidden-path"))
207                 ctx.cfg.scan_hidden_path = atoi(value);
208         else if (!strcmp(name, "section-from-path"))
209                 ctx.cfg.section_from_path = atoi(value);
210         else if (!strcmp(name, "source-filter"))
211                 ctx.cfg.source_filter = new_filter(value, 1);
212         else if (!strcmp(name, "summary-log"))
213                 ctx.cfg.summary_log = atoi(value);
214         else if (!strcmp(name, "summary-branches"))
215                 ctx.cfg.summary_branches = atoi(value);
216         else if (!strcmp(name, "summary-tags"))
217                 ctx.cfg.summary_tags = atoi(value);
218         else if (!strcmp(name, "side-by-side-diffs"))
219                 ctx.cfg.ssdiff = atoi(value);
220         else if (!strcmp(name, "agefile"))
221                 ctx.cfg.agefile = xstrdup(value);
222         else if (!strcmp(name, "renamelimit"))
223                 ctx.cfg.renamelimit = atoi(value);
224         else if (!strcmp(name, "remove-suffix"))
225                 ctx.cfg.remove_suffix = atoi(value);
226         else if (!strcmp(name, "robots"))
227                 ctx.cfg.robots = xstrdup(value);
228         else if (!strcmp(name, "clone-prefix"))
229                 ctx.cfg.clone_prefix = xstrdup(value);
230         else if (!strcmp(name, "local-time"))
231                 ctx.cfg.local_time = atoi(value);
232         else if (!prefixcmp(name, "mimetype."))
233                 add_mimetype(name + 9, value);
234         else if (!strcmp(name, "include"))
235                 parse_configfile(expand_macros(value), config_cb);
236 }
237
238 static void querystring_cb(const char *name, const char *value)
239 {
240         if (!value)
241                 value = "";
242
243         if (!strcmp(name,"r")) {
244                 ctx.qry.repo = xstrdup(value);
245                 ctx.repo = cgit_get_repoinfo(value);
246         } else if (!strcmp(name, "p")) {
247                 ctx.qry.page = xstrdup(value);
248         } else if (!strcmp(name, "url")) {
249                 if (*value == '/')
250                         value++;
251                 ctx.qry.url = xstrdup(value);
252                 cgit_parse_url(value);
253         } else if (!strcmp(name, "qt")) {
254                 ctx.qry.grep = xstrdup(value);
255         } else if (!strcmp(name, "q")) {
256                 ctx.qry.search = xstrdup(value);
257         } else if (!strcmp(name, "h")) {
258                 ctx.qry.head = xstrdup(value);
259                 ctx.qry.has_symref = 1;
260         } else if (!strcmp(name, "id")) {
261                 ctx.qry.sha1 = xstrdup(value);
262                 ctx.qry.has_sha1 = 1;
263         } else if (!strcmp(name, "id2")) {
264                 ctx.qry.sha2 = xstrdup(value);
265                 ctx.qry.has_sha1 = 1;
266         } else if (!strcmp(name, "ofs")) {
267                 ctx.qry.ofs = atoi(value);
268         } else if (!strcmp(name, "path")) {
269                 ctx.qry.path = trim_end(value, '/');
270         } else if (!strcmp(name, "name")) {
271                 ctx.qry.name = xstrdup(value);
272         } else if (!strcmp(name, "mimetype")) {
273                 ctx.qry.mimetype = xstrdup(value);
274         } else if (!strcmp(name, "s")){
275                 ctx.qry.sort = xstrdup(value);
276         } else if (!strcmp(name, "showmsg")) {
277                 ctx.qry.showmsg = atoi(value);
278         } else if (!strcmp(name, "period")) {
279                 ctx.qry.period = xstrdup(value);
280         } else if (!strcmp(name, "ss")) {
281                 ctx.qry.ssdiff = atoi(value);
282         } else if (!strcmp(name, "all")) {
283                 ctx.qry.show_all = atoi(value);
284         } else if (!strcmp(name, "context")) {
285                 ctx.qry.context = atoi(value);
286         } else if (!strcmp(name, "ignorews")) {
287                 ctx.qry.ignorews = atoi(value);
288         }
289 }
290
291 char *xstrdupn(const char *str)
292 {
293         return (str ? xstrdup(str) : NULL);
294 }
295
296 static void prepare_context(struct cgit_context *ctx)
297 {
298         memset(ctx, 0, sizeof(*ctx));
299         ctx->cfg.agefile = "info/web/last-modified";
300         ctx->cfg.nocache = 0;
301         ctx->cfg.cache_size = 0;
302         ctx->cfg.cache_dynamic_ttl = 5;
303         ctx->cfg.cache_max_create_time = 5;
304         ctx->cfg.cache_repo_ttl = 5;
305         ctx->cfg.cache_root = CGIT_CACHE_ROOT;
306         ctx->cfg.cache_root_ttl = 5;
307         ctx->cfg.cache_scanrc_ttl = 15;
308         ctx->cfg.cache_static_ttl = -1;
309         ctx->cfg.css = "/cgit.css";
310         ctx->cfg.logo = "/cgit.png";
311         ctx->cfg.local_time = 0;
312         ctx->cfg.enable_gitweb_owner = 1;
313         ctx->cfg.enable_tree_linenumbers = 1;
314         ctx->cfg.max_repo_count = 50;
315         ctx->cfg.max_commit_count = 50;
316         ctx->cfg.max_lock_attempts = 5;
317         ctx->cfg.max_msg_len = 80;
318         ctx->cfg.max_repodesc_len = 80;
319         ctx->cfg.max_blob_size = 0;
320         ctx->cfg.max_stats = 0;
321         ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
322         ctx->cfg.project_list = NULL;
323         ctx->cfg.renamelimit = -1;
324         ctx->cfg.remove_suffix = 0;
325         ctx->cfg.robots = "index, nofollow";
326         ctx->cfg.root_title = "Git repository browser";
327         ctx->cfg.root_desc = "a fast webinterface for the git dscm";
328         ctx->cfg.scan_hidden_path = 0;
329         ctx->cfg.script_name = CGIT_SCRIPT_NAME;
330         ctx->cfg.section = "";
331         ctx->cfg.summary_branches = 10;
332         ctx->cfg.summary_log = 10;
333         ctx->cfg.summary_tags = 10;
334         ctx->cfg.max_atom_items = 10;
335         ctx->cfg.ssdiff = 0;
336         ctx->env.cgit_config = xstrdupn(getenv("CGIT_CONFIG"));
337         ctx->env.http_host = xstrdupn(getenv("HTTP_HOST"));
338         ctx->env.https = xstrdupn(getenv("HTTPS"));
339         ctx->env.no_http = xstrdupn(getenv("NO_HTTP"));
340         ctx->env.path_info = xstrdupn(getenv("PATH_INFO"));
341         ctx->env.query_string = xstrdupn(getenv("QUERY_STRING"));
342         ctx->env.request_method = xstrdupn(getenv("REQUEST_METHOD"));
343         ctx->env.script_name = xstrdupn(getenv("SCRIPT_NAME"));
344         ctx->env.server_name = xstrdupn(getenv("SERVER_NAME"));
345         ctx->env.server_port = xstrdupn(getenv("SERVER_PORT"));
346         ctx->page.mimetype = "text/html";
347         ctx->page.charset = PAGE_ENCODING;
348         ctx->page.filename = NULL;
349         ctx->page.size = 0;
350         ctx->page.modified = time(NULL);
351         ctx->page.expires = ctx->page.modified;
352         ctx->page.etag = NULL;
353         memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list));
354         if (ctx->env.script_name)
355                 ctx->cfg.script_name = ctx->env.script_name;
356         if (ctx->env.query_string)
357                 ctx->qry.raw = ctx->env.query_string;
358         if (!ctx->env.cgit_config)
359                 ctx->env.cgit_config = CGIT_CONFIG;
360 }
361
362 struct refmatch {
363         char *req_ref;
364         char *first_ref;
365         int match;
366 };
367
368 int find_current_ref(const char *refname, const unsigned char *sha1,
369                      int flags, void *cb_data)
370 {
371         struct refmatch *info;
372
373         info = (struct refmatch *)cb_data;
374         if (!strcmp(refname, info->req_ref))
375                 info->match = 1;
376         if (!info->first_ref)
377                 info->first_ref = xstrdup(refname);
378         return info->match;
379 }
380
381 char *find_default_branch(struct cgit_repo *repo)
382 {
383         struct refmatch info;
384         char *ref;
385
386         info.req_ref = repo->defbranch;
387         info.first_ref = NULL;
388         info.match = 0;
389         for_each_branch_ref(find_current_ref, &info);
390         if (info.match)
391                 ref = info.req_ref;
392         else
393                 ref = info.first_ref;
394         if (ref)
395                 ref = xstrdup(ref);
396         return ref;
397 }
398
399 static int prepare_repo_cmd(struct cgit_context *ctx)
400 {
401         char *tmp;
402         unsigned char sha1[20];
403         int nongit = 0;
404
405         setenv("GIT_DIR", ctx->repo->path, 1);
406         setup_git_directory_gently(&nongit);
407         if (nongit) {
408                 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
409                                       "config error");
410                 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
411                 ctx->repo = NULL;
412                 cgit_print_http_headers(ctx);
413                 cgit_print_docstart(ctx);
414                 cgit_print_pageheader(ctx);
415                 cgit_print_error(tmp);
416                 cgit_print_docend();
417                 return 1;
418         }
419         ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
420
421         if (!ctx->qry.head) {
422                 ctx->qry.nohead = 1;
423                 ctx->qry.head = find_default_branch(ctx->repo);
424                 ctx->repo->defbranch = ctx->qry.head;
425         }
426
427         if (!ctx->qry.head) {
428                 cgit_print_http_headers(ctx);
429                 cgit_print_docstart(ctx);
430                 cgit_print_pageheader(ctx);
431                 cgit_print_error("Repository seems to be empty");
432                 cgit_print_docend();
433                 return 1;
434         }
435
436         if (get_sha1(ctx->qry.head, sha1)) {
437                 tmp = xstrdup(ctx->qry.head);
438                 ctx->qry.head = ctx->repo->defbranch;
439                 ctx->page.status = 404;
440                 ctx->page.statusmsg = "not found";
441                 cgit_print_http_headers(ctx);
442                 cgit_print_docstart(ctx);
443                 cgit_print_pageheader(ctx);
444                 cgit_print_error(fmt("Invalid branch: %s", tmp));
445                 cgit_print_docend();
446                 return 1;
447         }
448         return 0;
449 }
450
451 static void process_request(void *cbdata)
452 {
453         struct cgit_context *ctx = cbdata;
454         struct cgit_cmd *cmd;
455
456         cmd = cgit_get_cmd(ctx);
457         if (!cmd) {
458                 ctx->page.title = "cgit error";
459                 cgit_print_http_headers(ctx);
460                 cgit_print_docstart(ctx);
461                 cgit_print_pageheader(ctx);
462                 cgit_print_error("Invalid request");
463                 cgit_print_docend();
464                 return;
465         }
466
467         /* If cmd->want_vpath is set, assume ctx->qry.path contains a "virtual"
468          * in-project path limit to be made available at ctx->qry.vpath.
469          * Otherwise, no path limit is in effect (ctx->qry.vpath = NULL).
470          */
471         ctx->qry.vpath = cmd->want_vpath ? ctx->qry.path : NULL;
472
473         if (cmd->want_repo && !ctx->repo) {
474                 cgit_print_http_headers(ctx);
475                 cgit_print_docstart(ctx);
476                 cgit_print_pageheader(ctx);
477                 cgit_print_error(fmt("No repository selected"));
478                 cgit_print_docend();
479                 return;
480         }
481
482         if (ctx->repo && prepare_repo_cmd(ctx))
483                 return;
484
485         if (cmd->want_layout) {
486                 cgit_print_http_headers(ctx);
487                 cgit_print_docstart(ctx);
488                 cgit_print_pageheader(ctx);
489         }
490
491         cmd->fn(ctx);
492
493         if (cmd->want_layout)
494                 cgit_print_docend();
495 }
496
497 int cmp_repos(const void *a, const void *b)
498 {
499         const struct cgit_repo *ra = a, *rb = b;
500         return strcmp(ra->url, rb->url);
501 }
502
503 char *build_snapshot_setting(int bitmap)
504 {
505         const struct cgit_snapshot_format *f;
506         char *result = xstrdup("");
507         char *tmp;
508         int len;
509
510         for (f = cgit_snapshot_formats; f->suffix; f++) {
511                 if (f->bit & bitmap) {
512                         tmp = result;
513                         result = xstrdup(fmt("%s%s ", tmp, f->suffix));
514                         free(tmp);
515                 }
516         }
517         len = strlen(result);
518         if (len)
519                 result[len - 1] = '\0';
520         return result;
521 }
522
523 char *get_first_line(char *txt)
524 {
525         char *t = xstrdup(txt);
526         char *p = strchr(t, '\n');
527         if (p)
528                 *p = '\0';
529         return t;
530 }
531
532 void print_repo(FILE *f, struct cgit_repo *repo)
533 {
534         fprintf(f, "repo.url=%s\n", repo->url);
535         fprintf(f, "repo.name=%s\n", repo->name);
536         fprintf(f, "repo.path=%s\n", repo->path);
537         if (repo->owner)
538                 fprintf(f, "repo.owner=%s\n", repo->owner);
539         if (repo->desc) {
540                 char *tmp = get_first_line(repo->desc);
541                 fprintf(f, "repo.desc=%s\n", tmp);
542                 free(tmp);
543         }
544         if (repo->readme)
545                 fprintf(f, "repo.readme=%s\n", repo->readme);
546         if (repo->defbranch)
547                 fprintf(f, "repo.defbranch=%s\n", repo->defbranch);
548         if (repo->module_link)
549                 fprintf(f, "repo.module-link=%s\n", repo->module_link);
550         if (repo->section)
551                 fprintf(f, "repo.section=%s\n", repo->section);
552         if (repo->clone_url)
553                 fprintf(f, "repo.clone-url=%s\n", repo->clone_url);
554         fprintf(f, "repo.enable-commit-graph=%d\n",
555                 repo->enable_commit_graph);
556         fprintf(f, "repo.enable-log-filecount=%d\n",
557                 repo->enable_log_filecount);
558         fprintf(f, "repo.enable-log-linecount=%d\n",
559                 repo->enable_log_linecount);
560         if (repo->about_filter && repo->about_filter != ctx.cfg.about_filter)
561                 fprintf(f, "repo.about-filter=%s\n", repo->about_filter->cmd);
562         if (repo->commit_filter && repo->commit_filter != ctx.cfg.commit_filter)
563                 fprintf(f, "repo.commit-filter=%s\n", repo->commit_filter->cmd);
564         if (repo->source_filter && repo->source_filter != ctx.cfg.source_filter)
565                 fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd);
566         if (repo->snapshots != ctx.cfg.snapshots) {
567                 char *tmp = build_snapshot_setting(repo->snapshots);
568                 fprintf(f, "repo.snapshots=%s\n", tmp);
569                 free(tmp);
570         }
571         if (repo->max_stats != ctx.cfg.max_stats)
572                 fprintf(f, "repo.max-stats=%s\n",
573                         cgit_find_stats_periodname(repo->max_stats));
574         fprintf(f, "\n");
575 }
576
577 void print_repolist(FILE *f, struct cgit_repolist *list, int start)
578 {
579         int i;
580
581         for(i = start; i < list->count; i++)
582                 print_repo(f, &list->repos[i]);
583 }
584
585 /* Scan 'path' for git repositories, save the resulting repolist in 'cached_rc'
586  * and return 0 on success.
587  */
588 static int generate_cached_repolist(const char *path, const char *cached_rc)
589 {
590         char *locked_rc;
591         int idx;
592         FILE *f;
593
594         locked_rc = xstrdup(fmt("%s.lock", cached_rc));
595         f = fopen(locked_rc, "wx");
596         if (!f) {
597                 /* Inform about the error unless the lockfile already existed,
598                  * since that only means we've got concurrent requests.
599                  */
600                 if (errno != EEXIST)
601                         fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
602                                 locked_rc, strerror(errno), errno);
603                 return errno;
604         }
605         idx = cgit_repolist.count;
606         if (ctx.cfg.project_list)
607                 scan_projects(path, ctx.cfg.project_list, repo_config);
608         else
609                 scan_tree(path, repo_config);
610         print_repolist(f, &cgit_repolist, idx);
611         if (rename(locked_rc, cached_rc))
612                 fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
613                         locked_rc, cached_rc, strerror(errno), errno);
614         fclose(f);
615         return 0;
616 }
617
618 static void process_cached_repolist(const char *path)
619 {
620         struct stat st;
621         char *cached_rc;
622         time_t age;
623         unsigned long hash;
624
625         hash = hash_str(path);
626         if (ctx.cfg.project_list)
627                 hash += hash_str(ctx.cfg.project_list);
628         cached_rc = xstrdup(fmt("%s/rc-%8lx", ctx.cfg.cache_root, hash));
629
630         if (stat(cached_rc, &st)) {
631                 /* Nothing is cached, we need to scan without forking. And
632                  * if we fail to generate a cached repolist, we need to
633                  * invoke scan_tree manually.
634                  */
635                 if (generate_cached_repolist(path, cached_rc)) {
636                         if (ctx.cfg.project_list)
637                                 scan_projects(path, ctx.cfg.project_list,
638                                               repo_config);
639                         else
640                                 scan_tree(path, repo_config);
641                 }
642                 return;
643         }
644
645         parse_configfile(cached_rc, config_cb);
646
647         /* If the cached configfile hasn't expired, lets exit now */
648         age = time(NULL) - st.st_mtime;
649         if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
650                 return;
651
652         /* The cached repolist has been parsed, but it was old. So lets
653          * rescan the specified path and generate a new cached repolist
654          * in a child-process to avoid latency for the current request.
655          */
656         if (fork())
657                 return;
658
659         exit(generate_cached_repolist(path, cached_rc));
660 }
661
662 static void cgit_parse_args(int argc, const char **argv)
663 {
664         int i;
665         int scan = 0;
666
667         for (i = 1; i < argc; i++) {
668                 if (!strncmp(argv[i], "--cache=", 8)) {
669                         ctx.cfg.cache_root = xstrdup(argv[i]+8);
670                 }
671                 if (!strcmp(argv[i], "--nocache")) {
672                         ctx.cfg.nocache = 1;
673                 }
674                 if (!strcmp(argv[i], "--nohttp")) {
675                         ctx.env.no_http = "1";
676                 }
677                 if (!strncmp(argv[i], "--query=", 8)) {
678                         ctx.qry.raw = xstrdup(argv[i]+8);
679                 }
680                 if (!strncmp(argv[i], "--repo=", 7)) {
681                         ctx.qry.repo = xstrdup(argv[i]+7);
682                 }
683                 if (!strncmp(argv[i], "--page=", 7)) {
684                         ctx.qry.page = xstrdup(argv[i]+7);
685                 }
686                 if (!strncmp(argv[i], "--head=", 7)) {
687                         ctx.qry.head = xstrdup(argv[i]+7);
688                         ctx.qry.has_symref = 1;
689                 }
690                 if (!strncmp(argv[i], "--sha1=", 7)) {
691                         ctx.qry.sha1 = xstrdup(argv[i]+7);
692                         ctx.qry.has_sha1 = 1;
693                 }
694                 if (!strncmp(argv[i], "--ofs=", 6)) {
695                         ctx.qry.ofs = atoi(argv[i]+6);
696                 }
697                 if (!strncmp(argv[i], "--scan-tree=", 12) ||
698                     !strncmp(argv[i], "--scan-path=", 12)) {
699                         /* HACK: the global snapshot bitmask defines the
700                          * set of allowed snapshot formats, but the config
701                          * file hasn't been parsed yet so the mask is
702                          * currently 0. By setting all bits high before
703                          * scanning we make sure that any in-repo cgitrc
704                          * snapshot setting is respected by scan_tree().
705                          * BTW: we assume that there'll never be more than
706                          * 255 different snapshot formats supported by cgit...
707                          */
708                         ctx.cfg.snapshots = 0xFF;
709                         scan++;
710                         scan_tree(argv[i] + 12, repo_config);
711                 }
712         }
713         if (scan) {
714                 qsort(cgit_repolist.repos, cgit_repolist.count,
715                         sizeof(struct cgit_repo), cmp_repos);
716                 print_repolist(stdout, &cgit_repolist, 0);
717                 exit(0);
718         }
719 }
720
721 static int calc_ttl()
722 {
723         if (!ctx.repo)
724                 return ctx.cfg.cache_root_ttl;
725
726         if (!ctx.qry.page)
727                 return ctx.cfg.cache_repo_ttl;
728
729         if (ctx.qry.has_symref)
730                 return ctx.cfg.cache_dynamic_ttl;
731
732         if (ctx.qry.has_sha1)
733                 return ctx.cfg.cache_static_ttl;
734
735         return ctx.cfg.cache_repo_ttl;
736 }
737
738 int main(int argc, const char **argv)
739 {
740         const char *path;
741         char *qry;
742         int err, ttl;
743
744         prepare_context(&ctx);
745         cgit_repolist.length = 0;
746         cgit_repolist.count = 0;
747         cgit_repolist.repos = NULL;
748
749         cgit_parse_args(argc, argv);
750         parse_configfile(expand_macros(ctx.env.cgit_config), config_cb);
751         ctx.repo = NULL;
752         http_parse_querystring(ctx.qry.raw, querystring_cb);
753
754         /* If virtual-root isn't specified in cgitrc, lets pretend
755          * that virtual-root equals SCRIPT_NAME, minus any possibly
756          * trailing slashes.
757          */
758         if (!ctx.cfg.virtual_root)
759                 ctx.cfg.virtual_root = trim_end(ctx.cfg.script_name, '/');
760
761         /* If no url parameter is specified on the querystring, lets
762          * use PATH_INFO as url. This allows cgit to work with virtual
763          * urls without the need for rewriterules in the webserver (as
764          * long as PATH_INFO is included in the cache lookup key).
765          */
766         path = ctx.env.path_info;
767         if (!ctx.qry.url && path) {
768                 if (path[0] == '/')
769                         path++;
770                 ctx.qry.url = xstrdup(path);
771                 if (ctx.qry.raw) {
772                         qry = ctx.qry.raw;
773                         ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
774                         free(qry);
775                 } else
776                         ctx.qry.raw = xstrdup(ctx.qry.url);
777                 cgit_parse_url(ctx.qry.url);
778         }
779
780         ttl = calc_ttl();
781         ctx.page.expires += ttl*60;
782         if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
783                 ctx.cfg.nocache = 1;
784         if (ctx.cfg.nocache)
785                 ctx.cfg.cache_size = 0;
786         err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
787                             ctx.qry.raw, ttl, process_request, &ctx);
788         if (err)
789                 cgit_print_error(fmt("Error processing page: %s (%d)",
790                                      strerror(err), err));
791         return err;
792 }