blob: 594b95029b521c3807ddc47d0579eb0bd530877c [file] [log] [blame]
Lars Hjemli7640d902006-12-10 22:41:14 +01001/* cgit.c: cgi for the git scm
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli0d169ad2006-12-09 15:18:17 +01009#include "cgit.h"
Lars Hjemliee4056b2008-03-27 09:22:13 +010010#include "cache.h"
Lars Hjemlif3c1a182008-03-24 00:51:19 +010011#include "cmd.h"
Lars Hjemli20a33542008-03-28 00:09:11 +010012#include "configfile.h"
Lars Hjemlie87e8962008-04-08 21:11:36 +020013#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010014#include "ui-shared.h"
Lars Hjemlifb2f3f62008-12-07 13:17:21 +010015#include "ui-stats.h"
Lars Hjemli93397a72008-09-15 00:07:12 +020016#include "scan-tree.h"
Lars Hjemli0d169ad2006-12-09 15:18:17 +010017
Lars Hjemli92908af2008-03-24 23:10:59 +010018const char *cgit_version = CGIT_VERSION;
19
Lars Hjemlic4d46c72009-02-13 20:43:30 +010020void add_mimetype(const char *name, const char *value)
21{
22 struct string_list_item *item;
23
24 item = string_list_insert(xstrdup(name), &ctx.cfg.mimetypes);
25 item->util = xstrdup(value);
26}
27
Lars Hjemlid6f60722009-07-31 17:38:38 +020028struct cgit_filter *new_filter(const char *cmd, int extra_args)
29{
30 struct cgit_filter *f;
31
Lars Hjemli97b3d252009-08-09 13:39:44 +020032 if (!cmd || !cmd[0])
Lars Hjemlid6f60722009-07-31 17:38:38 +020033 return NULL;
34
35 f = xmalloc(sizeof(struct cgit_filter));
36 f->cmd = xstrdup(cmd);
37 f->argv = xmalloc((2 + extra_args) * sizeof(char *));
38 f->argv[0] = f->cmd;
39 f->argv[1] = NULL;
40 return f;
41}
42
Lars Hjemlid7468272009-08-20 17:41:54 +020043static void process_cached_repolist(const char *path);
44
Lars Hjemli163037e2008-03-24 17:26:08 +010045void config_cb(const char *name, const char *value)
46{
47 if (!strcmp(name, "root-title"))
48 ctx.cfg.root_title = xstrdup(value);
Lars Hjemli4c991602008-04-29 00:55:34 +020049 else if (!strcmp(name, "root-desc"))
50 ctx.cfg.root_desc = xstrdup(value);
Lars Hjemlic6431a72008-04-29 01:06:30 +020051 else if (!strcmp(name, "root-readme"))
52 ctx.cfg.root_readme = xstrdup(value);
Lars Hjemli163037e2008-03-24 17:26:08 +010053 else if (!strcmp(name, "css"))
54 ctx.cfg.css = xstrdup(value);
Lars Hjemli502865a2008-07-19 20:40:30 +020055 else if (!strcmp(name, "favicon"))
56 ctx.cfg.favicon = xstrdup(value);
Lars Hjemlide5e9282008-06-26 13:53:30 +020057 else if (!strcmp(name, "footer"))
58 ctx.cfg.footer = xstrdup(value);
Mark Lodatob5a3a202009-03-15 00:11:54 -040059 else if (!strcmp(name, "head-include"))
60 ctx.cfg.head_include = xstrdup(value);
Lars Hjemlib1159552009-01-29 21:27:39 +010061 else if (!strcmp(name, "header"))
62 ctx.cfg.header = xstrdup(value);
Lars Hjemli163037e2008-03-24 17:26:08 +010063 else if (!strcmp(name, "logo"))
64 ctx.cfg.logo = xstrdup(value);
65 else if (!strcmp(name, "index-header"))
66 ctx.cfg.index_header = xstrdup(value);
67 else if (!strcmp(name, "index-info"))
68 ctx.cfg.index_info = xstrdup(value);
69 else if (!strcmp(name, "logo-link"))
70 ctx.cfg.logo_link = xstrdup(value);
71 else if (!strcmp(name, "module-link"))
72 ctx.cfg.module_link = xstrdup(value);
73 else if (!strcmp(name, "virtual-root")) {
74 ctx.cfg.virtual_root = trim_end(value, '/');
75 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
76 ctx.cfg.virtual_root = "";
77 } else if (!strcmp(name, "nocache"))
78 ctx.cfg.nocache = atoi(value);
Martin Szulecki2f56e392009-08-07 14:05:17 +020079 else if (!strcmp(name, "noplainemail"))
80 ctx.cfg.noplainemail = atoi(value);
Lars Hjemlief0c6aa2009-07-25 12:19:31 +020081 else if (!strcmp(name, "noheader"))
82 ctx.cfg.noheader = atoi(value);
Lars Hjemli163037e2008-03-24 17:26:08 +010083 else if (!strcmp(name, "snapshots"))
84 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
85 else if (!strcmp(name, "enable-index-links"))
86 ctx.cfg.enable_index_links = atoi(value);
87 else if (!strcmp(name, "enable-log-filecount"))
88 ctx.cfg.enable_log_filecount = atoi(value);
89 else if (!strcmp(name, "enable-log-linecount"))
90 ctx.cfg.enable_log_linecount = atoi(value);
Lars Hjemlifb2f3f62008-12-07 13:17:21 +010091 else if (!strcmp(name, "max-stats"))
92 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
Lars Hjemli939d32f2008-04-28 11:32:42 +020093 else if (!strcmp(name, "cache-size"))
94 ctx.cfg.cache_size = atoi(value);
Lars Hjemli163037e2008-03-24 17:26:08 +010095 else if (!strcmp(name, "cache-root"))
96 ctx.cfg.cache_root = xstrdup(value);
97 else if (!strcmp(name, "cache-root-ttl"))
98 ctx.cfg.cache_root_ttl = atoi(value);
99 else if (!strcmp(name, "cache-repo-ttl"))
100 ctx.cfg.cache_repo_ttl = atoi(value);
Lars Hjemlid7468272009-08-20 17:41:54 +0200101 else if (!strcmp(name, "cache-scanrc-ttl"))
102 ctx.cfg.cache_scanrc_ttl = atoi(value);
Lars Hjemli163037e2008-03-24 17:26:08 +0100103 else if (!strcmp(name, "cache-static-ttl"))
104 ctx.cfg.cache_static_ttl = atoi(value);
105 else if (!strcmp(name, "cache-dynamic-ttl"))
106 ctx.cfg.cache_dynamic_ttl = atoi(value);
Lars Hjemli537c05f2009-08-09 13:27:21 +0200107 else if (!strcmp(name, "about-filter"))
108 ctx.cfg.about_filter = new_filter(value, 0);
Lars Hjemlif35db1c2009-07-31 17:42:57 +0200109 else if (!strcmp(name, "commit-filter"))
110 ctx.cfg.commit_filter = new_filter(value, 0);
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100111 else if (!strcmp(name, "embedded"))
112 ctx.cfg.embedded = atoi(value);
Lars Hjemli163037e2008-03-24 17:26:08 +0100113 else if (!strcmp(name, "max-message-length"))
114 ctx.cfg.max_msg_len = atoi(value);
115 else if (!strcmp(name, "max-repodesc-length"))
116 ctx.cfg.max_repodesc_len = atoi(value);
Lars Hjemlic6078b82008-05-03 10:54:39 +0200117 else if (!strcmp(name, "max-repo-count"))
118 ctx.cfg.max_repo_count = atoi(value);
Lars Hjemli163037e2008-03-24 17:26:08 +0100119 else if (!strcmp(name, "max-commit-count"))
120 ctx.cfg.max_commit_count = atoi(value);
Lars Hjemli46b7abe2009-07-31 16:55:27 +0200121 else if (!strcmp(name, "source-filter"))
122 ctx.cfg.source_filter = new_filter(value, 1);
Lars Hjemli163037e2008-03-24 17:26:08 +0100123 else if (!strcmp(name, "summary-log"))
124 ctx.cfg.summary_log = atoi(value);
125 else if (!strcmp(name, "summary-branches"))
126 ctx.cfg.summary_branches = atoi(value);
127 else if (!strcmp(name, "summary-tags"))
128 ctx.cfg.summary_tags = atoi(value);
129 else if (!strcmp(name, "agefile"))
130 ctx.cfg.agefile = xstrdup(value);
131 else if (!strcmp(name, "renamelimit"))
132 ctx.cfg.renamelimit = atoi(value);
133 else if (!strcmp(name, "robots"))
134 ctx.cfg.robots = xstrdup(value);
135 else if (!strcmp(name, "clone-prefix"))
136 ctx.cfg.clone_prefix = xstrdup(value);
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200137 else if (!strcmp(name, "local-time"))
138 ctx.cfg.local_time = atoi(value);
Lars Hjemlic4d46c72009-02-13 20:43:30 +0100139 else if (!prefixcmp(name, "mimetype."))
140 add_mimetype(name + 9, value);
Lars Hjemli163037e2008-03-24 17:26:08 +0100141 else if (!strcmp(name, "repo.group"))
142 ctx.cfg.repo_group = xstrdup(value);
Lars Hjemli6f3bf1a2009-08-11 10:09:06 +0200143 else if (!strcmp(name, "repo.scan"))
Lars Hjemlid7468272009-08-20 17:41:54 +0200144 if (!ctx.cfg.nocache && ctx.cfg.cache_size)
145 process_cached_repolist(value);
146 else
147 scan_tree(value);
Lars Hjemli163037e2008-03-24 17:26:08 +0100148 else if (!strcmp(name, "repo.url"))
149 ctx.repo = cgit_add_repo(value);
150 else if (!strcmp(name, "repo.name"))
151 ctx.repo->name = xstrdup(value);
152 else if (ctx.repo && !strcmp(name, "repo.path"))
153 ctx.repo->path = trim_end(value, '/');
154 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
155 ctx.repo->clone_url = xstrdup(value);
156 else if (ctx.repo && !strcmp(name, "repo.desc"))
157 ctx.repo->desc = xstrdup(value);
158 else if (ctx.repo && !strcmp(name, "repo.owner"))
159 ctx.repo->owner = xstrdup(value);
160 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
161 ctx.repo->defbranch = xstrdup(value);
162 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
163 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
164 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
165 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
166 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
167 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100168 else if (ctx.repo && !strcmp(name, "repo.max-stats"))
169 ctx.repo->max_stats = cgit_find_stats_period(value, NULL);
Lars Hjemli163037e2008-03-24 17:26:08 +0100170 else if (ctx.repo && !strcmp(name, "repo.module-link"))
171 ctx.repo->module_link= xstrdup(value);
Lars Hjemli537c05f2009-08-09 13:27:21 +0200172 else if (ctx.repo && !strcmp(name, "repo.about-filter"))
173 ctx.repo->about_filter = new_filter(value, 0);
Lars Hjemlie976df22009-08-09 13:22:00 +0200174 else if (ctx.repo && !strcmp(name, "repo.commit-filter"))
175 ctx.repo->commit_filter = new_filter(value, 0);
176 else if (ctx.repo && !strcmp(name, "repo.source-filter"))
177 ctx.repo->source_filter = new_filter(value, 1);
Lars Hjemli163037e2008-03-24 17:26:08 +0100178 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
179 if (*value == '/')
180 ctx.repo->readme = xstrdup(value);
181 else
182 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
183 } else if (!strcmp(name, "include"))
Lars Hjemli20a33542008-03-28 00:09:11 +0100184 parse_configfile(value, config_cb);
Lars Hjemli163037e2008-03-24 17:26:08 +0100185}
186
187static void querystring_cb(const char *name, const char *value)
188{
Lars Hjemli435a1da2009-08-17 09:05:13 +0200189 if (!value)
190 value = "";
191
Lars Hjemli163037e2008-03-24 17:26:08 +0100192 if (!strcmp(name,"r")) {
193 ctx.qry.repo = xstrdup(value);
194 ctx.repo = cgit_get_repoinfo(value);
195 } else if (!strcmp(name, "p")) {
196 ctx.qry.page = xstrdup(value);
197 } else if (!strcmp(name, "url")) {
Lars Hjemlie154edd2008-08-09 21:11:41 +0200198 ctx.qry.url = xstrdup(value);
Lars Hjemli163037e2008-03-24 17:26:08 +0100199 cgit_parse_url(value);
200 } else if (!strcmp(name, "qt")) {
201 ctx.qry.grep = xstrdup(value);
202 } else if (!strcmp(name, "q")) {
203 ctx.qry.search = xstrdup(value);
204 } else if (!strcmp(name, "h")) {
205 ctx.qry.head = xstrdup(value);
206 ctx.qry.has_symref = 1;
207 } else if (!strcmp(name, "id")) {
208 ctx.qry.sha1 = xstrdup(value);
209 ctx.qry.has_sha1 = 1;
210 } else if (!strcmp(name, "id2")) {
211 ctx.qry.sha2 = xstrdup(value);
212 ctx.qry.has_sha1 = 1;
213 } else if (!strcmp(name, "ofs")) {
214 ctx.qry.ofs = atoi(value);
215 } else if (!strcmp(name, "path")) {
216 ctx.qry.path = trim_end(value, '/');
217 } else if (!strcmp(name, "name")) {
218 ctx.qry.name = xstrdup(value);
Michael Krelin42effc92008-06-24 23:42:32 +0200219 } else if (!strcmp(name, "mimetype")) {
220 ctx.qry.mimetype = xstrdup(value);
Benjamin Closed71c0c72008-11-25 06:25:35 -0800221 } else if (!strcmp(name, "s")){
222 ctx.qry.sort = xstrdup(value);
Lars Hjemli0274b572008-11-29 18:39:41 +0100223 } else if (!strcmp(name, "showmsg")) {
224 ctx.qry.showmsg = atoi(value);
Lars Hjemlif86a23f2008-12-06 17:38:19 +0100225 } else if (!strcmp(name, "period")) {
226 ctx.qry.period = xstrdup(value);
Lars Hjemli163037e2008-03-24 17:26:08 +0100227 }
228}
229
Lars Hjemli60a26272009-08-10 08:21:09 +0200230char *xstrdupn(const char *str)
231{
232 return (str ? xstrdup(str) : NULL);
233}
234
Lars Hjemli163037e2008-03-24 17:26:08 +0100235static void prepare_context(struct cgit_context *ctx)
236{
237 memset(ctx, 0, sizeof(ctx));
238 ctx->cfg.agefile = "info/web/last-modified";
Lars Hjemli939d32f2008-04-28 11:32:42 +0200239 ctx->cfg.nocache = 0;
240 ctx->cfg.cache_size = 0;
Lars Hjemli163037e2008-03-24 17:26:08 +0100241 ctx->cfg.cache_dynamic_ttl = 5;
242 ctx->cfg.cache_max_create_time = 5;
243 ctx->cfg.cache_repo_ttl = 5;
244 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
245 ctx->cfg.cache_root_ttl = 5;
Lars Hjemlid7468272009-08-20 17:41:54 +0200246 ctx->cfg.cache_scanrc_ttl = 15;
Lars Hjemli163037e2008-03-24 17:26:08 +0100247 ctx->cfg.cache_static_ttl = -1;
248 ctx->cfg.css = "/cgit.css";
Lars Hjemli92f9b532009-08-10 18:14:12 +0200249 ctx->cfg.logo = "/cgit.png";
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200250 ctx->cfg.local_time = 0;
Lars Hjemlic6078b82008-05-03 10:54:39 +0200251 ctx->cfg.max_repo_count = 50;
Lars Hjemli163037e2008-03-24 17:26:08 +0100252 ctx->cfg.max_commit_count = 50;
253 ctx->cfg.max_lock_attempts = 5;
Lars Hjemli952f4ec2008-07-27 12:58:37 +0200254 ctx->cfg.max_msg_len = 80;
255 ctx->cfg.max_repodesc_len = 80;
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100256 ctx->cfg.max_stats = 0;
Lars Hjemli163037e2008-03-24 17:26:08 +0100257 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
258 ctx->cfg.renamelimit = -1;
259 ctx->cfg.robots = "index, nofollow";
260 ctx->cfg.root_title = "Git repository browser";
Lars Hjemli4c991602008-04-29 00:55:34 +0200261 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
Lars Hjemli163037e2008-03-24 17:26:08 +0100262 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
Lars Hjemli952f4ec2008-07-27 12:58:37 +0200263 ctx->cfg.summary_branches = 10;
264 ctx->cfg.summary_log = 10;
265 ctx->cfg.summary_tags = 10;
Lars Hjemli60a26272009-08-10 08:21:09 +0200266 ctx->env.cgit_config = xstrdupn(getenv("CGIT_CONFIG"));
267 ctx->env.http_host = xstrdupn(getenv("HTTP_HOST"));
268 ctx->env.https = xstrdupn(getenv("HTTPS"));
Lars Hjemli8b2252b2009-08-10 09:20:17 +0200269 ctx->env.no_http = xstrdupn(getenv("NO_HTTP"));
Lars Hjemli60a26272009-08-10 08:21:09 +0200270 ctx->env.path_info = xstrdupn(getenv("PATH_INFO"));
271 ctx->env.query_string = xstrdupn(getenv("QUERY_STRING"));
272 ctx->env.request_method = xstrdupn(getenv("REQUEST_METHOD"));
273 ctx->env.script_name = xstrdupn(getenv("SCRIPT_NAME"));
274 ctx->env.server_name = xstrdupn(getenv("SERVER_NAME"));
275 ctx->env.server_port = xstrdupn(getenv("SERVER_PORT"));
Lars Hjemli163037e2008-03-24 17:26:08 +0100276 ctx->page.mimetype = "text/html";
277 ctx->page.charset = PAGE_ENCODING;
278 ctx->page.filename = NULL;
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200279 ctx->page.size = 0;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200280 ctx->page.modified = time(NULL);
281 ctx->page.expires = ctx->page.modified;
Lars Hjemli488a2142009-02-19 22:38:36 +0100282 ctx->page.etag = NULL;
Lars Hjemlic4d46c72009-02-13 20:43:30 +0100283 memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list));
Lars Hjemli60a26272009-08-10 08:21:09 +0200284 if (ctx->env.script_name)
285 ctx->cfg.script_name = ctx->env.script_name;
286 if (ctx->env.query_string)
287 ctx->qry.raw = ctx->env.query_string;
288 if (!ctx->env.cgit_config)
289 ctx->env.cgit_config = CGIT_CONFIG;
Lars Hjemli83a5f352007-01-12 00:00:15 +0100290}
291
Lars Hjemlif80ff372008-01-04 13:43:40 +0100292struct refmatch {
293 char *req_ref;
294 char *first_ref;
295 int match;
296};
297
298int find_current_ref(const char *refname, const unsigned char *sha1,
299 int flags, void *cb_data)
300{
301 struct refmatch *info;
302
303 info = (struct refmatch *)cb_data;
304 if (!strcmp(refname, info->req_ref))
305 info->match = 1;
306 if (!info->first_ref)
307 info->first_ref = xstrdup(refname);
308 return info->match;
309}
310
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100311char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +0100312{
313 struct refmatch info;
Lars Hjemli25c84322008-07-27 12:32:08 +0200314 char *ref;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100315
316 info.req_ref = repo->defbranch;
317 info.first_ref = NULL;
318 info.match = 0;
319 for_each_branch_ref(find_current_ref, &info);
320 if (info.match)
Lars Hjemli25c84322008-07-27 12:32:08 +0200321 ref = info.req_ref;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100322 else
Lars Hjemli25c84322008-07-27 12:32:08 +0200323 ref = info.first_ref;
324 if (ref)
325 ref = xstrdup(ref);
326 return ref;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100327}
328
Lars Hjemlie0e44782008-03-24 01:09:39 +0100329static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100330{
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100331 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100332 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +0100333 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +0200334
Lars Hjemlie0e44782008-03-24 01:09:39 +0100335 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100336 setup_git_directory_gently(&nongit);
337 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +0100338 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
339 "config error");
340 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
341 ctx->repo = NULL;
342 cgit_print_http_headers(ctx);
343 cgit_print_docstart(ctx);
344 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100345 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100346 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100347 return 1;
348 }
349 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
350
351 if (!ctx->qry.head) {
Lars Hjemli5632ba32008-10-11 19:05:50 +0200352 ctx->qry.nohead = 1;
Lars Hjemli25c84322008-07-27 12:32:08 +0200353 ctx->qry.head = find_default_branch(ctx->repo);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100354 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100355 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100356
Lars Hjemlie0e44782008-03-24 01:09:39 +0100357 if (!ctx->qry.head) {
358 cgit_print_http_headers(ctx);
359 cgit_print_docstart(ctx);
360 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100361 cgit_print_error("Repository seems to be empty");
362 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100363 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100364 }
365
Lars Hjemlie0e44782008-03-24 01:09:39 +0100366 if (get_sha1(ctx->qry.head, sha1)) {
367 tmp = xstrdup(ctx->qry.head);
368 ctx->qry.head = ctx->repo->defbranch;
Lars Hjemlie429fb02009-06-07 20:43:08 +0200369 ctx->page.status = 404;
370 ctx->page.statusmsg = "not found";
Lars Hjemlie0e44782008-03-24 01:09:39 +0100371 cgit_print_http_headers(ctx);
372 cgit_print_docstart(ctx);
373 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100374 cgit_print_error(fmt("Invalid branch: %s", tmp));
375 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100376 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100377 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100378 return 0;
379}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100380
Lars Hjemli939d32f2008-04-28 11:32:42 +0200381static void process_request(void *cbdata)
Lars Hjemlie0e44782008-03-24 01:09:39 +0100382{
Lars Hjemli939d32f2008-04-28 11:32:42 +0200383 struct cgit_context *ctx = cbdata;
Lars Hjemlie0e44782008-03-24 01:09:39 +0100384 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100385
Lars Hjemlie0e44782008-03-24 01:09:39 +0100386 cmd = cgit_get_cmd(ctx);
387 if (!cmd) {
388 ctx->page.title = "cgit error";
Lars Hjemlie0e44782008-03-24 01:09:39 +0100389 cgit_print_http_headers(ctx);
390 cgit_print_docstart(ctx);
391 cgit_print_pageheader(ctx);
392 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100393 cgit_print_docend();
394 return;
395 }
396
Lars Hjemli651ef792008-04-29 01:01:30 +0200397 if (cmd->want_repo && !ctx->repo) {
398 cgit_print_http_headers(ctx);
399 cgit_print_docstart(ctx);
400 cgit_print_pageheader(ctx);
401 cgit_print_error(fmt("No repository selected"));
402 cgit_print_docend();
403 return;
404 }
405
406 if (ctx->repo && prepare_repo_cmd(ctx))
Lars Hjemlie0e44782008-03-24 01:09:39 +0100407 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100408
Lars Hjemlie0e44782008-03-24 01:09:39 +0100409 if (cmd->want_layout) {
410 cgit_print_http_headers(ctx);
411 cgit_print_docstart(ctx);
412 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100413 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100414
415 cmd->fn(ctx);
416
417 if (cmd->want_layout)
418 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100419}
420
Lars Hjemli93397a72008-09-15 00:07:12 +0200421int cmp_repos(const void *a, const void *b)
422{
423 const struct cgit_repo *ra = a, *rb = b;
424 return strcmp(ra->url, rb->url);
425}
426
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200427void print_repo(FILE *f, struct cgit_repo *repo)
Lars Hjemli93397a72008-09-15 00:07:12 +0200428{
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200429 fprintf(f, "repo.url=%s\n", repo->url);
430 fprintf(f, "repo.name=%s\n", repo->name);
431 fprintf(f, "repo.path=%s\n", repo->path);
Lars Hjemli93397a72008-09-15 00:07:12 +0200432 if (repo->owner)
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200433 fprintf(f, "repo.owner=%s\n", repo->owner);
Lars Hjemli93397a72008-09-15 00:07:12 +0200434 if (repo->desc)
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200435 fprintf(f, "repo.desc=%s\n", repo->desc);
Lars Hjemli93397a72008-09-15 00:07:12 +0200436 if (repo->readme)
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200437 fprintf(f, "repo.readme=%s\n", repo->readme);
438 fprintf(f, "\n");
Lars Hjemli93397a72008-09-15 00:07:12 +0200439}
440
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200441void print_repolist(FILE *f, struct cgit_repolist *list, int start)
Lars Hjemli93397a72008-09-15 00:07:12 +0200442{
443 int i;
444
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200445 for(i = start; i < list->count; i++)
446 print_repo(f, &list->repos[i]);
Lars Hjemli93397a72008-09-15 00:07:12 +0200447}
448
Lars Hjemlid7468272009-08-20 17:41:54 +0200449/* Scan 'path' for git repositories, save the resulting repolist in 'cached_rc'
450 * and return 0 on success.
451 */
452static int generate_cached_repolist(const char *path, const char *cached_rc)
453{
454 char *locked_rc;
455 int idx;
456 FILE *f;
457
458 locked_rc = xstrdup(fmt("%s.lock", cached_rc));
459 f = fopen(locked_rc, "wx");
460 if (!f) {
461 /* Inform about the error unless the lockfile already existed,
462 * since that only means we've got concurrent requests.
463 */
464 if (errno != EEXIST)
465 fprintf(stderr, "[cgit] Error opening %s: %s (%d)\n",
466 locked_rc, strerror(errno), errno);
467 return errno;
468 }
469 idx = cgit_repolist.count;
470 scan_tree(path);
471 print_repolist(f, &cgit_repolist, idx);
472 if (rename(locked_rc, cached_rc))
473 fprintf(stderr, "[cgit] Error renaming %s to %s: %s (%d)\n",
474 locked_rc, cached_rc, strerror(errno), errno);
475 fclose(f);
476 return 0;
477}
478
479static void process_cached_repolist(const char *path)
480{
481 struct stat st;
482 char *cached_rc;
483 time_t age;
484
485 cached_rc = xstrdup(fmt("%s/rc-%8x", ctx.cfg.cache_root,
486 hash_str(path)));
487
488 if (stat(cached_rc, &st)) {
489 /* Nothing is cached, we need to scan without forking. And
490 * if we fail to generate a cached repolist, we need to
491 * invoke scan_tree manually.
492 */
493 if (generate_cached_repolist(path, cached_rc))
494 scan_tree(path);
495 return;
496 }
497
498 parse_configfile(cached_rc, config_cb);
499
500 /* If the cached configfile hasn't expired, lets exit now */
501 age = time(NULL) - st.st_mtime;
502 if (age <= (ctx.cfg.cache_scanrc_ttl * 60))
503 return;
504
505 /* The cached repolist has been parsed, but it was old. So lets
506 * rescan the specified path and generate a new cached repolist
507 * in a child-process to avoid latency for the current request.
508 */
509 if (fork())
510 return;
511
512 exit(generate_cached_repolist(path, cached_rc));
513}
Lars Hjemli93397a72008-09-15 00:07:12 +0200514
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100515static void cgit_parse_args(int argc, const char **argv)
516{
517 int i;
Lars Hjemli93397a72008-09-15 00:07:12 +0200518 int scan = 0;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100519
520 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100521 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100522 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100523 }
524 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100525 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100526 }
Lars Hjemli8b2252b2009-08-10 09:20:17 +0200527 if (!strcmp(argv[i], "--nohttp")) {
528 ctx.env.no_http = "1";
529 }
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100530 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100531 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100532 }
533 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100534 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100535 }
536 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100537 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100538 }
539 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100540 ctx.qry.head = xstrdup(argv[i]+7);
541 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100542 }
543 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100544 ctx.qry.sha1 = xstrdup(argv[i]+7);
545 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100546 }
547 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100548 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100549 }
Lars Hjemli93397a72008-09-15 00:07:12 +0200550 if (!strncmp(argv[i], "--scan-tree=", 12)) {
551 scan++;
552 scan_tree(argv[i] + 12);
553 }
554 }
555 if (scan) {
556 qsort(cgit_repolist.repos, cgit_repolist.count,
557 sizeof(struct cgit_repo), cmp_repos);
Lars Hjemli302a3ef2009-08-19 17:47:24 +0200558 print_repolist(stdout, &cgit_repolist, 0);
Lars Hjemli93397a72008-09-15 00:07:12 +0200559 exit(0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100560 }
561}
562
Lars Hjemli939d32f2008-04-28 11:32:42 +0200563static int calc_ttl()
564{
565 if (!ctx.repo)
566 return ctx.cfg.cache_root_ttl;
567
568 if (!ctx.qry.page)
569 return ctx.cfg.cache_repo_ttl;
570
571 if (ctx.qry.has_symref)
572 return ctx.cfg.cache_dynamic_ttl;
573
574 if (ctx.qry.has_sha1)
575 return ctx.cfg.cache_static_ttl;
576
577 return ctx.cfg.cache_repo_ttl;
578}
579
Lars Hjemli25105d72006-12-10 22:31:36 +0100580int main(int argc, const char **argv)
581{
Lars Hjemlie154edd2008-08-09 21:11:41 +0200582 const char *path;
583 char *qry;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200584 int err, ttl;
Lars Hjemli44923f82006-12-11 17:25:41 +0100585
Lars Hjemli163037e2008-03-24 17:26:08 +0100586 prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100587 cgit_repolist.length = 0;
588 cgit_repolist.count = 0;
589 cgit_repolist.repos = NULL;
590
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100591 cgit_parse_args(argc, argv);
Lars Hjemli60a26272009-08-10 08:21:09 +0200592 parse_configfile(ctx.env.cgit_config, config_cb);
Lars Hjemli93397a72008-09-15 00:07:12 +0200593 ctx.repo = NULL;
Lars Hjemlie87e8962008-04-08 21:11:36 +0200594 http_parse_querystring(ctx.qry.raw, querystring_cb);
Lars Hjemli939d32f2008-04-28 11:32:42 +0200595
Lars Hjemli377a1c82008-12-26 10:33:01 +0100596 /* If virtual-root isn't specified in cgitrc, lets pretend
597 * that virtual-root equals SCRIPT_NAME.
Lars Hjemlie154edd2008-08-09 21:11:41 +0200598 */
Lars Hjemli377a1c82008-12-26 10:33:01 +0100599 if (!ctx.cfg.virtual_root)
Lars Hjemlie154edd2008-08-09 21:11:41 +0200600 ctx.cfg.virtual_root = ctx.cfg.script_name;
Lars Hjemli377a1c82008-12-26 10:33:01 +0100601
602 /* If no url parameter is specified on the querystring, lets
603 * use PATH_INFO as url. This allows cgit to work with virtual
604 * urls without the need for rewriterules in the webserver (as
605 * long as PATH_INFO is included in the cache lookup key).
606 */
Lars Hjemli60a26272009-08-10 08:21:09 +0200607 path = ctx.env.path_info;
Lars Hjemli377a1c82008-12-26 10:33:01 +0100608 if (!ctx.qry.url && path) {
609 if (path[0] == '/')
610 path++;
611 ctx.qry.url = xstrdup(path);
612 if (ctx.qry.raw) {
613 qry = ctx.qry.raw;
614 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
615 free(qry);
616 } else
Lars Hjemli0374a762009-08-20 10:07:51 +0200617 ctx.qry.raw = xstrdup(ctx.qry.url);
Lars Hjemli377a1c82008-12-26 10:33:01 +0100618 cgit_parse_url(ctx.qry.url);
Lars Hjemlie154edd2008-08-09 21:11:41 +0200619 }
620
Lars Hjemli939d32f2008-04-28 11:32:42 +0200621 ttl = calc_ttl();
622 ctx.page.expires += ttl*60;
Lars Hjemli60a26272009-08-10 08:21:09 +0200623 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
Lars Hjemli3ff58dd2009-02-19 23:24:15 +0100624 ctx.cfg.nocache = 1;
Lars Hjemli939d32f2008-04-28 11:32:42 +0200625 if (ctx.cfg.nocache)
626 ctx.cfg.cache_size = 0;
627 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
628 ctx.qry.raw, ttl, process_request, &ctx);
629 if (err)
Lars Hjemliaf2e7562008-05-18 23:59:11 +0200630 cgit_print_error(fmt("Error processing page: %s (%d)",
631 strerror(err), err));
Lars Hjemli939d32f2008-04-28 11:32:42 +0200632 return err;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100633}