]>
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)
11 static int cgit_prepare_cache(struct cacheitem
*item
)
13 if (!cgit_repo
&& ctx
.qry
.repo
) {
14 char *title
= fmt("%s - %s", ctx
.cfg
.root_title
, "Bad request");
15 cgit_print_docstart(title
, item
);
16 cgit_print_pageheader(title
, 0);
17 cgit_print_error(fmt("Unknown repo: %s", ctx
.qry
.repo
));
23 item
->name
= xstrdup(fmt("%s/index.html", ctx
.cfg
.cache_root
));
24 item
->ttl
= ctx
.cfg
.cache_root_ttl
;
29 item
->name
= xstrdup(fmt("%s/%s/index.%s.html", ctx
.cfg
.cache_root
,
30 cache_safe_filename(cgit_repo
->url
),
31 cache_safe_filename(ctx
.qry
.raw
)));
32 item
->ttl
= ctx
.cfg
.cache_repo_ttl
;
34 item
->name
= xstrdup(fmt("%s/%s/%s/%s.html", ctx
.cfg
.cache_root
,
35 cache_safe_filename(cgit_repo
->url
),
37 cache_safe_filename(ctx
.qry
.raw
)));
38 if (ctx
.qry
.has_symref
)
39 item
->ttl
= ctx
.cfg
.cache_dynamic_ttl
;
40 else if (ctx
.qry
.has_sha1
)
41 item
->ttl
= ctx
.cfg
.cache_static_ttl
;
43 item
->ttl
= ctx
.cfg
.cache_repo_ttl
;
54 int find_current_ref(const char *refname
, const unsigned char *sha1
,
55 int flags
, void *cb_data
)
57 struct refmatch
*info
;
59 info
= (struct refmatch
*)cb_data
;
60 if (!strcmp(refname
, info
->req_ref
))
63 info
->first_ref
= xstrdup(refname
);
67 char *find_default_branch(struct repoinfo
*repo
)
71 info
.req_ref
= repo
->defbranch
;
72 info
.first_ref
= NULL
;
74 for_each_branch_ref(find_current_ref
, &info
);
78 return info
.first_ref
;
81 static void cgit_print_repo_page(struct cacheitem
*item
)
85 unsigned char sha1
[20];
87 if (chdir(cgit_repo
->path
)) {
88 title
= fmt("%s - %s", ctx
.cfg
.root_title
, "Bad request");
89 cgit_print_docstart(title
, item
);
90 cgit_print_pageheader(title
, 0);
91 cgit_print_error(fmt("Unable to scan repository: %s",
97 title
= fmt("%s - %s", cgit_repo
->name
, cgit_repo
->desc
);
99 setenv("GIT_DIR", cgit_repo
->path
, 1);
102 ctx
.qry
.head
= xstrdup(find_default_branch(cgit_repo
));
103 cgit_repo
->defbranch
= ctx
.qry
.head
;
107 cgit_print_docstart(title
, item
);
108 cgit_print_pageheader(title
, 0);
109 cgit_print_error("Repository seems to be empty");
114 if (get_sha1(ctx
.qry
.head
, sha1
)) {
115 tmp
= xstrdup(ctx
.qry
.head
);
116 ctx
.qry
.head
= cgit_repo
->defbranch
;
117 cgit_print_docstart(title
, item
);
118 cgit_print_pageheader(title
, 0);
119 cgit_print_error(fmt("Invalid branch: %s", tmp
));
124 if ((cgit_cmd
== CMD_SNAPSHOT
) && cgit_repo
->snapshots
) {
125 cgit_print_snapshot(item
, ctx
.qry
.head
, ctx
.qry
.sha1
,
126 cgit_repobasename(cgit_repo
->url
),
128 cgit_repo
->snapshots
);
132 if (cgit_cmd
== CMD_PATCH
) {
133 cgit_print_patch(ctx
.qry
.sha1
, item
);
137 if (cgit_cmd
== CMD_BLOB
) {
138 cgit_print_blob(item
, ctx
.qry
.sha1
, ctx
.qry
.path
);
142 show_search
= (cgit_cmd
== CMD_LOG
);
143 cgit_print_docstart(title
, item
);
145 cgit_print_pageheader("summary", show_search
);
146 cgit_print_summary();
151 cgit_print_pageheader(ctx
.qry
.page
, show_search
);
155 cgit_print_log(ctx
.qry
.sha1
, ctx
.qry
.ofs
,
156 ctx
.cfg
.max_commit_count
, ctx
.qry
.grep
, ctx
.qry
.search
,
160 cgit_print_tree(ctx
.qry
.sha1
, ctx
.qry
.path
);
163 cgit_print_commit(ctx
.qry
.sha1
);
169 cgit_print_tag(ctx
.qry
.sha1
);
172 cgit_print_diff(ctx
.qry
.sha1
, ctx
.qry
.sha2
, ctx
.qry
.path
);
175 cgit_print_error("Invalid request");
180 static void cgit_fill_cache(struct cacheitem
*item
, int use_cache
)
182 static char buf
[PATH_MAX
];
185 getcwd(buf
, sizeof(buf
));
186 item
->st
.st_mtime
= time(NULL
);
189 stdout2
= chk_positive(dup(STDOUT_FILENO
),
190 "Preserving STDOUT");
191 chk_zero(close(STDOUT_FILENO
), "Closing STDOUT");
192 chk_positive(dup2(item
->fd
, STDOUT_FILENO
), "Dup2(cachefile)");
196 cgit_print_repo_page(item
);
198 cgit_print_repolist(item
);
201 chk_zero(close(STDOUT_FILENO
), "Close redirected STDOUT");
202 chk_positive(dup2(stdout2
, STDOUT_FILENO
),
203 "Restoring original STDOUT");
204 chk_zero(close(stdout2
), "Closing temporary STDOUT");
210 static void cgit_check_cache(struct cacheitem
*item
)
215 if (++i
> ctx
.cfg
.max_lock_attempts
) {
216 die("cgit_refresh_cache: unable to lock %s: %s",
217 item
->name
, strerror(errno
));
219 if (!cache_exist(item
)) {
220 if (!cache_lock(item
)) {
224 if (!cache_exist(item
)) {
225 cgit_fill_cache(item
, 1);
228 cache_cancel_lock(item
);
230 } else if (cache_expired(item
) && cache_lock(item
)) {
231 if (cache_expired(item
)) {
232 cgit_fill_cache(item
, 1);
235 cache_cancel_lock(item
);
240 static void cgit_print_cache(struct cacheitem
*item
)
242 static char buf
[4096];
245 int fd
= open(item
->name
, O_RDONLY
);
247 die("Unable to open cached file %s", item
->name
);
249 while((i
=read(fd
, buf
, sizeof(buf
))) > 0)
250 write(STDOUT_FILENO
, buf
, i
);
255 static void cgit_parse_args(int argc
, const char **argv
)
259 for (i
= 1; i
< argc
; i
++) {
260 if (!strncmp(argv
[i
], "--cache=", 8)) {
261 ctx
.cfg
.cache_root
= xstrdup(argv
[i
]+8);
263 if (!strcmp(argv
[i
], "--nocache")) {
266 if (!strncmp(argv
[i
], "--query=", 8)) {
267 ctx
.qry
.raw
= xstrdup(argv
[i
]+8);
269 if (!strncmp(argv
[i
], "--repo=", 7)) {
270 ctx
.qry
.repo
= xstrdup(argv
[i
]+7);
272 if (!strncmp(argv
[i
], "--page=", 7)) {
273 ctx
.qry
.page
= xstrdup(argv
[i
]+7);
275 if (!strncmp(argv
[i
], "--head=", 7)) {
276 ctx
.qry
.head
= xstrdup(argv
[i
]+7);
277 ctx
.qry
.has_symref
= 1;
279 if (!strncmp(argv
[i
], "--sha1=", 7)) {
280 ctx
.qry
.sha1
= xstrdup(argv
[i
]+7);
281 ctx
.qry
.has_sha1
= 1;
283 if (!strncmp(argv
[i
], "--ofs=", 6)) {
284 ctx
.qry
.ofs
= atoi(argv
[i
]+6);
289 int main(int argc
, const char **argv
)
291 struct cacheitem item
;
292 const char *cgit_config_env
= getenv("CGIT_CONFIG");
294 cgit_prepare_context(&ctx
);
295 htmlfd
= STDOUT_FILENO
;
296 item
.st
.st_mtime
= time(NULL
);
297 cgit_repolist
.length
= 0;
298 cgit_repolist
.count
= 0;
299 cgit_repolist
.repos
= NULL
;
301 cgit_read_config(cgit_config_env
? cgit_config_env
: CGIT_CONFIG
,
302 cgit_global_config_cb
);
304 if (getenv("SCRIPT_NAME"))
305 ctx
.cfg
.script_name
= xstrdup(getenv("SCRIPT_NAME"));
306 if (getenv("QUERY_STRING"))
307 ctx
.qry
.raw
= xstrdup(getenv("QUERY_STRING"));
308 cgit_parse_args(argc
, argv
);
309 cgit_parse_query(ctx
.qry
.raw
, cgit_querystring_cb
);
310 if (!cgit_prepare_cache(&item
))
312 if (ctx
.cfg
.nocache
) {
313 cgit_fill_cache(&item
, 0);
315 cgit_check_cache(&item
);
316 cgit_print_cache(&item
);