summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c1
1 files changed, 0 insertions, 1 deletions
diff --git a/cgit.c b/cgit.c
index 2c933dc..e6b4526 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,317 +1,316 @@
1/* cgit.c: cgi for the git scm 1/* cgit.c: cgi for the git scm
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11static int cgit_prepare_cache(struct cacheitem *item) 11static int cgit_prepare_cache(struct cacheitem *item)
12{ 12{
13 if (!ctx.repo && ctx.qry.repo) { 13 if (!ctx.repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); 14 char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request");
15 cgit_print_docstart(title, item); 15 cgit_print_docstart(title, item);
16 cgit_print_pageheader(title, 0); 16 cgit_print_pageheader(title, 0);
17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); 17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 18 cgit_print_docend();
19 return 0; 19 return 0;
20 } 20 }
21 21
22 if (!ctx.repo) { 22 if (!ctx.repo) {
23 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); 23 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
24 item->ttl = ctx.cfg.cache_root_ttl; 24 item->ttl = ctx.cfg.cache_root_ttl;
25 return 1; 25 return 1;
26 } 26 }
27 27
28 if (!cgit_cmd) { 28 if (!cgit_cmd) {
29 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, 29 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
30 cache_safe_filename(ctx.repo->url), 30 cache_safe_filename(ctx.repo->url),
31 cache_safe_filename(ctx.qry.raw))); 31 cache_safe_filename(ctx.qry.raw)));
32 item->ttl = ctx.cfg.cache_repo_ttl; 32 item->ttl = ctx.cfg.cache_repo_ttl;
33 } else { 33 } else {
34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, 34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
35 cache_safe_filename(ctx.repo->url), 35 cache_safe_filename(ctx.repo->url),
36 ctx.qry.page, 36 ctx.qry.page,
37 cache_safe_filename(ctx.qry.raw))); 37 cache_safe_filename(ctx.qry.raw)));
38 if (ctx.qry.has_symref) 38 if (ctx.qry.has_symref)
39 item->ttl = ctx.cfg.cache_dynamic_ttl; 39 item->ttl = ctx.cfg.cache_dynamic_ttl;
40 else if (ctx.qry.has_sha1) 40 else if (ctx.qry.has_sha1)
41 item->ttl = ctx.cfg.cache_static_ttl; 41 item->ttl = ctx.cfg.cache_static_ttl;
42 else 42 else
43 item->ttl = ctx.cfg.cache_repo_ttl; 43 item->ttl = ctx.cfg.cache_repo_ttl;
44 } 44 }
45 return 1; 45 return 1;
46} 46}
47 47
48struct refmatch { 48struct refmatch {
49 char *req_ref; 49 char *req_ref;
50 char *first_ref; 50 char *first_ref;
51 int match; 51 int match;
52}; 52};
53 53
54int find_current_ref(const char *refname, const unsigned char *sha1, 54int find_current_ref(const char *refname, const unsigned char *sha1,
55 int flags, void *cb_data) 55 int flags, void *cb_data)
56{ 56{
57 struct refmatch *info; 57 struct refmatch *info;
58 58
59 info = (struct refmatch *)cb_data; 59 info = (struct refmatch *)cb_data;
60 if (!strcmp(refname, info->req_ref)) 60 if (!strcmp(refname, info->req_ref))
61 info->match = 1; 61 info->match = 1;
62 if (!info->first_ref) 62 if (!info->first_ref)
63 info->first_ref = xstrdup(refname); 63 info->first_ref = xstrdup(refname);
64 return info->match; 64 return info->match;
65} 65}
66 66
67char *find_default_branch(struct cgit_repo *repo) 67char *find_default_branch(struct cgit_repo *repo)
68{ 68{
69 struct refmatch info; 69 struct refmatch info;
70 70
71 info.req_ref = repo->defbranch; 71 info.req_ref = repo->defbranch;
72 info.first_ref = NULL; 72 info.first_ref = NULL;
73 info.match = 0; 73 info.match = 0;
74 for_each_branch_ref(find_current_ref, &info); 74 for_each_branch_ref(find_current_ref, &info);
75 if (info.match) 75 if (info.match)
76 return info.req_ref; 76 return info.req_ref;
77 else 77 else
78 return info.first_ref; 78 return info.first_ref;
79} 79}
80 80
81static void cgit_print_repo_page(struct cacheitem *item) 81static void cgit_print_repo_page(struct cacheitem *item)
82{ 82{
83 char *title, *tmp; 83 char *title, *tmp;
84 int show_search; 84 int show_search;
85 unsigned char sha1[20]; 85 unsigned char sha1[20];
86 int nongit = 0; 86 int nongit = 0;
87 87
88 setenv("GIT_DIR", ctx.repo->path, 1); 88 setenv("GIT_DIR", ctx.repo->path, 1);
89 setup_git_directory_gently(&nongit); 89 setup_git_directory_gently(&nongit);
90 if (nongit) { 90 if (nongit) {
91 title = fmt("%s - %s", ctx.cfg.root_title, "config error"); 91 title = fmt("%s - %s", ctx.cfg.root_title, "config error");
92 tmp = fmt("Not a git repository: '%s'", ctx.repo->path); 92 tmp = fmt("Not a git repository: '%s'", ctx.repo->path);
93 ctx.repo = NULL; 93 ctx.repo = NULL;
94 cgit_print_docstart(title, item); 94 cgit_print_docstart(title, item);
95 cgit_print_pageheader(title, 0); 95 cgit_print_pageheader(title, 0);
96 cgit_print_error(tmp); 96 cgit_print_error(tmp);
97 cgit_print_docend(); 97 cgit_print_docend();
98 return; 98 return;
99 } 99 }
100 100
101 title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); 101 title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc);
102 show_search = 0; 102 show_search = 0;
103 103
104 if (!ctx.qry.head) { 104 if (!ctx.qry.head) {
105 ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); 105 ctx.qry.head = xstrdup(find_default_branch(ctx.repo));
106 ctx.repo->defbranch = ctx.qry.head; 106 ctx.repo->defbranch = ctx.qry.head;
107 } 107 }
108 108
109 if (!ctx.qry.head) { 109 if (!ctx.qry.head) {
110 cgit_print_docstart(title, item); 110 cgit_print_docstart(title, item);
111 cgit_print_pageheader(title, 0); 111 cgit_print_pageheader(title, 0);
112 cgit_print_error("Repository seems to be empty"); 112 cgit_print_error("Repository seems to be empty");
113 cgit_print_docend(); 113 cgit_print_docend();
114 return; 114 return;
115 } 115 }
116 116
117 if (get_sha1(ctx.qry.head, sha1)) { 117 if (get_sha1(ctx.qry.head, sha1)) {
118 tmp = xstrdup(ctx.qry.head); 118 tmp = xstrdup(ctx.qry.head);
119 ctx.qry.head = ctx.repo->defbranch; 119 ctx.qry.head = ctx.repo->defbranch;
120 cgit_print_docstart(title, item); 120 cgit_print_docstart(title, item);
121 cgit_print_pageheader(title, 0); 121 cgit_print_pageheader(title, 0);
122 cgit_print_error(fmt("Invalid branch: %s", tmp)); 122 cgit_print_error(fmt("Invalid branch: %s", tmp));
123 cgit_print_docend(); 123 cgit_print_docend();
124 return; 124 return;
125 } 125 }
126 126
127 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { 127 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) {
128 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, 128 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1,
129 cgit_repobasename(ctx.repo->url), 129 cgit_repobasename(ctx.repo->url),
130 ctx.qry.path, 130 ctx.qry.path,
131 ctx.repo->snapshots ); 131 ctx.repo->snapshots );
132 return; 132 return;
133 } 133 }
134 134
135 if (cgit_cmd == CMD_PATCH) { 135 if (cgit_cmd == CMD_PATCH) {
136 cgit_print_patch(ctx.qry.sha1, item); 136 cgit_print_patch(ctx.qry.sha1, item);
137 return; 137 return;
138 } 138 }
139 139
140 if (cgit_cmd == CMD_BLOB) { 140 if (cgit_cmd == CMD_BLOB) {
141 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); 141 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path);
142 return; 142 return;
143 } 143 }
144 144
145 show_search = (cgit_cmd == CMD_LOG); 145 show_search = (cgit_cmd == CMD_LOG);
146 cgit_print_docstart(title, item); 146 cgit_print_docstart(title, item);
147 if (!cgit_cmd) { 147 if (!cgit_cmd) {
148 cgit_print_pageheader("summary", show_search); 148 cgit_print_pageheader("summary", show_search);
149 cgit_print_summary(); 149 cgit_print_summary();
150 cgit_print_docend(); 150 cgit_print_docend();
151 return; 151 return;
152 } 152 }
153 153
154 cgit_print_pageheader(ctx.qry.page, show_search); 154 cgit_print_pageheader(ctx.qry.page, show_search);
155 155
156 switch(cgit_cmd) { 156 switch(cgit_cmd) {
157 case CMD_LOG: 157 case CMD_LOG:
158 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, 158 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs,
159 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, 159 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search,
160 ctx.qry.path, 1); 160 ctx.qry.path, 1);
161 break; 161 break;
162 case CMD_TREE: 162 case CMD_TREE:
163 cgit_print_tree(ctx.qry.sha1, ctx.qry.path); 163 cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
164 break; 164 break;
165 case CMD_COMMIT: 165 case CMD_COMMIT:
166 cgit_print_commit(ctx.qry.sha1); 166 cgit_print_commit(ctx.qry.sha1);
167 break; 167 break;
168 case CMD_REFS: 168 case CMD_REFS:
169 cgit_print_refs(); 169 cgit_print_refs();
170 break; 170 break;
171 case CMD_TAG: 171 case CMD_TAG:
172 cgit_print_tag(ctx.qry.sha1); 172 cgit_print_tag(ctx.qry.sha1);
173 break; 173 break;
174 case CMD_DIFF: 174 case CMD_DIFF:
175 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); 175 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path);
176 break; 176 break;
177 default: 177 default:
178 cgit_print_error("Invalid request"); 178 cgit_print_error("Invalid request");
179 } 179 }
180 cgit_print_docend(); 180 cgit_print_docend();
181} 181}
182 182
183static void cgit_fill_cache(struct cacheitem *item, int use_cache) 183static void cgit_fill_cache(struct cacheitem *item, int use_cache)
184{ 184{
185 int stdout2; 185 int stdout2;
186 186
187 item->st.st_mtime = time(NULL); 187 item->st.st_mtime = time(NULL);
188 188
189 if (use_cache) { 189 if (use_cache) {
190 stdout2 = chk_positive(dup(STDOUT_FILENO), 190 stdout2 = chk_positive(dup(STDOUT_FILENO),
191 "Preserving STDOUT"); 191 "Preserving STDOUT");
192 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 192 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
193 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 193 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
194 } 194 }
195 195
196 if (ctx.repo) 196 if (ctx.repo)
197 cgit_print_repo_page(item); 197 cgit_print_repo_page(item);
198 else 198 else
199 cgit_print_repolist(item); 199 cgit_print_repolist(item);
200 200
201 if (use_cache) { 201 if (use_cache) {
202 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 202 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
203 chk_positive(dup2(stdout2, STDOUT_FILENO), 203 chk_positive(dup2(stdout2, STDOUT_FILENO),
204 "Restoring original STDOUT"); 204 "Restoring original STDOUT");
205 chk_zero(close(stdout2), "Closing temporary STDOUT"); 205 chk_zero(close(stdout2), "Closing temporary STDOUT");
206 } 206 }
207} 207}
208 208
209static void cgit_check_cache(struct cacheitem *item) 209static void cgit_check_cache(struct cacheitem *item)
210{ 210{
211 int i = 0; 211 int i = 0;
212 212
213 top: 213 top:
214 if (++i > ctx.cfg.max_lock_attempts) { 214 if (++i > ctx.cfg.max_lock_attempts) {
215 die("cgit_refresh_cache: unable to lock %s: %s", 215 die("cgit_refresh_cache: unable to lock %s: %s",
216 item->name, strerror(errno)); 216 item->name, strerror(errno));
217 } 217 }
218 if (!cache_exist(item)) { 218 if (!cache_exist(item)) {
219 if (!cache_lock(item)) { 219 if (!cache_lock(item)) {
220 sleep(1); 220 sleep(1);
221 goto top; 221 goto top;
222 } 222 }
223 if (!cache_exist(item)) { 223 if (!cache_exist(item)) {
224 cgit_fill_cache(item, 1); 224 cgit_fill_cache(item, 1);
225 cache_unlock(item); 225 cache_unlock(item);
226 } else { 226 } else {
227 cache_cancel_lock(item); 227 cache_cancel_lock(item);
228 } 228 }
229 } else if (cache_expired(item) && cache_lock(item)) { 229 } else if (cache_expired(item) && cache_lock(item)) {
230 if (cache_expired(item)) { 230 if (cache_expired(item)) {
231 cgit_fill_cache(item, 1); 231 cgit_fill_cache(item, 1);
232 cache_unlock(item); 232 cache_unlock(item);
233 } else { 233 } else {
234 cache_cancel_lock(item); 234 cache_cancel_lock(item);
235 } 235 }
236 } 236 }
237} 237}
238 238
239static void cgit_print_cache(struct cacheitem *item) 239static void cgit_print_cache(struct cacheitem *item)
240{ 240{
241 static char buf[4096]; 241 static char buf[4096];
242 ssize_t i; 242 ssize_t i;
243 243
244 int fd = open(item->name, O_RDONLY); 244 int fd = open(item->name, O_RDONLY);
245 if (fd<0) 245 if (fd<0)
246 die("Unable to open cached file %s", item->name); 246 die("Unable to open cached file %s", item->name);
247 247
248 while((i=read(fd, buf, sizeof(buf))) > 0) 248 while((i=read(fd, buf, sizeof(buf))) > 0)
249 write(STDOUT_FILENO, buf, i); 249 write(STDOUT_FILENO, buf, i);
250 250
251 close(fd); 251 close(fd);
252} 252}
253 253
254static void cgit_parse_args(int argc, const char **argv) 254static void cgit_parse_args(int argc, const char **argv)
255{ 255{
256 int i; 256 int i;
257 257
258 for (i = 1; i < argc; i++) { 258 for (i = 1; i < argc; i++) {
259 if (!strncmp(argv[i], "--cache=", 8)) { 259 if (!strncmp(argv[i], "--cache=", 8)) {
260 ctx.cfg.cache_root = xstrdup(argv[i]+8); 260 ctx.cfg.cache_root = xstrdup(argv[i]+8);
261 } 261 }
262 if (!strcmp(argv[i], "--nocache")) { 262 if (!strcmp(argv[i], "--nocache")) {
263 ctx.cfg.nocache = 1; 263 ctx.cfg.nocache = 1;
264 } 264 }
265 if (!strncmp(argv[i], "--query=", 8)) { 265 if (!strncmp(argv[i], "--query=", 8)) {
266 ctx.qry.raw = xstrdup(argv[i]+8); 266 ctx.qry.raw = xstrdup(argv[i]+8);
267 } 267 }
268 if (!strncmp(argv[i], "--repo=", 7)) { 268 if (!strncmp(argv[i], "--repo=", 7)) {
269 ctx.qry.repo = xstrdup(argv[i]+7); 269 ctx.qry.repo = xstrdup(argv[i]+7);
270 } 270 }
271 if (!strncmp(argv[i], "--page=", 7)) { 271 if (!strncmp(argv[i], "--page=", 7)) {
272 ctx.qry.page = xstrdup(argv[i]+7); 272 ctx.qry.page = xstrdup(argv[i]+7);
273 } 273 }
274 if (!strncmp(argv[i], "--head=", 7)) { 274 if (!strncmp(argv[i], "--head=", 7)) {
275 ctx.qry.head = xstrdup(argv[i]+7); 275 ctx.qry.head = xstrdup(argv[i]+7);
276 ctx.qry.has_symref = 1; 276 ctx.qry.has_symref = 1;
277 } 277 }
278 if (!strncmp(argv[i], "--sha1=", 7)) { 278 if (!strncmp(argv[i], "--sha1=", 7)) {
279 ctx.qry.sha1 = xstrdup(argv[i]+7); 279 ctx.qry.sha1 = xstrdup(argv[i]+7);
280 ctx.qry.has_sha1 = 1; 280 ctx.qry.has_sha1 = 1;
281 } 281 }
282 if (!strncmp(argv[i], "--ofs=", 6)) { 282 if (!strncmp(argv[i], "--ofs=", 6)) {
283 ctx.qry.ofs = atoi(argv[i]+6); 283 ctx.qry.ofs = atoi(argv[i]+6);
284 } 284 }
285 } 285 }
286} 286}
287 287
288int main(int argc, const char **argv) 288int main(int argc, const char **argv)
289{ 289{
290 struct cacheitem item; 290 struct cacheitem item;
291 const char *cgit_config_env = getenv("CGIT_CONFIG"); 291 const char *cgit_config_env = getenv("CGIT_CONFIG");
292 292
293 cgit_prepare_context(&ctx); 293 cgit_prepare_context(&ctx);
294 htmlfd = STDOUT_FILENO;
295 item.st.st_mtime = time(NULL); 294 item.st.st_mtime = time(NULL);
296 cgit_repolist.length = 0; 295 cgit_repolist.length = 0;
297 cgit_repolist.count = 0; 296 cgit_repolist.count = 0;
298 cgit_repolist.repos = NULL; 297 cgit_repolist.repos = NULL;
299 298
300 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 299 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
301 cgit_global_config_cb); 300 cgit_global_config_cb);
302 if (getenv("SCRIPT_NAME")) 301 if (getenv("SCRIPT_NAME"))
303 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 302 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
304 if (getenv("QUERY_STRING")) 303 if (getenv("QUERY_STRING"))
305 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 304 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
306 cgit_parse_args(argc, argv); 305 cgit_parse_args(argc, argv);
307 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); 306 cgit_parse_query(ctx.qry.raw, cgit_querystring_cb);
308 if (!cgit_prepare_cache(&item)) 307 if (!cgit_prepare_cache(&item))
309 return 0; 308 return 0;
310 if (ctx.cfg.nocache) { 309 if (ctx.cfg.nocache) {
311 cgit_fill_cache(&item, 0); 310 cgit_fill_cache(&item, 0);
312 } else { 311 } else {
313 cgit_check_cache(&item); 312 cgit_check_cache(&item);
314 cgit_print_cache(&item); 313 cgit_print_cache(&item);
315 } 314 }
316 return 0; 315 return 0;
317} 316}