]>
git.cameronkatri.com Git - cgit.git/blob - cgit.c
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 int prepare_repo_cmd(struct cgit_context
*ctx
)
87 unsigned char sha1
[20];
90 setenv("GIT_DIR", ctx
->repo
->path
, 1);
91 setup_git_directory_gently(&nongit
);
93 ctx
->page
.title
= fmt("%s - %s", ctx
->cfg
.root_title
,
95 tmp
= fmt("Not a git repository: '%s'", ctx
->repo
->path
);
97 cgit_print_http_headers(ctx
);
98 cgit_print_docstart(ctx
);
99 cgit_print_pageheader(ctx
);
100 cgit_print_error(tmp
);
104 ctx
->page
.title
= fmt("%s - %s", ctx
->repo
->name
, ctx
->repo
->desc
);
106 if (!ctx
->qry
.head
) {
107 ctx
->qry
.head
= xstrdup(find_default_branch(ctx
->repo
));
108 ctx
->repo
->defbranch
= ctx
->qry
.head
;
111 if (!ctx
->qry
.head
) {
112 cgit_print_http_headers(ctx
);
113 cgit_print_docstart(ctx
);
114 cgit_print_pageheader(ctx
);
115 cgit_print_error("Repository seems to be empty");
120 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
);
126 cgit_print_error(fmt("Invalid branch: %s", tmp
));
133 static void process_request(struct cgit_context
*ctx
)
135 struct cgit_cmd
*cmd
;
137 cmd
= cgit_get_cmd(ctx
);
139 ctx
->page
.title
= "cgit error";
141 cgit_print_http_headers(ctx
);
142 cgit_print_docstart(ctx
);
143 cgit_print_pageheader(ctx
);
144 cgit_print_error("Invalid request");
149 if (cmd
->want_repo
&& prepare_repo_cmd(ctx
))
152 if (cmd
->want_layout
) {
153 cgit_print_http_headers(ctx
);
154 cgit_print_docstart(ctx
);
155 cgit_print_pageheader(ctx
);
160 if (cmd
->want_layout
)
164 static long ttl_seconds(long ttl
)
167 return 60 * 60 * 24 * 365;
172 static void cgit_fill_cache(struct cacheitem
*item
, int use_cache
)
177 stdout2
= chk_positive(dup(STDOUT_FILENO
),
178 "Preserving STDOUT");
179 chk_zero(close(STDOUT_FILENO
), "Closing STDOUT");
180 chk_positive(dup2(item
->fd
, STDOUT_FILENO
), "Dup2(cachefile)");
183 ctx
.page
.modified
= time(NULL
);
184 ctx
.page
.expires
= ctx
.page
.modified
+ ttl_seconds(item
->ttl
);
185 process_request(&ctx
);
188 chk_zero(close(STDOUT_FILENO
), "Close redirected STDOUT");
189 chk_positive(dup2(stdout2
, STDOUT_FILENO
),
190 "Restoring original STDOUT");
191 chk_zero(close(stdout2
), "Closing temporary STDOUT");
195 static void cgit_check_cache(struct cacheitem
*item
)
200 if (++i
> ctx
.cfg
.max_lock_attempts
) {
201 die("cgit_refresh_cache: unable to lock %s: %s",
202 item
->name
, strerror(errno
));
204 if (!cache_exist(item
)) {
205 if (!cache_lock(item
)) {
209 if (!cache_exist(item
)) {
210 cgit_fill_cache(item
, 1);
213 cache_cancel_lock(item
);
215 } else if (cache_expired(item
) && cache_lock(item
)) {
216 if (cache_expired(item
)) {
217 cgit_fill_cache(item
, 1);
220 cache_cancel_lock(item
);
225 static void cgit_print_cache(struct cacheitem
*item
)
227 static char buf
[4096];
230 int fd
= open(item
->name
, O_RDONLY
);
232 die("Unable to open cached file %s", item
->name
);
234 while((i
=read(fd
, buf
, sizeof(buf
))) > 0)
235 write(STDOUT_FILENO
, buf
, i
);
240 static void cgit_parse_args(int argc
, const char **argv
)
244 for (i
= 1; i
< argc
; i
++) {
245 if (!strncmp(argv
[i
], "--cache=", 8)) {
246 ctx
.cfg
.cache_root
= xstrdup(argv
[i
]+8);
248 if (!strcmp(argv
[i
], "--nocache")) {
251 if (!strncmp(argv
[i
], "--query=", 8)) {
252 ctx
.qry
.raw
= xstrdup(argv
[i
]+8);
254 if (!strncmp(argv
[i
], "--repo=", 7)) {
255 ctx
.qry
.repo
= xstrdup(argv
[i
]+7);
257 if (!strncmp(argv
[i
], "--page=", 7)) {
258 ctx
.qry
.page
= xstrdup(argv
[i
]+7);
260 if (!strncmp(argv
[i
], "--head=", 7)) {
261 ctx
.qry
.head
= xstrdup(argv
[i
]+7);
262 ctx
.qry
.has_symref
= 1;
264 if (!strncmp(argv
[i
], "--sha1=", 7)) {
265 ctx
.qry
.sha1
= xstrdup(argv
[i
]+7);
266 ctx
.qry
.has_sha1
= 1;
268 if (!strncmp(argv
[i
], "--ofs=", 6)) {
269 ctx
.qry
.ofs
= atoi(argv
[i
]+6);
274 int main(int argc
, const char **argv
)
276 struct cacheitem item
;
277 const char *cgit_config_env
= getenv("CGIT_CONFIG");
279 cgit_prepare_context(&ctx
);
280 item
.st
.st_mtime
= time(NULL
);
281 cgit_repolist
.length
= 0;
282 cgit_repolist
.count
= 0;
283 cgit_repolist
.repos
= NULL
;
285 cgit_read_config(cgit_config_env
? cgit_config_env
: CGIT_CONFIG
,
286 cgit_global_config_cb
);
287 if (getenv("SCRIPT_NAME"))
288 ctx
.cfg
.script_name
= xstrdup(getenv("SCRIPT_NAME"));
289 if (getenv("QUERY_STRING"))
290 ctx
.qry
.raw
= xstrdup(getenv("QUERY_STRING"));
291 cgit_parse_args(argc
, argv
);
292 cgit_parse_query(ctx
.qry
.raw
, cgit_querystring_cb
);
293 if (!cgit_prepare_cache(&item
))
295 if (ctx
.cfg
.nocache
) {
296 cgit_fill_cache(&item
, 0);
298 cgit_check_cache(&item
);
299 cgit_print_cache(&item
);