cgit.c (view raw)
1/* 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
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 "scan-tree.h"
17
18const char *cgit_version = CGIT_VERSION;
19
20void 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
28void config_cb(const char *name, const char *value)
29{
30 if (!strcmp(name, "root-title"))
31 ctx.cfg.root_title = xstrdup(value);
32 else if (!strcmp(name, "root-desc"))
33 ctx.cfg.root_desc = xstrdup(value);
34 else if (!strcmp(name, "root-readme"))
35 ctx.cfg.root_readme = xstrdup(value);
36 else if (!strcmp(name, "css"))
37 ctx.cfg.css = xstrdup(value);
38 else if (!strcmp(name, "favicon"))
39 ctx.cfg.favicon = xstrdup(value);
40 else if (!strcmp(name, "footer"))
41 ctx.cfg.footer = xstrdup(value);
42 else if (!strcmp(name, "head-include"))
43 ctx.cfg.head_include = xstrdup(value);
44 else if (!strcmp(name, "header"))
45 ctx.cfg.header = xstrdup(value);
46 else if (!strcmp(name, "logo"))
47 ctx.cfg.logo = xstrdup(value);
48 else if (!strcmp(name, "index-header"))
49 ctx.cfg.index_header = xstrdup(value);
50 else if (!strcmp(name, "index-info"))
51 ctx.cfg.index_info = xstrdup(value);
52 else if (!strcmp(name, "logo-link"))
53 ctx.cfg.logo_link = xstrdup(value);
54 else if (!strcmp(name, "module-link"))
55 ctx.cfg.module_link = xstrdup(value);
56 else if (!strcmp(name, "virtual-root")) {
57 ctx.cfg.virtual_root = trim_end(value, '/');
58 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
59 ctx.cfg.virtual_root = "";
60 } else if (!strcmp(name, "nocache"))
61 ctx.cfg.nocache = atoi(value);
62 else if (!strcmp(name, "noheader"))
63 ctx.cfg.noheader = atoi(value);
64 else if (!strcmp(name, "snapshots"))
65 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
66 else if (!strcmp(name, "enable-index-links"))
67 ctx.cfg.enable_index_links = atoi(value);
68 else if (!strcmp(name, "enable-log-filecount"))
69 ctx.cfg.enable_log_filecount = atoi(value);
70 else if (!strcmp(name, "enable-log-linecount"))
71 ctx.cfg.enable_log_linecount = atoi(value);
72 else if (!strcmp(name, "max-stats"))
73 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
74 else if (!strcmp(name, "cache-size"))
75 ctx.cfg.cache_size = atoi(value);
76 else if (!strcmp(name, "cache-root"))
77 ctx.cfg.cache_root = xstrdup(value);
78 else if (!strcmp(name, "cache-root-ttl"))
79 ctx.cfg.cache_root_ttl = atoi(value);
80 else if (!strcmp(name, "cache-repo-ttl"))
81 ctx.cfg.cache_repo_ttl = atoi(value);
82 else if (!strcmp(name, "cache-static-ttl"))
83 ctx.cfg.cache_static_ttl = atoi(value);
84 else if (!strcmp(name, "cache-dynamic-ttl"))
85 ctx.cfg.cache_dynamic_ttl = atoi(value);
86 else if (!strcmp(name, "embedded"))
87 ctx.cfg.embedded = atoi(value);
88 else if (!strcmp(name, "max-message-length"))
89 ctx.cfg.max_msg_len = atoi(value);
90 else if (!strcmp(name, "max-repodesc-length"))
91 ctx.cfg.max_repodesc_len = atoi(value);
92 else if (!strcmp(name, "max-repo-count"))
93 ctx.cfg.max_repo_count = atoi(value);
94 else if (!strcmp(name, "max-commit-count"))
95 ctx.cfg.max_commit_count = atoi(value);
96 else if (!strcmp(name, "summary-log"))
97 ctx.cfg.summary_log = atoi(value);
98 else if (!strcmp(name, "summary-branches"))
99 ctx.cfg.summary_branches = atoi(value);
100 else if (!strcmp(name, "summary-tags"))
101 ctx.cfg.summary_tags = atoi(value);
102 else if (!strcmp(name, "agefile"))
103 ctx.cfg.agefile = xstrdup(value);
104 else if (!strcmp(name, "renamelimit"))
105 ctx.cfg.renamelimit = atoi(value);
106 else if (!strcmp(name, "robots"))
107 ctx.cfg.robots = xstrdup(value);
108 else if (!strcmp(name, "clone-prefix"))
109 ctx.cfg.clone_prefix = xstrdup(value);
110 else if (!strcmp(name, "local-time"))
111 ctx.cfg.local_time = atoi(value);
112 else if (!prefixcmp(name, "mimetype."))
113 add_mimetype(name + 9, value);
114 else if (!strcmp(name, "repo.group"))
115 ctx.cfg.repo_group = xstrdup(value);
116 else if (!strcmp(name, "repo.url"))
117 ctx.repo = cgit_add_repo(value);
118 else if (!strcmp(name, "repo.name"))
119 ctx.repo->name = xstrdup(value);
120 else if (ctx.repo && !strcmp(name, "repo.path"))
121 ctx.repo->path = trim_end(value, '/');
122 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
123 ctx.repo->clone_url = xstrdup(value);
124 else if (ctx.repo && !strcmp(name, "repo.desc"))
125 ctx.repo->desc = xstrdup(value);
126 else if (ctx.repo && !strcmp(name, "repo.owner"))
127 ctx.repo->owner = xstrdup(value);
128 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
129 ctx.repo->defbranch = xstrdup(value);
130 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
131 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
132 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
133 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
134 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
135 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
136 else if (ctx.repo && !strcmp(name, "repo.max-stats"))
137 ctx.repo->max_stats = cgit_find_stats_period(value, NULL);
138 else if (ctx.repo && !strcmp(name, "repo.module-link"))
139 ctx.repo->module_link= xstrdup(value);
140 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
141 if (*value == '/')
142 ctx.repo->readme = xstrdup(value);
143 else
144 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
145 } else if (!strcmp(name, "include"))
146 parse_configfile(value, config_cb);
147}
148
149static void querystring_cb(const char *name, const char *value)
150{
151 if (!strcmp(name,"r")) {
152 ctx.qry.repo = xstrdup(value);
153 ctx.repo = cgit_get_repoinfo(value);
154 } else if (!strcmp(name, "p")) {
155 ctx.qry.page = xstrdup(value);
156 } else if (!strcmp(name, "url")) {
157 ctx.qry.url = xstrdup(value);
158 cgit_parse_url(value);
159 } else if (!strcmp(name, "qt")) {
160 ctx.qry.grep = xstrdup(value);
161 } else if (!strcmp(name, "q")) {
162 ctx.qry.search = xstrdup(value);
163 } else if (!strcmp(name, "h")) {
164 ctx.qry.head = xstrdup(value);
165 ctx.qry.has_symref = 1;
166 } else if (!strcmp(name, "id")) {
167 ctx.qry.sha1 = xstrdup(value);
168 ctx.qry.has_sha1 = 1;
169 } else if (!strcmp(name, "id2")) {
170 ctx.qry.sha2 = xstrdup(value);
171 ctx.qry.has_sha1 = 1;
172 } else if (!strcmp(name, "ofs")) {
173 ctx.qry.ofs = atoi(value);
174 } else if (!strcmp(name, "path")) {
175 ctx.qry.path = trim_end(value, '/');
176 } else if (!strcmp(name, "name")) {
177 ctx.qry.name = xstrdup(value);
178 } else if (!strcmp(name, "mimetype")) {
179 ctx.qry.mimetype = xstrdup(value);
180 } else if (!strcmp(name, "s")){
181 ctx.qry.sort = xstrdup(value);
182 } else if (!strcmp(name, "showmsg")) {
183 ctx.qry.showmsg = atoi(value);
184 } else if (!strcmp(name, "period")) {
185 ctx.qry.period = xstrdup(value);
186 }
187}
188
189static void prepare_context(struct cgit_context *ctx)
190{
191 memset(ctx, 0, sizeof(ctx));
192 ctx->cfg.agefile = "info/web/last-modified";
193 ctx->cfg.nocache = 0;
194 ctx->cfg.cache_size = 0;
195 ctx->cfg.cache_dynamic_ttl = 5;
196 ctx->cfg.cache_max_create_time = 5;
197 ctx->cfg.cache_repo_ttl = 5;
198 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
199 ctx->cfg.cache_root_ttl = 5;
200 ctx->cfg.cache_static_ttl = -1;
201 ctx->cfg.css = "/cgit.css";
202 ctx->cfg.logo = "/git-logo.png";
203 ctx->cfg.local_time = 0;
204 ctx->cfg.max_repo_count = 50;
205 ctx->cfg.max_commit_count = 50;
206 ctx->cfg.max_lock_attempts = 5;
207 ctx->cfg.max_msg_len = 80;
208 ctx->cfg.max_repodesc_len = 80;
209 ctx->cfg.max_stats = 0;
210 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
211 ctx->cfg.renamelimit = -1;
212 ctx->cfg.robots = "index, nofollow";
213 ctx->cfg.root_title = "Git repository browser";
214 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
215 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
216 ctx->cfg.summary_branches = 10;
217 ctx->cfg.summary_log = 10;
218 ctx->cfg.summary_tags = 10;
219 ctx->page.mimetype = "text/html";
220 ctx->page.charset = PAGE_ENCODING;
221 ctx->page.filename = NULL;
222 ctx->page.size = 0;
223 ctx->page.modified = time(NULL);
224 ctx->page.expires = ctx->page.modified;
225 ctx->page.etag = NULL;
226 memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list));
227}
228
229struct refmatch {
230 char *req_ref;
231 char *first_ref;
232 int match;
233};
234
235int find_current_ref(const char *refname, const unsigned char *sha1,
236 int flags, void *cb_data)
237{
238 struct refmatch *info;
239
240 info = (struct refmatch *)cb_data;
241 if (!strcmp(refname, info->req_ref))
242 info->match = 1;
243 if (!info->first_ref)
244 info->first_ref = xstrdup(refname);
245 return info->match;
246}
247
248char *find_default_branch(struct cgit_repo *repo)
249{
250 struct refmatch info;
251 char *ref;
252
253 info.req_ref = repo->defbranch;
254 info.first_ref = NULL;
255 info.match = 0;
256 for_each_branch_ref(find_current_ref, &info);
257 if (info.match)
258 ref = info.req_ref;
259 else
260 ref = info.first_ref;
261 if (ref)
262 ref = xstrdup(ref);
263 return ref;
264}
265
266static int prepare_repo_cmd(struct cgit_context *ctx)
267{
268 char *tmp;
269 unsigned char sha1[20];
270 int nongit = 0;
271
272 setenv("GIT_DIR", ctx->repo->path, 1);
273 setup_git_directory_gently(&nongit);
274 if (nongit) {
275 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
276 "config error");
277 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
278 ctx->repo = NULL;
279 cgit_print_http_headers(ctx);
280 cgit_print_docstart(ctx);
281 cgit_print_pageheader(ctx);
282 cgit_print_error(tmp);
283 cgit_print_docend();
284 return 1;
285 }
286 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
287
288 if (!ctx->qry.head) {
289 ctx->qry.nohead = 1;
290 ctx->qry.head = find_default_branch(ctx->repo);
291 ctx->repo->defbranch = ctx->qry.head;
292 }
293
294 if (!ctx->qry.head) {
295 cgit_print_http_headers(ctx);
296 cgit_print_docstart(ctx);
297 cgit_print_pageheader(ctx);
298 cgit_print_error("Repository seems to be empty");
299 cgit_print_docend();
300 return 1;
301 }
302
303 if (get_sha1(ctx->qry.head, sha1)) {
304 tmp = xstrdup(ctx->qry.head);
305 ctx->qry.head = ctx->repo->defbranch;
306 ctx->page.status = 404;
307 ctx->page.statusmsg = "not found";
308 cgit_print_http_headers(ctx);
309 cgit_print_docstart(ctx);
310 cgit_print_pageheader(ctx);
311 cgit_print_error(fmt("Invalid branch: %s", tmp));
312 cgit_print_docend();
313 return 1;
314 }
315 return 0;
316}
317
318static void process_request(void *cbdata)
319{
320 struct cgit_context *ctx = cbdata;
321 struct cgit_cmd *cmd;
322
323 cmd = cgit_get_cmd(ctx);
324 if (!cmd) {
325 ctx->page.title = "cgit error";
326 cgit_print_http_headers(ctx);
327 cgit_print_docstart(ctx);
328 cgit_print_pageheader(ctx);
329 cgit_print_error("Invalid request");
330 cgit_print_docend();
331 return;
332 }
333
334 if (cmd->want_repo && !ctx->repo) {
335 cgit_print_http_headers(ctx);
336 cgit_print_docstart(ctx);
337 cgit_print_pageheader(ctx);
338 cgit_print_error(fmt("No repository selected"));
339 cgit_print_docend();
340 return;
341 }
342
343 if (ctx->repo && prepare_repo_cmd(ctx))
344 return;
345
346 if (cmd->want_layout) {
347 cgit_print_http_headers(ctx);
348 cgit_print_docstart(ctx);
349 cgit_print_pageheader(ctx);
350 }
351
352 cmd->fn(ctx);
353
354 if (cmd->want_layout)
355 cgit_print_docend();
356}
357
358int cmp_repos(const void *a, const void *b)
359{
360 const struct cgit_repo *ra = a, *rb = b;
361 return strcmp(ra->url, rb->url);
362}
363
364void print_repo(struct cgit_repo *repo)
365{
366 printf("repo.url=%s\n", repo->url);
367 printf("repo.name=%s\n", repo->name);
368 printf("repo.path=%s\n", repo->path);
369 if (repo->owner)
370 printf("repo.owner=%s\n", repo->owner);
371 if (repo->desc)
372 printf("repo.desc=%s\n", repo->desc);
373 if (repo->readme)
374 printf("repo.readme=%s\n", repo->readme);
375 printf("\n");
376}
377
378void print_repolist(struct cgit_repolist *list)
379{
380 int i;
381
382 for(i = 0; i < list->count; i++)
383 print_repo(&list->repos[i]);
384}
385
386
387static void cgit_parse_args(int argc, const char **argv)
388{
389 int i;
390 int scan = 0;
391
392 for (i = 1; i < argc; i++) {
393 if (!strncmp(argv[i], "--cache=", 8)) {
394 ctx.cfg.cache_root = xstrdup(argv[i]+8);
395 }
396 if (!strcmp(argv[i], "--nocache")) {
397 ctx.cfg.nocache = 1;
398 }
399 if (!strncmp(argv[i], "--query=", 8)) {
400 ctx.qry.raw = xstrdup(argv[i]+8);
401 }
402 if (!strncmp(argv[i], "--repo=", 7)) {
403 ctx.qry.repo = xstrdup(argv[i]+7);
404 }
405 if (!strncmp(argv[i], "--page=", 7)) {
406 ctx.qry.page = xstrdup(argv[i]+7);
407 }
408 if (!strncmp(argv[i], "--head=", 7)) {
409 ctx.qry.head = xstrdup(argv[i]+7);
410 ctx.qry.has_symref = 1;
411 }
412 if (!strncmp(argv[i], "--sha1=", 7)) {
413 ctx.qry.sha1 = xstrdup(argv[i]+7);
414 ctx.qry.has_sha1 = 1;
415 }
416 if (!strncmp(argv[i], "--ofs=", 6)) {
417 ctx.qry.ofs = atoi(argv[i]+6);
418 }
419 if (!strncmp(argv[i], "--scan-tree=", 12)) {
420 scan++;
421 scan_tree(argv[i] + 12);
422 }
423 }
424 if (scan) {
425 qsort(cgit_repolist.repos, cgit_repolist.count,
426 sizeof(struct cgit_repo), cmp_repos);
427 print_repolist(&cgit_repolist);
428 exit(0);
429 }
430}
431
432static int calc_ttl()
433{
434 if (!ctx.repo)
435 return ctx.cfg.cache_root_ttl;
436
437 if (!ctx.qry.page)
438 return ctx.cfg.cache_repo_ttl;
439
440 if (ctx.qry.has_symref)
441 return ctx.cfg.cache_dynamic_ttl;
442
443 if (ctx.qry.has_sha1)
444 return ctx.cfg.cache_static_ttl;
445
446 return ctx.cfg.cache_repo_ttl;
447}
448
449int main(int argc, const char **argv)
450{
451 const char *cgit_config_env = getenv("CGIT_CONFIG");
452 const char *method = getenv("REQUEST_METHOD");
453 const char *path;
454 char *qry;
455 int err, ttl;
456
457 prepare_context(&ctx);
458 cgit_repolist.length = 0;
459 cgit_repolist.count = 0;
460 cgit_repolist.repos = NULL;
461
462 if (getenv("SCRIPT_NAME"))
463 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
464 if (getenv("QUERY_STRING"))
465 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
466 cgit_parse_args(argc, argv);
467 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
468 config_cb);
469 ctx.repo = NULL;
470 http_parse_querystring(ctx.qry.raw, querystring_cb);
471
472 /* If virtual-root isn't specified in cgitrc, lets pretend
473 * that virtual-root equals SCRIPT_NAME.
474 */
475 if (!ctx.cfg.virtual_root)
476 ctx.cfg.virtual_root = ctx.cfg.script_name;
477
478 /* If no url parameter is specified on the querystring, lets
479 * use PATH_INFO as url. This allows cgit to work with virtual
480 * urls without the need for rewriterules in the webserver (as
481 * long as PATH_INFO is included in the cache lookup key).
482 */
483 path = getenv("PATH_INFO");
484 if (!ctx.qry.url && path) {
485 if (path[0] == '/')
486 path++;
487 ctx.qry.url = xstrdup(path);
488 if (ctx.qry.raw) {
489 qry = ctx.qry.raw;
490 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
491 free(qry);
492 } else
493 ctx.qry.raw = ctx.qry.url;
494 cgit_parse_url(ctx.qry.url);
495 }
496
497 ttl = calc_ttl();
498 ctx.page.expires += ttl*60;
499 if (method && !strcmp(method, "HEAD"))
500 ctx.cfg.nocache = 1;
501 if (ctx.cfg.nocache)
502 ctx.cfg.cache_size = 0;
503 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
504 ctx.qry.raw, ttl, process_request, &ctx);
505 if (err)
506 cgit_print_error(fmt("Error processing page: %s (%d)",
507 strerror(err), err));
508 return err;
509}