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