2006-12-10 21:41:14 +00:00
|
|
|
/* cgit.c: cgi for the git scm
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Lars Hjemli
|
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2006-12-09 14:18:17 +00:00
|
|
|
#include "cgit.h"
|
2008-03-23 23:51:19 +00:00
|
|
|
#include "cmd.h"
|
2006-12-09 14:18:17 +00:00
|
|
|
|
2007-02-03 14:02:55 +00:00
|
|
|
static int cgit_prepare_cache(struct cacheitem *item)
|
2007-01-11 23:00:15 +00:00
|
|
|
{
|
2008-02-16 12:56:09 +00:00
|
|
|
if (!ctx.repo && ctx.qry.repo) {
|
2008-03-23 23:51:19 +00:00
|
|
|
ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
|
|
|
|
"Bad request");
|
|
|
|
cgit_print_http_headers(&ctx);
|
|
|
|
cgit_print_docstart(&ctx);
|
|
|
|
cgit_print_pageheader(&ctx);
|
2008-02-16 10:53:40 +00:00
|
|
|
cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
|
2007-02-03 14:02:55 +00:00
|
|
|
cgit_print_docend();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-16 12:56:09 +00:00
|
|
|
if (!ctx.repo) {
|
2008-02-16 12:07:13 +00:00
|
|
|
item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
|
|
|
|
item->ttl = ctx.cfg.cache_root_ttl;
|
2007-05-18 01:00:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cgit_cmd) {
|
2008-02-16 12:07:13 +00:00
|
|
|
item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
|
2008-02-16 12:56:09 +00:00
|
|
|
cache_safe_filename(ctx.repo->url),
|
2008-02-16 10:53:40 +00:00
|
|
|
cache_safe_filename(ctx.qry.raw)));
|
2008-02-16 12:07:13 +00:00
|
|
|
item->ttl = ctx.cfg.cache_repo_ttl;
|
2007-01-11 23:00:15 +00:00
|
|
|
} else {
|
2008-02-16 12:07:13 +00:00
|
|
|
item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
|
2008-02-16 12:56:09 +00:00
|
|
|
cache_safe_filename(ctx.repo->url),
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.page,
|
|
|
|
cache_safe_filename(ctx.qry.raw)));
|
|
|
|
if (ctx.qry.has_symref)
|
2008-02-16 12:07:13 +00:00
|
|
|
item->ttl = ctx.cfg.cache_dynamic_ttl;
|
2008-02-16 10:53:40 +00:00
|
|
|
else if (ctx.qry.has_sha1)
|
2008-02-16 12:07:13 +00:00
|
|
|
item->ttl = ctx.cfg.cache_static_ttl;
|
2007-01-11 23:00:15 +00:00
|
|
|
else
|
2008-02-16 12:07:13 +00:00
|
|
|
item->ttl = ctx.cfg.cache_repo_ttl;
|
2007-01-11 23:00:15 +00:00
|
|
|
}
|
2007-02-03 14:02:55 +00:00
|
|
|
return 1;
|
2007-01-11 23:00:15 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 12:43:40 +00:00
|
|
|
struct refmatch {
|
|
|
|
char *req_ref;
|
|
|
|
char *first_ref;
|
|
|
|
int match;
|
|
|
|
};
|
|
|
|
|
|
|
|
int find_current_ref(const char *refname, const unsigned char *sha1,
|
|
|
|
int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct refmatch *info;
|
|
|
|
|
|
|
|
info = (struct refmatch *)cb_data;
|
|
|
|
if (!strcmp(refname, info->req_ref))
|
|
|
|
info->match = 1;
|
|
|
|
if (!info->first_ref)
|
|
|
|
info->first_ref = xstrdup(refname);
|
|
|
|
return info->match;
|
|
|
|
}
|
|
|
|
|
2008-02-16 12:56:09 +00:00
|
|
|
char *find_default_branch(struct cgit_repo *repo)
|
2008-01-04 12:43:40 +00:00
|
|
|
{
|
|
|
|
struct refmatch info;
|
|
|
|
|
|
|
|
info.req_ref = repo->defbranch;
|
|
|
|
info.first_ref = NULL;
|
|
|
|
info.match = 0;
|
|
|
|
for_each_branch_ref(find_current_ref, &info);
|
|
|
|
if (info.match)
|
|
|
|
return info.req_ref;
|
|
|
|
else
|
|
|
|
return info.first_ref;
|
|
|
|
}
|
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
static int prepare_repo_cmd(struct cgit_context *ctx)
|
2006-12-09 14:18:17 +00:00
|
|
|
{
|
2008-03-23 23:51:19 +00:00
|
|
|
char *tmp;
|
2008-01-04 12:43:40 +00:00
|
|
|
unsigned char sha1[20];
|
2008-02-16 20:16:53 +00:00
|
|
|
int nongit = 0;
|
2007-05-15 22:14:51 +00:00
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
setenv("GIT_DIR", ctx->repo->path, 1);
|
2008-02-16 20:16:53 +00:00
|
|
|
setup_git_directory_gently(&nongit);
|
|
|
|
if (nongit) {
|
2008-03-24 00:09:39 +00:00
|
|
|
ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
|
|
|
|
"config error");
|
|
|
|
tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
|
|
|
|
ctx->repo = NULL;
|
|
|
|
cgit_print_http_headers(ctx);
|
|
|
|
cgit_print_docstart(ctx);
|
|
|
|
cgit_print_pageheader(ctx);
|
2008-02-16 20:16:53 +00:00
|
|
|
cgit_print_error(tmp);
|
2006-12-09 14:18:17 +00:00
|
|
|
cgit_print_docend();
|
2008-03-24 00:09:39 +00:00
|
|
|
return 1;
|
2006-12-09 14:18:17 +00:00
|
|
|
}
|
2008-03-24 00:09:39 +00:00
|
|
|
ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
|
2007-02-03 14:02:55 +00:00
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
if (!ctx->qry.head) {
|
|
|
|
ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
|
|
|
|
ctx->repo->defbranch = ctx->qry.head;
|
2008-01-04 12:43:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
if (!ctx->qry.head) {
|
|
|
|
cgit_print_http_headers(ctx);
|
|
|
|
cgit_print_docstart(ctx);
|
|
|
|
cgit_print_pageheader(ctx);
|
2008-01-04 12:43:40 +00:00
|
|
|
cgit_print_error("Repository seems to be empty");
|
|
|
|
cgit_print_docend();
|
2008-03-24 00:09:39 +00:00
|
|
|
return 1;
|
2008-01-04 12:43:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
if (get_sha1(ctx->qry.head, sha1)) {
|
|
|
|
tmp = xstrdup(ctx->qry.head);
|
|
|
|
ctx->qry.head = ctx->repo->defbranch;
|
|
|
|
cgit_print_http_headers(ctx);
|
|
|
|
cgit_print_docstart(ctx);
|
|
|
|
cgit_print_pageheader(ctx);
|
2008-01-04 12:43:40 +00:00
|
|
|
cgit_print_error(fmt("Invalid branch: %s", tmp));
|
|
|
|
cgit_print_docend();
|
2008-03-24 00:09:39 +00:00
|
|
|
return 1;
|
2008-01-04 12:43:40 +00:00
|
|
|
}
|
2008-03-24 00:09:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-01-04 12:43:40 +00:00
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
static void process_request(struct cgit_context *ctx)
|
|
|
|
{
|
|
|
|
struct cgit_cmd *cmd;
|
|
|
|
|
|
|
|
cmd = cgit_get_cmd(ctx);
|
|
|
|
if (!cmd) {
|
|
|
|
ctx->page.title = "cgit error";
|
|
|
|
ctx->repo = NULL;
|
|
|
|
cgit_print_http_headers(ctx);
|
|
|
|
cgit_print_docstart(ctx);
|
|
|
|
cgit_print_pageheader(ctx);
|
|
|
|
cgit_print_error("Invalid request");
|
|
|
|
cgit_print_docend();
|
2007-02-08 12:53:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
if (cmd->want_repo && prepare_repo_cmd(ctx))
|
2007-12-10 20:47:29 +00:00
|
|
|
return;
|
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
if (cmd->want_layout) {
|
|
|
|
cgit_print_http_headers(ctx);
|
|
|
|
cgit_print_docstart(ctx);
|
|
|
|
cgit_print_pageheader(ctx);
|
2007-05-18 01:00:54 +00:00
|
|
|
}
|
2007-02-17 12:46:18 +00:00
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
cmd->fn(ctx);
|
2007-02-17 12:46:18 +00:00
|
|
|
|
2008-03-24 00:09:39 +00:00
|
|
|
if (cmd->want_layout)
|
|
|
|
cgit_print_docend();
|
2006-12-09 14:18:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 23:51:19 +00:00
|
|
|
static long ttl_seconds(long ttl)
|
|
|
|
{
|
|
|
|
if (ttl<0)
|
|
|
|
return 60 * 60 * 24 * 365;
|
|
|
|
else
|
|
|
|
return ttl * 60;
|
|
|
|
}
|
|
|
|
|
2007-02-08 12:53:13 +00:00
|
|
|
static void cgit_fill_cache(struct cacheitem *item, int use_cache)
|
2006-12-09 14:18:17 +00:00
|
|
|
{
|
2007-02-08 12:53:13 +00:00
|
|
|
int stdout2;
|
2006-12-16 12:55:58 +00:00
|
|
|
|
2007-02-08 12:53:13 +00:00
|
|
|
if (use_cache) {
|
2007-05-14 20:58:01 +00:00
|
|
|
stdout2 = chk_positive(dup(STDOUT_FILENO),
|
2007-02-08 12:53:13 +00:00
|
|
|
"Preserving STDOUT");
|
|
|
|
chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
|
|
|
|
chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
|
|
|
|
}
|
|
|
|
|
2008-03-23 23:51:19 +00:00
|
|
|
ctx.page.modified = time(NULL);
|
|
|
|
ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
|
2008-03-24 00:09:39 +00:00
|
|
|
process_request(&ctx);
|
2007-02-08 12:53:13 +00:00
|
|
|
|
|
|
|
if (use_cache) {
|
|
|
|
chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
|
2007-05-14 20:58:01 +00:00
|
|
|
chk_positive(dup2(stdout2, STDOUT_FILENO),
|
2007-02-08 12:53:13 +00:00
|
|
|
"Restoring original STDOUT");
|
|
|
|
chk_zero(close(stdout2), "Closing temporary STDOUT");
|
|
|
|
}
|
2006-12-10 21:31:36 +00:00
|
|
|
}
|
|
|
|
|
2006-12-11 16:25:41 +00:00
|
|
|
static void cgit_check_cache(struct cacheitem *item)
|
2006-12-10 21:31:36 +00:00
|
|
|
{
|
2006-12-11 11:10:12 +00:00
|
|
|
int i = 0;
|
|
|
|
|
2006-12-10 21:31:36 +00:00
|
|
|
top:
|
2008-02-16 12:07:13 +00:00
|
|
|
if (++i > ctx.cfg.max_lock_attempts) {
|
2006-12-11 11:10:12 +00:00
|
|
|
die("cgit_refresh_cache: unable to lock %s: %s",
|
|
|
|
item->name, strerror(errno));
|
|
|
|
}
|
2006-12-11 08:57:58 +00:00
|
|
|
if (!cache_exist(item)) {
|
|
|
|
if (!cache_lock(item)) {
|
2006-12-11 11:10:12 +00:00
|
|
|
sleep(1);
|
2006-12-10 21:31:36 +00:00
|
|
|
goto top;
|
|
|
|
}
|
2006-12-11 21:53:50 +00:00
|
|
|
if (!cache_exist(item)) {
|
2007-02-08 12:53:13 +00:00
|
|
|
cgit_fill_cache(item, 1);
|
2006-12-11 21:53:50 +00:00
|
|
|
cache_unlock(item);
|
|
|
|
} else {
|
|
|
|
cache_cancel_lock(item);
|
|
|
|
}
|
2006-12-11 08:57:58 +00:00
|
|
|
} else if (cache_expired(item) && cache_lock(item)) {
|
2006-12-11 21:53:50 +00:00
|
|
|
if (cache_expired(item)) {
|
2007-02-08 12:53:13 +00:00
|
|
|
cgit_fill_cache(item, 1);
|
2006-12-11 21:53:50 +00:00
|
|
|
cache_unlock(item);
|
|
|
|
} else {
|
|
|
|
cache_cancel_lock(item);
|
|
|
|
}
|
2006-12-10 21:31:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cgit_print_cache(struct cacheitem *item)
|
|
|
|
{
|
|
|
|
static char buf[4096];
|
|
|
|
ssize_t i;
|
|
|
|
|
|
|
|
int fd = open(item->name, O_RDONLY);
|
|
|
|
if (fd<0)
|
|
|
|
die("Unable to open cached file %s", item->name);
|
|
|
|
|
|
|
|
while((i=read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
write(STDOUT_FILENO, buf, i);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2006-12-16 12:33:32 +00:00
|
|
|
static void cgit_parse_args(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (!strncmp(argv[i], "--cache=", 8)) {
|
2008-02-16 12:07:13 +00:00
|
|
|
ctx.cfg.cache_root = xstrdup(argv[i]+8);
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (!strcmp(argv[i], "--nocache")) {
|
2008-02-16 12:07:13 +00:00
|
|
|
ctx.cfg.nocache = 1;
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (!strncmp(argv[i], "--query=", 8)) {
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.raw = xstrdup(argv[i]+8);
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (!strncmp(argv[i], "--repo=", 7)) {
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.repo = xstrdup(argv[i]+7);
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (!strncmp(argv[i], "--page=", 7)) {
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.page = xstrdup(argv[i]+7);
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (!strncmp(argv[i], "--head=", 7)) {
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.head = xstrdup(argv[i]+7);
|
|
|
|
ctx.qry.has_symref = 1;
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (!strncmp(argv[i], "--sha1=", 7)) {
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.sha1 = xstrdup(argv[i]+7);
|
|
|
|
ctx.qry.has_sha1 = 1;
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
if (!strncmp(argv[i], "--ofs=", 6)) {
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.ofs = atoi(argv[i]+6);
|
2006-12-16 12:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-10 21:31:36 +00:00
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
2006-12-11 16:25:41 +00:00
|
|
|
struct cacheitem item;
|
2007-07-02 00:29:12 +00:00
|
|
|
const char *cgit_config_env = getenv("CGIT_CONFIG");
|
2006-12-11 16:25:41 +00:00
|
|
|
|
2008-02-16 12:07:13 +00:00
|
|
|
cgit_prepare_context(&ctx);
|
2007-02-03 14:02:55 +00:00
|
|
|
item.st.st_mtime = time(NULL);
|
|
|
|
cgit_repolist.length = 0;
|
|
|
|
cgit_repolist.count = 0;
|
|
|
|
cgit_repolist.repos = NULL;
|
|
|
|
|
2007-07-02 00:29:12 +00:00
|
|
|
cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
|
|
|
|
cgit_global_config_cb);
|
2007-05-14 22:48:31 +00:00
|
|
|
if (getenv("SCRIPT_NAME"))
|
2008-02-16 12:07:13 +00:00
|
|
|
ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
|
2006-12-16 12:33:32 +00:00
|
|
|
if (getenv("QUERY_STRING"))
|
2008-02-16 10:53:40 +00:00
|
|
|
ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
|
2006-12-16 12:33:32 +00:00
|
|
|
cgit_parse_args(argc, argv);
|
2008-02-16 10:53:40 +00:00
|
|
|
cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
|
2007-02-03 14:02:55 +00:00
|
|
|
if (!cgit_prepare_cache(&item))
|
|
|
|
return 0;
|
2008-02-16 12:07:13 +00:00
|
|
|
if (ctx.cfg.nocache) {
|
2007-02-08 12:53:13 +00:00
|
|
|
cgit_fill_cache(&item, 0);
|
2006-12-16 12:33:32 +00:00
|
|
|
} else {
|
|
|
|
cgit_check_cache(&item);
|
|
|
|
cgit_print_cache(&item);
|
|
|
|
}
|
2006-12-09 14:18:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|