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