|
diff --git a/cgit.c b/cgit.c index 9ca93a7..e8acc03 100644 --- a/ cgit.c+++ b/ cgit.c |
|
@@ -1,197 +1,251 @@ |
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 | |
11 | static int cgit_prepare_cache(struct cacheitem *item) |
11 | static int cgit_prepare_cache(struct cacheitem *item) |
12 | { |
12 | { |
13 | if (!cgit_repo && cgit_query_repo) { |
13 | if (!cgit_repo && cgit_query_repo) { |
14 | char *title = fmt("%s - %s", cgit_root_title, "Bad request"); |
14 | char *title = fmt("%s - %s", cgit_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", cgit_query_repo)); |
17 | cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); |
18 | cgit_print_docend(); |
18 | cgit_print_docend(); |
19 | return 0; |
19 | return 0; |
20 | } |
20 | } |
21 | |
21 | |
22 | if (!cgit_repo) { |
22 | if (!cgit_repo) { |
23 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); |
23 | item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); |
24 | item->ttl = cgit_cache_root_ttl; |
24 | item->ttl = cgit_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", cgit_cache_root, |
29 | item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, |
30 | cache_safe_filename(cgit_repo->url), |
30 | cache_safe_filename(cgit_repo->url), |
31 | cache_safe_filename(cgit_querystring))); |
31 | cache_safe_filename(cgit_querystring))); |
32 | item->ttl = cgit_cache_repo_ttl; |
32 | item->ttl = cgit_cache_repo_ttl; |
33 | } else { |
33 | } else { |
34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, |
34 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, |
35 | cache_safe_filename(cgit_repo->url), |
35 | cache_safe_filename(cgit_repo->url), |
36 | cgit_query_page, |
36 | cgit_query_page, |
37 | cache_safe_filename(cgit_querystring))); |
37 | cache_safe_filename(cgit_querystring))); |
38 | if (cgit_query_has_symref) |
38 | if (cgit_query_has_symref) |
39 | item->ttl = cgit_cache_dynamic_ttl; |
39 | item->ttl = cgit_cache_dynamic_ttl; |
40 | else if (cgit_query_has_sha1) |
40 | else if (cgit_query_has_sha1) |
41 | item->ttl = cgit_cache_static_ttl; |
41 | item->ttl = cgit_cache_static_ttl; |
42 | else |
42 | else |
43 | item->ttl = cgit_cache_repo_ttl; |
43 | item->ttl = cgit_cache_repo_ttl; |
44 | } |
44 | } |
45 | return 1; |
45 | return 1; |
46 | } |
46 | } |
47 | |
47 | |
| |
48 | struct refmatch { |
| |
49 | char *req_ref; |
| |
50 | char *first_ref; |
| |
51 | int match; |
| |
52 | }; |
| |
53 | |
| |
54 | int find_current_ref(const char *refname, const unsigned char *sha1, |
| |
55 | int flags, void *cb_data) |
| |
56 | { |
| |
57 | struct refmatch *info; |
| |
58 | |
| |
59 | info = (struct refmatch *)cb_data; |
| |
60 | if (!strcmp(refname, info->req_ref)) |
| |
61 | info->match = 1; |
| |
62 | if (!info->first_ref) |
| |
63 | info->first_ref = xstrdup(refname); |
| |
64 | return info->match; |
| |
65 | } |
| |
66 | |
| |
67 | char *find_default_branch(struct repoinfo *repo) |
| |
68 | { |
| |
69 | struct refmatch info; |
| |
70 | |
| |
71 | info.req_ref = repo->defbranch; |
| |
72 | info.first_ref = NULL; |
| |
73 | info.match = 0; |
| |
74 | for_each_branch_ref(find_current_ref, &info); |
| |
75 | if (info.match) |
| |
76 | return info.req_ref; |
| |
77 | else |
| |
78 | return info.first_ref; |
| |
79 | } |
| |
80 | |
48 | static void cgit_print_repo_page(struct cacheitem *item) |
81 | static void cgit_print_repo_page(struct cacheitem *item) |
49 | { |
82 | { |
50 | char *title; |
83 | char *title, *tmp; |
51 | int show_search; |
84 | int show_search; |
52 | |
85 | unsigned char sha1[20]; |
53 | if (!cgit_query_head) |
| |
54 | cgit_query_head = cgit_repo->defbranch; |
| |
55 | |
86 | |
56 | if (chdir(cgit_repo->path)) { |
87 | if (chdir(cgit_repo->path)) { |
57 | title = fmt("%s - %s", cgit_root_title, "Bad request"); |
88 | title = fmt("%s - %s", cgit_root_title, "Bad request"); |
58 | cgit_print_docstart(title, item); |
89 | cgit_print_docstart(title, item); |
59 | cgit_print_pageheader(title, 0); |
90 | cgit_print_pageheader(title, 0); |
60 | cgit_print_error(fmt("Unable to scan repository: %s", |
91 | cgit_print_error(fmt("Unable to scan repository: %s", |
61 | strerror(errno))); |
92 | strerror(errno))); |
62 | cgit_print_docend(); |
93 | cgit_print_docend(); |
63 | return; |
94 | return; |
64 | } |
95 | } |
65 | |
96 | |
66 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); |
97 | title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); |
67 | show_search = 0; |
98 | show_search = 0; |
68 | setenv("GIT_DIR", cgit_repo->path, 1); |
99 | setenv("GIT_DIR", cgit_repo->path, 1); |
69 | |
100 | |
| |
101 | if (!cgit_query_head) { |
| |
102 | cgit_query_head = xstrdup(find_default_branch(cgit_repo)); |
| |
103 | cgit_repo->defbranch = cgit_query_head; |
| |
104 | } |
| |
105 | |
| |
106 | if (!cgit_query_head) { |
| |
107 | cgit_print_docstart(title, item); |
| |
108 | cgit_print_pageheader(title, 0); |
| |
109 | cgit_print_error("Repository seems to be empty"); |
| |
110 | cgit_print_docend(); |
| |
111 | return; |
| |
112 | } |
| |
113 | |
| |
114 | if (get_sha1(cgit_query_head, sha1)) { |
| |
115 | tmp = xstrdup(cgit_query_head); |
| |
116 | cgit_query_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)); |
| |
120 | cgit_print_docend(); |
| |
121 | return; |
| |
122 | } |
| |
123 | |
70 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { |
124 | if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { |
71 | cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, |
125 | cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, |
72 | cgit_repobasename(cgit_repo->url), |
126 | cgit_repobasename(cgit_repo->url), |
73 | cgit_query_path, |
127 | cgit_query_path, |
74 | cgit_repo->snapshots ); |
128 | cgit_repo->snapshots ); |
75 | return; |
129 | return; |
76 | } |
130 | } |
77 | |
131 | |
78 | if (cgit_cmd == CMD_PATCH) { |
132 | if (cgit_cmd == CMD_PATCH) { |
79 | cgit_print_patch(cgit_query_sha1, item); |
133 | cgit_print_patch(cgit_query_sha1, item); |
80 | return; |
134 | return; |
81 | } |
135 | } |
82 | |
136 | |
83 | if (cgit_cmd == CMD_BLOB) { |
137 | if (cgit_cmd == CMD_BLOB) { |
84 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); |
138 | cgit_print_blob(item, cgit_query_sha1, cgit_query_path); |
85 | return; |
139 | return; |
86 | } |
140 | } |
87 | |
141 | |
88 | show_search = (cgit_cmd == CMD_LOG); |
142 | show_search = (cgit_cmd == CMD_LOG); |
89 | cgit_print_docstart(title, item); |
143 | cgit_print_docstart(title, item); |
90 | if (!cgit_cmd) { |
144 | if (!cgit_cmd) { |
91 | cgit_print_pageheader("summary", show_search); |
145 | cgit_print_pageheader("summary", show_search); |
92 | cgit_print_summary(); |
146 | cgit_print_summary(); |
93 | cgit_print_docend(); |
147 | cgit_print_docend(); |
94 | return; |
148 | return; |
95 | } |
149 | } |
96 | |
150 | |
97 | cgit_print_pageheader(cgit_query_page, show_search); |
151 | cgit_print_pageheader(cgit_query_page, show_search); |
98 | |
152 | |
99 | switch(cgit_cmd) { |
153 | switch(cgit_cmd) { |
100 | case CMD_LOG: |
154 | case CMD_LOG: |
101 | cgit_print_log(cgit_query_sha1, cgit_query_ofs, |
155 | cgit_print_log(cgit_query_sha1, cgit_query_ofs, |
102 | cgit_max_commit_count, cgit_query_grep, cgit_query_search, |
156 | cgit_max_commit_count, cgit_query_grep, cgit_query_search, |
103 | cgit_query_path, 1); |
157 | cgit_query_path, 1); |
104 | break; |
158 | break; |
105 | case CMD_TREE: |
159 | case CMD_TREE: |
106 | cgit_print_tree(cgit_query_sha1, cgit_query_path); |
160 | cgit_print_tree(cgit_query_sha1, cgit_query_path); |
107 | break; |
161 | break; |
108 | case CMD_COMMIT: |
162 | case CMD_COMMIT: |
109 | cgit_print_commit(cgit_query_sha1); |
163 | cgit_print_commit(cgit_query_sha1); |
110 | break; |
164 | break; |
111 | case CMD_REFS: |
165 | case CMD_REFS: |
112 | cgit_print_refs(); |
166 | cgit_print_refs(); |
113 | break; |
167 | break; |
114 | case CMD_TAG: |
168 | case CMD_TAG: |
115 | cgit_print_tag(cgit_query_sha1); |
169 | cgit_print_tag(cgit_query_sha1); |
116 | break; |
170 | break; |
117 | case CMD_DIFF: |
171 | case CMD_DIFF: |
118 | cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path); |
172 | cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path); |
119 | break; |
173 | break; |
120 | default: |
174 | default: |
121 | cgit_print_error("Invalid request"); |
175 | cgit_print_error("Invalid request"); |
122 | } |
176 | } |
123 | cgit_print_docend(); |
177 | cgit_print_docend(); |
124 | } |
178 | } |
125 | |
179 | |
126 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
180 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
127 | { |
181 | { |
128 | static char buf[PATH_MAX]; |
182 | static char buf[PATH_MAX]; |
129 | int stdout2; |
183 | int stdout2; |
130 | |
184 | |
131 | getcwd(buf, sizeof(buf)); |
185 | getcwd(buf, sizeof(buf)); |
132 | item->st.st_mtime = time(NULL); |
186 | item->st.st_mtime = time(NULL); |
133 | |
187 | |
134 | if (use_cache) { |
188 | if (use_cache) { |
135 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
189 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
136 | "Preserving STDOUT"); |
190 | "Preserving STDOUT"); |
137 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
191 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
138 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
192 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
139 | } |
193 | } |
140 | |
194 | |
141 | if (cgit_repo) |
195 | if (cgit_repo) |
142 | cgit_print_repo_page(item); |
196 | cgit_print_repo_page(item); |
143 | else |
197 | else |
144 | cgit_print_repolist(item); |
198 | cgit_print_repolist(item); |
145 | |
199 | |
146 | if (use_cache) { |
200 | if (use_cache) { |
147 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
201 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
148 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
202 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
149 | "Restoring original STDOUT"); |
203 | "Restoring original STDOUT"); |
150 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
204 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
151 | } |
205 | } |
152 | |
206 | |
153 | chdir(buf); |
207 | chdir(buf); |
154 | } |
208 | } |
155 | |
209 | |
156 | static void cgit_check_cache(struct cacheitem *item) |
210 | static void cgit_check_cache(struct cacheitem *item) |
157 | { |
211 | { |
158 | int i = 0; |
212 | int i = 0; |
159 | |
213 | |
160 | top: |
214 | top: |
161 | if (++i > cgit_max_lock_attempts) { |
215 | if (++i > cgit_max_lock_attempts) { |
162 | die("cgit_refresh_cache: unable to lock %s: %s", |
216 | die("cgit_refresh_cache: unable to lock %s: %s", |
163 | item->name, strerror(errno)); |
217 | item->name, strerror(errno)); |
164 | } |
218 | } |
165 | if (!cache_exist(item)) { |
219 | if (!cache_exist(item)) { |
166 | if (!cache_lock(item)) { |
220 | if (!cache_lock(item)) { |
167 | sleep(1); |
221 | sleep(1); |
168 | goto top; |
222 | goto top; |
169 | } |
223 | } |
170 | if (!cache_exist(item)) { |
224 | if (!cache_exist(item)) { |
171 | cgit_fill_cache(item, 1); |
225 | cgit_fill_cache(item, 1); |
172 | cache_unlock(item); |
226 | cache_unlock(item); |
173 | } else { |
227 | } else { |
174 | cache_cancel_lock(item); |
228 | cache_cancel_lock(item); |
175 | } |
229 | } |
176 | } else if (cache_expired(item) && cache_lock(item)) { |
230 | } else if (cache_expired(item) && cache_lock(item)) { |
177 | if (cache_expired(item)) { |
231 | if (cache_expired(item)) { |
178 | cgit_fill_cache(item, 1); |
232 | cgit_fill_cache(item, 1); |
179 | cache_unlock(item); |
233 | cache_unlock(item); |
180 | } else { |
234 | } else { |
181 | cache_cancel_lock(item); |
235 | cache_cancel_lock(item); |
182 | } |
236 | } |
183 | } |
237 | } |
184 | } |
238 | } |
185 | |
239 | |
186 | static void cgit_print_cache(struct cacheitem *item) |
240 | static void cgit_print_cache(struct cacheitem *item) |
187 | { |
241 | { |
188 | static char buf[4096]; |
242 | static char buf[4096]; |
189 | ssize_t i; |
243 | ssize_t i; |
190 | |
244 | |
191 | int fd = open(item->name, O_RDONLY); |
245 | int fd = open(item->name, O_RDONLY); |
192 | if (fd<0) |
246 | if (fd<0) |
193 | die("Unable to open cached file %s", item->name); |
247 | die("Unable to open cached file %s", item->name); |
194 | |
248 | |
195 | while((i=read(fd, buf, sizeof(buf))) > 0) |
249 | while((i=read(fd, buf, sizeof(buf))) > 0) |
196 | write(STDOUT_FILENO, buf, i); |
250 | write(STDOUT_FILENO, buf, i); |
197 | |
251 | |
|