blob: dbb023e62bd88b85bc4e488a785f0cd5e2375f4b [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 Hjemlia4d1ca12008-03-24 16:50:57 +010011#include "ui-shared.h"
Lars Hjemli0d169ad2006-12-09 15:18:17 +010012
Lars Hjemlice1c7332007-02-03 15:02:55 +010013static int cgit_prepare_cache(struct cacheitem *item)
Lars Hjemli83a5f352007-01-12 00:00:15 +010014{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010015 if (!ctx.repo && ctx.qry.repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +010016 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
17 "Bad request");
18 cgit_print_http_headers(&ctx);
19 cgit_print_docstart(&ctx);
20 cgit_print_pageheader(&ctx);
Lars Hjemlid14d77f2008-02-16 11:53:40 +010021 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +010022 cgit_print_docend();
23 return 0;
24 }
25
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010026 if (!ctx.repo) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010027 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
28 item->ttl = ctx.cfg.cache_root_ttl;
Lars Hjemli30ccdca2007-05-18 03:00:54 +020029 return 1;
30 }
31
32 if (!cgit_cmd) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010033 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010034 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +010035 cache_safe_filename(ctx.qry.raw)));
Lars Hjemlib228d4f2008-02-16 13:07:13 +010036 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +010037 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +010038 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010039 cache_safe_filename(ctx.repo->url),
Lars Hjemlid14d77f2008-02-16 11:53:40 +010040 ctx.qry.page,
41 cache_safe_filename(ctx.qry.raw)));
42 if (ctx.qry.has_symref)
Lars Hjemlib228d4f2008-02-16 13:07:13 +010043 item->ttl = ctx.cfg.cache_dynamic_ttl;
Lars Hjemlid14d77f2008-02-16 11:53:40 +010044 else if (ctx.qry.has_sha1)
Lars Hjemlib228d4f2008-02-16 13:07:13 +010045 item->ttl = ctx.cfg.cache_static_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +010046 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +010047 item->ttl = ctx.cfg.cache_repo_ttl;
Lars Hjemli83a5f352007-01-12 00:00:15 +010048 }
Lars Hjemlice1c7332007-02-03 15:02:55 +010049 return 1;
Lars Hjemli83a5f352007-01-12 00:00:15 +010050}
51
Lars Hjemlif80ff372008-01-04 13:43:40 +010052struct refmatch {
53 char *req_ref;
54 char *first_ref;
55 int match;
56};
57
58int find_current_ref(const char *refname, const unsigned char *sha1,
59 int flags, void *cb_data)
60{
61 struct refmatch *info;
62
63 info = (struct refmatch *)cb_data;
64 if (!strcmp(refname, info->req_ref))
65 info->match = 1;
66 if (!info->first_ref)
67 info->first_ref = xstrdup(refname);
68 return info->match;
69}
70
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010071char *find_default_branch(struct cgit_repo *repo)
Lars Hjemlif80ff372008-01-04 13:43:40 +010072{
73 struct refmatch info;
74
75 info.req_ref = repo->defbranch;
76 info.first_ref = NULL;
77 info.match = 0;
78 for_each_branch_ref(find_current_ref, &info);
79 if (info.match)
80 return info.req_ref;
81 else
82 return info.first_ref;
83}
84
Lars Hjemlie0e44782008-03-24 01:09:39 +010085static int prepare_repo_cmd(struct cgit_context *ctx)
Lars Hjemli0d169ad2006-12-09 15:18:17 +010086{
Lars Hjemlif3c1a182008-03-24 00:51:19 +010087 char *tmp;
Lars Hjemlif80ff372008-01-04 13:43:40 +010088 unsigned char sha1[20];
Lars Hjemlib88fb012008-02-16 21:16:53 +010089 int nongit = 0;
Lars Hjemlib28b1052007-05-16 00:14:51 +020090
Lars Hjemlie0e44782008-03-24 01:09:39 +010091 setenv("GIT_DIR", ctx->repo->path, 1);
Lars Hjemlib88fb012008-02-16 21:16:53 +010092 setup_git_directory_gently(&nongit);
93 if (nongit) {
Lars Hjemlie0e44782008-03-24 01:09:39 +010094 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
95 "config error");
96 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
97 ctx->repo = NULL;
98 cgit_print_http_headers(ctx);
99 cgit_print_docstart(ctx);
100 cgit_print_pageheader(ctx);
Lars Hjemlib88fb012008-02-16 21:16:53 +0100101 cgit_print_error(tmp);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100102 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100103 return 1;
104 }
105 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
106
107 if (!ctx->qry.head) {
108 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
109 ctx->repo->defbranch = ctx->qry.head;
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100110 }
Lars Hjemlice1c7332007-02-03 15:02:55 +0100111
Lars Hjemlie0e44782008-03-24 01:09:39 +0100112 if (!ctx->qry.head) {
113 cgit_print_http_headers(ctx);
114 cgit_print_docstart(ctx);
115 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100116 cgit_print_error("Repository seems to be empty");
117 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100118 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100119 }
120
Lars Hjemlie0e44782008-03-24 01:09:39 +0100121 if (get_sha1(ctx->qry.head, sha1)) {
122 tmp = xstrdup(ctx->qry.head);
123 ctx->qry.head = ctx->repo->defbranch;
124 cgit_print_http_headers(ctx);
125 cgit_print_docstart(ctx);
126 cgit_print_pageheader(ctx);
Lars Hjemlif80ff372008-01-04 13:43:40 +0100127 cgit_print_error(fmt("Invalid branch: %s", tmp));
128 cgit_print_docend();
Lars Hjemlie0e44782008-03-24 01:09:39 +0100129 return 1;
Lars Hjemlif80ff372008-01-04 13:43:40 +0100130 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100131 return 0;
132}
Lars Hjemlif80ff372008-01-04 13:43:40 +0100133
Lars Hjemlie0e44782008-03-24 01:09:39 +0100134static void process_request(struct cgit_context *ctx)
135{
136 struct cgit_cmd *cmd;
Lars Hjemliab2ab952007-02-08 13:53:13 +0100137
Lars Hjemlie0e44782008-03-24 01:09:39 +0100138 cmd = cgit_get_cmd(ctx);
139 if (!cmd) {
140 ctx->page.title = "cgit error";
141 ctx->repo = NULL;
142 cgit_print_http_headers(ctx);
143 cgit_print_docstart(ctx);
144 cgit_print_pageheader(ctx);
145 cgit_print_error("Invalid request");
Lars Hjemli66cacd02007-02-17 13:46:18 +0100146 cgit_print_docend();
147 return;
148 }
149
Lars Hjemlie0e44782008-03-24 01:09:39 +0100150 if (cmd->want_repo && prepare_repo_cmd(ctx))
151 return;
Lars Hjemli66cacd02007-02-17 13:46:18 +0100152
Lars Hjemlie0e44782008-03-24 01:09:39 +0100153 if (cmd->want_layout) {
154 cgit_print_http_headers(ctx);
155 cgit_print_docstart(ctx);
156 cgit_print_pageheader(ctx);
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100157 }
Lars Hjemlie0e44782008-03-24 01:09:39 +0100158
159 cmd->fn(ctx);
160
161 if (cmd->want_layout)
162 cgit_print_docend();
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100163}
164
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100165static long ttl_seconds(long ttl)
166{
167 if (ttl<0)
168 return 60 * 60 * 24 * 365;
169 else
170 return ttl * 60;
171}
172
Lars Hjemliab2ab952007-02-08 13:53:13 +0100173static void cgit_fill_cache(struct cacheitem *item, int use_cache)
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100174{
Lars Hjemliab2ab952007-02-08 13:53:13 +0100175 int stdout2;
Lars Hjemli7c849d92006-12-16 13:55:58 +0100176
Lars Hjemliab2ab952007-02-08 13:53:13 +0100177 if (use_cache) {
Lars Hjemli5b94c962007-05-14 22:58:01 +0200178 stdout2 = chk_positive(dup(STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100179 "Preserving STDOUT");
180 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
181 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
182 }
183
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100184 ctx.page.modified = time(NULL);
185 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
Lars Hjemlie0e44782008-03-24 01:09:39 +0100186 process_request(&ctx);
Lars Hjemliab2ab952007-02-08 13:53:13 +0100187
188 if (use_cache) {
189 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
Lars Hjemli5b94c962007-05-14 22:58:01 +0200190 chk_positive(dup2(stdout2, STDOUT_FILENO),
Lars Hjemliab2ab952007-02-08 13:53:13 +0100191 "Restoring original STDOUT");
192 chk_zero(close(stdout2), "Closing temporary STDOUT");
193 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100194}
195
Lars Hjemli44923f82006-12-11 17:25:41 +0100196static void cgit_check_cache(struct cacheitem *item)
Lars Hjemli25105d72006-12-10 22:31:36 +0100197{
Lars Hjemli318d1062006-12-11 12:10:12 +0100198 int i = 0;
199
Lars Hjemli25105d72006-12-10 22:31:36 +0100200 top:
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100201 if (++i > ctx.cfg.max_lock_attempts) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100202 die("cgit_refresh_cache: unable to lock %s: %s",
203 item->name, strerror(errno));
204 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100205 if (!cache_exist(item)) {
206 if (!cache_lock(item)) {
Lars Hjemli318d1062006-12-11 12:10:12 +0100207 sleep(1);
Lars Hjemli25105d72006-12-10 22:31:36 +0100208 goto top;
209 }
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100210 if (!cache_exist(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100211 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100212 cache_unlock(item);
213 } else {
214 cache_cancel_lock(item);
215 }
Lars Hjemlif5069d82006-12-11 09:57:58 +0100216 } else if (cache_expired(item) && cache_lock(item)) {
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100217 if (cache_expired(item)) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100218 cgit_fill_cache(item, 1);
Lars Hjemlifbaf1172006-12-11 22:53:50 +0100219 cache_unlock(item);
220 } else {
221 cache_cancel_lock(item);
222 }
Lars Hjemli25105d72006-12-10 22:31:36 +0100223 }
224}
225
226static void cgit_print_cache(struct cacheitem *item)
227{
228 static char buf[4096];
229 ssize_t i;
230
231 int fd = open(item->name, O_RDONLY);
232 if (fd<0)
233 die("Unable to open cached file %s", item->name);
234
235 while((i=read(fd, buf, sizeof(buf))) > 0)
236 write(STDOUT_FILENO, buf, i);
237
238 close(fd);
239}
240
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100241static void cgit_parse_args(int argc, const char **argv)
242{
243 int i;
244
245 for (i = 1; i < argc; i++) {
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100246 if (!strncmp(argv[i], "--cache=", 8)) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100247 ctx.cfg.cache_root = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100248 }
249 if (!strcmp(argv[i], "--nocache")) {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100250 ctx.cfg.nocache = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100251 }
252 if (!strncmp(argv[i], "--query=", 8)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100253 ctx.qry.raw = xstrdup(argv[i]+8);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100254 }
255 if (!strncmp(argv[i], "--repo=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100256 ctx.qry.repo = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100257 }
258 if (!strncmp(argv[i], "--page=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100259 ctx.qry.page = xstrdup(argv[i]+7);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100260 }
261 if (!strncmp(argv[i], "--head=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100262 ctx.qry.head = xstrdup(argv[i]+7);
263 ctx.qry.has_symref = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100264 }
265 if (!strncmp(argv[i], "--sha1=", 7)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100266 ctx.qry.sha1 = xstrdup(argv[i]+7);
267 ctx.qry.has_sha1 = 1;
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100268 }
269 if (!strncmp(argv[i], "--ofs=", 6)) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100270 ctx.qry.ofs = atoi(argv[i]+6);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100271 }
272 }
273}
274
Lars Hjemli25105d72006-12-10 22:31:36 +0100275int main(int argc, const char **argv)
276{
Lars Hjemli44923f82006-12-11 17:25:41 +0100277 struct cacheitem item;
Michael Krelin51f65472007-07-02 02:29:12 +0200278 const char *cgit_config_env = getenv("CGIT_CONFIG");
Lars Hjemli44923f82006-12-11 17:25:41 +0100279
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100280 cgit_prepare_context(&ctx);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100281 item.st.st_mtime = time(NULL);
282 cgit_repolist.length = 0;
283 cgit_repolist.count = 0;
284 cgit_repolist.repos = NULL;
285
Michael Krelin51f65472007-07-02 02:29:12 +0200286 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
287 cgit_global_config_cb);
Lars Hjemliea2831f2007-05-15 00:48:31 +0200288 if (getenv("SCRIPT_NAME"))
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100289 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100290 if (getenv("QUERY_STRING"))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100291 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100292 cgit_parse_args(argc, argv);
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100293 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
Lars Hjemlice1c7332007-02-03 15:02:55 +0100294 if (!cgit_prepare_cache(&item))
295 return 0;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100296 if (ctx.cfg.nocache) {
Lars Hjemliab2ab952007-02-08 13:53:13 +0100297 cgit_fill_cache(&item, 0);
Lars Hjemli521dc7a2006-12-16 13:33:32 +0100298 } else {
299 cgit_check_cache(&item);
300 cgit_print_cache(&item);
301 }
Lars Hjemli0d169ad2006-12-09 15:18:17 +0100302 return 0;
303}