1 /* cgit.c: cgi for the git scm
3 * Copyright (C) 2006 Lars Hjemli
4 * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
13 #include "configfile.h"
15 #include "ui-shared.h"
18 #include "ui-summary.h"
19 #include "scan-tree.h"
21 const char *cgit_version = CGIT_VERSION;
23 static void add_mimetype(const char *name, const char *value)
25 struct string_list_item *item;
27 item = string_list_insert(&ctx.cfg.mimetypes, xstrdup(name));
28 item->util = xstrdup(value);
31 static struct cgit_filter *new_filter(const char *cmd, filter_type filtertype)
33 struct cgit_filter *f;
52 f = xmalloc(sizeof(struct cgit_filter));
53 f->cmd = xstrdup(cmd);
54 args_size = (2 + extra_args) * sizeof(char *);
55 f->argv = xmalloc(args_size);
56 memset(f->argv, 0, args_size);
61 static void process_cached_repolist(const char *path);
63 static void repo_config(struct cgit_repo *repo, const char *name, const char *value)
65 struct string_list_item *item;
67 if (!strcmp(name, "name"))
68 repo->name = xstrdup(value);
69 else if (!strcmp(name, "clone-url"))
70 repo->clone_url = xstrdup(value);
71 else if (!strcmp(name, "desc"))
72 repo->desc = xstrdup(value);
73 else if (!strcmp(name, "owner"))
74 repo->owner = xstrdup(value);
75 else if (!strcmp(name, "defbranch"))
76 repo->defbranch = xstrdup(value);
77 else if (!strcmp(name, "snapshots"))
78 repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value);
79 else if (!strcmp(name, "enable-commit-graph"))
80 repo->enable_commit_graph = atoi(value);
81 else if (!strcmp(name, "enable-log-filecount"))
82 repo->enable_log_filecount = atoi(value);
83 else if (!strcmp(name, "enable-log-linecount"))
84 repo->enable_log_linecount = atoi(value);
85 else if (!strcmp(name, "enable-remote-branches"))
86 repo->enable_remote_branches = atoi(value);
87 else if (!strcmp(name, "enable-subject-links"))
88 repo->enable_subject_links = atoi(value);
89 else if (!strcmp(name, "branch-sort")) {
90 if (!strcmp(value, "age"))
91 repo->branch_sort = 1;
92 if (!strcmp(value, "name"))
93 repo->branch_sort = 0;
94 } else if (!strcmp(name, "commit-sort")) {
95 if (!strcmp(value, "date"))
96 repo->commit_sort = 1;
97 if (!strcmp(value, "topo"))
98 repo->commit_sort = 2;
99 } else if (!strcmp(name, "max-stats"))
100 repo->max_stats = cgit_find_stats_period(value, NULL);
101 else if (!strcmp(name, "module-link"))
102 repo->module_link= xstrdup(value);
103 else if (!prefixcmp(name, "module-link.")) {
104 item = string_list_append(&repo->submodules, xstrdup(name + 12));
105 item->util = xstrdup(value);
106 } else if (!strcmp(name, "section"))
107 repo->section = xstrdup(value);
108 else if (!strcmp(name, "readme") && value != NULL) {
109 if (repo->readme.items == ctx.cfg.readme.items)
110 memset(&repo->readme, 0, sizeof(repo->readme));
111 string_list_append(&repo->readme, xstrdup(value));
112 } else if (!strcmp(name, "logo") && value != NULL)
113 repo->logo = xstrdup(value);
114 else if (!strcmp(name, "logo-link") && value != NULL)
115 repo->logo_link = xstrdup(value);
116 else if (ctx.cfg.enable_filter_overrides) {
117 if (!strcmp(name, "about-filter"))
118 repo->about_filter = new_filter(value, ABOUT);
119 else if (!strcmp(name, "commit-filter"))
120 repo->commit_filter = new_filter(value, COMMIT);
121 else if (!strcmp(name, "source-filter"))
122 repo->source_filter = new_filter(value, SOURCE);
126 static void config_cb(const char *name, const char *value)
128 if (!strcmp(name, "section") || !strcmp(name, "repo.group"))
129 ctx.cfg.section = xstrdup(value);
130 else if (!strcmp(name, "repo.url"))
131 ctx.repo = cgit_add_repo(value);
132 else if (ctx.repo && !strcmp(name, "repo.path"))
133 ctx.repo->path = trim_end(value, '/');
134 else if (ctx.repo && !prefixcmp(name, "repo."))
135 repo_config(ctx.repo, name + 5, value);
136 else if (!strcmp(name, "readme") && value != NULL)
137 string_list_append(&ctx.cfg.readme, xstrdup(value));
138 else if (!strcmp(name, "root-title"))
139 ctx.cfg.root_title = xstrdup(value);
140 else if (!strcmp(name, "root-desc"))
141 ctx.cfg.root_desc = xstrdup(value);
142 else if (!strcmp(name, "root-readme"))
143 ctx.cfg.root_readme = xstrdup(value);
144 else if (!strcmp(name, "css"))
145 ctx.cfg.css = xstrdup(value);
146 else if (!strcmp(name, "favicon"))
147 ctx.cfg.favicon = xstrdup(value);
148 else if (!strcmp(name, "footer"))
149 ctx.cfg.footer = xstrdup(value);
150 else if (!strcmp(name, "head-include"))
151 ctx.cfg.head_include = xstrdup(value);
152 else if (!strcmp(name, "header"))
153 ctx.cfg.header = xstrdup(value);
154 else if (!strcmp(name, "logo"))
155 ctx.cfg.logo = xstrdup(value);
156 else if (!strcmp(name, "index-header"))
157 ctx.cfg.index_header = xstrdup(value);
158 else if (!strcmp(name, "index-info"))
159 ctx.cfg.index_info = xstrdup(value);
160 else if (!strcmp(name, "logo-link"))
161 ctx.cfg.logo_link = xstrdup(value);
162 else if (!strcmp(name, "module-link"))
163 ctx.cfg.module_link = xstrdup(value);
164 else if (!strcmp(name, "strict-export"))
165 ctx.cfg.strict_export = xstrdup(value);
166 else if (!strcmp(name, "virtual-root")) {
167 ctx.cfg.virtual_root = ensure_end(value, '/');
168 } else if (!strcmp(name, "nocache"))
169 ctx.cfg.nocache = atoi(value);
170 else if (!strcmp(name, "noplainemail"))
171 ctx.cfg.noplainemail = atoi(value);
172 else if (!strcmp(name, "noheader"))
173 ctx.cfg.noheader = atoi(value);
174 else if (!strcmp(name, "snapshots"))
175 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
176 else if (!strcmp(name, "enable-filter-overrides"))
177 ctx.cfg.enable_filter_overrides = atoi(value);
178 else if (!strcmp(name, "enable-http-clone"))
179 ctx.cfg.enable_http_clone = atoi(value);
180 else if (!strcmp(name, "enable-index-links"))
181 ctx.cfg.enable_index_links = atoi(value);
182 else if (!strcmp(name, "enable-index-owner"))
183 ctx.cfg.enable_index_owner = atoi(value);
184 else if (!strcmp(name, "enable-commit-graph"))
185 ctx.cfg.enable_commit_graph = atoi(value);
186 else if (!strcmp(name, "enable-log-filecount"))
187 ctx.cfg.enable_log_filecount = atoi(value);
188 else if (!strcmp(name, "enable-log-linecount"))
189 ctx.cfg.enable_log_linecount = atoi(value);
190 else if (!strcmp(name, "enable-remote-branches"))
191 ctx.cfg.enable_remote_branches = atoi(value);
192 else if (!strcmp(name, "enable-subject-links"))
193 ctx.cfg.enable_subject_links = atoi(value);
194 else if (!strcmp(name, "enable-tree-linenumbers"))
195 ctx.cfg.enable_tree_linenumbers = atoi(value);
196 else if (!strcmp(name, "enable-git-config"))
197 ctx.cfg.enable_git_config = atoi(value);
198 else if (!strcmp(name, "max-stats"))
199 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
200 else if (!strcmp(name, "cache-size"))
201 ctx.cfg.cache_size = atoi(value);
202 else if (!strcmp(name, "cache-root"))
203 ctx.cfg.cache_root = xstrdup(expand_macros(value));
204 else if (!strcmp(name, "cache-root-ttl"))
205 ctx.cfg.cache_root_ttl = atoi(value);
206 else if (!strcmp(name, "cache-repo-ttl"))
207 ctx.cfg.cache_repo_ttl = atoi(value);
208 else if (!strcmp(name, "cache-scanrc-ttl"))
209 ctx.cfg.cache_scanrc_ttl = atoi(value);
210 else if (!strcmp(name, "cache-static-ttl"))
211 ctx.cfg.cache_static_ttl = atoi(value);
212 else if (!strcmp(name, "cache-dynamic-ttl"))
213 ctx.cfg.cache_dynamic_ttl = atoi(value);
214 else if (!strcmp(name, "case-sensitive-sort"))
215 ctx.cfg.case_sensitive_sort = atoi(value);
216 else if (!strcmp(name, "about-filter"))
217 ctx.cfg.about_filter = new_filter(value, ABOUT);
218 else if (!strcmp(name, "commit-filter"))
219 ctx.cfg.commit_filter = new_filter(value, COMMIT);
220 else if (!strcmp(name, "embedded"))
221 ctx.cfg.embedded = atoi(value);
222 else if (!strcmp(name, "max-atom-items"))
223 ctx.cfg.max_atom_items = atoi(value);
224 else if (!strcmp(name, "max-message-length"))
225 ctx.cfg.max_msg_len = atoi(value);
226 else if (!strcmp(name, "max-repodesc-length"))
227 ctx.cfg.max_repodesc_len = atoi(value);
228 else if (!strcmp(name, "max-blob-size"))
229 ctx.cfg.max_blob_size = atoi(value);
230 else if (!strcmp(name, "max-repo-count"))
231 ctx.cfg.max_repo_count = atoi(value);
232 else if (!strcmp(name, "max-commit-count"))
233 ctx.cfg.max_commit_count = atoi(value);
234 else if (!strcmp(name, "project-list"))
235 ctx.cfg.project_list = xstrdup(expand_macros(value));
236 else if (!strcmp(name, "scan-path"))
237 if (!ctx.cfg.nocache && ctx.cfg.cache_size)
238 process_cached_repolist(expand_macros(value));
239 else if (ctx.cfg.project_list)
240 scan_projects(expand_macros(value),
241 ctx.cfg.project_list, repo_config);
243 scan_tree(expand_macros(value), repo_config);
244 else if (!strcmp(name, "scan-hidden-path"))
245 ctx.cfg.scan_hidden_path = atoi(value);
246 else if (!strcmp(name, "section-from-path"))
247 ctx.cfg.section_from_path = atoi(value);
248 else if (!strcmp(name, "repository-sort"))
249 ctx.cfg.repository_sort = xstrdup(value);
250 else if (!strcmp(name, "section-sort"))
251 ctx.cfg.section_sort = atoi(value);
252 else if (!strcmp(name, "source-filter"))
253 ctx.cfg.source_filter = new_filter(value, SOURCE);
254 else if (!strcmp(name, "summary-log"))
255 ctx.cfg.summary_log = atoi(value);
256 else if (!strcmp(name, "summary-branches"))
257 ctx.cfg.summary_branches = atoi(value);
258 else if (!strcmp(name, "summary-tags"))
259 ctx.cfg.summary_tags = atoi(value);
260 else if (!strcmp(name, "side-by-side-diffs"))
261 ctx.cfg.ssdiff = atoi(value);
262 else if (!strcmp(name, "agefile"))
263 ctx.cfg.agefile = xstrdup(value);
264 else if (!strcmp(name, "mimetype-file"))
265 ctx.cfg.mimetype_file = xstrdup(value);
266 else if (!strcmp(name, "renamelimit"))
267 ctx.cfg.renamelimit = atoi(value);
268 else if (!strcmp(name, "remove-suffix"))
269 ctx.cfg.remove_suffix = atoi(value);
270 else if (!strcmp(name, "robots"))
271 ctx.cfg.robots = xstrdup(value);
272 else if (!strcmp(name, "clone-prefix"))
273 ctx.cfg.clone_prefix = xstrdup(value);
274 else if (!strcmp(name, "clone-url"))
275 ctx.cfg.clone_url = xstrdup(value);
276 else if (!strcmp(name, "local-time"))
277 ctx.cfg.local_time = atoi(value);
278 else if (!strcmp(name, "commit-sort")) {
279 if (!strcmp(value, "date"))
280 ctx.cfg.commit_sort = 1;
281 if (!strcmp(value, "topo"))
282 ctx.cfg.commit_sort = 2;
283 } else if (!strcmp(name, "branch-sort")) {
284 if (!strcmp(value, "age"))
285 ctx.cfg.branch_sort = 1;
286 if (!strcmp(value, "name"))
287 ctx.cfg.branch_sort = 0;
288 } else if (!prefixcmp(name, "mimetype."))
289 add_mimetype(name + 9, value);
290 else if (!strcmp(name, "include"))
291 parse_configfile(expand_macros(value), config_cb);
294 static void querystring_cb(const char *name, const char *value)
299 if (!strcmp(name,"r")) {
300 ctx.qry.repo = xstrdup(value);
301 ctx.repo = cgit_get_repoinfo(value);
302 } else if (!strcmp(name, "p")) {
303 ctx.qry.page = xstrdup(value);
304 } else if (!strcmp(name, "url")) {
307 ctx.qry.url = xstrdup(value);
308 cgit_parse_url(value);
309 } else if (!strcmp(name, "qt")) {
310 ctx.qry.grep = xstrdup(value);
311 } else if (!strcmp(name, "q")) {
312 ctx.qry.search = xstrdup(value);
313 } else if (!strcmp(name, "h")) {
314 ctx.qry.head = xstrdup(value);
315 ctx.qry.has_symref = 1;
316 } else if (!strcmp(name, "id")) {
317 ctx.qry.sha1 = xstrdup(value);
318 ctx.qry.has_sha1 = 1;
319 } else if (!strcmp(name, "id2")) {
320 ctx.qry.sha2 = xstrdup(value);
321 ctx.qry.has_sha1 = 1;
322 } else if (!strcmp(name, "ofs")) {
323 ctx.qry.ofs = atoi(value);
324 } else if (!strcmp(name, "path")) {
325 ctx.qry.path = trim_end(value, '/');
326 } else if (!strcmp(name, "name")) {
327 ctx.qry.name = xstrdup(value);
328 } else if (!strcmp(name, "mimetype")) {
329 ctx.qry.mimetype = xstrdup(value);
330 } else if (!strcmp(name, "s")) {
331 ctx.qry.sort = xstrdup(value);
332 } else if (!strcmp(name, "showmsg")) {
333 ctx.qry.showmsg = atoi(value);
334 } else if (!strcmp(name, "period")) {
335 ctx.qry.period = xstrdup(value);
336 } else if (!strcmp(name, "ss")) {
337 ctx.qry.ssdiff = atoi(value);
338 ctx.qry.has_ssdiff = 1;
339 } else if (!strcmp(name, "all")) {
340 ctx.qry.show_all = atoi(value);
341 } else if (!strcmp(name, "context")) {
342 ctx.qry.context = atoi(value);
343 } else if (!strcmp(name, "ignorews")) {
344 ctx.qry.ignorews = atoi(value);
348 static void prepare_context(struct cgit_context *ctx)
350 memset(ctx, 0, sizeof(*ctx));
351 ctx->cfg.agefile = "info/web/last-modified";
352 ctx->cfg.nocache = 0;
353 ctx->cfg.cache_size = 0;
354 ctx->cfg.cache_dynamic_ttl = 5;
355 ctx->cfg.cache_max_create_time = 5;
356 ctx->cfg.cache_repo_ttl = 5;
357 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
358 ctx->cfg.cache_root_ttl = 5;
359 ctx->cfg.cache_scanrc_ttl = 15;
360 ctx->cfg.cache_static_ttl = -1;
361 ctx->cfg.case_sensitive_sort = 1;
362 ctx->cfg.branch_sort = 0;
363 ctx->cfg.commit_sort = 0;
364 ctx->cfg.css = "/cgit.css";
365 ctx->cfg.logo = "/cgit.png";
366 ctx->cfg.favicon = "/favicon.ico";
367 ctx->cfg.local_time = 0;
368 ctx->cfg.enable_http_clone = 1;
369 ctx->cfg.enable_index_owner = 1;
370 ctx->cfg.enable_tree_linenumbers = 1;
371 ctx->cfg.enable_git_config = 0;
372 ctx->cfg.max_repo_count = 50;
373 ctx->cfg.max_commit_count = 50;
374 ctx->cfg.max_lock_attempts = 5;
375 ctx->cfg.max_msg_len = 80;
376 ctx->cfg.max_repodesc_len = 80;
377 ctx->cfg.max_blob_size = 0;
378 ctx->cfg.max_stats = 0;
379 ctx->cfg.project_list = NULL;
380 ctx->cfg.renamelimit = -1;
381 ctx->cfg.remove_suffix = 0;
382 ctx->cfg.robots = "index, nofollow";
383 ctx->cfg.root_title = "Git repository browser";
384 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
385 ctx->cfg.scan_hidden_path = 0;
386 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
387 ctx->cfg.section = "";
388 ctx->cfg.repository_sort = "name";
389 ctx->cfg.section_sort = 1;
390 ctx->cfg.summary_branches = 10;
391 ctx->cfg.summary_log = 10;
392 ctx->cfg.summary_tags = 10;
393 ctx->cfg.max_atom_items = 10;
395 ctx->env.cgit_config = getenv("CGIT_CONFIG");
396 ctx->env.http_host = getenv("HTTP_HOST");
397 ctx->env.https = getenv("HTTPS");
398 ctx->env.no_http = getenv("NO_HTTP");
399 ctx->env.path_info = getenv("PATH_INFO");
400 ctx->env.query_string = getenv("QUERY_STRING");
401 ctx->env.request_method = getenv("REQUEST_METHOD");
402 ctx->env.script_name = getenv("SCRIPT_NAME");
403 ctx->env.server_name = getenv("SERVER_NAME");
404 ctx->env.server_port = getenv("SERVER_PORT");
405 ctx->page.mimetype = "text/html";
406 ctx->page.charset = PAGE_ENCODING;
407 ctx->page.filename = NULL;
409 ctx->page.modified = time(NULL);
410 ctx->page.expires = ctx->page.modified;
411 ctx->page.etag = NULL;
412 memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list));
413 if (ctx->env.script_name)
414 ctx->cfg.script_name = xstrdup(ctx->env.script_name);
415 if (ctx->env.query_string)
416 ctx->qry.raw = xstrdup(ctx->env.query_string);
417 if (!ctx->env.cgit_config)
418 ctx->env.cgit_config = CGIT_CONFIG;
427 static int find_current_ref(const char *refname, const unsigned char *sha1,
428 int flags, void *cb_data)
430 struct refmatch *info;
432 info = (struct refmatch *)cb_data;
433 if (!strcmp(refname, info->req_ref))
435 if (!info->first_ref)
436 info->first_ref = xstrdup(refname);
440 static void free_refmatch_inner(struct refmatch *info)
443 free(info->first_ref);
446 static char *find_default_branch(struct cgit_repo *repo)
448 struct refmatch info;
451 info.req_ref = repo->defbranch;
452 info.first_ref = NULL;
454 for_each_branch_ref(find_current_ref, &info);
458 ref = info.first_ref;
461 free_refmatch_inner(&info);
466 static char *guess_defbranch(void)
469 unsigned char sha1[20];
471 ref = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
472 if (!ref || prefixcmp(ref, "refs/heads/"))
474 return xstrdup(ref + 11);
476 /* The caller must free filename and ref after calling this. */
477 static inline void parse_readme(const char *readme, char **filename, char **ref, struct cgit_repo *repo)
484 if (!readme || !readme[0])
487 /* Check if the readme is tracked in the git repo. */
488 colon = strchr(readme, ':');
489 if (colon && strlen(colon) > 1) {
490 /* If it starts with a colon, we want to use
491 * the default branch */
492 if (colon == readme && repo->defbranch)
493 *ref = xstrdup(repo->defbranch);
495 *ref = xstrndup(readme, colon - readme);
499 /* Prepend repo path to relative readme path unless tracked. */
500 if (!(*ref) && readme[0] != '/')
501 *filename = fmtalloc("%s/%s", repo->path, readme);
503 *filename = xstrdup(readme);
505 static void choose_readme(struct cgit_repo *repo)
508 char *filename, *ref;
509 struct string_list_item *entry;
511 if (!repo->readme.nr)
515 for_each_string_list_item(entry, &repo->readme) {
516 parse_readme(entry->string, &filename, &ref, repo);
522 /* If there's only one item, we skip the possibly expensive
523 * selection process. */
524 if (repo->readme.nr == 1) {
529 if (cgit_ref_path_exists(filename, ref, 1)) {
534 else if (!access(filename, R_OK)) {
541 repo->readme.strdup_strings = 1;
542 string_list_clear(&repo->readme, 0);
543 repo->readme.strdup_strings = 0;
545 string_list_append(&repo->readme, filename)->util = ref;
548 static int prepare_repo_cmd(struct cgit_context *ctx)
550 unsigned char sha1[20];
554 /* The path to the git repository. */
555 setenv("GIT_DIR", ctx->repo->path, 1);
557 /* Do not look in /etc/ for gitconfig and gitattributes. */
558 setenv("GIT_CONFIG_NOSYSTEM", "1", 1);
559 setenv("GIT_ATTR_NOSYSTEM", "1", 1);
561 unsetenv("XDG_CONFIG_HOME");
563 /* Setup the git directory and initialize the notes system. Both of these
564 * load local configuration from the git repository, so we do them both while
565 * the HOME variables are unset. */
566 setup_git_directory_gently(&nongit);
567 init_display_notes(NULL);
570 const char *name = ctx->repo->name;
572 ctx->page.title = fmtalloc("%s - %s", ctx->cfg.root_title,
575 cgit_print_http_headers(ctx);
576 cgit_print_docstart(ctx);
577 cgit_print_pageheader(ctx);
578 cgit_print_error("Failed to open %s: %s", name,
579 rc ? strerror(rc) : "Not a valid git repository");
583 ctx->page.title = fmtalloc("%s - %s", ctx->repo->name, ctx->repo->desc);
585 if (!ctx->repo->defbranch)
586 ctx->repo->defbranch = guess_defbranch();
588 if (!ctx->qry.head) {
590 ctx->qry.head = find_default_branch(ctx->repo);
593 if (!ctx->qry.head) {
594 cgit_print_http_headers(ctx);
595 cgit_print_docstart(ctx);
596 cgit_print_pageheader(ctx);
597 cgit_print_error("Repository seems to be empty");
602 if (get_sha1(ctx->qry.head, sha1)) {
603 char *tmp = xstrdup(ctx->qry.head);
604 ctx->qry.head = ctx->repo->defbranch;
605 ctx->page.status = 404;
606 ctx->page.statusmsg = "Not found";
607 cgit_print_http_headers(ctx);
608 cgit_print_docstart(ctx);
609 cgit_print_pageheader(ctx);
610 cgit_print_error("Invalid branch: %s", tmp);
614 sort_string_list(&ctx->repo->submodules);
615 cgit_prepare_repo_env(ctx->repo);
616 choose_readme(ctx->repo);
620 static void process_request(void *cbdata)
622 struct cgit_context *ctx = cbdata;
623 struct cgit_cmd *cmd;
625 cmd = cgit_get_cmd(ctx);
627 ctx->page.title = "cgit error";
628 ctx->page.status = 404;
629 ctx->page.statusmsg = "Not found";
630 cgit_print_http_headers(ctx);
631 cgit_print_docstart(ctx);
632 cgit_print_pageheader(ctx);
633 cgit_print_error("Invalid request");
638 if (!ctx->cfg.enable_http_clone && cmd->is_clone) {
639 html_status(404, "Not found", 0);
643 /* If cmd->want_vpath is set, assume ctx->qry.path contains a "virtual"
644 * in-project path limit to be made available at ctx->qry.vpath.
645 * Otherwise, no path limit is in effect (ctx->qry.vpath = NULL).
647 ctx->qry.vpath = cmd->want_vpath ? ctx->qry.path : NULL;
649 if (cmd->want_repo && !ctx->repo) {
650 cgit_print_http_headers(ctx);
651 cgit_print_docstart(ctx);
652 cgit_print_pageheader(ctx);
653 cgit_print_error("No repository selected");
658 if (ctx->repo && prepare_repo_cmd(ctx))
661 if (cmd->want_layout) {
662 cgit_print_http_headers(ctx);
663 cgit_print_docstart(ctx);
664 cgit_print_pageheader(ctx);
669 if (cmd->want_layout)
673 static int cmp_repos(const void *a, const void *b)
675 const struct cgit_repo *ra = a, *rb = b;
676 return strcmp(ra->url, rb->url);
679 static char *build_snapshot_setting(int bitmap)
681 const struct cgit_snapshot_format *f;
682 struct strbuf result = STRBUF_INIT;
684 for (f = cgit_snapshot_formats; f->suffix; f++) {
685 if (f->bit & bitmap) {
687 strbuf_addch(&result, ' ');
688 strbuf_addstr(&result, f->suffix);
691 return strbuf_detach(&result, NULL);
694 static char *get_first_line(char *txt)
696 char *t = xstrdup(txt);
697 char *p = strchr(t, '\n');
703 static void print_repo(FILE *f, struct cgit_repo *repo)
705 struct string_list_item *item;
706 fprintf(f, "repo.url=%s\n", repo->url);
707 fprintf(f, "repo.name=%s\n", repo->name);
708 fprintf(f, "repo.path=%s\n", repo->path);
710 fprintf(f, "repo.owner=%s\n", repo->owner);
712 char *tmp = get_first_line(repo->desc);
713 fprintf(f, "repo.desc=%s\n", tmp);
716 for_each_string_list_item(item, &repo->readme) {
718 fprintf(f, "repo.readme=%s:%s\n", (char *)item->util, item->string);
720 fprintf(f, "repo.readme=%s\n", item->string);
723 fprintf(f, "repo.defbranch=%s\n", repo->defbranch);
724 if (repo->module_link)
725 fprintf(f, "repo.module-link=%s\n", repo->module_link);
727 fprintf(f, "repo.section=%s\n", repo->section);
729 fprintf(f, "repo.clone-url=%s\n", repo->clone_url);
730 fprintf(f, "repo.enable-commit-graph=%d\n",
731 repo->enable_commit_graph);
732 fprintf(f, "repo.enable-log-filecount=%d\n",
733 repo->enable_log_filecount);
734 fprintf(f, "repo.enable-log-linecount=%d\n",
735 repo->enable_log_linecount);
736 if (repo->about_filter && repo->about_filter != ctx.cfg.about_filter)
737 fprintf(f, "repo.about-filter=%s\n", repo->about_filter->cmd);
738 if (repo->commit_filter && repo->commit_filter != ctx.cfg.commit_filter)
739 fprintf(f, "repo.commit-filter=%s\n", repo->commit_filter->cmd);
740 if (repo->source_filter && repo->source_filter != ctx.cfg.source_filter)
741 fprintf(f, "repo.source-filter=%s\n", repo->source_filter->cmd);
742 if (repo->snapshots != ctx.cfg.snapshots) {
743 char *tmp = build_snapshot_setting(repo->snapshots);
744 fprintf(f, "repo.snapshots=%s\n", tmp ? tmp : "");
747 if (repo->max_stats != ctx.cfg.max_stats)
748 fprintf(f, "repo.max-stats=%s\n",
749 cgit_find_stats_periodname(repo->max_stats));
751 fprintf(f, "repo.logo=%s\n", repo->logo);
753 fprintf(f, "repo.logo-link=%s\n", repo->logo_link);
754 fprintf(f, "repo.enable-remote-branches=%d\n", repo->enable_remote_branches);
755 fprintf(f, "repo.enable-subject-links=%d\n", repo->enable_subject_links);
756 if (repo->branch_sort == 1)
757 fprintf(f, "repo.branch-sort=age\n");
758 if (repo->commit_sort) {
759 if (repo->commit_sort == 1)
760 fprintf(f, "repo.commit-sort=date\n");
761 else if (repo->commit_sort == 2)
762 fprintf(f, "repo.commit-sort=topo\n");
767 static void print_repolist(FILE *f, struct cgit_repolist *list, int start)
771 for (i = start; i < list->count; i++)
772 print_repo(f, &list->repos[i]);
775 /* Scan 'path' for git repositories, save the resulting repolist in 'cached_rc'
776 * and return 0 on success.
778 static int generate_cached_repolist(const char *path, const char *cached_rc)
780 struct strbuf locked_rc = STRBUF_INIT;
785 strbuf_addf(&locked_rc, "%s.lock", cached_rc);
786 f = fopen(locked_rc.buf, "wx");
788 /* Inform about the error unless the lockfile already existed,
789 * since that only means we've got concurrent requests.
792 if (result != EEXIST)
793 fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
794 locked_rc.buf, strerror(result), result);
797 idx = cgit_repolist.count;
798 if (ctx.cfg.project_list)
799 scan_projects(path, ctx.cfg.project_list, repo_config);
801 scan_tree(path, repo_config);
802 print_repolist(f, &cgit_repolist, idx);
803 if (rename(locked_rc.buf, cached_rc))
804 fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
805 locked_rc.buf, cached_rc, strerror(errno), errno);
808 strbuf_release(&locked_rc);
812 static void process_cached_repolist(const char *path)
815 struct strbuf cached_rc = STRBUF_INIT;
819 hash = hash_str(path);
820 if (ctx.cfg.project_list)
821 hash += hash_str(ctx.cfg.project_list);
822 strbuf_addf(&cached_rc, "%s/rc-%8lx", ctx.cfg.cache_root, hash);
824 if (stat(cached_rc.buf, &st)) {
825 /* Nothing is cached, we need to scan without forking. And
826 * if we fail to generate a cached repolist, we need to
827 * invoke scan_tree manually.
829 if (generate_cached_repolist(path, cached_rc.buf)) {
830 if (ctx.cfg.project_list)
831 scan_projects(path, ctx.cfg.project_list,
834 scan_tree(path, repo_config);
839 parse_configfile(cached_rc.buf, config_cb);
841 /* If the cached configfile hasn't expired, lets exit now */
842 age = time(NULL) - st.st_mtime;
843 if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
846 /* The cached repolist has been parsed, but it was old. So lets
847 * rescan the specified path and generate a new cached repolist
848 * in a child-process to avoid latency for the current request.
853 exit(generate_cached_repolist(path, cached_rc.buf));
855 strbuf_release(&cached_rc);
858 static void cgit_parse_args(int argc, const char **argv)
863 for (i = 1; i < argc; i++) {
864 if (!strncmp(argv[i], "--cache=", 8)) {
865 ctx.cfg.cache_root = xstrdup(argv[i] + 8);
867 if (!strcmp(argv[i], "--nocache")) {
870 if (!strcmp(argv[i], "--nohttp")) {
871 ctx.env.no_http = "1";
873 if (!strncmp(argv[i], "--query=", 8)) {
874 ctx.qry.raw = xstrdup(argv[i] + 8);
876 if (!strncmp(argv[i], "--repo=", 7)) {
877 ctx.qry.repo = xstrdup(argv[i] + 7);
879 if (!strncmp(argv[i], "--page=", 7)) {
880 ctx.qry.page = xstrdup(argv[i] + 7);
882 if (!strncmp(argv[i], "--head=", 7)) {
883 ctx.qry.head = xstrdup(argv[i] + 7);
884 ctx.qry.has_symref = 1;
886 if (!strncmp(argv[i], "--sha1=", 7)) {
887 ctx.qry.sha1 = xstrdup(argv[i] + 7);
888 ctx.qry.has_sha1 = 1;
890 if (!strncmp(argv[i], "--ofs=", 6)) {
891 ctx.qry.ofs = atoi(argv[i] + 6);
893 if (!strncmp(argv[i], "--scan-tree=", 12) ||
894 !strncmp(argv[i], "--scan-path=", 12)) {
895 /* HACK: the global snapshot bitmask defines the
896 * set of allowed snapshot formats, but the config
897 * file hasn't been parsed yet so the mask is
898 * currently 0. By setting all bits high before
899 * scanning we make sure that any in-repo cgitrc
900 * snapshot setting is respected by scan_tree().
901 * BTW: we assume that there'll never be more than
902 * 255 different snapshot formats supported by cgit...
904 ctx.cfg.snapshots = 0xFF;
906 scan_tree(argv[i] + 12, repo_config);
910 qsort(cgit_repolist.repos, cgit_repolist.count,
911 sizeof(struct cgit_repo), cmp_repos);
912 print_repolist(stdout, &cgit_repolist, 0);
917 static int calc_ttl()
920 return ctx.cfg.cache_root_ttl;
923 return ctx.cfg.cache_repo_ttl;
925 if (ctx.qry.has_symref)
926 return ctx.cfg.cache_dynamic_ttl;
928 if (ctx.qry.has_sha1)
929 return ctx.cfg.cache_static_ttl;
931 return ctx.cfg.cache_repo_ttl;
934 int main(int argc, const char **argv)
939 prepare_context(&ctx);
940 cgit_repolist.length = 0;
941 cgit_repolist.count = 0;
942 cgit_repolist.repos = NULL;
944 cgit_parse_args(argc, argv);
945 parse_configfile(expand_macros(ctx.env.cgit_config), config_cb);
947 http_parse_querystring(ctx.qry.raw, querystring_cb);
949 /* If virtual-root isn't specified in cgitrc, lets pretend
950 * that virtual-root equals SCRIPT_NAME, minus any possibly
953 if (!ctx.cfg.virtual_root && ctx.cfg.script_name)
954 ctx.cfg.virtual_root = ensure_end(ctx.cfg.script_name, '/');
956 /* If no url parameter is specified on the querystring, lets
957 * use PATH_INFO as url. This allows cgit to work with virtual
958 * urls without the need for rewriterules in the webserver (as
959 * long as PATH_INFO is included in the cache lookup key).
961 path = ctx.env.path_info;
962 if (!ctx.qry.url && path) {
965 ctx.qry.url = xstrdup(path);
967 char *newqry = fmtalloc("%s?%s", path, ctx.qry.raw);
969 ctx.qry.raw = newqry;
971 ctx.qry.raw = xstrdup(ctx.qry.url);
972 cgit_parse_url(ctx.qry.url);
976 ctx.page.expires += ttl * 60;
977 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
980 ctx.cfg.cache_size = 0;
981 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
982 ctx.qry.raw, ttl, process_request, &ctx);
984 cgit_print_error("Error processing page: %s (%d)",