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