summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c50
-rw-r--r--cgit.h18
-rw-r--r--shared.c3
-rw-r--r--ui-blob.c4
-rw-r--r--ui-patch.c4
-rw-r--r--ui-repolist.c6
-rw-r--r--ui-shared.c112
-rw-r--r--ui-snapshot.c4
8 files changed, 113 insertions, 88 deletions
diff --git a/cgit.c b/cgit.c
index e6b4526..d0f6905 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,291 +1,307 @@
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#include "cmd.h"
10 11
11static int cgit_prepare_cache(struct cacheitem *item) 12static int cgit_prepare_cache(struct cacheitem *item)
12{ 13{
13 if (!ctx.repo && ctx.qry.repo) { 14 if (!ctx.repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", ctx.cfg.root_title, "Bad request"); 15 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
15 cgit_print_docstart(title, item); 16 "Bad request");
16 cgit_print_pageheader(title, 0); 17 cgit_print_http_headers(&ctx);
18 cgit_print_docstart(&ctx);
19 cgit_print_pageheader(&ctx);
17 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); 20 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 21 cgit_print_docend();
19 return 0; 22 return 0;
20 } 23 }
21 24
22 if (!ctx.repo) { 25 if (!ctx.repo) {
23 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); 26 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
24 item->ttl = ctx.cfg.cache_root_ttl; 27 item->ttl = ctx.cfg.cache_root_ttl;
25 return 1; 28 return 1;
26 } 29 }
27 30
28 if (!cgit_cmd) { 31 if (!cgit_cmd) {
29 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, 32 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
30 cache_safe_filename(ctx.repo->url), 33 cache_safe_filename(ctx.repo->url),
31 cache_safe_filename(ctx.qry.raw))); 34 cache_safe_filename(ctx.qry.raw)));
32 item->ttl = ctx.cfg.cache_repo_ttl; 35 item->ttl = ctx.cfg.cache_repo_ttl;
33 } else { 36 } else {
34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, 37 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
35 cache_safe_filename(ctx.repo->url), 38 cache_safe_filename(ctx.repo->url),
36 ctx.qry.page, 39 ctx.qry.page,
37 cache_safe_filename(ctx.qry.raw))); 40 cache_safe_filename(ctx.qry.raw)));
38 if (ctx.qry.has_symref) 41 if (ctx.qry.has_symref)
39 item->ttl = ctx.cfg.cache_dynamic_ttl; 42 item->ttl = ctx.cfg.cache_dynamic_ttl;
40 else if (ctx.qry.has_sha1) 43 else if (ctx.qry.has_sha1)
41 item->ttl = ctx.cfg.cache_static_ttl; 44 item->ttl = ctx.cfg.cache_static_ttl;
42 else 45 else
43 item->ttl = ctx.cfg.cache_repo_ttl; 46 item->ttl = ctx.cfg.cache_repo_ttl;
44 } 47 }
45 return 1; 48 return 1;
46} 49}
47 50
48struct refmatch { 51struct refmatch {
49 char *req_ref; 52 char *req_ref;
50 char *first_ref; 53 char *first_ref;
51 int match; 54 int match;
52}; 55};
53 56
54int find_current_ref(const char *refname, const unsigned char *sha1, 57int find_current_ref(const char *refname, const unsigned char *sha1,
55 int flags, void *cb_data) 58 int flags, void *cb_data)
56{ 59{
57 struct refmatch *info; 60 struct refmatch *info;
58 61
59 info = (struct refmatch *)cb_data; 62 info = (struct refmatch *)cb_data;
60 if (!strcmp(refname, info->req_ref)) 63 if (!strcmp(refname, info->req_ref))
61 info->match = 1; 64 info->match = 1;
62 if (!info->first_ref) 65 if (!info->first_ref)
63 info->first_ref = xstrdup(refname); 66 info->first_ref = xstrdup(refname);
64 return info->match; 67 return info->match;
65} 68}
66 69
67char *find_default_branch(struct cgit_repo *repo) 70char *find_default_branch(struct cgit_repo *repo)
68{ 71{
69 struct refmatch info; 72 struct refmatch info;
70 73
71 info.req_ref = repo->defbranch; 74 info.req_ref = repo->defbranch;
72 info.first_ref = NULL; 75 info.first_ref = NULL;
73 info.match = 0; 76 info.match = 0;
74 for_each_branch_ref(find_current_ref, &info); 77 for_each_branch_ref(find_current_ref, &info);
75 if (info.match) 78 if (info.match)
76 return info.req_ref; 79 return info.req_ref;
77 else 80 else
78 return info.first_ref; 81 return info.first_ref;
79} 82}
80 83
81static void cgit_print_repo_page(struct cacheitem *item) 84static void cgit_print_repo_page(struct cacheitem *item)
82{ 85{
83 char *title, *tmp; 86 char *tmp;
84 int show_search; 87 int show_search;
85 unsigned char sha1[20]; 88 unsigned char sha1[20];
86 int nongit = 0; 89 int nongit = 0;
87 90
88 setenv("GIT_DIR", ctx.repo->path, 1); 91 setenv("GIT_DIR", ctx.repo->path, 1);
89 setup_git_directory_gently(&nongit); 92 setup_git_directory_gently(&nongit);
90 if (nongit) { 93 if (nongit) {
91 title = fmt("%s - %s", ctx.cfg.root_title, "config error"); 94 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
95 "config error");
92 tmp = fmt("Not a git repository: '%s'", ctx.repo->path); 96 tmp = fmt("Not a git repository: '%s'", ctx.repo->path);
93 ctx.repo = NULL; 97 ctx.repo = NULL;
94 cgit_print_docstart(title, item); 98 cgit_print_http_headers(&ctx);
95 cgit_print_pageheader(title, 0); 99 cgit_print_docstart(&ctx);
100 cgit_print_pageheader(&ctx);
96 cgit_print_error(tmp); 101 cgit_print_error(tmp);
97 cgit_print_docend(); 102 cgit_print_docend();
98 return; 103 return;
99 } 104 }
100 105
101 title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc); 106 ctx.page.title = fmt("%s - %s", ctx.repo->name, ctx.repo->desc);
102 show_search = 0; 107 show_search = 0;
103 108
104 if (!ctx.qry.head) { 109 if (!ctx.qry.head) {
105 ctx.qry.head = xstrdup(find_default_branch(ctx.repo)); 110 ctx.qry.head = xstrdup(find_default_branch(ctx.repo));
106 ctx.repo->defbranch = ctx.qry.head; 111 ctx.repo->defbranch = ctx.qry.head;
107 } 112 }
108 113
109 if (!ctx.qry.head) { 114 if (!ctx.qry.head) {
110 cgit_print_docstart(title, item); 115 cgit_print_http_headers(&ctx);
111 cgit_print_pageheader(title, 0); 116 cgit_print_docstart(&ctx);
117 cgit_print_pageheader(&ctx);
112 cgit_print_error("Repository seems to be empty"); 118 cgit_print_error("Repository seems to be empty");
113 cgit_print_docend(); 119 cgit_print_docend();
114 return; 120 return;
115 } 121 }
116 122
117 if (get_sha1(ctx.qry.head, sha1)) { 123 if (get_sha1(ctx.qry.head, sha1)) {
118 tmp = xstrdup(ctx.qry.head); 124 tmp = xstrdup(ctx.qry.head);
119 ctx.qry.head = ctx.repo->defbranch; 125 ctx.qry.head = ctx.repo->defbranch;
120 cgit_print_docstart(title, item); 126 cgit_print_http_headers(&ctx);
121 cgit_print_pageheader(title, 0); 127 cgit_print_docstart(&ctx);
128 cgit_print_pageheader(&ctx);
122 cgit_print_error(fmt("Invalid branch: %s", tmp)); 129 cgit_print_error(fmt("Invalid branch: %s", tmp));
123 cgit_print_docend(); 130 cgit_print_docend();
124 return; 131 return;
125 } 132 }
126 133
127 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) { 134 if ((cgit_cmd == CMD_SNAPSHOT) && ctx.repo->snapshots) {
128 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1, 135 cgit_print_snapshot(item, ctx.qry.head, ctx.qry.sha1,
129 cgit_repobasename(ctx.repo->url), 136 cgit_repobasename(ctx.repo->url),
130 ctx.qry.path, 137 ctx.qry.path,
131 ctx.repo->snapshots ); 138 ctx.repo->snapshots );
132 return; 139 return;
133 } 140 }
134 141
135 if (cgit_cmd == CMD_PATCH) { 142 if (cgit_cmd == CMD_PATCH) {
136 cgit_print_patch(ctx.qry.sha1, item); 143 cgit_print_patch(ctx.qry.sha1, item);
137 return; 144 return;
138 } 145 }
139 146
140 if (cgit_cmd == CMD_BLOB) { 147 if (cgit_cmd == CMD_BLOB) {
141 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path); 148 cgit_print_blob(item, ctx.qry.sha1, ctx.qry.path);
142 return; 149 return;
143 } 150 }
144 151
145 show_search = (cgit_cmd == CMD_LOG); 152 show_search = (cgit_cmd == CMD_LOG);
146 cgit_print_docstart(title, item); 153 cgit_print_http_headers(&ctx);
154 cgit_print_docstart(&ctx);
147 if (!cgit_cmd) { 155 if (!cgit_cmd) {
148 cgit_print_pageheader("summary", show_search); 156 cgit_print_pageheader(&ctx);
149 cgit_print_summary(); 157 cgit_print_summary();
150 cgit_print_docend(); 158 cgit_print_docend();
151 return; 159 return;
152 } 160 }
153 161
154 cgit_print_pageheader(ctx.qry.page, show_search); 162 cgit_print_pageheader(&ctx);
155 163
156 switch(cgit_cmd) { 164 switch(cgit_cmd) {
157 case CMD_LOG: 165 case CMD_LOG:
158 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, 166 cgit_print_log(ctx.qry.sha1, ctx.qry.ofs,
159 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search, 167 ctx.cfg.max_commit_count, ctx.qry.grep, ctx.qry.search,
160 ctx.qry.path, 1); 168 ctx.qry.path, 1);
161 break; 169 break;
162 case CMD_TREE: 170 case CMD_TREE:
163 cgit_print_tree(ctx.qry.sha1, ctx.qry.path); 171 cgit_print_tree(ctx.qry.sha1, ctx.qry.path);
164 break; 172 break;
165 case CMD_COMMIT: 173 case CMD_COMMIT:
166 cgit_print_commit(ctx.qry.sha1); 174 cgit_print_commit(ctx.qry.sha1);
167 break; 175 break;
168 case CMD_REFS: 176 case CMD_REFS:
169 cgit_print_refs(); 177 cgit_print_refs();
170 break; 178 break;
171 case CMD_TAG: 179 case CMD_TAG:
172 cgit_print_tag(ctx.qry.sha1); 180 cgit_print_tag(ctx.qry.sha1);
173 break; 181 break;
174 case CMD_DIFF: 182 case CMD_DIFF:
175 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); 183 cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path);
176 break; 184 break;
177 default: 185 default:
178 cgit_print_error("Invalid request"); 186 cgit_print_error("Invalid request");
179 } 187 }
180 cgit_print_docend(); 188 cgit_print_docend();
181} 189}
182 190
191static long ttl_seconds(long ttl)
192{
193 if (ttl<0)
194 return 60 * 60 * 24 * 365;
195 else
196 return ttl * 60;
197}
198
183static void cgit_fill_cache(struct cacheitem *item, int use_cache) 199static void cgit_fill_cache(struct cacheitem *item, int use_cache)
184{ 200{
185 int stdout2; 201 int stdout2;
186 202
187 item->st.st_mtime = time(NULL);
188
189 if (use_cache) { 203 if (use_cache) {
190 stdout2 = chk_positive(dup(STDOUT_FILENO), 204 stdout2 = chk_positive(dup(STDOUT_FILENO),
191 "Preserving STDOUT"); 205 "Preserving STDOUT");
192 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 206 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
193 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 207 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
194 } 208 }
195 209
210 ctx.page.modified = time(NULL);
211 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
196 if (ctx.repo) 212 if (ctx.repo)
197 cgit_print_repo_page(item); 213 cgit_print_repo_page(item);
198 else 214 else
199 cgit_print_repolist(item); 215 cgit_print_repolist(item);
200 216
201 if (use_cache) { 217 if (use_cache) {
202 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 218 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
203 chk_positive(dup2(stdout2, STDOUT_FILENO), 219 chk_positive(dup2(stdout2, STDOUT_FILENO),
204 "Restoring original STDOUT"); 220 "Restoring original STDOUT");
205 chk_zero(close(stdout2), "Closing temporary STDOUT"); 221 chk_zero(close(stdout2), "Closing temporary STDOUT");
206 } 222 }
207} 223}
208 224
209static void cgit_check_cache(struct cacheitem *item) 225static void cgit_check_cache(struct cacheitem *item)
210{ 226{
211 int i = 0; 227 int i = 0;
212 228
213 top: 229 top:
214 if (++i > ctx.cfg.max_lock_attempts) { 230 if (++i > ctx.cfg.max_lock_attempts) {
215 die("cgit_refresh_cache: unable to lock %s: %s", 231 die("cgit_refresh_cache: unable to lock %s: %s",
216 item->name, strerror(errno)); 232 item->name, strerror(errno));
217 } 233 }
218 if (!cache_exist(item)) { 234 if (!cache_exist(item)) {
219 if (!cache_lock(item)) { 235 if (!cache_lock(item)) {
220 sleep(1); 236 sleep(1);
221 goto top; 237 goto top;
222 } 238 }
223 if (!cache_exist(item)) { 239 if (!cache_exist(item)) {
224 cgit_fill_cache(item, 1); 240 cgit_fill_cache(item, 1);
225 cache_unlock(item); 241 cache_unlock(item);
226 } else { 242 } else {
227 cache_cancel_lock(item); 243 cache_cancel_lock(item);
228 } 244 }
229 } else if (cache_expired(item) && cache_lock(item)) { 245 } else if (cache_expired(item) && cache_lock(item)) {
230 if (cache_expired(item)) { 246 if (cache_expired(item)) {
231 cgit_fill_cache(item, 1); 247 cgit_fill_cache(item, 1);
232 cache_unlock(item); 248 cache_unlock(item);
233 } else { 249 } else {
234 cache_cancel_lock(item); 250 cache_cancel_lock(item);
235 } 251 }
236 } 252 }
237} 253}
238 254
239static void cgit_print_cache(struct cacheitem *item) 255static void cgit_print_cache(struct cacheitem *item)
240{ 256{
241 static char buf[4096]; 257 static char buf[4096];
242 ssize_t i; 258 ssize_t i;
243 259
244 int fd = open(item->name, O_RDONLY); 260 int fd = open(item->name, O_RDONLY);
245 if (fd<0) 261 if (fd<0)
246 die("Unable to open cached file %s", item->name); 262 die("Unable to open cached file %s", item->name);
247 263
248 while((i=read(fd, buf, sizeof(buf))) > 0) 264 while((i=read(fd, buf, sizeof(buf))) > 0)
249 write(STDOUT_FILENO, buf, i); 265 write(STDOUT_FILENO, buf, i);
250 266
251 close(fd); 267 close(fd);
252} 268}
253 269
254static void cgit_parse_args(int argc, const char **argv) 270static void cgit_parse_args(int argc, const char **argv)
255{ 271{
256 int i; 272 int i;
257 273
258 for (i = 1; i < argc; i++) { 274 for (i = 1; i < argc; i++) {
259 if (!strncmp(argv[i], "--cache=", 8)) { 275 if (!strncmp(argv[i], "--cache=", 8)) {
260 ctx.cfg.cache_root = xstrdup(argv[i]+8); 276 ctx.cfg.cache_root = xstrdup(argv[i]+8);
261 } 277 }
262 if (!strcmp(argv[i], "--nocache")) { 278 if (!strcmp(argv[i], "--nocache")) {
263 ctx.cfg.nocache = 1; 279 ctx.cfg.nocache = 1;
264 } 280 }
265 if (!strncmp(argv[i], "--query=", 8)) { 281 if (!strncmp(argv[i], "--query=", 8)) {
266 ctx.qry.raw = xstrdup(argv[i]+8); 282 ctx.qry.raw = xstrdup(argv[i]+8);
267 } 283 }
268 if (!strncmp(argv[i], "--repo=", 7)) { 284 if (!strncmp(argv[i], "--repo=", 7)) {
269 ctx.qry.repo = xstrdup(argv[i]+7); 285 ctx.qry.repo = xstrdup(argv[i]+7);
270 } 286 }
271 if (!strncmp(argv[i], "--page=", 7)) { 287 if (!strncmp(argv[i], "--page=", 7)) {
272 ctx.qry.page = xstrdup(argv[i]+7); 288 ctx.qry.page = xstrdup(argv[i]+7);
273 } 289 }
274 if (!strncmp(argv[i], "--head=", 7)) { 290 if (!strncmp(argv[i], "--head=", 7)) {
275 ctx.qry.head = xstrdup(argv[i]+7); 291 ctx.qry.head = xstrdup(argv[i]+7);
276 ctx.qry.has_symref = 1; 292 ctx.qry.has_symref = 1;
277 } 293 }
278 if (!strncmp(argv[i], "--sha1=", 7)) { 294 if (!strncmp(argv[i], "--sha1=", 7)) {
279 ctx.qry.sha1 = xstrdup(argv[i]+7); 295 ctx.qry.sha1 = xstrdup(argv[i]+7);
280 ctx.qry.has_sha1 = 1; 296 ctx.qry.has_sha1 = 1;
281 } 297 }
282 if (!strncmp(argv[i], "--ofs=", 6)) { 298 if (!strncmp(argv[i], "--ofs=", 6)) {
283 ctx.qry.ofs = atoi(argv[i]+6); 299 ctx.qry.ofs = atoi(argv[i]+6);
284 } 300 }
285 } 301 }
286} 302}
287 303
288int main(int argc, const char **argv) 304int main(int argc, const char **argv)
289{ 305{
290 struct cacheitem item; 306 struct cacheitem item;
291 const char *cgit_config_env = getenv("CGIT_CONFIG"); 307 const char *cgit_config_env = getenv("CGIT_CONFIG");
diff --git a/cgit.h b/cgit.h
index 5b7ee1c..8ab8e07 100644
--- a/cgit.h
+++ b/cgit.h
@@ -81,211 +81,219 @@ struct cgit_repo {
81 int enable_log_filecount; 81 int enable_log_filecount;
82 int enable_log_linecount; 82 int enable_log_linecount;
83}; 83};
84 84
85struct cgit_repolist { 85struct cgit_repolist {
86 int length; 86 int length;
87 int count; 87 int count;
88 struct cgit_repo *repos; 88 struct cgit_repo *repos;
89}; 89};
90 90
91struct commitinfo { 91struct commitinfo {
92 struct commit *commit; 92 struct commit *commit;
93 char *author; 93 char *author;
94 char *author_email; 94 char *author_email;
95 unsigned long author_date; 95 unsigned long author_date;
96 char *committer; 96 char *committer;
97 char *committer_email; 97 char *committer_email;
98 unsigned long committer_date; 98 unsigned long committer_date;
99 char *subject; 99 char *subject;
100 char *msg; 100 char *msg;
101 char *msg_encoding; 101 char *msg_encoding;
102}; 102};
103 103
104struct taginfo { 104struct taginfo {
105 char *tagger; 105 char *tagger;
106 char *tagger_email; 106 char *tagger_email;
107 int tagger_date; 107 int tagger_date;
108 char *msg; 108 char *msg;
109}; 109};
110 110
111struct refinfo { 111struct refinfo {
112 const char *refname; 112 const char *refname;
113 struct object *object; 113 struct object *object;
114 union { 114 union {
115 struct taginfo *tag; 115 struct taginfo *tag;
116 struct commitinfo *commit; 116 struct commitinfo *commit;
117 }; 117 };
118}; 118};
119 119
120struct reflist { 120struct reflist {
121 struct refinfo **refs; 121 struct refinfo **refs;
122 int alloc; 122 int alloc;
123 int count; 123 int count;
124}; 124};
125 125
126struct cgit_query { 126struct cgit_query {
127 int has_symref; 127 int has_symref;
128 int has_sha1; 128 int has_sha1;
129 char *raw; 129 char *raw;
130 char *repo; 130 char *repo;
131 char *page; 131 char *page;
132 char *search; 132 char *search;
133 char *grep; 133 char *grep;
134 char *head; 134 char *head;
135 char *sha1; 135 char *sha1;
136 char *sha2; 136 char *sha2;
137 char *path; 137 char *path;
138 char *name; 138 char *name;
139 int ofs; 139 int ofs;
140}; 140};
141 141
142struct cgit_config { 142struct cgit_config {
143 char *agefile; 143 char *agefile;
144 char *cache_root; 144 char *cache_root;
145 char *clone_prefix; 145 char *clone_prefix;
146 char *css; 146 char *css;
147 char *index_header; 147 char *index_header;
148 char *index_info; 148 char *index_info;
149 char *logo; 149 char *logo;
150 char *logo_link; 150 char *logo_link;
151 char *module_link; 151 char *module_link;
152 char *repo_group; 152 char *repo_group;
153 char *robots; 153 char *robots;
154 char *root_title; 154 char *root_title;
155 char *script_name; 155 char *script_name;
156 char *virtual_root; 156 char *virtual_root;
157 int cache_dynamic_ttl; 157 int cache_dynamic_ttl;
158 int cache_max_create_time; 158 int cache_max_create_time;
159 int cache_repo_ttl; 159 int cache_repo_ttl;
160 int cache_root_ttl; 160 int cache_root_ttl;
161 int cache_static_ttl; 161 int cache_static_ttl;
162 int enable_index_links; 162 int enable_index_links;
163 int enable_log_filecount; 163 int enable_log_filecount;
164 int enable_log_linecount; 164 int enable_log_linecount;
165 int max_commit_count; 165 int max_commit_count;
166 int max_lock_attempts; 166 int max_lock_attempts;
167 int max_msg_len; 167 int max_msg_len;
168 int max_repodesc_len; 168 int max_repodesc_len;
169 int nocache; 169 int nocache;
170 int renamelimit; 170 int renamelimit;
171 int snapshots; 171 int snapshots;
172 int summary_branches; 172 int summary_branches;
173 int summary_log; 173 int summary_log;
174 int summary_tags; 174 int summary_tags;
175}; 175};
176 176
177struct cgit_page {
178 time_t modified;
179 time_t expires;
180 char *mimetype;
181 char *charset;
182 char *filename;
183 char *title;
184};
185
177struct cgit_context { 186struct cgit_context {
178 struct cgit_query qry; 187 struct cgit_query qry;
179 struct cgit_config cfg; 188 struct cgit_config cfg;
180 struct cgit_repo *repo; 189 struct cgit_repo *repo;
190 struct cgit_page page;
181}; 191};
182 192
183extern const char *cgit_version; 193extern const char *cgit_version;
184 194
185extern struct cgit_repolist cgit_repolist; 195extern struct cgit_repolist cgit_repolist;
186extern struct cgit_context ctx; 196extern struct cgit_context ctx;
187extern int cgit_cmd; 197extern int cgit_cmd;
188 198
189extern void cgit_prepare_context(struct cgit_context *ctx); 199extern void cgit_prepare_context(struct cgit_context *ctx);
190extern int cgit_get_cmd_index(const char *cmd); 200extern int cgit_get_cmd_index(const char *cmd);
191extern struct cgit_repo *cgit_get_repoinfo(const char *url); 201extern struct cgit_repo *cgit_get_repoinfo(const char *url);
192extern void cgit_global_config_cb(const char *name, const char *value); 202extern void cgit_global_config_cb(const char *name, const char *value);
193extern void cgit_repo_config_cb(const char *name, const char *value); 203extern void cgit_repo_config_cb(const char *name, const char *value);
194extern void cgit_querystring_cb(const char *name, const char *value); 204extern void cgit_querystring_cb(const char *name, const char *value);
195 205
196extern int chk_zero(int result, char *msg); 206extern int chk_zero(int result, char *msg);
197extern int chk_positive(int result, char *msg); 207extern int chk_positive(int result, char *msg);
198extern int chk_non_negative(int result, char *msg); 208extern int chk_non_negative(int result, char *msg);
199 209
200extern int hextoint(char c); 210extern int hextoint(char c);
201extern char *trim_end(const char *str, char c); 211extern char *trim_end(const char *str, char c);
202extern char *strlpart(char *txt, int maxlen); 212extern char *strlpart(char *txt, int maxlen);
203extern char *strrpart(char *txt, int maxlen); 213extern char *strrpart(char *txt, int maxlen);
204 214
205extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 215extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
206extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 216extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
207 int flags, void *cb_data); 217 int flags, void *cb_data);
208 218
209extern void *cgit_free_commitinfo(struct commitinfo *info); 219extern void *cgit_free_commitinfo(struct commitinfo *info);
210 220
211extern int cgit_diff_files(const unsigned char *old_sha1, 221extern int cgit_diff_files(const unsigned char *old_sha1,
212 const unsigned char *new_sha1, 222 const unsigned char *new_sha1,
213 linediff_fn fn); 223 linediff_fn fn);
214 224
215extern void cgit_diff_tree(const unsigned char *old_sha1, 225extern void cgit_diff_tree(const unsigned char *old_sha1,
216 const unsigned char *new_sha1, 226 const unsigned char *new_sha1,
217 filepair_fn fn, const char *prefix); 227 filepair_fn fn, const char *prefix);
218 228
219extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 229extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
220 230
221extern char *fmt(const char *format,...); 231extern char *fmt(const char *format,...);
222 232
223extern int cgit_read_config(const char *filename, configfn fn); 233extern int cgit_read_config(const char *filename, configfn fn);
224extern int cgit_parse_query(char *txt, configfn fn); 234extern int cgit_parse_query(char *txt, configfn fn);
225extern struct commitinfo *cgit_parse_commit(struct commit *commit); 235extern struct commitinfo *cgit_parse_commit(struct commit *commit);
226extern struct taginfo *cgit_parse_tag(struct tag *tag); 236extern struct taginfo *cgit_parse_tag(struct tag *tag);
227extern void cgit_parse_url(const char *url); 237extern void cgit_parse_url(const char *url);
228 238
229extern char *cache_safe_filename(const char *unsafe); 239extern char *cache_safe_filename(const char *unsafe);
230extern int cache_lock(struct cacheitem *item); 240extern int cache_lock(struct cacheitem *item);
231extern int cache_unlock(struct cacheitem *item); 241extern int cache_unlock(struct cacheitem *item);
232extern int cache_cancel_lock(struct cacheitem *item); 242extern int cache_cancel_lock(struct cacheitem *item);
233extern int cache_exist(struct cacheitem *item); 243extern int cache_exist(struct cacheitem *item);
234extern int cache_expired(struct cacheitem *item); 244extern int cache_expired(struct cacheitem *item);
235 245
236extern char *cgit_repourl(const char *reponame); 246extern char *cgit_repourl(const char *reponame);
237extern char *cgit_fileurl(const char *reponame, const char *pagename, 247extern char *cgit_fileurl(const char *reponame, const char *pagename,
238 const char *filename, const char *query); 248 const char *filename, const char *query);
239extern char *cgit_pageurl(const char *reponame, const char *pagename, 249extern char *cgit_pageurl(const char *reponame, const char *pagename,
240 const char *query); 250 const char *query);
241 251
242extern const char *cgit_repobasename(const char *reponame); 252extern const char *cgit_repobasename(const char *reponame);
243 253
244extern void cgit_tree_link(char *name, char *title, char *class, char *head, 254extern void cgit_tree_link(char *name, char *title, char *class, char *head,
245 char *rev, char *path); 255 char *rev, char *path);
246extern void cgit_log_link(char *name, char *title, char *class, char *head, 256extern void cgit_log_link(char *name, char *title, char *class, char *head,
247 char *rev, char *path, int ofs, char *grep, 257 char *rev, char *path, int ofs, char *grep,
248 char *pattern); 258 char *pattern);
249extern void cgit_commit_link(char *name, char *title, char *class, char *head, 259extern void cgit_commit_link(char *name, char *title, char *class, char *head,
250 char *rev); 260 char *rev);
251extern void cgit_refs_link(char *name, char *title, char *class, char *head, 261extern void cgit_refs_link(char *name, char *title, char *class, char *head,
252 char *rev, char *path); 262 char *rev, char *path);
253extern void cgit_snapshot_link(char *name, char *title, char *class, 263extern void cgit_snapshot_link(char *name, char *title, char *class,
254 char *head, char *rev, char *archivename); 264 char *head, char *rev, char *archivename);
255extern void cgit_diff_link(char *name, char *title, char *class, char *head, 265extern void cgit_diff_link(char *name, char *title, char *class, char *head,
256 char *new_rev, char *old_rev, char *path); 266 char *new_rev, char *old_rev, char *path);
257 267
258extern void cgit_object_link(struct object *obj); 268extern void cgit_object_link(struct object *obj);
259 269
260extern void cgit_print_error(char *msg); 270extern void cgit_print_error(char *msg);
261extern void cgit_print_date(time_t secs, char *format); 271extern void cgit_print_date(time_t secs, char *format);
262extern void cgit_print_age(time_t t, time_t max_relative, char *format); 272extern void cgit_print_age(time_t t, time_t max_relative, char *format);
263extern void cgit_print_docstart(char *title, struct cacheitem *item); 273extern void cgit_print_http_headers(struct cgit_context *ctx);
274extern void cgit_print_docstart(struct cgit_context *ctx);
264extern void cgit_print_docend(); 275extern void cgit_print_docend();
265extern void cgit_print_pageheader(char *title, int show_search); 276extern void cgit_print_pageheader(struct cgit_context *ctx);
266extern void cgit_print_snapshot_start(const char *mimetype,
267 const char *filename,
268 struct cacheitem *item);
269extern void cgit_print_filemode(unsigned short mode); 277extern void cgit_print_filemode(unsigned short mode);
270extern void cgit_print_branches(int maxcount); 278extern void cgit_print_branches(int maxcount);
271extern void cgit_print_tags(int maxcount); 279extern void cgit_print_tags(int maxcount);
272 280
273extern void cgit_print_repolist(struct cacheitem *item); 281extern void cgit_print_repolist(struct cacheitem *item);
274extern void cgit_print_summary(); 282extern void cgit_print_summary();
275extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, 283extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep,
276 char *pattern, char *path, int pager); 284 char *pattern, char *path, int pager);
277extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); 285extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path);
278extern void cgit_print_tree(const char *rev, char *path); 286extern void cgit_print_tree(const char *rev, char *path);
279extern void cgit_print_commit(char *hex); 287extern void cgit_print_commit(char *hex);
280extern void cgit_print_refs(); 288extern void cgit_print_refs();
281extern void cgit_print_tag(char *revname); 289extern void cgit_print_tag(char *revname);
282extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix); 290extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix);
283extern void cgit_print_patch(char *hex, struct cacheitem *item); 291extern void cgit_print_patch(char *hex, struct cacheitem *item);
284extern void cgit_print_snapshot(struct cacheitem *item, const char *head, 292extern void cgit_print_snapshot(struct cacheitem *item, const char *head,
285 const char *hex, const char *prefix, 293 const char *hex, const char *prefix,
286 const char *filename, int snapshot); 294 const char *filename, int snapshot);
287extern void cgit_print_snapshot_links(const char *repo, const char *head, 295extern void cgit_print_snapshot_links(const char *repo, const char *head,
288 const char *hex, int snapshots); 296 const char *hex, int snapshots);
289extern int cgit_parse_snapshots_mask(const char *str); 297extern int cgit_parse_snapshots_mask(const char *str);
290 298
291#endif /* CGIT_H */ 299#endif /* CGIT_H */
diff --git a/shared.c b/shared.c
index 76e10d0..539d533 100644
--- a/shared.c
+++ b/shared.c
@@ -1,133 +1,136 @@
1/* shared.c: global vars + some callback functions 1/* shared.c: global vars + some callback functions
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
11struct cgit_repolist cgit_repolist; 11struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 12struct cgit_context ctx;
13int cgit_cmd; 13int cgit_cmd;
14 14
15const char *cgit_version = CGIT_VERSION; 15const char *cgit_version = CGIT_VERSION;
16 16
17void cgit_prepare_context(struct cgit_context *ctx) 17void cgit_prepare_context(struct cgit_context *ctx)
18{ 18{
19 memset(ctx, 0, sizeof(ctx)); 19 memset(ctx, 0, sizeof(ctx));
20 ctx->cfg.agefile = "info/web/last-modified"; 20 ctx->cfg.agefile = "info/web/last-modified";
21 ctx->cfg.cache_dynamic_ttl = 5; 21 ctx->cfg.cache_dynamic_ttl = 5;
22 ctx->cfg.cache_max_create_time = 5; 22 ctx->cfg.cache_max_create_time = 5;
23 ctx->cfg.cache_repo_ttl = 5; 23 ctx->cfg.cache_repo_ttl = 5;
24 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 24 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
25 ctx->cfg.cache_root_ttl = 5; 25 ctx->cfg.cache_root_ttl = 5;
26 ctx->cfg.cache_static_ttl = -1; 26 ctx->cfg.cache_static_ttl = -1;
27 ctx->cfg.css = "/cgit.css"; 27 ctx->cfg.css = "/cgit.css";
28 ctx->cfg.logo = "/git-logo.png"; 28 ctx->cfg.logo = "/git-logo.png";
29 ctx->cfg.max_commit_count = 50; 29 ctx->cfg.max_commit_count = 50;
30 ctx->cfg.max_lock_attempts = 5; 30 ctx->cfg.max_lock_attempts = 5;
31 ctx->cfg.max_msg_len = 60; 31 ctx->cfg.max_msg_len = 60;
32 ctx->cfg.max_repodesc_len = 60; 32 ctx->cfg.max_repodesc_len = 60;
33 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 33 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
34 ctx->cfg.renamelimit = -1; 34 ctx->cfg.renamelimit = -1;
35 ctx->cfg.robots = "index, nofollow"; 35 ctx->cfg.robots = "index, nofollow";
36 ctx->cfg.root_title = "Git repository browser"; 36 ctx->cfg.root_title = "Git repository browser";
37 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 37 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
38 ctx->page.mimetype = "text/html";
39 ctx->page.charset = PAGE_ENCODING;
40 ctx->page.filename = NULL;
38} 41}
39 42
40int cgit_get_cmd_index(const char *cmd) 43int cgit_get_cmd_index(const char *cmd)
41{ 44{
42 static char *cmds[] = {"log", "commit", "diff", "tree", "blob", 45 static char *cmds[] = {"log", "commit", "diff", "tree", "blob",
43 "snapshot", "tag", "refs", "patch", NULL}; 46 "snapshot", "tag", "refs", "patch", NULL};
44 int i; 47 int i;
45 48
46 for(i = 0; cmds[i]; i++) 49 for(i = 0; cmds[i]; i++)
47 if (!strcmp(cmd, cmds[i])) 50 if (!strcmp(cmd, cmds[i]))
48 return i + 1; 51 return i + 1;
49 return 0; 52 return 0;
50} 53}
51 54
52int chk_zero(int result, char *msg) 55int chk_zero(int result, char *msg)
53{ 56{
54 if (result != 0) 57 if (result != 0)
55 die("%s: %s", msg, strerror(errno)); 58 die("%s: %s", msg, strerror(errno));
56 return result; 59 return result;
57} 60}
58 61
59int chk_positive(int result, char *msg) 62int chk_positive(int result, char *msg)
60{ 63{
61 if (result <= 0) 64 if (result <= 0)
62 die("%s: %s", msg, strerror(errno)); 65 die("%s: %s", msg, strerror(errno));
63 return result; 66 return result;
64} 67}
65 68
66int chk_non_negative(int result, char *msg) 69int chk_non_negative(int result, char *msg)
67{ 70{
68 if (result < 0) 71 if (result < 0)
69 die("%s: %s",msg, strerror(errno)); 72 die("%s: %s",msg, strerror(errno));
70 return result; 73 return result;
71} 74}
72 75
73struct cgit_repo *add_repo(const char *url) 76struct cgit_repo *add_repo(const char *url)
74{ 77{
75 struct cgit_repo *ret; 78 struct cgit_repo *ret;
76 79
77 if (++cgit_repolist.count > cgit_repolist.length) { 80 if (++cgit_repolist.count > cgit_repolist.length) {
78 if (cgit_repolist.length == 0) 81 if (cgit_repolist.length == 0)
79 cgit_repolist.length = 8; 82 cgit_repolist.length = 8;
80 else 83 else
81 cgit_repolist.length *= 2; 84 cgit_repolist.length *= 2;
82 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 85 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
83 cgit_repolist.length * 86 cgit_repolist.length *
84 sizeof(struct cgit_repo)); 87 sizeof(struct cgit_repo));
85 } 88 }
86 89
87 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 90 ret = &cgit_repolist.repos[cgit_repolist.count-1];
88 ret->url = trim_end(url, '/'); 91 ret->url = trim_end(url, '/');
89 ret->name = ret->url; 92 ret->name = ret->url;
90 ret->path = NULL; 93 ret->path = NULL;
91 ret->desc = "[no description]"; 94 ret->desc = "[no description]";
92 ret->owner = NULL; 95 ret->owner = NULL;
93 ret->group = ctx.cfg.repo_group; 96 ret->group = ctx.cfg.repo_group;
94 ret->defbranch = "master"; 97 ret->defbranch = "master";
95 ret->snapshots = ctx.cfg.snapshots; 98 ret->snapshots = ctx.cfg.snapshots;
96 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 99 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
97 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 100 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
98 ret->module_link = ctx.cfg.module_link; 101 ret->module_link = ctx.cfg.module_link;
99 ret->readme = NULL; 102 ret->readme = NULL;
100 return ret; 103 return ret;
101} 104}
102 105
103struct cgit_repo *cgit_get_repoinfo(const char *url) 106struct cgit_repo *cgit_get_repoinfo(const char *url)
104{ 107{
105 int i; 108 int i;
106 struct cgit_repo *repo; 109 struct cgit_repo *repo;
107 110
108 for (i=0; i<cgit_repolist.count; i++) { 111 for (i=0; i<cgit_repolist.count; i++) {
109 repo = &cgit_repolist.repos[i]; 112 repo = &cgit_repolist.repos[i];
110 if (!strcmp(repo->url, url)) 113 if (!strcmp(repo->url, url))
111 return repo; 114 return repo;
112 } 115 }
113 return NULL; 116 return NULL;
114} 117}
115 118
116void cgit_global_config_cb(const char *name, const char *value) 119void cgit_global_config_cb(const char *name, const char *value)
117{ 120{
118 if (!strcmp(name, "root-title")) 121 if (!strcmp(name, "root-title"))
119 ctx.cfg.root_title = xstrdup(value); 122 ctx.cfg.root_title = xstrdup(value);
120 else if (!strcmp(name, "css")) 123 else if (!strcmp(name, "css"))
121 ctx.cfg.css = xstrdup(value); 124 ctx.cfg.css = xstrdup(value);
122 else if (!strcmp(name, "logo")) 125 else if (!strcmp(name, "logo"))
123 ctx.cfg.logo = xstrdup(value); 126 ctx.cfg.logo = xstrdup(value);
124 else if (!strcmp(name, "index-header")) 127 else if (!strcmp(name, "index-header"))
125 ctx.cfg.index_header = xstrdup(value); 128 ctx.cfg.index_header = xstrdup(value);
126 else if (!strcmp(name, "index-info")) 129 else if (!strcmp(name, "index-info"))
127 ctx.cfg.index_info = xstrdup(value); 130 ctx.cfg.index_info = xstrdup(value);
128 else if (!strcmp(name, "logo-link")) 131 else if (!strcmp(name, "logo-link"))
129 ctx.cfg.logo_link = xstrdup(value); 132 ctx.cfg.logo_link = xstrdup(value);
130 else if (!strcmp(name, "module-link")) 133 else if (!strcmp(name, "module-link"))
131 ctx.cfg.module_link = xstrdup(value); 134 ctx.cfg.module_link = xstrdup(value);
132 else if (!strcmp(name, "virtual-root")) { 135 else if (!strcmp(name, "virtual-root")) {
133 ctx.cfg.virtual_root = trim_end(value, '/'); 136 ctx.cfg.virtual_root = trim_end(value, '/');
diff --git a/ui-blob.c b/ui-blob.c
index be4fb88..bd44574 100644
--- a/ui-blob.c
+++ b/ui-blob.c
@@ -1,40 +1,42 @@
1/* ui-blob.c: show blob content 1/* ui-blob.c: show blob content
2 * 2 *
3 * Copyright (C) 2008 Lars Hjemli 3 * Copyright (C) 2008 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#include "html.h" 10#include "html.h"
11 11
12void cgit_print_blob(struct cacheitem *item, const char *hex, char *path) 12void cgit_print_blob(struct cacheitem *item, const char *hex, char *path)
13{ 13{
14 14
15 unsigned char sha1[20]; 15 unsigned char sha1[20];
16 enum object_type type; 16 enum object_type type;
17 unsigned char *buf; 17 unsigned char *buf;
18 unsigned long size; 18 unsigned long size;
19 19
20 if (get_sha1_hex(hex, sha1)){ 20 if (get_sha1_hex(hex, sha1)){
21 cgit_print_error(fmt("Bad hex value: %s", hex)); 21 cgit_print_error(fmt("Bad hex value: %s", hex));
22 return; 22 return;
23 } 23 }
24 24
25 type = sha1_object_info(sha1, &size); 25 type = sha1_object_info(sha1, &size);
26 if (type == OBJ_BAD) { 26 if (type == OBJ_BAD) {
27 cgit_print_error(fmt("Bad object name: %s", hex)); 27 cgit_print_error(fmt("Bad object name: %s", hex));
28 return; 28 return;
29 } 29 }
30 30
31 buf = read_sha1_file(sha1, &type, &size); 31 buf = read_sha1_file(sha1, &type, &size);
32 if (!buf) { 32 if (!buf) {
33 cgit_print_error(fmt("Error reading object %s", hex)); 33 cgit_print_error(fmt("Error reading object %s", hex));
34 return; 34 return;
35 } 35 }
36 36
37 buf[size] = '\0'; 37 buf[size] = '\0';
38 cgit_print_snapshot_start("text/plain", path, item); 38 ctx.page.mimetype = "text/plain";
39 ctx.page.filename = path;
40 cgit_print_http_headers(&ctx);
39 write(htmlfd, buf, size); 41 write(htmlfd, buf, size);
40} 42}
diff --git a/ui-patch.c b/ui-patch.c
index d93426b..a77f3f6 100644
--- a/ui-patch.c
+++ b/ui-patch.c
@@ -1,111 +1,113 @@
1/* ui-patch.c: generate patch view 1/* ui-patch.c: generate patch view
2 * 2 *
3 * Copyright (C) 2007 Lars Hjemli 3 * Copyright (C) 2007 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#include "html.h" 10#include "html.h"
11 11
12static void print_line(char *line, int len) 12static void print_line(char *line, int len)
13{ 13{
14 char c = line[len-1]; 14 char c = line[len-1];
15 15
16 line[len-1] = '\0'; 16 line[len-1] = '\0';
17 htmlf("%s\n", line); 17 htmlf("%s\n", line);
18 line[len-1] = c; 18 line[len-1] = c;
19} 19}
20 20
21static void header(unsigned char *sha1, char *path1, int mode1, 21static void header(unsigned char *sha1, char *path1, int mode1,
22 unsigned char *sha2, char *path2, int mode2) 22 unsigned char *sha2, char *path2, int mode2)
23{ 23{
24 char *abbrev1, *abbrev2; 24 char *abbrev1, *abbrev2;
25 int subproject; 25 int subproject;
26 26
27 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2)); 27 subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
28 htmlf("diff --git a/%s b/%s\n", path1, path2); 28 htmlf("diff --git a/%s b/%s\n", path1, path2);
29 29
30 if (is_null_sha1(sha1)) 30 if (is_null_sha1(sha1))
31 path1 = "dev/null"; 31 path1 = "dev/null";
32 if (is_null_sha1(sha2)) 32 if (is_null_sha1(sha2))
33 path2 = "dev/null"; 33 path2 = "dev/null";
34 34
35 if (mode1 == 0) 35 if (mode1 == 0)
36 htmlf("new file mode %.6o\n", mode2); 36 htmlf("new file mode %.6o\n", mode2);
37 37
38 if (mode2 == 0) 38 if (mode2 == 0)
39 htmlf("deleted file mode %.6o\n", mode1); 39 htmlf("deleted file mode %.6o\n", mode1);
40 40
41 if (!subproject) { 41 if (!subproject) {
42 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); 42 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
43 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); 43 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
44 htmlf("index %s..%s", abbrev1, abbrev2); 44 htmlf("index %s..%s", abbrev1, abbrev2);
45 free(abbrev1); 45 free(abbrev1);
46 free(abbrev2); 46 free(abbrev2);
47 if (mode1 != 0 && mode2 != 0) { 47 if (mode1 != 0 && mode2 != 0) {
48 htmlf(" %.6o", mode1); 48 htmlf(" %.6o", mode1);
49 if (mode2 != mode1) 49 if (mode2 != mode1)
50 htmlf("..%.6o", mode2); 50 htmlf("..%.6o", mode2);
51 } 51 }
52 htmlf("\n--- a/%s\n", path1); 52 htmlf("\n--- a/%s\n", path1);
53 htmlf("+++ b/%s\n", path2); 53 htmlf("+++ b/%s\n", path2);
54 } 54 }
55} 55}
56 56
57static void filepair_cb(struct diff_filepair *pair) 57static void filepair_cb(struct diff_filepair *pair)
58{ 58{
59 header(pair->one->sha1, pair->one->path, pair->one->mode, 59 header(pair->one->sha1, pair->one->path, pair->one->mode,
60 pair->two->sha1, pair->two->path, pair->two->mode); 60 pair->two->sha1, pair->two->path, pair->two->mode);
61 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) { 61 if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
62 if (S_ISGITLINK(pair->one->mode)) 62 if (S_ISGITLINK(pair->one->mode))
63 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52); 63 print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
64 if (S_ISGITLINK(pair->two->mode)) 64 if (S_ISGITLINK(pair->two->mode))
65 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52); 65 print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
66 return; 66 return;
67 } 67 }
68 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) 68 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
69 html("Error running diff"); 69 html("Error running diff");
70} 70}
71 71
72void cgit_print_patch(char *hex, struct cacheitem *item) 72void cgit_print_patch(char *hex, struct cacheitem *item)
73{ 73{
74 struct commit *commit; 74 struct commit *commit;
75 struct commitinfo *info; 75 struct commitinfo *info;
76 unsigned char sha1[20], old_sha1[20]; 76 unsigned char sha1[20], old_sha1[20];
77 char *patchname; 77 char *patchname;
78 78
79 if (!hex) 79 if (!hex)
80 hex = ctx.qry.head; 80 hex = ctx.qry.head;
81 81
82 if (get_sha1(hex, sha1)) { 82 if (get_sha1(hex, sha1)) {
83 cgit_print_error(fmt("Bad object id: %s", hex)); 83 cgit_print_error(fmt("Bad object id: %s", hex));
84 return; 84 return;
85 } 85 }
86 commit = lookup_commit_reference(sha1); 86 commit = lookup_commit_reference(sha1);
87 if (!commit) { 87 if (!commit) {
88 cgit_print_error(fmt("Bad commit reference: %s", hex)); 88 cgit_print_error(fmt("Bad commit reference: %s", hex));
89 return; 89 return;
90 } 90 }
91 info = cgit_parse_commit(commit); 91 info = cgit_parse_commit(commit);
92 hashcpy(old_sha1, commit->parents->item->object.sha1); 92 hashcpy(old_sha1, commit->parents->item->object.sha1);
93 93
94 patchname = fmt("%s.patch", sha1_to_hex(sha1)); 94 patchname = fmt("%s.patch", sha1_to_hex(sha1));
95 cgit_print_snapshot_start("text/plain", patchname, item); 95 ctx.page.mimetype = "text/plain";
96 ctx.page.filename = patchname;
97 cgit_print_http_headers(&ctx);
96 htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1)); 98 htmlf("From %s Mon Sep 17 00:00:00 2001\n", sha1_to_hex(sha1));
97 htmlf("From: %s%s\n", info->author, info->author_email); 99 htmlf("From: %s%s\n", info->author, info->author_email);
98 html("Date: "); 100 html("Date: ");
99 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n"); 101 cgit_print_date(info->author_date, "%a, %d %b %Y %H:%M:%S %z%n");
100 htmlf("Subject: %s\n\n", info->subject); 102 htmlf("Subject: %s\n\n", info->subject);
101 if (info->msg && *info->msg) { 103 if (info->msg && *info->msg) {
102 htmlf("%s", info->msg); 104 htmlf("%s", info->msg);
103 if (info->msg[strlen(info->msg) - 1] != '\n') 105 if (info->msg[strlen(info->msg) - 1] != '\n')
104 html("\n"); 106 html("\n");
105 } 107 }
106 html("---\n"); 108 html("---\n");
107 cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL); 109 cgit_diff_tree(old_sha1, sha1, filepair_cb, NULL);
108 html("--\n"); 110 html("--\n");
109 htmlf("cgit %s\n", CGIT_VERSION); 111 htmlf("cgit %s\n", CGIT_VERSION);
110 cgit_free_commitinfo(info); 112 cgit_free_commitinfo(info);
111} 113}
diff --git a/ui-repolist.c b/ui-repolist.c
index cd4e41d..e663585 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -1,111 +1,113 @@
1/* ui-repolist.c: functions for generating the repolist page 1/* ui-repolist.c: functions for generating the repolist page
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 <time.h> 9#include <time.h>
10 10
11#include "cgit.h" 11#include "cgit.h"
12#include "html.h" 12#include "html.h"
13 13
14time_t read_agefile(char *path) 14time_t read_agefile(char *path)
15{ 15{
16 FILE *f; 16 FILE *f;
17 static char buf[64], buf2[64]; 17 static char buf[64], buf2[64];
18 18
19 if (!(f = fopen(path, "r"))) 19 if (!(f = fopen(path, "r")))
20 return -1; 20 return -1;
21 fgets(buf, sizeof(buf), f); 21 fgets(buf, sizeof(buf), f);
22 fclose(f); 22 fclose(f);
23 if (parse_date(buf, buf2, sizeof(buf2))) 23 if (parse_date(buf, buf2, sizeof(buf2)))
24 return strtoul(buf2, NULL, 10); 24 return strtoul(buf2, NULL, 10);
25 else 25 else
26 return 0; 26 return 0;
27} 27}
28 28
29static void print_modtime(struct cgit_repo *repo) 29static void print_modtime(struct cgit_repo *repo)
30{ 30{
31 char *path; 31 char *path;
32 struct stat s; 32 struct stat s;
33 33
34 path = fmt("%s/%s", repo->path, ctx.cfg.agefile); 34 path = fmt("%s/%s", repo->path, ctx.cfg.agefile);
35 if (stat(path, &s) == 0) { 35 if (stat(path, &s) == 0) {
36 cgit_print_age(read_agefile(path), -1, NULL); 36 cgit_print_age(read_agefile(path), -1, NULL);
37 return; 37 return;
38 } 38 }
39 39
40 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); 40 path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch);
41 if (stat(path, &s) != 0) 41 if (stat(path, &s) != 0)
42 return; 42 return;
43 cgit_print_age(s.st_mtime, -1, NULL); 43 cgit_print_age(s.st_mtime, -1, NULL);
44} 44}
45 45
46void cgit_print_repolist(struct cacheitem *item) 46void cgit_print_repolist(struct cacheitem *item)
47{ 47{
48 int i, columns = 4; 48 int i, columns = 4;
49 char *last_group = NULL; 49 char *last_group = NULL;
50 50
51 if (ctx.cfg.enable_index_links) 51 if (ctx.cfg.enable_index_links)
52 columns++; 52 columns++;
53 53
54 cgit_print_docstart(ctx.cfg.root_title, item); 54 ctx.page.title = ctx.cfg.root_title;
55 cgit_print_pageheader(ctx.cfg.root_title, 0); 55 cgit_print_http_headers(&ctx);
56 cgit_print_docstart(&ctx);
57 cgit_print_pageheader(&ctx);
56 58
57 html("<table summary='repository list' class='list nowrap'>"); 59 html("<table summary='repository list' class='list nowrap'>");
58 if (ctx.cfg.index_header) { 60 if (ctx.cfg.index_header) {
59 htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>", 61 htmlf("<tr class='nohover'><td colspan='%d' class='include-block'>",
60 columns); 62 columns);
61 html_include(ctx.cfg.index_header); 63 html_include(ctx.cfg.index_header);
62 html("</td></tr>"); 64 html("</td></tr>");
63 } 65 }
64 html("<tr class='nohover'>" 66 html("<tr class='nohover'>"
65 "<th class='left'>Name</th>" 67 "<th class='left'>Name</th>"
66 "<th class='left'>Description</th>" 68 "<th class='left'>Description</th>"
67 "<th class='left'>Owner</th>" 69 "<th class='left'>Owner</th>"
68 "<th class='left'>Idle</th>"); 70 "<th class='left'>Idle</th>");
69 if (ctx.cfg.enable_index_links) 71 if (ctx.cfg.enable_index_links)
70 html("<th>Links</th>"); 72 html("<th>Links</th>");
71 html("</tr>\n"); 73 html("</tr>\n");
72 74
73 for (i=0; i<cgit_repolist.count; i++) { 75 for (i=0; i<cgit_repolist.count; i++) {
74 ctx.repo = &cgit_repolist.repos[i]; 76 ctx.repo = &cgit_repolist.repos[i];
75 if ((last_group == NULL && ctx.repo->group != NULL) || 77 if ((last_group == NULL && ctx.repo->group != NULL) ||
76 (last_group != NULL && ctx.repo->group == NULL) || 78 (last_group != NULL && ctx.repo->group == NULL) ||
77 (last_group != NULL && ctx.repo->group != NULL && 79 (last_group != NULL && ctx.repo->group != NULL &&
78 strcmp(ctx.repo->group, last_group))) { 80 strcmp(ctx.repo->group, last_group))) {
79 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>", 81 htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",
80 columns); 82 columns);
81 html_txt(ctx.repo->group); 83 html_txt(ctx.repo->group);
82 html("</td></tr>"); 84 html("</td></tr>");
83 last_group = ctx.repo->group; 85 last_group = ctx.repo->group;
84 } 86 }
85 htmlf("<tr><td class='%s'>", 87 htmlf("<tr><td class='%s'>",
86 ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); 88 ctx.repo->group ? "sublevel-repo" : "toplevel-repo");
87 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); 89 html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL);
88 html_txt(ctx.repo->name); 90 html_txt(ctx.repo->name);
89 html_link_close(); 91 html_link_close();
90 html("</td><td>"); 92 html("</td><td>");
91 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc); 93 html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
92 html("</td><td>"); 94 html("</td><td>");
93 html_txt(ctx.repo->owner); 95 html_txt(ctx.repo->owner);
94 html("</td><td>"); 96 html("</td><td>");
95 print_modtime(ctx.repo); 97 print_modtime(ctx.repo);
96 html("</td>"); 98 html("</td>");
97 if (ctx.cfg.enable_index_links) { 99 if (ctx.cfg.enable_index_links) {
98 html("<td>"); 100 html("<td>");
99 html_link_open(cgit_repourl(ctx.repo->url), 101 html_link_open(cgit_repourl(ctx.repo->url),
100 NULL, "button"); 102 NULL, "button");
101 html("summary</a>"); 103 html("summary</a>");
102 cgit_log_link("log", NULL, "button", NULL, NULL, NULL, 104 cgit_log_link("log", NULL, "button", NULL, NULL, NULL,
103 0, NULL, NULL); 105 0, NULL, NULL);
104 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL); 106 cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);
105 html("</td>"); 107 html("</td>");
106 } 108 }
107 html("</tr>\n"); 109 html("</tr>\n");
108 } 110 }
109 html("</table>"); 111 html("</table>");
110 cgit_print_docend(); 112 cgit_print_docend();
111} 113}
diff --git a/ui-shared.c b/ui-shared.c
index 2eff79d..2596023 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,132 +1,124 @@
1/* ui-shared.c: common web output functions 1/* ui-shared.c: common web output functions
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#include "html.h" 10#include "html.h"
11 11
12const char cgit_doctype[] = 12const char cgit_doctype[] =
13"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 13"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
14" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 14" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
15 15
16static char *http_date(time_t t) 16static char *http_date(time_t t)
17{ 17{
18 static char day[][4] = 18 static char day[][4] =
19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 19 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
20 static char month[][4] = 20 static char month[][4] =
21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 21 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 22 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
23 struct tm *tm = gmtime(&t); 23 struct tm *tm = gmtime(&t);
24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 24 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
25 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 25 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
26 tm->tm_hour, tm->tm_min, tm->tm_sec); 26 tm->tm_hour, tm->tm_min, tm->tm_sec);
27} 27}
28 28
29static long ttl_seconds(long ttl)
30{
31 if (ttl<0)
32 return 60 * 60 * 24 * 365;
33 else
34 return ttl * 60;
35}
36
37void cgit_print_error(char *msg) 29void cgit_print_error(char *msg)
38{ 30{
39 html("<div class='error'>"); 31 html("<div class='error'>");
40 html_txt(msg); 32 html_txt(msg);
41 html("</div>\n"); 33 html("</div>\n");
42} 34}
43 35
44char *cgit_rooturl() 36char *cgit_rooturl()
45{ 37{
46 if (ctx.cfg.virtual_root) 38 if (ctx.cfg.virtual_root)
47 return fmt("%s/", ctx.cfg.virtual_root); 39 return fmt("%s/", ctx.cfg.virtual_root);
48 else 40 else
49 return ctx.cfg.script_name; 41 return ctx.cfg.script_name;
50} 42}
51 43
52char *cgit_repourl(const char *reponame) 44char *cgit_repourl(const char *reponame)
53{ 45{
54 if (ctx.cfg.virtual_root) { 46 if (ctx.cfg.virtual_root) {
55 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); 47 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
56 } else { 48 } else {
57 return fmt("?r=%s", reponame); 49 return fmt("?r=%s", reponame);
58 } 50 }
59} 51}
60 52
61char *cgit_fileurl(const char *reponame, const char *pagename, 53char *cgit_fileurl(const char *reponame, const char *pagename,
62 const char *filename, const char *query) 54 const char *filename, const char *query)
63{ 55{
64 char *tmp; 56 char *tmp;
65 char *delim; 57 char *delim;
66 58
67 if (ctx.cfg.virtual_root) { 59 if (ctx.cfg.virtual_root) {
68 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, 60 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
69 pagename, (filename ? filename:"")); 61 pagename, (filename ? filename:""));
70 delim = "?"; 62 delim = "?";
71 } else { 63 } else {
72 tmp = fmt("?url=%s/%s/%s", reponame, pagename, 64 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
73 (filename ? filename : "")); 65 (filename ? filename : ""));
74 delim = "&"; 66 delim = "&";
75 } 67 }
76 if (query) 68 if (query)
77 tmp = fmt("%s%s%s", tmp, delim, query); 69 tmp = fmt("%s%s%s", tmp, delim, query);
78 return tmp; 70 return tmp;
79} 71}
80 72
81char *cgit_pageurl(const char *reponame, const char *pagename, 73char *cgit_pageurl(const char *reponame, const char *pagename,
82 const char *query) 74 const char *query)
83{ 75{
84 return cgit_fileurl(reponame,pagename,0,query); 76 return cgit_fileurl(reponame,pagename,0,query);
85} 77}
86 78
87const char *cgit_repobasename(const char *reponame) 79const char *cgit_repobasename(const char *reponame)
88{ 80{
89 /* I assume we don't need to store more than one repo basename */ 81 /* I assume we don't need to store more than one repo basename */
90 static char rvbuf[1024]; 82 static char rvbuf[1024];
91 int p; 83 int p;
92 const char *rv; 84 const char *rv;
93 strncpy(rvbuf,reponame,sizeof(rvbuf)); 85 strncpy(rvbuf,reponame,sizeof(rvbuf));
94 if(rvbuf[sizeof(rvbuf)-1]) 86 if(rvbuf[sizeof(rvbuf)-1])
95 die("cgit_repobasename: truncated repository name '%s'", reponame); 87 die("cgit_repobasename: truncated repository name '%s'", reponame);
96 p = strlen(rvbuf)-1; 88 p = strlen(rvbuf)-1;
97 /* strip trailing slashes */ 89 /* strip trailing slashes */
98 while(p && rvbuf[p]=='/') rvbuf[p--]=0; 90 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
99 /* strip trailing .git */ 91 /* strip trailing .git */
100 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 92 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
101 p -= 3; rvbuf[p--] = 0; 93 p -= 3; rvbuf[p--] = 0;
102 } 94 }
103 /* strip more trailing slashes if any */ 95 /* strip more trailing slashes if any */
104 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 96 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
105 /* find last slash in the remaining string */ 97 /* find last slash in the remaining string */
106 rv = strrchr(rvbuf,'/'); 98 rv = strrchr(rvbuf,'/');
107 if(rv) 99 if(rv)
108 return ++rv; 100 return ++rv;
109 return rvbuf; 101 return rvbuf;
110} 102}
111 103
112char *cgit_currurl() 104char *cgit_currurl()
113{ 105{
114 if (!ctx.cfg.virtual_root) 106 if (!ctx.cfg.virtual_root)
115 return ctx.cfg.script_name; 107 return ctx.cfg.script_name;
116 else if (ctx.qry.page) 108 else if (ctx.qry.page)
117 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); 109 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
118 else if (ctx.qry.repo) 110 else if (ctx.qry.repo)
119 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); 111 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
120 else 112 else
121 return fmt("%s/", ctx.cfg.virtual_root); 113 return fmt("%s/", ctx.cfg.virtual_root);
122} 114}
123 115
124static char *repolink(char *title, char *class, char *page, char *head, 116static char *repolink(char *title, char *class, char *page, char *head,
125 char *path) 117 char *path)
126{ 118{
127 char *delim = "?"; 119 char *delim = "?";
128 120
129 html("<a"); 121 html("<a");
130 if (title) { 122 if (title) {
131 html(" title='"); 123 html(" title='");
132 html_attr(title); 124 html_attr(title);
@@ -268,319 +260,317 @@ void cgit_diff_link(char *name, char *title, char *class, char *head,
268 html("id2="); 260 html("id2=");
269 html_attr(old_rev); 261 html_attr(old_rev);
270 } 262 }
271 html("'>"); 263 html("'>");
272 html_txt(name); 264 html_txt(name);
273 html("</a>"); 265 html("</a>");
274} 266}
275 267
276void cgit_patch_link(char *name, char *title, char *class, char *head, 268void cgit_patch_link(char *name, char *title, char *class, char *head,
277 char *rev) 269 char *rev)
278{ 270{
279 reporevlink("patch", name, title, class, head, rev, NULL); 271 reporevlink("patch", name, title, class, head, rev, NULL);
280} 272}
281 273
282void cgit_object_link(struct object *obj) 274void cgit_object_link(struct object *obj)
283{ 275{
284 char *page, *arg, *url; 276 char *page, *arg, *url;
285 277
286 if (obj->type == OBJ_COMMIT) { 278 if (obj->type == OBJ_COMMIT) {
287 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 279 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
288 ctx.qry.head, sha1_to_hex(obj->sha1)); 280 ctx.qry.head, sha1_to_hex(obj->sha1));
289 return; 281 return;
290 } else if (obj->type == OBJ_TREE) { 282 } else if (obj->type == OBJ_TREE) {
291 page = "tree"; 283 page = "tree";
292 arg = "id"; 284 arg = "id";
293 } else if (obj->type == OBJ_TAG) { 285 } else if (obj->type == OBJ_TAG) {
294 page = "tag"; 286 page = "tag";
295 arg = "id"; 287 arg = "id";
296 } else { 288 } else {
297 page = "blob"; 289 page = "blob";
298 arg = "id"; 290 arg = "id";
299 } 291 }
300 292
301 url = cgit_pageurl(ctx.qry.repo, page, 293 url = cgit_pageurl(ctx.qry.repo, page,
302 fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); 294 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
303 html_link_open(url, NULL, NULL); 295 html_link_open(url, NULL, NULL);
304 htmlf("%s %s", typename(obj->type), 296 htmlf("%s %s", typename(obj->type),
305 sha1_to_hex(obj->sha1)); 297 sha1_to_hex(obj->sha1));
306 html_link_close(); 298 html_link_close();
307} 299}
308 300
309void cgit_print_date(time_t secs, char *format) 301void cgit_print_date(time_t secs, char *format)
310{ 302{
311 char buf[64]; 303 char buf[64];
312 struct tm *time; 304 struct tm *time;
313 305
314 if (!secs) 306 if (!secs)
315 return; 307 return;
316 time = gmtime(&secs); 308 time = gmtime(&secs);
317 strftime(buf, sizeof(buf)-1, format, time); 309 strftime(buf, sizeof(buf)-1, format, time);
318 html_txt(buf); 310 html_txt(buf);
319} 311}
320 312
321void cgit_print_age(time_t t, time_t max_relative, char *format) 313void cgit_print_age(time_t t, time_t max_relative, char *format)
322{ 314{
323 time_t now, secs; 315 time_t now, secs;
324 316
325 if (!t) 317 if (!t)
326 return; 318 return;
327 time(&now); 319 time(&now);
328 secs = now - t; 320 secs = now - t;
329 321
330 if (secs > max_relative && max_relative >= 0) { 322 if (secs > max_relative && max_relative >= 0) {
331 cgit_print_date(t, format); 323 cgit_print_date(t, format);
332 return; 324 return;
333 } 325 }
334 326
335 if (secs < TM_HOUR * 2) { 327 if (secs < TM_HOUR * 2) {
336 htmlf("<span class='age-mins'>%.0f min.</span>", 328 htmlf("<span class='age-mins'>%.0f min.</span>",
337 secs * 1.0 / TM_MIN); 329 secs * 1.0 / TM_MIN);
338 return; 330 return;
339 } 331 }
340 if (secs < TM_DAY * 2) { 332 if (secs < TM_DAY * 2) {
341 htmlf("<span class='age-hours'>%.0f hours</span>", 333 htmlf("<span class='age-hours'>%.0f hours</span>",
342 secs * 1.0 / TM_HOUR); 334 secs * 1.0 / TM_HOUR);
343 return; 335 return;
344 } 336 }
345 if (secs < TM_WEEK * 2) { 337 if (secs < TM_WEEK * 2) {
346 htmlf("<span class='age-days'>%.0f days</span>", 338 htmlf("<span class='age-days'>%.0f days</span>",
347 secs * 1.0 / TM_DAY); 339 secs * 1.0 / TM_DAY);
348 return; 340 return;
349 } 341 }
350 if (secs < TM_MONTH * 2) { 342 if (secs < TM_MONTH * 2) {
351 htmlf("<span class='age-weeks'>%.0f weeks</span>", 343 htmlf("<span class='age-weeks'>%.0f weeks</span>",
352 secs * 1.0 / TM_WEEK); 344 secs * 1.0 / TM_WEEK);
353 return; 345 return;
354 } 346 }
355 if (secs < TM_YEAR * 2) { 347 if (secs < TM_YEAR * 2) {
356 htmlf("<span class='age-months'>%.0f months</span>", 348 htmlf("<span class='age-months'>%.0f months</span>",
357 secs * 1.0 / TM_MONTH); 349 secs * 1.0 / TM_MONTH);
358 return; 350 return;
359 } 351 }
360 htmlf("<span class='age-years'>%.0f years</span>", 352 htmlf("<span class='age-years'>%.0f years</span>",
361 secs * 1.0 / TM_YEAR); 353 secs * 1.0 / TM_YEAR);
362} 354}
363 355
364void cgit_print_docstart(char *title, struct cacheitem *item) 356void cgit_print_http_headers(struct cgit_context *ctx)
365{ 357{
366 html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); 358 if (ctx->page.mimetype && ctx->page.charset)
367 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); 359 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
368 htmlf("Expires: %s\n", http_date(item->st.st_mtime + 360 ctx->page.charset);
369 ttl_seconds(item->ttl))); 361 else if (ctx->page.mimetype)
362 htmlf("Content-Type: %s\n", ctx->page.mimetype);
363 if (ctx->page.filename)
364 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
365 ctx->page.filename);
366 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
367 htmlf("Expires: %s\n", http_date(ctx->page.expires));
370 html("\n"); 368 html("\n");
369}
370
371void cgit_print_docstart(struct cgit_context *ctx)
372{
371 html(cgit_doctype); 373 html(cgit_doctype);
372 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 374 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
373 html("<head>\n"); 375 html("<head>\n");
374 html("<title>"); 376 html("<title>");
375 html_txt(title); 377 html_txt(ctx->page.title);
376 html("</title>\n"); 378 html("</title>\n");
377 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 379 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
378 if (ctx.cfg.robots && *ctx.cfg.robots) 380 if (ctx->cfg.robots && *ctx->cfg.robots)
379 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots); 381 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
380 html("<link rel='stylesheet' type='text/css' href='"); 382 html("<link rel='stylesheet' type='text/css' href='");
381 html_attr(ctx.cfg.css); 383 html_attr(ctx->cfg.css);
382 html("'/>\n"); 384 html("'/>\n");
383 html("</head>\n"); 385 html("</head>\n");
384 html("<body>\n"); 386 html("<body>\n");
385} 387}
386 388
387void cgit_print_docend() 389void cgit_print_docend()
388{ 390{
389 html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); 391 html("</td>\n</tr>\n</table>\n</body>\n</html>\n");
390} 392}
391 393
392int print_branch_option(const char *refname, const unsigned char *sha1, 394int print_branch_option(const char *refname, const unsigned char *sha1,
393 int flags, void *cb_data) 395 int flags, void *cb_data)
394{ 396{
395 char *name = (char *)refname; 397 char *name = (char *)refname;
396 html_option(name, name, ctx.qry.head); 398 html_option(name, name, ctx.qry.head);
397 return 0; 399 return 0;
398} 400}
399 401
400int print_archive_ref(const char *refname, const unsigned char *sha1, 402int print_archive_ref(const char *refname, const unsigned char *sha1,
401 int flags, void *cb_data) 403 int flags, void *cb_data)
402{ 404{
403 struct tag *tag; 405 struct tag *tag;
404 struct taginfo *info; 406 struct taginfo *info;
405 struct object *obj; 407 struct object *obj;
406 char buf[256], *url; 408 char buf[256], *url;
407 unsigned char fileid[20]; 409 unsigned char fileid[20];
408 int *header = (int *)cb_data; 410 int *header = (int *)cb_data;
409 411
410 if (prefixcmp(refname, "refs/archives")) 412 if (prefixcmp(refname, "refs/archives"))
411 return 0; 413 return 0;
412 strncpy(buf, refname+14, sizeof(buf)); 414 strncpy(buf, refname+14, sizeof(buf));
413 obj = parse_object(sha1); 415 obj = parse_object(sha1);
414 if (!obj) 416 if (!obj)
415 return 1; 417 return 1;
416 if (obj->type == OBJ_TAG) { 418 if (obj->type == OBJ_TAG) {
417 tag = lookup_tag(sha1); 419 tag = lookup_tag(sha1);
418 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 420 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
419 return 0; 421 return 0;
420 hashcpy(fileid, tag->tagged->sha1); 422 hashcpy(fileid, tag->tagged->sha1);
421 } else if (obj->type != OBJ_BLOB) { 423 } else if (obj->type != OBJ_BLOB) {
422 return 0; 424 return 0;
423 } else { 425 } else {
424 hashcpy(fileid, sha1); 426 hashcpy(fileid, sha1);
425 } 427 }
426 if (!*header) { 428 if (!*header) {
427 html("<h1>download</h1>\n"); 429 html("<h1>download</h1>\n");
428 *header = 1; 430 *header = 1;
429 } 431 }
430 url = cgit_pageurl(ctx.qry.repo, "blob", 432 url = cgit_pageurl(ctx.qry.repo, "blob",
431 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 433 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
432 buf)); 434 buf));
433 html_link_open(url, NULL, "menu"); 435 html_link_open(url, NULL, "menu");
434 html_txt(strlpart(buf, 20)); 436 html_txt(strlpart(buf, 20));
435 html_link_close(); 437 html_link_close();
436 return 0; 438 return 0;
437} 439}
438 440
439void add_hidden_formfields(int incl_head, int incl_search, char *page) 441void add_hidden_formfields(int incl_head, int incl_search, char *page)
440{ 442{
441 char *url; 443 char *url;
442 444
443 if (!ctx.cfg.virtual_root) { 445 if (!ctx.cfg.virtual_root) {
444 url = fmt("%s/%s", ctx.qry.repo, page); 446 url = fmt("%s/%s", ctx.qry.repo, page);
445 if (ctx.qry.path) 447 if (ctx.qry.path)
446 url = fmt("%s/%s", url, ctx.qry.path); 448 url = fmt("%s/%s", url, ctx.qry.path);
447 html_hidden("url", url); 449 html_hidden("url", url);
448 } 450 }
449 451
450 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) 452 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
451 html_hidden("h", ctx.qry.head); 453 html_hidden("h", ctx.qry.head);
452 454
453 if (ctx.qry.sha1) 455 if (ctx.qry.sha1)
454 html_hidden("id", ctx.qry.sha1); 456 html_hidden("id", ctx.qry.sha1);
455 if (ctx.qry.sha2) 457 if (ctx.qry.sha2)
456 html_hidden("id2", ctx.qry.sha2); 458 html_hidden("id2", ctx.qry.sha2);
457 459
458 if (incl_search) { 460 if (incl_search) {
459 if (ctx.qry.grep) 461 if (ctx.qry.grep)
460 html_hidden("qt", ctx.qry.grep); 462 html_hidden("qt", ctx.qry.grep);
461 if (ctx.qry.search) 463 if (ctx.qry.search)
462 html_hidden("q", ctx.qry.search); 464 html_hidden("q", ctx.qry.search);
463 } 465 }
464} 466}
465 467
466void cgit_print_pageheader(char *title, int show_search) 468void cgit_print_pageheader(struct cgit_context *ctx)
467{ 469{
468 static const char *default_info = "This is cgit, a fast webinterface for git repositories"; 470 static const char *default_info = "This is cgit, a fast webinterface for git repositories";
469 int header = 0; 471 int header = 0;
470 char *url; 472 char *url;
471 473
472 html("<table id='layout' summary=''>\n"); 474 html("<table id='layout' summary=''>\n");
473 html("<tr><td id='sidebar'>\n"); 475 html("<tr><td id='sidebar'>\n");
474 html("<table class='sidebar' cellspacing='0' summary=''>\n"); 476 html("<table class='sidebar' cellspacing='0' summary=''>\n");
475 html("<tr><td class='sidebar'>\n<a href='"); 477 html("<tr><td class='sidebar'>\n<a href='");
476 html_attr(cgit_rooturl()); 478 html_attr(cgit_rooturl());
477 htmlf("'><img src='%s' alt='cgit'/></a>\n", 479 htmlf("'><img src='%s' alt='cgit'/></a>\n",
478 ctx.cfg.logo); 480 ctx->cfg.logo);
479 html("</td></tr>\n<tr><td class='sidebar'>\n"); 481 html("</td></tr>\n<tr><td class='sidebar'>\n");
480 if (ctx.repo) { 482 if (ctx->repo) {
481 html("<h1 class='first'>"); 483 html("<h1 class='first'>");
482 html_txt(strrpart(ctx.repo->name, 20)); 484 html_txt(strrpart(ctx->repo->name, 20));
483 html("</h1>\n"); 485 html("</h1>\n");
484 html_txt(ctx.repo->desc); 486 html_txt(ctx->repo->desc);
485 if (ctx.repo->owner) { 487 if (ctx->repo->owner) {
486 html("<h1>owner</h1>\n"); 488 html("<h1>owner</h1>\n");
487 html_txt(ctx.repo->owner); 489 html_txt(ctx->repo->owner);
488 } 490 }
489 html("<h1>navigate</h1>\n"); 491 html("<h1>navigate</h1>\n");
490 reporevlink(NULL, "summary", NULL, "menu", ctx.qry.head, 492 reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head,
491 NULL, NULL); 493 NULL, NULL);
492 cgit_log_link("log", NULL, "menu", ctx.qry.head, NULL, NULL, 494 cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL,
493 0, NULL, NULL); 495 0, NULL, NULL);
494 cgit_tree_link("tree", NULL, "menu", ctx.qry.head, 496 cgit_tree_link("tree", NULL, "menu", ctx->qry.head,
495 ctx.qry.sha1, NULL); 497 ctx->qry.sha1, NULL);
496 cgit_commit_link("commit", NULL, "menu", ctx.qry.head, 498 cgit_commit_link("commit", NULL, "menu", ctx->qry.head,
497 ctx.qry.sha1); 499 ctx->qry.sha1);
498 cgit_diff_link("diff", NULL, "menu", ctx.qry.head, 500 cgit_diff_link("diff", NULL, "menu", ctx->qry.head,
499 ctx.qry.sha1, ctx.qry.sha2, NULL); 501 ctx->qry.sha1, ctx->qry.sha2, NULL);
500 cgit_patch_link("patch", NULL, "menu", ctx.qry.head, 502 cgit_patch_link("patch", NULL, "menu", ctx->qry.head,
501 ctx.qry.sha1); 503 ctx->qry.sha1);
502 504
503 for_each_ref(print_archive_ref, &header); 505 for_each_ref(print_archive_ref, &header);
504 506
505 if (ctx.repo->clone_url || ctx.cfg.clone_prefix) { 507 if (ctx->repo->clone_url || ctx->cfg.clone_prefix) {
506 html("<h1>clone</h1>\n"); 508 html("<h1>clone</h1>\n");
507 if (ctx.repo->clone_url) 509 if (ctx->repo->clone_url)
508 url = ctx.repo->clone_url; 510 url = ctx->repo->clone_url;
509 else 511 else
510 url = fmt("%s%s", ctx.cfg.clone_prefix, 512 url = fmt("%s%s", ctx->cfg.clone_prefix,
511 ctx.repo->url); 513 ctx->repo->url);
512 html("<a class='menu' href='"); 514 html("<a class='menu' href='");
513 html_attr(url); 515 html_attr(url);
514 html("' title='"); 516 html("' title='");
515 html_attr(url); 517 html_attr(url);
516 html("'>\n"); 518 html("'>\n");
517 html_txt(strrpart(url, 20)); 519 html_txt(strrpart(url, 20));
518 html("</a>\n"); 520 html("</a>\n");
519 } 521 }
520 522
521 html("<h1>branch</h1>\n"); 523 html("<h1>branch</h1>\n");
522 html("<form method='get' action=''>\n"); 524 html("<form method='get' action=''>\n");
523 add_hidden_formfields(0, 1, ctx.qry.page); 525 add_hidden_formfields(0, 1, ctx->qry.page);
524 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>"); 526 // html("<table summary='branch selector' class='grid'><tr><td id='branch-dropdown-cell'>");
525 html("<select name='h' onchange='this.form.submit();'>\n"); 527 html("<select name='h' onchange='this.form.submit();'>\n");
526 for_each_branch_ref(print_branch_option, ctx.qry.head); 528 for_each_branch_ref(print_branch_option, ctx->qry.head);
527 html("</select>\n"); 529 html("</select>\n");
528 // html("</td><td>"); 530 // html("</td><td>");
529 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n"); 531 html("<noscript><input type='submit' id='switch-btn' value='switch'/></noscript>\n");
530 // html("</td></tr></table>"); 532 // html("</td></tr></table>");
531 html("</form>\n"); 533 html("</form>\n");
532 534
533 html("<h1>search</h1>\n"); 535 html("<h1>search</h1>\n");
534 html("<form method='get' action='"); 536 html("<form method='get' action='");
535 if (ctx.cfg.virtual_root) 537 if (ctx->cfg.virtual_root)
536 html_attr(cgit_fileurl(ctx.qry.repo, "log", 538 html_attr(cgit_fileurl(ctx->qry.repo, "log",
537 ctx.qry.path, NULL)); 539 ctx->qry.path, NULL));
538 html("'>\n"); 540 html("'>\n");
539 add_hidden_formfields(1, 0, "log"); 541 add_hidden_formfields(1, 0, "log");
540 html("<select name='qt'>\n"); 542 html("<select name='qt'>\n");
541 html_option("grep", "log msg", ctx.qry.grep); 543 html_option("grep", "log msg", ctx->qry.grep);
542 html_option("author", "author", ctx.qry.grep); 544 html_option("author", "author", ctx->qry.grep);
543 html_option("committer", "committer", ctx.qry.grep); 545 html_option("committer", "committer", ctx->qry.grep);
544 html("</select>\n"); 546 html("</select>\n");
545 html("<input class='txt' type='text' name='q' value='"); 547 html("<input class='txt' type='text' name='q' value='");
546 html_attr(ctx.qry.search); 548 html_attr(ctx->qry.search);
547 html("'/>\n"); 549 html("'/>\n");
548 html("</form>\n"); 550 html("</form>\n");
549 } else { 551 } else {
550 if (!ctx.cfg.index_info || html_include(ctx.cfg.index_info)) 552 if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info))
551 html(default_info); 553 html(default_info);
552 } 554 }
553 555
554 html("</td></tr></table></td>\n"); 556 html("</td></tr></table></td>\n");
555 557
556 html("<td id='content'>\n"); 558 html("<td id='content'>\n");
557} 559}
558 560
559
560void cgit_print_snapshot_start(const char *mimetype, const char *filename,
561 struct cacheitem *item)
562{
563 htmlf("Content-Type: %s\n", mimetype);
564 htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename);
565 htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime));
566 htmlf("Expires: %s\n", http_date(item->st.st_mtime +
567 ttl_seconds(item->ttl)));
568 html("\n");
569}
570
571void cgit_print_filemode(unsigned short mode) 561void cgit_print_filemode(unsigned short mode)
572{ 562{
573 if (S_ISDIR(mode)) 563 if (S_ISDIR(mode))
574 html("d"); 564 html("d");
575 else if (S_ISLNK(mode)) 565 else if (S_ISLNK(mode))
576 html("l"); 566 html("l");
577 else if (S_ISGITLINK(mode)) 567 else if (S_ISGITLINK(mode))
578 html("m"); 568 html("m");
579 else 569 else
580 html("-"); 570 html("-");
581 html_fileperm(mode >> 6); 571 html_fileperm(mode >> 6);
582 html_fileperm(mode >> 3); 572 html_fileperm(mode >> 3);
583 html_fileperm(mode); 573 html_fileperm(mode);
584} 574}
585 575
586/* vim:set sw=8: */ 576/* vim:set sw=8: */
diff --git a/ui-snapshot.c b/ui-snapshot.c
index 67dbbdd..4449803 100644
--- a/ui-snapshot.c
+++ b/ui-snapshot.c
@@ -8,151 +8,153 @@
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "html.h" 10#include "html.h"
11 11
12static int write_compressed_tar_archive(struct archiver_args *args,const char *filter) 12static int write_compressed_tar_archive(struct archiver_args *args,const char *filter)
13{ 13{
14 int rw[2]; 14 int rw[2];
15 pid_t gzpid; 15 pid_t gzpid;
16 int stdout2; 16 int stdout2;
17 int status; 17 int status;
18 int rv; 18 int rv;
19 19
20 stdout2 = chk_non_negative(dup(STDIN_FILENO), "Preserving STDOUT before compressing"); 20 stdout2 = chk_non_negative(dup(STDIN_FILENO), "Preserving STDOUT before compressing");
21 chk_zero(pipe(rw), "Opening pipe from compressor subprocess"); 21 chk_zero(pipe(rw), "Opening pipe from compressor subprocess");
22 gzpid = chk_non_negative(fork(), "Forking compressor subprocess"); 22 gzpid = chk_non_negative(fork(), "Forking compressor subprocess");
23 if(gzpid==0) { 23 if(gzpid==0) {
24 /* child */ 24 /* child */
25 chk_zero(close(rw[1]), "Closing write end of pipe in child"); 25 chk_zero(close(rw[1]), "Closing write end of pipe in child");
26 chk_zero(close(STDIN_FILENO), "Closing STDIN"); 26 chk_zero(close(STDIN_FILENO), "Closing STDIN");
27 chk_non_negative(dup2(rw[0],STDIN_FILENO), "Redirecting compressor input to stdin"); 27 chk_non_negative(dup2(rw[0],STDIN_FILENO), "Redirecting compressor input to stdin");
28 execlp(filter,filter,NULL); 28 execlp(filter,filter,NULL);
29 _exit(-1); 29 _exit(-1);
30 } 30 }
31 /* parent */ 31 /* parent */
32 chk_zero(close(rw[0]), "Closing read end of pipe"); 32 chk_zero(close(rw[0]), "Closing read end of pipe");
33 chk_non_negative(dup2(rw[1],STDOUT_FILENO), "Redirecting output to compressor"); 33 chk_non_negative(dup2(rw[1],STDOUT_FILENO), "Redirecting output to compressor");
34 34
35 rv = write_tar_archive(args); 35 rv = write_tar_archive(args);
36 36
37 chk_zero(close(STDOUT_FILENO), "Closing STDOUT redirected to compressor"); 37 chk_zero(close(STDOUT_FILENO), "Closing STDOUT redirected to compressor");
38 chk_non_negative(dup2(stdout2,STDOUT_FILENO), "Restoring uncompressed STDOUT"); 38 chk_non_negative(dup2(stdout2,STDOUT_FILENO), "Restoring uncompressed STDOUT");
39 chk_zero(close(stdout2), "Closing uncompressed STDOUT"); 39 chk_zero(close(stdout2), "Closing uncompressed STDOUT");
40 chk_zero(close(rw[1]), "Closing write end of pipe in parent"); 40 chk_zero(close(rw[1]), "Closing write end of pipe in parent");
41 chk_positive(waitpid(gzpid,&status,0), "Waiting on compressor process"); 41 chk_positive(waitpid(gzpid,&status,0), "Waiting on compressor process");
42 if(! ( WIFEXITED(status) && WEXITSTATUS(status)==0 ) ) 42 if(! ( WIFEXITED(status) && WEXITSTATUS(status)==0 ) )
43 cgit_print_error("Failed to compress archive"); 43 cgit_print_error("Failed to compress archive");
44 44
45 return rv; 45 return rv;
46} 46}
47 47
48static int write_tar_gzip_archive(struct archiver_args *args) 48static int write_tar_gzip_archive(struct archiver_args *args)
49{ 49{
50 return write_compressed_tar_archive(args,"gzip"); 50 return write_compressed_tar_archive(args,"gzip");
51} 51}
52 52
53static int write_tar_bzip2_archive(struct archiver_args *args) 53static int write_tar_bzip2_archive(struct archiver_args *args)
54{ 54{
55 return write_compressed_tar_archive(args,"bzip2"); 55 return write_compressed_tar_archive(args,"bzip2");
56} 56}
57 57
58static const struct snapshot_archive_t { 58static const struct snapshot_archive_t {
59 const char *suffix; 59 const char *suffix;
60 const char *mimetype; 60 const char *mimetype;
61 write_archive_fn_t write_func; 61 write_archive_fn_t write_func;
62 int bit; 62 int bit;
63 }snapshot_archives[] = { 63 }snapshot_archives[] = {
64 { ".zip", "application/x-zip", write_zip_archive, 0x1 }, 64 { ".zip", "application/x-zip", write_zip_archive, 0x1 },
65 { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, 65 { ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 },
66 { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, 66 { ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 },
67 { ".tar", "application/x-tar", write_tar_archive, 0x8 } 67 { ".tar", "application/x-tar", write_tar_archive, 0x8 }
68}; 68};
69 69
70#define snapshot_archives_len (sizeof(snapshot_archives) / sizeof(*snapshot_archives)) 70#define snapshot_archives_len (sizeof(snapshot_archives) / sizeof(*snapshot_archives))
71 71
72void cgit_print_snapshot(struct cacheitem *item, const char *head, 72void cgit_print_snapshot(struct cacheitem *item, const char *head,
73 const char *hex, const char *prefix, 73 const char *hex, const char *prefix,
74 const char *filename, int snapshots) 74 const char *filename, int snapshots)
75{ 75{
76 const struct snapshot_archive_t* sat; 76 const struct snapshot_archive_t* sat;
77 struct archiver_args args; 77 struct archiver_args args;
78 struct commit *commit; 78 struct commit *commit;
79 unsigned char sha1[20]; 79 unsigned char sha1[20];
80 int f, sl, fnl = strlen(filename); 80 int f, sl, fnl = strlen(filename);
81 81
82 for(f=0; f<snapshot_archives_len; f++) { 82 for(f=0; f<snapshot_archives_len; f++) {
83 sat = &snapshot_archives[f]; 83 sat = &snapshot_archives[f];
84 if(!(snapshots & sat->bit)) 84 if(!(snapshots & sat->bit))
85 continue; 85 continue;
86 sl = strlen(sat->suffix); 86 sl = strlen(sat->suffix);
87 if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix)) 87 if(fnl<sl || strcmp(&filename[fnl-sl],sat->suffix))
88 continue; 88 continue;
89 if (!hex) 89 if (!hex)
90 hex = head; 90 hex = head;
91 if(get_sha1(hex, sha1)) { 91 if(get_sha1(hex, sha1)) {
92 cgit_print_error(fmt("Bad object id: %s", hex)); 92 cgit_print_error(fmt("Bad object id: %s", hex));
93 return; 93 return;
94 } 94 }
95 commit = lookup_commit_reference(sha1); 95 commit = lookup_commit_reference(sha1);
96 if(!commit) { 96 if(!commit) {
97 cgit_print_error(fmt("Not a commit reference: %s", hex)); 97 cgit_print_error(fmt("Not a commit reference: %s", hex));
98 return;; 98 return;;
99 } 99 }
100 memset(&args,0,sizeof(args)); 100 memset(&args,0,sizeof(args));
101 args.base = fmt("%s/", prefix); 101 args.base = fmt("%s/", prefix);
102 args.tree = commit->tree; 102 args.tree = commit->tree;
103 args.time = commit->date; 103 args.time = commit->date;
104 cgit_print_snapshot_start(sat->mimetype, filename, item); 104 ctx.page.mimetype = xstrdup(sat->mimetype);
105 ctx.page.filename = xstrdup(filename);
106 cgit_print_http_headers(&ctx);
105 (*sat->write_func)(&args); 107 (*sat->write_func)(&args);
106 return; 108 return;
107 } 109 }
108 cgit_print_error(fmt("Unsupported snapshot format: %s", filename)); 110 cgit_print_error(fmt("Unsupported snapshot format: %s", filename));
109} 111}
110 112
111void cgit_print_snapshot_links(const char *repo, const char *head, 113void cgit_print_snapshot_links(const char *repo, const char *head,
112 const char *hex, int snapshots) 114 const char *hex, int snapshots)
113{ 115{
114 const struct snapshot_archive_t* sat; 116 const struct snapshot_archive_t* sat;
115 char *filename; 117 char *filename;
116 int f; 118 int f;
117 119
118 for(f=0; f<snapshot_archives_len; f++) { 120 for(f=0; f<snapshot_archives_len; f++) {
119 sat = &snapshot_archives[f]; 121 sat = &snapshot_archives[f];
120 if(!(snapshots & sat->bit)) 122 if(!(snapshots & sat->bit))
121 continue; 123 continue;
122 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 124 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
123 sat->suffix); 125 sat->suffix);
124 cgit_snapshot_link(filename, NULL, NULL, (char *)head, 126 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
125 (char *)hex, filename); 127 (char *)hex, filename);
126 html("<br/>"); 128 html("<br/>");
127 } 129 }
128} 130}
129 131
130int cgit_parse_snapshots_mask(const char *str) 132int cgit_parse_snapshots_mask(const char *str)
131{ 133{
132 const struct snapshot_archive_t* sat; 134 const struct snapshot_archive_t* sat;
133 static const char *delim = " \t,:/|;"; 135 static const char *delim = " \t,:/|;";
134 int f, tl, sl, rv = 0; 136 int f, tl, sl, rv = 0;
135 137
136 /* favor legacy setting */ 138 /* favor legacy setting */
137 if(atoi(str)) 139 if(atoi(str))
138 return 1; 140 return 1;
139 for(;;) { 141 for(;;) {
140 str += strspn(str,delim); 142 str += strspn(str,delim);
141 tl = strcspn(str,delim); 143 tl = strcspn(str,delim);
142 if(!tl) 144 if(!tl)
143 break; 145 break;
144 for(f=0; f<snapshot_archives_len; f++) { 146 for(f=0; f<snapshot_archives_len; f++) {
145 sat = &snapshot_archives[f]; 147 sat = &snapshot_archives[f];
146 sl = strlen(sat->suffix); 148 sl = strlen(sat->suffix);
147 if((tl == sl && !strncmp(sat->suffix, str, tl)) || 149 if((tl == sl && !strncmp(sat->suffix, str, tl)) ||
148 (tl == sl-1 && !strncmp(sat->suffix+1, str, tl-1))) { 150 (tl == sl-1 && !strncmp(sat->suffix+1, str, tl-1))) {
149 rv |= sat->bit; 151 rv |= sat->bit;
150 break; 152 break;
151 } 153 }
152 } 154 }
153 str += tl; 155 str += tl;
154 } 156 }
155 return rv; 157 return rv;
156} 158}
157 159
158/* vim:set sw=8: */ 160/* vim:set sw=8: */