]> gitweb.ps.run Git - ps-cgit/blob - cgit.c
Prepare for 'about site' page / add 'root-readme' option to cgitrc
[ps-cgit] / cgit.c
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
16 const char *cgit_version = CGIT_VERSION;
17
18 void config_cb(const char *name, const char *value)
19 {
20         if (!strcmp(name, "root-title"))
21                 ctx.cfg.root_title = xstrdup(value);
22         else if (!strcmp(name, "root-desc"))
23                 ctx.cfg.root_desc = xstrdup(value);
24         else if (!strcmp(name, "root-readme"))
25                 ctx.cfg.root_readme = xstrdup(value);
26         else if (!strcmp(name, "css"))
27                 ctx.cfg.css = xstrdup(value);
28         else if (!strcmp(name, "logo"))
29                 ctx.cfg.logo = xstrdup(value);
30         else if (!strcmp(name, "index-header"))
31                 ctx.cfg.index_header = xstrdup(value);
32         else if (!strcmp(name, "index-info"))
33                 ctx.cfg.index_info = xstrdup(value);
34         else if (!strcmp(name, "logo-link"))
35                 ctx.cfg.logo_link = xstrdup(value);
36         else if (!strcmp(name, "module-link"))
37                 ctx.cfg.module_link = xstrdup(value);
38         else if (!strcmp(name, "virtual-root")) {
39                 ctx.cfg.virtual_root = trim_end(value, '/');
40                 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
41                         ctx.cfg.virtual_root = "";
42         } else if (!strcmp(name, "nocache"))
43                 ctx.cfg.nocache = atoi(value);
44         else if (!strcmp(name, "snapshots"))
45                 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
46         else if (!strcmp(name, "enable-index-links"))
47                 ctx.cfg.enable_index_links = atoi(value);
48         else if (!strcmp(name, "enable-log-filecount"))
49                 ctx.cfg.enable_log_filecount = atoi(value);
50         else if (!strcmp(name, "enable-log-linecount"))
51                 ctx.cfg.enable_log_linecount = atoi(value);
52         else if (!strcmp(name, "cache-root"))
53                 ctx.cfg.cache_root = xstrdup(value);
54         else if (!strcmp(name, "cache-root-ttl"))
55                 ctx.cfg.cache_root_ttl = atoi(value);
56         else if (!strcmp(name, "cache-repo-ttl"))
57                 ctx.cfg.cache_repo_ttl = atoi(value);
58         else if (!strcmp(name, "cache-static-ttl"))
59                 ctx.cfg.cache_static_ttl = atoi(value);
60         else if (!strcmp(name, "cache-dynamic-ttl"))
61                 ctx.cfg.cache_dynamic_ttl = atoi(value);
62         else if (!strcmp(name, "max-message-length"))
63                 ctx.cfg.max_msg_len = atoi(value);
64         else if (!strcmp(name, "max-repodesc-length"))
65                 ctx.cfg.max_repodesc_len = atoi(value);
66         else if (!strcmp(name, "max-commit-count"))
67                 ctx.cfg.max_commit_count = atoi(value);
68         else if (!strcmp(name, "summary-log"))
69                 ctx.cfg.summary_log = atoi(value);
70         else if (!strcmp(name, "summary-branches"))
71                 ctx.cfg.summary_branches = atoi(value);
72         else if (!strcmp(name, "summary-tags"))
73                 ctx.cfg.summary_tags = atoi(value);
74         else if (!strcmp(name, "agefile"))
75                 ctx.cfg.agefile = xstrdup(value);
76         else if (!strcmp(name, "renamelimit"))
77                 ctx.cfg.renamelimit = atoi(value);
78         else if (!strcmp(name, "robots"))
79                 ctx.cfg.robots = xstrdup(value);
80         else if (!strcmp(name, "clone-prefix"))
81                 ctx.cfg.clone_prefix = xstrdup(value);
82         else if (!strcmp(name, "repo.group"))
83                 ctx.cfg.repo_group = xstrdup(value);
84         else if (!strcmp(name, "repo.url"))
85                 ctx.repo = cgit_add_repo(value);
86         else if (!strcmp(name, "repo.name"))
87                 ctx.repo->name = xstrdup(value);
88         else if (ctx.repo && !strcmp(name, "repo.path"))
89                 ctx.repo->path = trim_end(value, '/');
90         else if (ctx.repo && !strcmp(name, "repo.clone-url"))
91                 ctx.repo->clone_url = xstrdup(value);
92         else if (ctx.repo && !strcmp(name, "repo.desc"))
93                 ctx.repo->desc = xstrdup(value);
94         else if (ctx.repo && !strcmp(name, "repo.owner"))
95                 ctx.repo->owner = xstrdup(value);
96         else if (ctx.repo && !strcmp(name, "repo.defbranch"))
97                 ctx.repo->defbranch = xstrdup(value);
98         else if (ctx.repo && !strcmp(name, "repo.snapshots"))
99                 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
100         else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
101                 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
102         else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
103                 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
104         else if (ctx.repo && !strcmp(name, "repo.module-link"))
105                 ctx.repo->module_link= xstrdup(value);
106         else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
107                 if (*value == '/')
108                         ctx.repo->readme = xstrdup(value);
109                 else
110                         ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
111         } else if (!strcmp(name, "include"))
112                 parse_configfile(value, config_cb);
113 }
114
115 static void querystring_cb(const char *name, const char *value)
116 {
117         if (!strcmp(name,"r")) {
118                 ctx.qry.repo = xstrdup(value);
119                 ctx.repo = cgit_get_repoinfo(value);
120         } else if (!strcmp(name, "p")) {
121                 ctx.qry.page = xstrdup(value);
122         } else if (!strcmp(name, "url")) {
123                 cgit_parse_url(value);
124         } else if (!strcmp(name, "qt")) {
125                 ctx.qry.grep = xstrdup(value);
126         } else if (!strcmp(name, "q")) {
127                 ctx.qry.search = xstrdup(value);
128         } else if (!strcmp(name, "h")) {
129                 ctx.qry.head = xstrdup(value);
130                 ctx.qry.has_symref = 1;
131         } else if (!strcmp(name, "id")) {
132                 ctx.qry.sha1 = xstrdup(value);
133                 ctx.qry.has_sha1 = 1;
134         } else if (!strcmp(name, "id2")) {
135                 ctx.qry.sha2 = xstrdup(value);
136                 ctx.qry.has_sha1 = 1;
137         } else if (!strcmp(name, "ofs")) {
138                 ctx.qry.ofs = atoi(value);
139         } else if (!strcmp(name, "path")) {
140                 ctx.qry.path = trim_end(value, '/');
141         } else if (!strcmp(name, "name")) {
142                 ctx.qry.name = xstrdup(value);
143         }
144 }
145
146 static void prepare_context(struct cgit_context *ctx)
147 {
148         memset(ctx, 0, sizeof(ctx));
149         ctx->cfg.agefile = "info/web/last-modified";
150         ctx->cfg.cache_dynamic_ttl = 5;
151         ctx->cfg.cache_max_create_time = 5;
152         ctx->cfg.cache_repo_ttl = 5;
153         ctx->cfg.cache_root = CGIT_CACHE_ROOT;
154         ctx->cfg.cache_root_ttl = 5;
155         ctx->cfg.cache_static_ttl = -1;
156         ctx->cfg.css = "/cgit.css";
157         ctx->cfg.logo = "/git-logo.png";
158         ctx->cfg.max_commit_count = 50;
159         ctx->cfg.max_lock_attempts = 5;
160         ctx->cfg.max_msg_len = 60;
161         ctx->cfg.max_repodesc_len = 60;
162         ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
163         ctx->cfg.renamelimit = -1;
164         ctx->cfg.robots = "index, nofollow";
165         ctx->cfg.root_title = "Git repository browser";
166         ctx->cfg.root_desc = "a fast webinterface for the git dscm";
167         ctx->cfg.script_name = CGIT_SCRIPT_NAME;
168         ctx->page.mimetype = "text/html";
169         ctx->page.charset = PAGE_ENCODING;
170         ctx->page.filename = NULL;
171 }
172
173 static int cgit_prepare_cache(struct cacheitem *item)
174 {
175         if (!ctx.repo && ctx.qry.repo) {
176                 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
177                                       "Bad request");
178                 cgit_print_http_headers(&ctx);
179                 cgit_print_docstart(&ctx);
180                 cgit_print_pageheader(&ctx);
181                 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
182                 cgit_print_docend();
183                 return 0;
184         }
185
186         if (!ctx.repo) {
187                 item->name = xstrdup(fmt("%s/index.%s.html",
188                                          ctx.cfg.cache_root,
189                                          cache_safe_filename(ctx.qry.raw)));
190                 item->ttl = ctx.cfg.cache_root_ttl;
191                 return 1;
192         }
193
194         if (!ctx.qry.page) {
195                 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
196                                          cache_safe_filename(ctx.repo->url),
197                                          cache_safe_filename(ctx.qry.raw)));
198                 item->ttl = ctx.cfg.cache_repo_ttl;
199         } else {
200                 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
201                                          cache_safe_filename(ctx.repo->url),
202                                          ctx.qry.page,
203                                          cache_safe_filename(ctx.qry.raw)));
204                 if (ctx.qry.has_symref)
205                         item->ttl = ctx.cfg.cache_dynamic_ttl;
206                 else if (ctx.qry.has_sha1)
207                         item->ttl = ctx.cfg.cache_static_ttl;
208                 else
209                         item->ttl = ctx.cfg.cache_repo_ttl;
210         }
211         return 1;
212 }
213
214 struct refmatch {
215         char *req_ref;
216         char *first_ref;
217         int match;
218 };
219
220 int find_current_ref(const char *refname, const unsigned char *sha1,
221                      int flags, void *cb_data)
222 {
223         struct refmatch *info;
224
225         info = (struct refmatch *)cb_data;
226         if (!strcmp(refname, info->req_ref))
227                 info->match = 1;
228         if (!info->first_ref)
229                 info->first_ref = xstrdup(refname);
230         return info->match;
231 }
232
233 char *find_default_branch(struct cgit_repo *repo)
234 {
235         struct refmatch info;
236
237         info.req_ref = repo->defbranch;
238         info.first_ref = NULL;
239         info.match = 0;
240         for_each_branch_ref(find_current_ref, &info);
241         if (info.match)
242                 return info.req_ref;
243         else
244                 return info.first_ref;
245 }
246
247 static int prepare_repo_cmd(struct cgit_context *ctx)
248 {
249         char *tmp;
250         unsigned char sha1[20];
251         int nongit = 0;
252
253         setenv("GIT_DIR", ctx->repo->path, 1);
254         setup_git_directory_gently(&nongit);
255         if (nongit) {
256                 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
257                                       "config error");
258                 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
259                 ctx->repo = NULL;
260                 cgit_print_http_headers(ctx);
261                 cgit_print_docstart(ctx);
262                 cgit_print_pageheader(ctx);
263                 cgit_print_error(tmp);
264                 cgit_print_docend();
265                 return 1;
266         }
267         ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
268
269         if (!ctx->qry.head) {
270                 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
271                 ctx->repo->defbranch = ctx->qry.head;
272         }
273
274         if (!ctx->qry.head) {
275                 cgit_print_http_headers(ctx);
276                 cgit_print_docstart(ctx);
277                 cgit_print_pageheader(ctx);
278                 cgit_print_error("Repository seems to be empty");
279                 cgit_print_docend();
280                 return 1;
281         }
282
283         if (get_sha1(ctx->qry.head, sha1)) {
284                 tmp = xstrdup(ctx->qry.head);
285                 ctx->qry.head = ctx->repo->defbranch;
286                 cgit_print_http_headers(ctx);
287                 cgit_print_docstart(ctx);
288                 cgit_print_pageheader(ctx);
289                 cgit_print_error(fmt("Invalid branch: %s", tmp));
290                 cgit_print_docend();
291                 return 1;
292         }
293         return 0;
294 }
295
296 static void process_request(struct cgit_context *ctx)
297 {
298         struct cgit_cmd *cmd;
299
300         cmd = cgit_get_cmd(ctx);
301         if (!cmd) {
302                 ctx->page.title = "cgit error";
303                 ctx->repo = NULL;
304                 cgit_print_http_headers(ctx);
305                 cgit_print_docstart(ctx);
306                 cgit_print_pageheader(ctx);
307                 cgit_print_error("Invalid request");
308                 cgit_print_docend();
309                 return;
310         }
311
312         if (cmd->want_repo && !ctx->repo) {
313                 cgit_print_http_headers(ctx);
314                 cgit_print_docstart(ctx);
315                 cgit_print_pageheader(ctx);
316                 cgit_print_error(fmt("No repository selected"));
317                 cgit_print_docend();
318                 return;
319         }
320
321         if (ctx->repo && prepare_repo_cmd(ctx))
322                 return;
323
324         if (cmd->want_layout) {
325                 cgit_print_http_headers(ctx);
326                 cgit_print_docstart(ctx);
327                 cgit_print_pageheader(ctx);
328         }
329
330         cmd->fn(ctx);
331
332         if (cmd->want_layout)
333                 cgit_print_docend();
334 }
335
336 static long ttl_seconds(long ttl)
337 {
338         if (ttl<0)
339                 return 60 * 60 * 24 * 365;
340         else
341                 return ttl * 60;
342 }
343
344 static void cgit_fill_cache(struct cacheitem *item, int use_cache)
345 {
346         int stdout2;
347
348         if (use_cache) {
349                 stdout2 = chk_positive(dup(STDOUT_FILENO),
350                                        "Preserving STDOUT");
351                 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
352                 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
353         }
354
355         ctx.page.modified = time(NULL);
356         ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
357         process_request(&ctx);
358
359         if (use_cache) {
360                 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
361                 chk_positive(dup2(stdout2, STDOUT_FILENO),
362                              "Restoring original STDOUT");
363                 chk_zero(close(stdout2), "Closing temporary STDOUT");
364         }
365 }
366
367 static void cgit_check_cache(struct cacheitem *item)
368 {
369         int i = 0;
370
371  top:
372         if (++i > ctx.cfg.max_lock_attempts) {
373                 die("cgit_refresh_cache: unable to lock %s: %s",
374                     item->name, strerror(errno));
375         }
376         if (!cache_exist(item)) {
377                 if (!cache_lock(item)) {
378                         sleep(1);
379                         goto top;
380                 }
381                 if (!cache_exist(item)) {
382                         cgit_fill_cache(item, 1);
383                         cache_unlock(item);
384                 } else {
385                         cache_cancel_lock(item);
386                 }
387         } else if (cache_expired(item) && cache_lock(item)) {
388                 if (cache_expired(item)) {
389                         cgit_fill_cache(item, 1);
390                         cache_unlock(item);
391                 } else {
392                         cache_cancel_lock(item);
393                 }
394         }
395 }
396
397 static void cgit_print_cache(struct cacheitem *item)
398 {
399         static char buf[4096];
400         ssize_t i;
401
402         int fd = open(item->name, O_RDONLY);
403         if (fd<0)
404                 die("Unable to open cached file %s", item->name);
405
406         while((i=read(fd, buf, sizeof(buf))) > 0)
407                 write(STDOUT_FILENO, buf, i);
408
409         close(fd);
410 }
411
412 static void cgit_parse_args(int argc, const char **argv)
413 {
414         int i;
415
416         for (i = 1; i < argc; i++) {
417                 if (!strncmp(argv[i], "--cache=", 8)) {
418                         ctx.cfg.cache_root = xstrdup(argv[i]+8);
419                 }
420                 if (!strcmp(argv[i], "--nocache")) {
421                         ctx.cfg.nocache = 1;
422                 }
423                 if (!strncmp(argv[i], "--query=", 8)) {
424                         ctx.qry.raw = xstrdup(argv[i]+8);
425                 }
426                 if (!strncmp(argv[i], "--repo=", 7)) {
427                         ctx.qry.repo = xstrdup(argv[i]+7);
428                 }
429                 if (!strncmp(argv[i], "--page=", 7)) {
430                         ctx.qry.page = xstrdup(argv[i]+7);
431                 }
432                 if (!strncmp(argv[i], "--head=", 7)) {
433                         ctx.qry.head = xstrdup(argv[i]+7);
434                         ctx.qry.has_symref = 1;
435                 }
436                 if (!strncmp(argv[i], "--sha1=", 7)) {
437                         ctx.qry.sha1 = xstrdup(argv[i]+7);
438                         ctx.qry.has_sha1 = 1;
439                 }
440                 if (!strncmp(argv[i], "--ofs=", 6)) {
441                         ctx.qry.ofs = atoi(argv[i]+6);
442                 }
443         }
444 }
445
446 int main(int argc, const char **argv)
447 {
448         struct cacheitem item;
449         const char *cgit_config_env = getenv("CGIT_CONFIG");
450
451         prepare_context(&ctx);
452         item.st.st_mtime = time(NULL);
453         cgit_repolist.length = 0;
454         cgit_repolist.count = 0;
455         cgit_repolist.repos = NULL;
456
457         parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
458                          config_cb);
459         ctx.repo = NULL;
460         if (getenv("SCRIPT_NAME"))
461                 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
462         if (getenv("QUERY_STRING"))
463                 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
464         cgit_parse_args(argc, argv);
465         http_parse_querystring(ctx.qry.raw, querystring_cb);
466         if (!cgit_prepare_cache(&item))
467                 return 0;
468         if (ctx.cfg.nocache) {
469                 cgit_fill_cache(&item, 0);
470         } else {
471                 cgit_check_cache(&item);
472                 cgit_print_cache(&item);
473         }
474         return 0;
475 }