1 /* cgit.c: cgi for the git scm
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 static int cgit_prepare_cache(struct cacheitem *item)
14 if (!ctx.repo && ctx.qry.repo) {
15 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
17 cgit_print_http_headers(&ctx);
18 cgit_print_docstart(&ctx);
19 cgit_print_pageheader(&ctx);
20 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
26 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
27 item->ttl = ctx.cfg.cache_root_ttl;
32 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
33 cache_safe_filename(ctx.repo->url),
34 cache_safe_filename(ctx.qry.raw)));
35 item->ttl = ctx.cfg.cache_repo_ttl;
37 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
38 cache_safe_filename(ctx.repo->url),
40 cache_safe_filename(ctx.qry.raw)));
41 if (ctx.qry.has_symref)
42 item->ttl = ctx.cfg.cache_dynamic_ttl;
43 else if (ctx.qry.has_sha1)
44 item->ttl = ctx.cfg.cache_static_ttl;
46 item->ttl = ctx.cfg.cache_repo_ttl;
57 int find_current_ref(const char *refname, const unsigned char *sha1,
58 int flags, void *cb_data)
60 struct refmatch *info;
62 info = (struct refmatch *)cb_data;
63 if (!strcmp(refname, info->req_ref))
66 info->first_ref = xstrdup(refname);
70 char *find_default_branch(struct cgit_repo *repo)
74 info.req_ref = repo->defbranch;
75 info.first_ref = NULL;
77 for_each_branch_ref(find_current_ref, &info);
81 return info.first_ref;
84 static void cgit_print_repo_page(struct cacheitem *item)
88 unsigned char sha1[20];
91 setenv("GIT_DIR", ctx.repo->path, 1);
92 setup_git_directory_gently(&nongit);
94 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
96 tmp = fmt("Not a git repository: '%s'", ctx.repo->path);
98 cgit_print_http_headers(&ctx);
99 cgit_print_docstart(&ctx);
100 cgit_print_pageheader(&ctx);
101 cgit_print_error(tmp);
106 ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc);
110 ctx.qry.head = xstrdup(find_default_branch(ctx.repo));
111 ctx.repo->defbranch = ctx.qry.head;
115 cgit_print_http_headers(&ctx);
116 cgit_print_docstart(&ctx);
117 cgit_print_pageheader(&ctx);
118 cgit_print_error("Repository seems to be empty");
123 if (get_sha1(ctx.qry.head, sha1)) {
124 tmp = xstrdup(ctx.qry.head);
125 ctx.qry.head = ctx.repo->defbranch;
126 cgit_print_http_headers(&ctx);
127 cgit_print_docstart(&ctx);
128 cgit_print_pageheader(&ctx);
129 cgit_print_error(fmt("Invalid branch: %s", tmp));
134 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) {
135 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1,
136 cgit_repobasename(ctx.repo->url),
138 ctx.repo->snapshots );
142 if (cgit_cmd == CMD_PATCH) {
143 cgit_print_patch(ctx.qry.sha1, item);
147 if (cgit_cmd == CMD_BLOB) {
148 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path);
152 show_search = (cgit_cmd == CMD_LOG);
153 cgit_print_http_headers(&ctx);
154 cgit_print_docstart(&ctx);
156 cgit_print_pageheader(&ctx);
157 cgit_print_summary();
162 cgit_print_pageheader(&ctx);
166 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs,
167 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search,
171 cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
174 cgit_print_commit(ctx.qry.sha1);
180 cgit_print_tag(ctx.qry.sha1);
183 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path);
186 cgit_print_error("Invalid request");
191 static long ttl_seconds(long ttl)
194 return 60 * 60 * 24 * 365;
199 static void cgit_fill_cache(struct cacheitem *item, int use_cache)
204 stdout2 = chk_positive(dup(STDOUT_FILENO),
205 "Preserving STDOUT");
206 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
207 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
210 ctx.page.modified = time(NULL);
211 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
213 cgit_print_repo_page(item);
215 cgit_print_repolist(item);
218 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
219 chk_positive(dup2(stdout2, STDOUT_FILENO),
220 "Restoring original STDOUT");
221 chk_zero(close(stdout2), "Closing temporary STDOUT");
225 static void cgit_check_cache(struct cacheitem *item)
230 if (++i > ctx.cfg.max_lock_attempts) {
231 die("cgit_refresh_cache: unable to lock %s: %s",
232 item->name, strerror(errno));
234 if (!cache_exist(item)) {
235 if (!cache_lock(item)) {
239 if (!cache_exist(item)) {
240 cgit_fill_cache(item, 1);
243 cache_cancel_lock(item);
245 } else if (cache_expired(item) && cache_lock(item)) {
246 if (cache_expired(item)) {
247 cgit_fill_cache(item, 1);
250 cache_cancel_lock(item);
255 static void cgit_print_cache(struct cacheitem *item)
257 static char buf[4096];
260 int fd = open(item->name, O_RDONLY);
262 die("Unable to open cached file %s", item->name);
264 while((i=read(fd, buf, sizeof(buf))) > 0)
265 write(STDOUT_FILENO, buf, i);
270 static void cgit_parse_args(int argc, const char **argv)
274 for (i = 1; i < argc; i++) {
275 if (!strncmp(argv[i], "--cache=", 8)) {
276 ctx.cfg.cache_root = xstrdup(argv[i]+8);
278 if (!strcmp(argv[i], "--nocache")) {
281 if (!strncmp(argv[i], "--query=", 8)) {
282 ctx.qry.raw = xstrdup(argv[i]+8);
284 if (!strncmp(argv[i], "--repo=", 7)) {
285 ctx.qry.repo = xstrdup(argv[i]+7);
287 if (!strncmp(argv[i], "--page=", 7)) {
288 ctx.qry.page = xstrdup(argv[i]+7);
290 if (!strncmp(argv[i], "--head=", 7)) {
291 ctx.qry.head = xstrdup(argv[i]+7);
292 ctx.qry.has_symref = 1;
294 if (!strncmp(argv[i], "--sha1=", 7)) {
295 ctx.qry.sha1 = xstrdup(argv[i]+7);
296 ctx.qry.has_sha1 = 1;
298 if (!strncmp(argv[i], "--ofs=", 6)) {
299 ctx.qry.ofs = atoi(argv[i]+6);
304 int main(int argc, const char **argv)
306 struct cacheitem item;
307 const char *cgit_config_env = getenv("CGIT_CONFIG");
309 cgit_prepare_context(&ctx);
310 item.st.st_mtime = time(NULL);
311 cgit_repolist.length = 0;
312 cgit_repolist.count = 0;
313 cgit_repolist.repos = NULL;
315 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
316 cgit_global_config_cb);
317 if (getenv("SCRIPT_NAME"))
318 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
319 if (getenv("QUERY_STRING"))
320 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
321 cgit_parse_args(argc, argv);
322 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
323 if (!cgit_prepare_cache(&item))
325 if (ctx.cfg.nocache) {
326 cgit_fill_cache(&item, 0);
328 cgit_check_cache(&item);
329 cgit_print_cache(&item);