]> git.cameronkatri.com Git - cgit.git/blob - cgit.c
Add ui-shared.h
[cgit.git] / cgit.c
1 /* 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
9 #include "cgit.h"
10 #include "cmd.h"
11 #include "ui-shared.h"
12
13 static int cgit_prepare_cache(struct cacheitem *item)
14 {
15 if (!ctx.repo && ctx.qry.repo) {
16 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);
21 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
22 cgit_print_docend();
23 return 0;
24 }
25
26 if (!ctx.repo) {
27 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
28 item->ttl = ctx.cfg.cache_root_ttl;
29 return 1;
30 }
31
32 if (!cgit_cmd) {
33 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
34 cache_safe_filename(ctx.repo->url),
35 cache_safe_filename(ctx.qry.raw)));
36 item->ttl = ctx.cfg.cache_repo_ttl;
37 } else {
38 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
39 cache_safe_filename(ctx.repo->url),
40 ctx.qry.page,
41 cache_safe_filename(ctx.qry.raw)));
42 if (ctx.qry.has_symref)
43 item->ttl = ctx.cfg.cache_dynamic_ttl;
44 else if (ctx.qry.has_sha1)
45 item->ttl = ctx.cfg.cache_static_ttl;
46 else
47 item->ttl = ctx.cfg.cache_repo_ttl;
48 }
49 return 1;
50 }
51
52 struct refmatch {
53 char *req_ref;
54 char *first_ref;
55 int match;
56 };
57
58 int 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
71 char *find_default_branch(struct cgit_repo *repo)
72 {
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
85 static int prepare_repo_cmd(struct cgit_context *ctx)
86 {
87 char *tmp;
88 unsigned char sha1[20];
89 int nongit = 0;
90
91 setenv("GIT_DIR", ctx->repo->path, 1);
92 setup_git_directory_gently(&nongit);
93 if (nongit) {
94 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);
101 cgit_print_error(tmp);
102 cgit_print_docend();
103 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;
110 }
111
112 if (!ctx->qry.head) {
113 cgit_print_http_headers(ctx);
114 cgit_print_docstart(ctx);
115 cgit_print_pageheader(ctx);
116 cgit_print_error("Repository seems to be empty");
117 cgit_print_docend();
118 return 1;
119 }
120
121 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);
127 cgit_print_error(fmt("Invalid branch: %s", tmp));
128 cgit_print_docend();
129 return 1;
130 }
131 return 0;
132 }
133
134 static void process_request(struct cgit_context *ctx)
135 {
136 struct cgit_cmd *cmd;
137
138 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");
146 cgit_print_docend();
147 return;
148 }
149
150 if (cmd->want_repo && prepare_repo_cmd(ctx))
151 return;
152
153 if (cmd->want_layout) {
154 cgit_print_http_headers(ctx);
155 cgit_print_docstart(ctx);
156 cgit_print_pageheader(ctx);
157 }
158
159 cmd->fn(ctx);
160
161 if (cmd->want_layout)
162 cgit_print_docend();
163 }
164
165 static long ttl_seconds(long ttl)
166 {
167 if (ttl<0)
168 return 60 * 60 * 24 * 365;
169 else
170 return ttl * 60;
171 }
172
173 static void cgit_fill_cache(struct cacheitem *item, int use_cache)
174 {
175 int stdout2;
176
177 if (use_cache) {
178 stdout2 = chk_positive(dup(STDOUT_FILENO),
179 "Preserving STDOUT");
180 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
181 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
182 }
183
184 ctx.page.modified = time(NULL);
185 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
186 process_request(&ctx);
187
188 if (use_cache) {
189 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
190 chk_positive(dup2(stdout2, STDOUT_FILENO),
191 "Restoring original STDOUT");
192 chk_zero(close(stdout2), "Closing temporary STDOUT");
193 }
194 }
195
196 static void cgit_check_cache(struct cacheitem *item)
197 {
198 int i = 0;
199
200 top:
201 if (++i > ctx.cfg.max_lock_attempts) {
202 die("cgit_refresh_cache: unable to lock %s: %s",
203 item->name, strerror(errno));
204 }
205 if (!cache_exist(item)) {
206 if (!cache_lock(item)) {
207 sleep(1);
208 goto top;
209 }
210 if (!cache_exist(item)) {
211 cgit_fill_cache(item, 1);
212 cache_unlock(item);
213 } else {
214 cache_cancel_lock(item);
215 }
216 } else if (cache_expired(item) && cache_lock(item)) {
217 if (cache_expired(item)) {
218 cgit_fill_cache(item, 1);
219 cache_unlock(item);
220 } else {
221 cache_cancel_lock(item);
222 }
223 }
224 }
225
226 static 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
241 static void cgit_parse_args(int argc, const char **argv)
242 {
243 int i;
244
245 for (i = 1; i < argc; i++) {
246 if (!strncmp(argv[i], "--cache=", 8)) {
247 ctx.cfg.cache_root = xstrdup(argv[i]+8);
248 }
249 if (!strcmp(argv[i], "--nocache")) {
250 ctx.cfg.nocache = 1;
251 }
252 if (!strncmp(argv[i], "--query=", 8)) {
253 ctx.qry.raw = xstrdup(argv[i]+8);
254 }
255 if (!strncmp(argv[i], "--repo=", 7)) {
256 ctx.qry.repo = xstrdup(argv[i]+7);
257 }
258 if (!strncmp(argv[i], "--page=", 7)) {
259 ctx.qry.page = xstrdup(argv[i]+7);
260 }
261 if (!strncmp(argv[i], "--head=", 7)) {
262 ctx.qry.head = xstrdup(argv[i]+7);
263 ctx.qry.has_symref = 1;
264 }
265 if (!strncmp(argv[i], "--sha1=", 7)) {
266 ctx.qry.sha1 = xstrdup(argv[i]+7);
267 ctx.qry.has_sha1 = 1;
268 }
269 if (!strncmp(argv[i], "--ofs=", 6)) {
270 ctx.qry.ofs = atoi(argv[i]+6);
271 }
272 }
273 }
274
275 int main(int argc, const char **argv)
276 {
277 struct cacheitem item;
278 const char *cgit_config_env = getenv("CGIT_CONFIG");
279
280 cgit_prepare_context(&ctx);
281 item.st.st_mtime = time(NULL);
282 cgit_repolist.length = 0;
283 cgit_repolist.count = 0;
284 cgit_repolist.repos = NULL;
285
286 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
287 cgit_global_config_cb);
288 if (getenv("SCRIPT_NAME"))
289 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
290 if (getenv("QUERY_STRING"))
291 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
292 cgit_parse_args(argc, argv);
293 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
294 if (!cgit_prepare_cache(&item))
295 return 0;
296 if (ctx.cfg.nocache) {
297 cgit_fill_cache(&item, 0);
298 } else {
299 cgit_check_cache(&item);
300 cgit_print_cache(&item);
301 }
302 return 0;
303 }