]> gitweb.ps.run Git - ps-cgit/blob - cgit.c
Redesign the caching layer
[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, "css"))
23                 ctx.cfg.css = xstrdup(value);
24         else if (!strcmp(name, "logo"))
25                 ctx.cfg.logo = xstrdup(value);
26         else if (!strcmp(name, "index-header"))
27                 ctx.cfg.index_header = xstrdup(value);
28         else if (!strcmp(name, "index-info"))
29                 ctx.cfg.index_info = xstrdup(value);
30         else if (!strcmp(name, "logo-link"))
31                 ctx.cfg.logo_link = xstrdup(value);
32         else if (!strcmp(name, "module-link"))
33                 ctx.cfg.module_link = xstrdup(value);
34         else if (!strcmp(name, "virtual-root")) {
35                 ctx.cfg.virtual_root = trim_end(value, '/');
36                 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
37                         ctx.cfg.virtual_root = "";
38         } else if (!strcmp(name, "nocache"))
39                 ctx.cfg.nocache = atoi(value);
40         else if (!strcmp(name, "snapshots"))
41                 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
42         else if (!strcmp(name, "enable-index-links"))
43                 ctx.cfg.enable_index_links = atoi(value);
44         else if (!strcmp(name, "enable-log-filecount"))
45                 ctx.cfg.enable_log_filecount = atoi(value);
46         else if (!strcmp(name, "enable-log-linecount"))
47                 ctx.cfg.enable_log_linecount = atoi(value);
48         else if (!strcmp(name, "cache-size"))
49                 ctx.cfg.cache_size = atoi(value);
50         else if (!strcmp(name, "cache-root"))
51                 ctx.cfg.cache_root = xstrdup(value);
52         else if (!strcmp(name, "cache-root-ttl"))
53                 ctx.cfg.cache_root_ttl = atoi(value);
54         else if (!strcmp(name, "cache-repo-ttl"))
55                 ctx.cfg.cache_repo_ttl = atoi(value);
56         else if (!strcmp(name, "cache-static-ttl"))
57                 ctx.cfg.cache_static_ttl = atoi(value);
58         else if (!strcmp(name, "cache-dynamic-ttl"))
59                 ctx.cfg.cache_dynamic_ttl = atoi(value);
60         else if (!strcmp(name, "max-message-length"))
61                 ctx.cfg.max_msg_len = atoi(value);
62         else if (!strcmp(name, "max-repodesc-length"))
63                 ctx.cfg.max_repodesc_len = atoi(value);
64         else if (!strcmp(name, "max-commit-count"))
65                 ctx.cfg.max_commit_count = atoi(value);
66         else if (!strcmp(name, "summary-log"))
67                 ctx.cfg.summary_log = atoi(value);
68         else if (!strcmp(name, "summary-branches"))
69                 ctx.cfg.summary_branches = atoi(value);
70         else if (!strcmp(name, "summary-tags"))
71                 ctx.cfg.summary_tags = atoi(value);
72         else if (!strcmp(name, "agefile"))
73                 ctx.cfg.agefile = xstrdup(value);
74         else if (!strcmp(name, "renamelimit"))
75                 ctx.cfg.renamelimit = atoi(value);
76         else if (!strcmp(name, "robots"))
77                 ctx.cfg.robots = xstrdup(value);
78         else if (!strcmp(name, "clone-prefix"))
79                 ctx.cfg.clone_prefix = xstrdup(value);
80         else if (!strcmp(name, "repo.group"))
81                 ctx.cfg.repo_group = xstrdup(value);
82         else if (!strcmp(name, "repo.url"))
83                 ctx.repo = cgit_add_repo(value);
84         else if (!strcmp(name, "repo.name"))
85                 ctx.repo->name = xstrdup(value);
86         else if (ctx.repo && !strcmp(name, "repo.path"))
87                 ctx.repo->path = trim_end(value, '/');
88         else if (ctx.repo && !strcmp(name, "repo.clone-url"))
89                 ctx.repo->clone_url = xstrdup(value);
90         else if (ctx.repo && !strcmp(name, "repo.desc"))
91                 ctx.repo->desc = xstrdup(value);
92         else if (ctx.repo && !strcmp(name, "repo.owner"))
93                 ctx.repo->owner = xstrdup(value);
94         else if (ctx.repo && !strcmp(name, "repo.defbranch"))
95                 ctx.repo->defbranch = xstrdup(value);
96         else if (ctx.repo && !strcmp(name, "repo.snapshots"))
97                 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
98         else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
99                 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
100         else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
101                 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
102         else if (ctx.repo && !strcmp(name, "repo.module-link"))
103                 ctx.repo->module_link= xstrdup(value);
104         else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
105                 if (*value == '/')
106                         ctx.repo->readme = xstrdup(value);
107                 else
108                         ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
109         } else if (!strcmp(name, "include"))
110                 parse_configfile(value, config_cb);
111 }
112
113 static void querystring_cb(const char *name, const char *value)
114 {
115         if (!strcmp(name,"r")) {
116                 ctx.qry.repo = xstrdup(value);
117                 ctx.repo = cgit_get_repoinfo(value);
118         } else if (!strcmp(name, "p")) {
119                 ctx.qry.page = xstrdup(value);
120         } else if (!strcmp(name, "url")) {
121                 cgit_parse_url(value);
122         } else if (!strcmp(name, "qt")) {
123                 ctx.qry.grep = xstrdup(value);
124         } else if (!strcmp(name, "q")) {
125                 ctx.qry.search = xstrdup(value);
126         } else if (!strcmp(name, "h")) {
127                 ctx.qry.head = xstrdup(value);
128                 ctx.qry.has_symref = 1;
129         } else if (!strcmp(name, "id")) {
130                 ctx.qry.sha1 = xstrdup(value);
131                 ctx.qry.has_sha1 = 1;
132         } else if (!strcmp(name, "id2")) {
133                 ctx.qry.sha2 = xstrdup(value);
134                 ctx.qry.has_sha1 = 1;
135         } else if (!strcmp(name, "ofs")) {
136                 ctx.qry.ofs = atoi(value);
137         } else if (!strcmp(name, "path")) {
138                 ctx.qry.path = trim_end(value, '/');
139         } else if (!strcmp(name, "name")) {
140                 ctx.qry.name = xstrdup(value);
141         }
142 }
143
144 static void prepare_context(struct cgit_context *ctx)
145 {
146         memset(ctx, 0, sizeof(ctx));
147         ctx->cfg.agefile = "info/web/last-modified";
148         ctx->cfg.nocache = 0;
149         ctx->cfg.cache_size = 0;
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.script_name = CGIT_SCRIPT_NAME;
167         ctx->page.mimetype = "text/html";
168         ctx->page.charset = PAGE_ENCODING;
169         ctx->page.filename = NULL;
170         ctx->page.modified = time(NULL);
171         ctx->page.expires = ctx->page.modified;
172 }
173
174 struct refmatch {
175         char *req_ref;
176         char *first_ref;
177         int match;
178 };
179
180 int find_current_ref(const char *refname, const unsigned char *sha1,
181                      int flags, void *cb_data)
182 {
183         struct refmatch *info;
184
185         info = (struct refmatch *)cb_data;
186         if (!strcmp(refname, info->req_ref))
187                 info->match = 1;
188         if (!info->first_ref)
189                 info->first_ref = xstrdup(refname);
190         return info->match;
191 }
192
193 char *find_default_branch(struct cgit_repo *repo)
194 {
195         struct refmatch info;
196
197         info.req_ref = repo->defbranch;
198         info.first_ref = NULL;
199         info.match = 0;
200         for_each_branch_ref(find_current_ref, &info);
201         if (info.match)
202                 return info.req_ref;
203         else
204                 return info.first_ref;
205 }
206
207 static int prepare_repo_cmd(struct cgit_context *ctx)
208 {
209         char *tmp;
210         unsigned char sha1[20];
211         int nongit = 0;
212
213         setenv("GIT_DIR", ctx->repo->path, 1);
214         setup_git_directory_gently(&nongit);
215         if (nongit) {
216                 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
217                                       "config error");
218                 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
219                 ctx->repo = NULL;
220                 cgit_print_http_headers(ctx);
221                 cgit_print_docstart(ctx);
222                 cgit_print_pageheader(ctx);
223                 cgit_print_error(tmp);
224                 cgit_print_docend();
225                 return 1;
226         }
227         ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
228
229         if (!ctx->qry.head) {
230                 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
231                 ctx->repo->defbranch = ctx->qry.head;
232         }
233
234         if (!ctx->qry.head) {
235                 cgit_print_http_headers(ctx);
236                 cgit_print_docstart(ctx);
237                 cgit_print_pageheader(ctx);
238                 cgit_print_error("Repository seems to be empty");
239                 cgit_print_docend();
240                 return 1;
241         }
242
243         if (get_sha1(ctx->qry.head, sha1)) {
244                 tmp = xstrdup(ctx->qry.head);
245                 ctx->qry.head = ctx->repo->defbranch;
246                 cgit_print_http_headers(ctx);
247                 cgit_print_docstart(ctx);
248                 cgit_print_pageheader(ctx);
249                 cgit_print_error(fmt("Invalid branch: %s", tmp));
250                 cgit_print_docend();
251                 return 1;
252         }
253         return 0;
254 }
255
256 static void process_request(void *cbdata)
257 {
258         struct cgit_context *ctx = cbdata;
259         struct cgit_cmd *cmd;
260
261         cmd = cgit_get_cmd(ctx);
262         if (!cmd) {
263                 ctx->page.title = "cgit error";
264                 ctx->repo = NULL;
265                 cgit_print_http_headers(ctx);
266                 cgit_print_docstart(ctx);
267                 cgit_print_pageheader(ctx);
268                 cgit_print_error("Invalid request");
269                 cgit_print_docend();
270                 return;
271         }
272
273         if (cmd->want_repo && prepare_repo_cmd(ctx))
274                 return;
275
276         if (cmd->want_layout) {
277                 cgit_print_http_headers(ctx);
278                 cgit_print_docstart(ctx);
279                 cgit_print_pageheader(ctx);
280         }
281
282         cmd->fn(ctx);
283
284         if (cmd->want_layout)
285                 cgit_print_docend();
286 }
287
288 static void cgit_parse_args(int argc, const char **argv)
289 {
290         int i;
291
292         for (i = 1; i < argc; i++) {
293                 if (!strncmp(argv[i], "--cache=", 8)) {
294                         ctx.cfg.cache_root = xstrdup(argv[i]+8);
295                 }
296                 if (!strcmp(argv[i], "--nocache")) {
297                         ctx.cfg.nocache = 1;
298                 }
299                 if (!strncmp(argv[i], "--query=", 8)) {
300                         ctx.qry.raw = xstrdup(argv[i]+8);
301                 }
302                 if (!strncmp(argv[i], "--repo=", 7)) {
303                         ctx.qry.repo = xstrdup(argv[i]+7);
304                 }
305                 if (!strncmp(argv[i], "--page=", 7)) {
306                         ctx.qry.page = xstrdup(argv[i]+7);
307                 }
308                 if (!strncmp(argv[i], "--head=", 7)) {
309                         ctx.qry.head = xstrdup(argv[i]+7);
310                         ctx.qry.has_symref = 1;
311                 }
312                 if (!strncmp(argv[i], "--sha1=", 7)) {
313                         ctx.qry.sha1 = xstrdup(argv[i]+7);
314                         ctx.qry.has_sha1 = 1;
315                 }
316                 if (!strncmp(argv[i], "--ofs=", 6)) {
317                         ctx.qry.ofs = atoi(argv[i]+6);
318                 }
319         }
320 }
321
322 static int calc_ttl()
323 {
324         if (!ctx.repo)
325                 return ctx.cfg.cache_root_ttl;
326
327         if (!ctx.qry.page)
328                 return ctx.cfg.cache_repo_ttl;
329
330         if (ctx.qry.has_symref)
331                 return ctx.cfg.cache_dynamic_ttl;
332
333         if (ctx.qry.has_sha1)
334                 return ctx.cfg.cache_static_ttl;
335
336         return ctx.cfg.cache_repo_ttl;
337 }
338
339 int main(int argc, const char **argv)
340 {
341         const char *cgit_config_env = getenv("CGIT_CONFIG");
342         int err, ttl;
343
344         prepare_context(&ctx);
345         cgit_repolist.length = 0;
346         cgit_repolist.count = 0;
347         cgit_repolist.repos = NULL;
348
349         parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
350                          config_cb);
351         ctx.repo = NULL;
352         if (getenv("SCRIPT_NAME"))
353                 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
354         if (getenv("QUERY_STRING"))
355                 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
356         cgit_parse_args(argc, argv);
357         http_parse_querystring(ctx.qry.raw, querystring_cb);
358
359         ttl = calc_ttl();
360         ctx.page.expires += ttl*60;
361         if (ctx.cfg.nocache)
362                 ctx.cfg.cache_size = 0;
363         err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
364                             ctx.qry.raw, ttl, process_request, &ctx);
365         if (err)
366                 cache_log("[cgit] error %d - %s\n",
367                           err, strerror(err));
368         return err;
369 }