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