blob: 79e0e430db79691e1735dbeb2cf4d7400b7e5bc0 [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 Hjemlif3c1a182008-03-24 00:51:19 +010010#include "cmd.h"
Lars Hjemli0d169ad2006-12-09 15:18:17 +010011
Lars Hjemlice1c7332007-02-03 15:02:55 +010012static int cgit_prepare_cache(struct cacheitem *item)
Lars Hjemli83a5f352007-01-12 00:00:15 +010013{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010014 if (!ctx.repo && ctx.qry.repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +010015 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
16 "Bad request");
17 cgit_print_http_headers(&ctx);
18 cgit_print_docstart(&ctx);
19 cgit_print_pageheader(&ctx);
Lars Hjemlid14d77f2008-02-16 11:53:40 +010020 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +010021 cgit_print_docend();
22 return 0;
23 }
24
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010025 if (!ctx.repo) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010026 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
27 item->ttl = ctx.cfg.cache_root_ttl;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020028 return 1;
29 }
30
31 if (!cgit_cmd) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010032 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010033 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +010034 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +010035 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +010036 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010037 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010038 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +010039 ctx.qry.page,
40 cache_safe_filename(ctx.qry.raw)));
41 if (ctx.qry.has_symref)
Lars Hjemlib228d4f2008-02-16 13:07:13 +010042 item->ttl = ctx.cfg.cache_dynamic_ttl;
Lars Hjemlid14d77f2008-02-16 11:53:40 +010043 else if (ctx.qry.has_sha1)
Lars Hjemlib228d4f2008-02-16 13:07:13 +010044 item->ttl = ctx.cfg.cache_static_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +010045 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +010046 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +010047 }
Lars Hjemlice1c7332007-02-03 15:02:55 +010048 return 1;
Lars Hjemli83a5f352007-01-12 00:00:15 +010049}
50
Lars Hjemlif80ff372008-01-04 13:43:40 +010051struct refmatch {
52 char *req_ref;
53 char *first_ref;
54 int match;
55};
56
57int find_current_ref(const char *refname, const unsigned char *sha1,
58 int flags, void *cb_data)
59{
60 struct refmatch *info;
61
62 info = (struct refmatch *)cb_data;
63 if (!strcmp(refname, info->req_ref))
64 info->match = 1;
65 if (!info->first_ref)
66 info->first_ref = xstrdup(refname);
67 return info->match;
68}
69
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010070char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +010071{
72 struct refmatch info;
73
74 info.req_ref = repo->defbranch;
75 info.first_ref = NULL;
76 info.match = 0;
77 for_each_branch_ref(find_current_ref, &info);
78 if (info.match)
79 return info.req_ref;
80 else
81 return info.first_ref;
82}
83
Lars Hjemlie0e44782008-03-24 01:09:39 +010084static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010085{
Lars Hjemlif3c1a182008-03-24 00:51:19 +010086 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +010087 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +010088 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +020089
Lars Hjemlie0e44782008-03-24 01:09:39 +010090 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +010091 setup_git_directory_gently(&nongit);
92 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +010093 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
94 "config error");
95 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
96 ctx->repo = NULL;
97 cgit_print_http_headers(ctx);
98 cgit_print_docstart(ctx);
99 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100100 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100101 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100102 return 1;
103 }
104 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
105
106 if (!ctx->qry.head) {
107 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
108 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100109 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100110
Lars Hjemlie0e44782008-03-24 01:09:39 +0100111 if (!ctx->qry.head) {
112 cgit_print_http_headers(ctx);
113 cgit_print_docstart(ctx);
114 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100115 cgit_print_error("Repository seems to be empty");
116 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100117 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100118 }
119
Lars Hjemlie0e44782008-03-24 01:09:39 +0100120 if (get_sha1(ctx->qry.head, sha1)) {
121 tmp = xstrdup(ctx->qry.head);
122 ctx->qry.head = ctx->repo->defbranch;
123 cgit_print_http_headers(ctx);
124 cgit_print_docstart(ctx);
125 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100126 cgit_print_error(fmt("Invalid branch: %s", tmp));
127 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100128 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100129 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100130 return 0;
131}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100132
Lars Hjemlie0e44782008-03-24 01:09:39 +0100133static void process_request(struct cgit_context *ctx)
134{
135 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100136
Lars Hjemlie0e44782008-03-24 01:09:39 +0100137 cmd = cgit_get_cmd(ctx);
138 if (!cmd) {
139 ctx->page.title = "cgit error";
140 ctx->repo = NULL;
141 cgit_print_http_headers(ctx);
142 cgit_print_docstart(ctx);
143 cgit_print_pageheader(ctx);
144 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100145 cgit_print_docend();
146 return;
147 }
148
Lars Hjemlie0e44782008-03-24 01:09:39 +0100149 if (cmd->want_repo && prepare_repo_cmd(ctx))
150 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100151
Lars Hjemlie0e44782008-03-24 01:09:39 +0100152 if (cmd->want_layout) {
153 cgit_print_http_headers(ctx);
154 cgit_print_docstart(ctx);
155 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100156 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100157
158 cmd->fn(ctx);
159
160 if (cmd->want_layout)
161 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100162}
163
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100164static long ttl_seconds(long ttl)
165{
166 if (ttl<0)
167 return 60 * 60 * 24 * 365;
168 else
169 return ttl * 60;
170}
171
Lars Hjemliab2ab952007-02-08 13:53:13 +0100172static void cgit_fill_cache(struct cacheitem *item, int use_cache)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100173{
Lars Hjemliab2ab952007-02-08 13:53:13 +0100174 int stdout2;
Lars Hjemli7c849d92006-12-16 13:55:58 +0100175
Lars Hjemliab2ab952007-02-08 13:53:13 +0100176 if (use_cache) {
Lars Hjemli5b94c962007-05-14 22:58:01 +0200177 stdout2 = chk_positive(dup(STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100178 "Preserving STDOUT");
179 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
180 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
181 }
182
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100183 ctx.page.modified = time(NULL);
184 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100185 process_request(&ctx);
Lars Hjemliab2ab952007-02-08 13:53:13 +0100186
187 if (use_cache) {
188 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
Lars Hjemli5b94c962007-05-14 22:58:01 +0200189 chk_positive(dup2(stdout2, STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100190 "Restoring original STDOUT");
191 chk_zero(close(stdout2), "Closing temporary STDOUT");
192 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100193}
194
Lars Hjemli44923f82006-12-11 17:25:41 +0100195static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +0100196{
Lars Hjemli318d1062006-12-11 12:10:12 +0100197 int i = 0;
198
Lars Hjemli25105d72006-12-10 22:31:36 +0100199 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100200 if (++i > ctx.cfg.max_lock_attempts) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100201 die("cgit_refresh_cache: unable to lock %s: %s",
202 item->name, strerror(errno));
203 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100204 if (!cache_exist(item)) {
205 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100206 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100207 goto top;
208 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100209 if (!cache_exist(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100210 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100211 cache_unlock(item);
212 } else {
213 cache_cancel_lock(item);
214 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100215 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100216 if (cache_expired(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100217 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100218 cache_unlock(item);
219 } else {
220 cache_cancel_lock(item);
221 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100222 }
223}
224
225static void cgit_print_cache(struct cacheitem *item)
226{
227 static char buf[4096];
228 ssize_t i;
229
230 int fd = open(item->name, O_RDONLY);
231 if (fd<0)
232 die("Unable to open cached file %s", item->name);
233
234 while((i=read(fd, buf, sizeof(buf))) > 0)
235 write(STDOUT_FILENO, buf, i);
236
237 close(fd);
238}
239
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100240static void cgit_parse_args(int argc, const char **argv)
241{
242 int i;
243
244 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100245 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100246 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100247 }
248 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100249 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100250 }
251 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100252 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100253 }
254 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100255 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100256 }
257 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100258 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100259 }
260 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100261 ctx.qry.head = xstrdup(argv[i]+7);
262 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100263 }
264 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100265 ctx.qry.sha1 = xstrdup(argv[i]+7);
266 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100267 }
268 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100269 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100270 }
271 }
272}
273
Lars Hjemli25105d72006-12-10 22:31:36 +0100274int main(int argc, const char **argv)
275{
Lars Hjemli44923f82006-12-11 17:25:41 +0100276 struct cacheitem item;
Michael Krelin51f65472007-07-02 02:29:12 +0200277 const char *cgit_config_env = getenv("CGIT_CONFIG");
Lars Hjemli44923f82006-12-11 17:25:41 +0100278
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100279 cgit_prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100280 item.st.st_mtime = time(NULL);
281 cgit_repolist.length = 0;
282 cgit_repolist.count = 0;
283 cgit_repolist.repos = NULL;
284
Michael Krelin51f65472007-07-02 02:29:12 +0200285 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
286 cgit_global_config_cb);
Lars Hjemliea2831f2007-05-15 00:48:31 +0200287 if (getenv("SCRIPT_NAME"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100288 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100289 if (getenv("QUERY_STRING"))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100290 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100291 cgit_parse_args(argc, argv);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100292 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100293 if (!cgit_prepare_cache(&item))
294 return 0;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100295 if (ctx.cfg.nocache) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100296 cgit_fill_cache(&item, 0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100297 } else {
298 cgit_check_cache(&item);
299 cgit_print_cache(&item);
300 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100301 return 0;
302}