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