-rw-r--r-- | ui-shared.c | 237 |
1 files changed, 129 insertions, 108 deletions
diff --git a/ui-shared.c b/ui-shared.c index 60aa2e3..aa65988 100644 --- a/ui-shared.c +++ b/ui-shared.c | |||
@@ -1,570 +1,591 @@ | |||
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 | 11 | ||
11 | const char cgit_doctype[] = | 12 | const char cgit_doctype[] = |
12 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" | 13 | "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
13 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; | 14 | " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; |
14 | 15 | ||
15 | static char *http_date(time_t t) | 16 | static char *http_date(time_t t) |
16 | { | 17 | { |
17 | static char day[][4] = | 18 | static char day[][4] = |
18 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | 19 | {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
19 | static char month[][4] = | 20 | static char month[][4] = |
20 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | 21 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
21 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; | 22 | "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; |
22 | struct tm *tm = gmtime(&t); | 23 | struct tm *tm = gmtime(&t); |
23 | 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], |
24 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, | 25 | tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, |
25 | tm->tm_hour, tm->tm_min, tm->tm_sec); | 26 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
26 | } | 27 | } |
27 | 28 | ||
28 | static long ttl_seconds(long ttl) | ||
29 | { | ||
30 | if (ttl<0) | ||
31 | return 60 * 60 * 24 * 365; | ||
32 | else | ||
33 | return ttl * 60; | ||
34 | } | ||
35 | |||
36 | void cgit_print_error(char *msg) | 29 | void cgit_print_error(char *msg) |
37 | { | 30 | { |
38 | html("<div class='error'>"); | 31 | html("<div class='error'>"); |
39 | html_txt(msg); | 32 | html_txt(msg); |
40 | html("</div>\n"); | 33 | html("</div>\n"); |
41 | } | 34 | } |
42 | 35 | ||
43 | char *cgit_rooturl() | 36 | char *cgit_rooturl() |
44 | { | 37 | { |
45 | if (cgit_virtual_root) | 38 | if (ctx.cfg.virtual_root) |
46 | return fmt("%s/", cgit_virtual_root); | 39 | return fmt("%s/", ctx.cfg.virtual_root); |
47 | else | 40 | else |
48 | return cgit_script_name; | 41 | return ctx.cfg.script_name; |
49 | } | 42 | } |
50 | 43 | ||
51 | char *cgit_repourl(const char *reponame) | 44 | char *cgit_repourl(const char *reponame) |
52 | { | 45 | { |
53 | if (cgit_virtual_root) { | 46 | if (ctx.cfg.virtual_root) { |
54 | return fmt("%s/%s/", cgit_virtual_root, reponame); | 47 | return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); |
55 | } else { | 48 | } else { |
56 | return fmt("?r=%s", reponame); | 49 | return fmt("?r=%s", reponame); |
57 | } | 50 | } |
58 | } | 51 | } |
59 | 52 | ||
60 | char *cgit_fileurl(const char *reponame, const char *pagename, | 53 | char *cgit_fileurl(const char *reponame, const char *pagename, |
61 | const char *filename, const char *query) | 54 | const char *filename, const char *query) |
62 | { | 55 | { |
63 | char *tmp; | 56 | char *tmp; |
64 | char *delim; | 57 | char *delim; |
65 | 58 | ||
66 | if (cgit_virtual_root) { | 59 | if (ctx.cfg.virtual_root) { |
67 | tmp = fmt("%s/%s/%s/%s", cgit_virtual_root, reponame, | 60 | tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, |
68 | pagename, (filename ? filename:"")); | 61 | pagename, (filename ? filename:"")); |
69 | delim = "?"; | 62 | delim = "?"; |
70 | } else { | 63 | } else { |
71 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, | 64 | tmp = fmt("?url=%s/%s/%s", reponame, pagename, |
72 | (filename ? filename : "")); | 65 | (filename ? filename : "")); |
73 | delim = "&"; | 66 | delim = "&"; |
74 | } | 67 | } |
75 | if (query) | 68 | if (query) |
76 | tmp = fmt("%s%s%s", tmp, delim, query); | 69 | tmp = fmt("%s%s%s", tmp, delim, query); |
77 | return tmp; | 70 | return tmp; |
78 | } | 71 | } |
79 | 72 | ||
80 | char *cgit_pageurl(const char *reponame, const char *pagename, | 73 | char *cgit_pageurl(const char *reponame, const char *pagename, |
81 | const char *query) | 74 | const char *query) |
82 | { | 75 | { |
83 | return cgit_fileurl(reponame,pagename,0,query); | 76 | return cgit_fileurl(reponame,pagename,0,query); |
84 | } | 77 | } |
85 | 78 | ||
86 | const char *cgit_repobasename(const char *reponame) | 79 | const char *cgit_repobasename(const char *reponame) |
87 | { | 80 | { |
88 | /* 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 */ |
89 | static char rvbuf[1024]; | 82 | static char rvbuf[1024]; |
90 | int p; | 83 | int p; |
91 | const char *rv; | 84 | const char *rv; |
92 | strncpy(rvbuf,reponame,sizeof(rvbuf)); | 85 | strncpy(rvbuf,reponame,sizeof(rvbuf)); |
93 | if(rvbuf[sizeof(rvbuf)-1]) | 86 | if(rvbuf[sizeof(rvbuf)-1]) |
94 | die("cgit_repobasename: truncated repository name '%s'", reponame); | 87 | die("cgit_repobasename: truncated repository name '%s'", reponame); |
95 | p = strlen(rvbuf)-1; | 88 | p = strlen(rvbuf)-1; |
96 | /* strip trailing slashes */ | 89 | /* strip trailing slashes */ |
97 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; | 90 | while(p && rvbuf[p]=='/') rvbuf[p--]=0; |
98 | /* strip trailing .git */ | 91 | /* strip trailing .git */ |
99 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { | 92 | if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { |
100 | p -= 3; rvbuf[p--] = 0; | 93 | p -= 3; rvbuf[p--] = 0; |
101 | } | 94 | } |
102 | /* strip more trailing slashes if any */ | 95 | /* strip more trailing slashes if any */ |
103 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; | 96 | while( p && rvbuf[p]=='/') rvbuf[p--]=0; |
104 | /* find last slash in the remaining string */ | 97 | /* find last slash in the remaining string */ |
105 | rv = strrchr(rvbuf,'/'); | 98 | rv = strrchr(rvbuf,'/'); |
106 | if(rv) | 99 | if(rv) |
107 | return ++rv; | 100 | return ++rv; |
108 | return rvbuf; | 101 | return rvbuf; |
109 | } | 102 | } |
110 | 103 | ||
111 | char *cgit_currurl() | 104 | char *cgit_currurl() |
112 | { | 105 | { |
113 | if (!cgit_virtual_root) | 106 | if (!ctx.cfg.virtual_root) |
114 | return cgit_script_name; | 107 | return ctx.cfg.script_name; |
115 | else if (cgit_query_page) | 108 | else if (ctx.qry.page) |
116 | return fmt("%s/%s/%s/", cgit_virtual_root, cgit_query_repo, cgit_query_page); | 109 | return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); |
117 | else if (cgit_query_repo) | 110 | else if (ctx.qry.repo) |
118 | return fmt("%s/%s/", cgit_virtual_root, cgit_query_repo); | 111 | return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); |
119 | else | 112 | else |
120 | return fmt("%s/", cgit_virtual_root); | 113 | return fmt("%s/", ctx.cfg.virtual_root); |
121 | } | 114 | } |
122 | 115 | ||
123 | static char *repolink(char *title, char *class, char *page, char *head, | 116 | static char *repolink(char *title, char *class, char *page, char *head, |
124 | char *path) | 117 | char *path) |
125 | { | 118 | { |
126 | char *delim = "?"; | 119 | char *delim = "?"; |
127 | 120 | ||
128 | html("<a"); | 121 | html("<a"); |
129 | if (title) { | 122 | if (title) { |
130 | html(" title='"); | 123 | html(" title='"); |
131 | html_attr(title); | 124 | html_attr(title); |
132 | html("'"); | 125 | html("'"); |
133 | } | 126 | } |
134 | if (class) { | 127 | if (class) { |
135 | html(" class='"); | 128 | html(" class='"); |
136 | html_attr(class); | 129 | html_attr(class); |
137 | html("'"); | 130 | html("'"); |
138 | } | 131 | } |
139 | html(" href='"); | 132 | html(" href='"); |
140 | if (cgit_virtual_root) { | 133 | if (ctx.cfg.virtual_root) { |
141 | html_attr(cgit_virtual_root); | 134 | html_attr(ctx.cfg.virtual_root); |
142 | if (cgit_virtual_root[strlen(cgit_virtual_root) - 1] != '/') | 135 | if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') |
143 | html("/"); | 136 | html("/"); |
144 | html_attr(cgit_repo->url); | 137 | html_attr(ctx.repo->url); |
145 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 138 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
146 | html("/"); | 139 | html("/"); |
147 | if (page) { | 140 | if (page) { |
148 | html(page); | 141 | html(page); |
149 | html("/"); | 142 | html("/"); |
150 | if (path) | 143 | if (path) |
151 | html_attr(path); | 144 | html_attr(path); |
152 | } | 145 | } |
153 | } else { | 146 | } else { |
154 | html(cgit_script_name); | 147 | html(ctx.cfg.script_name); |
155 | html("?url="); | 148 | html("?url="); |
156 | html_attr(cgit_repo->url); | 149 | html_attr(ctx.repo->url); |
157 | if (cgit_repo->url[strlen(cgit_repo->url) - 1] != '/') | 150 | if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') |
158 | html("/"); | 151 | html("/"); |
159 | if (page) { | 152 | if (page) { |
160 | html(page); | 153 | html(page); |
161 | html("/"); | 154 | html("/"); |
162 | if (path) | 155 | if (path) |
163 | html_attr(path); | 156 | html_attr(path); |
164 | } | 157 | } |
165 | delim = "&"; | 158 | delim = "&"; |
166 | } | 159 | } |
167 | if (head && strcmp(head, cgit_repo->defbranch)) { | 160 | if (head && strcmp(head, ctx.repo->defbranch)) { |
168 | html(delim); | 161 | html(delim); |
169 | html("h="); | 162 | html("h="); |
170 | html_attr(head); | 163 | html_attr(head); |
171 | delim = "&"; | 164 | delim = "&"; |
172 | } | 165 | } |
173 | return fmt("%s", delim); | 166 | return fmt("%s", delim); |
174 | } | 167 | } |
175 | 168 | ||
176 | static void reporevlink(char *page, char *name, char *title, char *class, | 169 | static void reporevlink(char *page, char *name, char *title, char *class, |
177 | char *head, char *rev, char *path) | 170 | char *head, char *rev, char *path) |
178 | { | 171 | { |
179 | char *delim; | 172 | char *delim; |
180 | 173 | ||
181 | delim = repolink(title, class, page, head, path); | 174 | delim = repolink(title, class, page, head, path); |
182 | if (rev && strcmp(rev, cgit_query_head)) { | 175 | if (rev && strcmp(rev, ctx.qry.head)) { |
183 | html(delim); | 176 | html(delim); |
184 | html("id="); | 177 | html("id="); |
185 | html_attr(rev); | 178 | html_attr(rev); |
186 | } | 179 | } |
187 | html("'>"); | 180 | html("'>"); |
188 | html_txt(name); | 181 | html_txt(name); |
189 | html("</a>"); | 182 | html("</a>"); |
190 | } | 183 | } |
191 | 184 | ||
192 | void cgit_tree_link(char *name, char *title, char *class, char *head, | 185 | void cgit_tree_link(char *name, char *title, char *class, char *head, |
193 | char *rev, char *path) | 186 | char *rev, char *path) |
194 | { | 187 | { |
195 | reporevlink("tree", name, title, class, head, rev, path); | 188 | reporevlink("tree", name, title, class, head, rev, path); |
196 | } | 189 | } |
197 | 190 | ||
198 | void cgit_log_link(char *name, char *title, char *class, char *head, | 191 | void cgit_log_link(char *name, char *title, char *class, char *head, |
199 | char *rev, char *path, int ofs, char *grep, char *pattern) | 192 | char *rev, char *path, int ofs, char *grep, char *pattern) |
200 | { | 193 | { |
201 | char *delim; | 194 | char *delim; |
202 | 195 | ||
203 | delim = repolink(title, class, "log", head, path); | 196 | delim = repolink(title, class, "log", head, path); |
204 | if (rev && strcmp(rev, cgit_query_head)) { | 197 | if (rev && strcmp(rev, ctx.qry.head)) { |
205 | html(delim); | 198 | html(delim); |
206 | html("id="); | 199 | html("id="); |
207 | html_attr(rev); | 200 | html_attr(rev); |
208 | delim = "&"; | 201 | delim = "&"; |
209 | } | 202 | } |
210 | if (grep && pattern) { | 203 | if (grep && pattern) { |
211 | html(delim); | 204 | html(delim); |
212 | html("qt="); | 205 | html("qt="); |
213 | html_attr(grep); | 206 | html_attr(grep); |
214 | delim = "&"; | 207 | delim = "&"; |
215 | html(delim); | 208 | html(delim); |
216 | html("q="); | 209 | html("q="); |
217 | html_attr(pattern); | 210 | html_attr(pattern); |
218 | } | 211 | } |
219 | if (ofs > 0) { | 212 | if (ofs > 0) { |
220 | html(delim); | 213 | html(delim); |
221 | html("ofs="); | 214 | html("ofs="); |
222 | htmlf("%d", ofs); | 215 | htmlf("%d", ofs); |
223 | } | 216 | } |
224 | html("'>"); | 217 | html("'>"); |
225 | html_txt(name); | 218 | html_txt(name); |
226 | html("</a>"); | 219 | html("</a>"); |
227 | } | 220 | } |
228 | 221 | ||
229 | void cgit_commit_link(char *name, char *title, char *class, char *head, | 222 | void cgit_commit_link(char *name, char *title, char *class, char *head, |
230 | char *rev) | 223 | char *rev) |
231 | { | 224 | { |
232 | if (strlen(name) > cgit_max_msg_len && cgit_max_msg_len >= 15) { | 225 | if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { |
233 | name[cgit_max_msg_len] = '\0'; | 226 | name[ctx.cfg.max_msg_len] = '\0'; |
234 | name[cgit_max_msg_len - 1] = '.'; | 227 | name[ctx.cfg.max_msg_len - 1] = '.'; |
235 | name[cgit_max_msg_len - 2] = '.'; | 228 | name[ctx.cfg.max_msg_len - 2] = '.'; |
236 | name[cgit_max_msg_len - 3] = '.'; | 229 | name[ctx.cfg.max_msg_len - 3] = '.'; |
237 | } | 230 | } |
238 | reporevlink("commit", name, title, class, head, rev, NULL); | 231 | reporevlink("commit", name, title, class, head, rev, NULL); |
239 | } | 232 | } |
240 | 233 | ||
241 | void cgit_refs_link(char *name, char *title, char *class, char *head, | 234 | void cgit_refs_link(char *name, char *title, char *class, char *head, |
242 | char *rev, char *path) | 235 | char *rev, char *path) |
243 | { | 236 | { |
244 | reporevlink("refs", name, title, class, head, rev, path); | 237 | reporevlink("refs", name, title, class, head, rev, path); |
245 | } | 238 | } |
246 | 239 | ||
247 | void cgit_snapshot_link(char *name, char *title, char *class, char *head, | 240 | void cgit_snapshot_link(char *name, char *title, char *class, char *head, |
248 | char *rev, char *archivename) | 241 | char *rev, char *archivename) |
249 | { | 242 | { |
250 | reporevlink("snapshot", name, title, class, head, rev, archivename); | 243 | reporevlink("snapshot", name, title, class, head, rev, archivename); |
251 | } | 244 | } |
252 | 245 | ||
253 | void cgit_diff_link(char *name, char *title, char *class, char *head, | 246 | void cgit_diff_link(char *name, char *title, char *class, char *head, |
254 | char *new_rev, char *old_rev, char *path) | 247 | char *new_rev, char *old_rev, char *path) |
255 | { | 248 | { |
256 | char *delim; | 249 | char *delim; |
257 | 250 | ||
258 | delim = repolink(title, class, "diff", head, path); | 251 | delim = repolink(title, class, "diff", head, path); |
259 | if (new_rev && strcmp(new_rev, cgit_query_head)) { | 252 | if (new_rev && strcmp(new_rev, ctx.qry.head)) { |
260 | html(delim); | 253 | html(delim); |
261 | html("id="); | 254 | html("id="); |
262 | html_attr(new_rev); | 255 | html_attr(new_rev); |
263 | delim = "&"; | 256 | delim = "&"; |
264 | } | 257 | } |
265 | if (old_rev) { | 258 | if (old_rev) { |
266 | html(delim); | 259 | html(delim); |
267 | html("id2="); | 260 | html("id2="); |
268 | html_attr(old_rev); | 261 | html_attr(old_rev); |
269 | } | 262 | } |
270 | html("'>"); | 263 | html("'>"); |
271 | html_txt(name); | 264 | html_txt(name); |
272 | html("</a>"); | 265 | html("</a>"); |
273 | } | 266 | } |
274 | 267 | ||
275 | void cgit_patch_link(char *name, char *title, char *class, char *head, | 268 | void cgit_patch_link(char *name, char *title, char *class, char *head, |
276 | char *rev) | 269 | char *rev) |
277 | { | 270 | { |
278 | reporevlink("patch", name, title, class, head, rev, NULL); | 271 | reporevlink("patch", name, title, class, head, rev, NULL); |
279 | } | 272 | } |
280 | 273 | ||
281 | void cgit_object_link(struct object *obj) | 274 | void cgit_object_link(struct object *obj) |
282 | { | 275 | { |
283 | char *page, *arg, *url; | 276 | char *page, *arg, *url; |
284 | 277 | ||
285 | if (obj->type == OBJ_COMMIT) { | 278 | if (obj->type == OBJ_COMMIT) { |
286 | 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, |
287 | cgit_query_head, sha1_to_hex(obj->sha1)); | 280 | ctx.qry.head, sha1_to_hex(obj->sha1)); |
288 | return; | 281 | return; |
289 | } else if (obj->type == OBJ_TREE) { | 282 | } else if (obj->type == OBJ_TREE) { |
290 | page = "tree"; | 283 | page = "tree"; |
291 | arg = "id"; | 284 | arg = "id"; |
292 | } else if (obj->type == OBJ_TAG) { | 285 | } else if (obj->type == OBJ_TAG) { |
293 | page = "tag"; | 286 | page = "tag"; |
294 | arg = "id"; | 287 | arg = "id"; |
295 | } else { | 288 | } else { |
296 | page = "blob"; | 289 | page = "blob"; |
297 | arg = "id"; | 290 | arg = "id"; |
298 | } | 291 | } |
299 | 292 | ||
300 | url = cgit_pageurl(cgit_query_repo, page, | 293 | url = cgit_pageurl(ctx.qry.repo, page, |
301 | fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); | 294 | fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); |
302 | html_link_open(url, NULL, NULL); | 295 | html_link_open(url, NULL, NULL); |
303 | htmlf("%s %s", typename(obj->type), | 296 | htmlf("%s %s", typename(obj->type), |
304 | sha1_to_hex(obj->sha1)); | 297 | sha1_to_hex(obj->sha1)); |
305 | html_link_close(); | 298 | html_link_close(); |
306 | } | 299 | } |
307 | 300 | ||
308 | void cgit_print_date(time_t secs, char *format) | 301 | void cgit_print_date(time_t secs, char *format) |
309 | { | 302 | { |
310 | char buf[64]; | 303 | char buf[64]; |
311 | struct tm *time; | 304 | struct tm *time; |
312 | 305 | ||
313 | if (!secs) | 306 | if (!secs) |
314 | return; | 307 | return; |
315 | time = gmtime(&secs); | 308 | time = gmtime(&secs); |
316 | strftime(buf, sizeof(buf)-1, format, time); | 309 | strftime(buf, sizeof(buf)-1, format, time); |
317 | html_txt(buf); | 310 | html_txt(buf); |
318 | } | 311 | } |
319 | 312 | ||
320 | void cgit_print_age(time_t t, time_t max_relative, char *format) | 313 | void cgit_print_age(time_t t, time_t max_relative, char *format) |
321 | { | 314 | { |
322 | time_t now, secs; | 315 | time_t now, secs; |
323 | 316 | ||
324 | if (!t) | 317 | if (!t) |
325 | return; | 318 | return; |
326 | time(&now); | 319 | time(&now); |
327 | secs = now - t; | 320 | secs = now - t; |
328 | 321 | ||
329 | if (secs > max_relative && max_relative >= 0) { | 322 | if (secs > max_relative && max_relative >= 0) { |
330 | cgit_print_date(t, format); | 323 | cgit_print_date(t, format); |
331 | return; | 324 | return; |
332 | } | 325 | } |
333 | 326 | ||
334 | if (secs < TM_HOUR * 2) { | 327 | if (secs < TM_HOUR * 2) { |
335 | htmlf("<span class='age-mins'>%.0f min.</span>", | 328 | htmlf("<span class='age-mins'>%.0f min.</span>", |
336 | secs * 1.0 / TM_MIN); | 329 | secs * 1.0 / TM_MIN); |
337 | return; | 330 | return; |
338 | } | 331 | } |
339 | if (secs < TM_DAY * 2) { | 332 | if (secs < TM_DAY * 2) { |
340 | htmlf("<span class='age-hours'>%.0f hours</span>", | 333 | htmlf("<span class='age-hours'>%.0f hours</span>", |
341 | secs * 1.0 / TM_HOUR); | 334 | secs * 1.0 / TM_HOUR); |
342 | return; | 335 | return; |
343 | } | 336 | } |
344 | if (secs < TM_WEEK * 2) { | 337 | if (secs < TM_WEEK * 2) { |
345 | htmlf("<span class='age-days'>%.0f days</span>", | 338 | htmlf("<span class='age-days'>%.0f days</span>", |
346 | secs * 1.0 / TM_DAY); | 339 | secs * 1.0 / TM_DAY); |
347 | return; | 340 | return; |
348 | } | 341 | } |
349 | if (secs < TM_MONTH * 2) { | 342 | if (secs < TM_MONTH * 2) { |
350 | htmlf("<span class='age-weeks'>%.0f weeks</span>", | 343 | htmlf("<span class='age-weeks'>%.0f weeks</span>", |
351 | secs * 1.0 / TM_WEEK); | 344 | secs * 1.0 / TM_WEEK); |
352 | return; | 345 | return; |
353 | } | 346 | } |
354 | if (secs < TM_YEAR * 2) { | 347 | if (secs < TM_YEAR * 2) { |
355 | htmlf("<span class='age-months'>%.0f months</span>", | 348 | htmlf("<span class='age-months'>%.0f months</span>", |
356 | secs * 1.0 / TM_MONTH); | 349 | secs * 1.0 / TM_MONTH); |
357 | return; | 350 | return; |
358 | } | 351 | } |
359 | htmlf("<span class='age-years'>%.0f years</span>", | 352 | htmlf("<span class='age-years'>%.0f years</span>", |
360 | secs * 1.0 / TM_YEAR); | 353 | secs * 1.0 / TM_YEAR); |
361 | } | 354 | } |
362 | 355 | ||
363 | void cgit_print_docstart(char *title, struct cacheitem *item) | 356 | void cgit_print_http_headers(struct cgit_context *ctx) |
364 | { | 357 | { |
365 | html("Content-Type: text/html; charset=" PAGE_ENCODING "\n"); | 358 | if (ctx->page.mimetype && ctx->page.charset) |
366 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 359 | htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, |
367 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 360 | ctx->page.charset); |
368 | 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)); | ||
369 | html("\n"); | 368 | html("\n"); |
369 | } | ||
370 | |||
371 | void cgit_print_docstart(struct cgit_context *ctx) | ||
372 | { | ||
370 | html(cgit_doctype); | 373 | html(cgit_doctype); |
371 | 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"); |
372 | html("<head>\n"); | 375 | html("<head>\n"); |
373 | html("<title>"); | 376 | html("<title>"); |
374 | html_txt(title); | 377 | html_txt(ctx->page.title); |
375 | html("</title>\n"); | 378 | html("</title>\n"); |
376 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); | 379 | htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); |
377 | if (cgit_robots && *cgit_robots) | 380 | if (ctx->cfg.robots && *ctx->cfg.robots) |
378 | htmlf("<meta name='robots' content='%s'/>\n", cgit_robots); | 381 | htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); |
379 | html("<link rel='stylesheet' type='text/css' href='"); | 382 | html("<link rel='stylesheet' type='text/css' href='"); |
380 | html_attr(cgit_css); | 383 | html_attr(ctx->cfg.css); |
381 | html("'/>\n"); | 384 | html("'/>\n"); |
382 | html("</head>\n"); | 385 | html("</head>\n"); |
383 | html("<body>\n"); | 386 | html("<body>\n"); |
384 | } | 387 | } |
385 | 388 | ||
386 | void cgit_print_docend() | 389 | void cgit_print_docend() |
387 | { | 390 | { |
388 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); | 391 | html("</td>\n</tr>\n</table>\n</body>\n</html>\n"); |
389 | } | 392 | } |
390 | 393 | ||
391 | int print_branch_option(const char *refname, const unsigned char *sha1, | 394 | int print_branch_option(const char *refname, const unsigned char *sha1, |
392 | int flags, void *cb_data) | 395 | int flags, void *cb_data) |
393 | { | 396 | { |
394 | char *name = (char *)refname; | 397 | char *name = (char *)refname; |
395 | html_option(name, name, cgit_query_head); | 398 | html_option(name, name, ctx.qry.head); |
396 | return 0; | 399 | return 0; |
397 | } | 400 | } |
398 | 401 | ||
399 | int print_archive_ref(const char *refname, const unsigned char *sha1, | 402 | int print_archive_ref(const char *refname, const unsigned char *sha1, |
400 | int flags, void *cb_data) | 403 | int flags, void *cb_data) |
401 | { | 404 | { |
402 | struct tag *tag; | 405 | struct tag *tag; |
403 | struct taginfo *info; | 406 | struct taginfo *info; |
404 | struct object *obj; | 407 | struct object *obj; |
405 | char buf[256], *url; | 408 | char buf[256], *url; |
406 | unsigned char fileid[20]; | 409 | unsigned char fileid[20]; |
407 | int *header = (int *)cb_data; | 410 | int *header = (int *)cb_data; |
408 | 411 | ||
409 | if (prefixcmp(refname, "refs/archives")) | 412 | if (prefixcmp(refname, "refs/archives")) |
410 | return 0; | 413 | return 0; |
411 | strncpy(buf, refname+14, sizeof(buf)); | 414 | strncpy(buf, refname+14, sizeof(buf)); |
412 | obj = parse_object(sha1); | 415 | obj = parse_object(sha1); |
413 | if (!obj) | 416 | if (!obj) |
414 | return 1; | 417 | return 1; |
415 | if (obj->type == OBJ_TAG) { | 418 | if (obj->type == OBJ_TAG) { |
416 | tag = lookup_tag(sha1); | 419 | tag = lookup_tag(sha1); |
417 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) | 420 | if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) |
418 | return 0; | 421 | return 0; |
419 | hashcpy(fileid, tag->tagged->sha1); | 422 | hashcpy(fileid, tag->tagged->sha1); |
420 | } else if (obj->type != OBJ_BLOB) { | 423 | } else if (obj->type != OBJ_BLOB) { |
421 | return 0; | 424 | return 0; |
422 | } else { | 425 | } else { |
423 | hashcpy(fileid, sha1); | 426 | hashcpy(fileid, sha1); |
424 | } | 427 | } |
425 | if (!*header) { | 428 | if (!*header) { |
426 | html("<h1>download</h1>\n"); | 429 | html("<h1>download</h1>\n"); |
427 | *header = 1; | 430 | *header = 1; |
428 | } | 431 | } |
429 | url = cgit_pageurl(cgit_query_repo, "blob", | 432 | url = cgit_pageurl(ctx.qry.repo, "blob", |
430 | fmt("id=%s&path=%s", sha1_to_hex(fileid), | 433 | fmt("id=%s&path=%s", sha1_to_hex(fileid), |
431 | buf)); | 434 | buf)); |
432 | html_link_open(url, NULL, "menu"); | 435 | html_link_open(url, NULL, "menu"); |
433 | html_txt(strlpart(buf, 20)); | 436 | html_txt(strlpart(buf, 20)); |
434 | html_link_close(); | 437 | html_link_close(); |
435 | return 0; | 438 | return 0; |
436 | } | 439 | } |
437 | 440 | ||
438 | void add_hidden_formfields(int incl_head, int incl_search, char *page) | 441 | void add_hidden_formfields(int incl_head, int incl_search, char *page) |
439 | { | 442 | { |
440 | char *url; | 443 | char *url; |
441 | 444 | ||
442 | if (!cgit_virtual_root) { | 445 | if (!ctx.cfg.virtual_root) { |
443 | url = fmt("%s/%s", cgit_query_repo, page); | 446 | url = fmt("%s/%s", ctx.qry.repo, page); |
444 | if (cgit_query_path) | 447 | if (ctx.qry.path) |
445 | url = fmt("%s/%s", url, cgit_query_path); | 448 | url = fmt("%s/%s", url, ctx.qry.path); |
446 | html_hidden("url", url); | 449 | html_hidden("url", url); |
447 | } | 450 | } |
448 | 451 | ||
449 | if (incl_head && strcmp(cgit_query_head, cgit_repo->defbranch)) | 452 | if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) |
450 | html_hidden("h", cgit_query_head); | 453 | html_hidden("h", ctx.qry.head); |
451 | 454 | ||
452 | if (cgit_query_sha1) | 455 | if (ctx.qry.sha1) |
453 | html_hidden("id", cgit_query_sha1); | 456 | html_hidden("id", ctx.qry.sha1); |
454 | if (cgit_query_sha2) | 457 | if (ctx.qry.sha2) |
455 | html_hidden("id2", cgit_query_sha2); | 458 | html_hidden("id2", ctx.qry.sha2); |
456 | 459 | ||
457 | if (incl_search) { | 460 | if (incl_search) { |
458 | if (cgit_query_grep) | 461 | if (ctx.qry.grep) |
459 | html_hidden("qt", cgit_query_grep); | 462 | html_hidden("qt", ctx.qry.grep); |
460 | if (cgit_query_search) | 463 | if (ctx.qry.search) |
461 | html_hidden("q", cgit_query_search); | 464 | html_hidden("q", ctx.qry.search); |
462 | } | 465 | } |
463 | } | 466 | } |
464 | 467 | ||
465 | void cgit_print_pageheader(char *title, int show_search) | 468 | void cgit_print_pageheader(struct cgit_context *ctx) |
466 | { | 469 | { |
467 | 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"; |
468 | int header = 0; | 471 | int header = 0; |
469 | char *url; | 472 | char *url; |
470 | 473 | ||
471 | html("<table id='layout' summary=''>\n"); | 474 | html("<table id='layout' summary=''>\n"); |
472 | html("<tr><td id='sidebar'>\n"); | 475 | html("<tr><td id='sidebar'>\n"); |
473 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); | 476 | html("<table class='sidebar' cellspacing='0' summary=''>\n"); |
474 | html("<tr><td class='sidebar'>\n<a href='"); | 477 | html("<tr><td class='sidebar'>\n<a href='"); |
475 | html_attr(cgit_rooturl()); | 478 | html_attr(cgit_rooturl()); |
476 | htmlf("'><img src='%s' alt='cgit'/></a>\n", | 479 | htmlf("'><img src='%s' alt='cgit'/></a>\n", |
477 | cgit_logo); | 480 | ctx->cfg.logo); |
478 | html("</td></tr>\n<tr><td class='sidebar'>\n"); | 481 | html("</td></tr>\n<tr><td class='sidebar'>\n"); |
479 | if (cgit_query_repo) { | 482 | if (ctx->repo) { |
480 | html("<h1 class='first'>"); | 483 | html("<h1 class='first'>"); |
481 | html_txt(strrpart(cgit_repo->name, 20)); | 484 | html_txt(strrpart(ctx->repo->name, 20)); |
482 | html("</h1>\n"); | 485 | html("</h1>\n"); |
483 | html_txt(cgit_repo->desc); | 486 | html_txt(ctx->repo->desc); |
484 | if (cgit_repo->owner) { | 487 | if (ctx->repo->owner) { |
485 | html("<h1>owner</h1>\n"); | 488 | html("<h1>owner</h1>\n"); |
486 | html_txt(cgit_repo->owner); | 489 | html_txt(ctx->repo->owner); |
487 | } | 490 | } |
488 | html("<h1>navigate</h1>\n"); | 491 | html("<h1>navigate</h1>\n"); |
489 | reporevlink(NULL, "summary", NULL, "menu", cgit_query_head, | 492 | reporevlink(NULL, "summary", NULL, "menu", ctx->qry.head, |
490 | NULL, NULL); | 493 | NULL, NULL); |
491 | cgit_log_link("log", NULL, "menu", cgit_query_head, NULL, NULL, | 494 | cgit_log_link("log", NULL, "menu", ctx->qry.head, NULL, NULL, |
492 | 0, NULL, NULL); | 495 | 0, NULL, NULL); |
493 | cgit_tree_link("tree", NULL, "menu", cgit_query_head, | 496 | cgit_tree_link("tree", NULL, "menu", ctx->qry.head, |
494 | cgit_query_sha1, NULL); | 497 | ctx->qry.sha1, NULL); |
495 | cgit_commit_link("commit", NULL, "menu", cgit_query_head, | 498 | cgit_commit_link("commit", NULL, "menu", ctx->qry.head, |
496 | cgit_query_sha1); | 499 | ctx->qry.sha1); |
497 | cgit_diff_link("diff", NULL, "menu", cgit_query_head, | 500 | cgit_diff_link("diff", NULL, "menu", ctx->qry.head, |
498 | cgit_query_sha1, cgit_query_sha2, NULL); | 501 | ctx->qry.sha1, ctx->qry.sha2, NULL); |
499 | cgit_patch_link("patch", NULL, "menu", cgit_query_head, | 502 | cgit_patch_link("patch", NULL, "menu", ctx->qry.head, |
500 | cgit_query_sha1); | 503 | ctx->qry.sha1); |
501 | 504 | ||
502 | for_each_ref(print_archive_ref, &header); | 505 | for_each_ref(print_archive_ref, &header); |
503 | 506 | ||
504 | if (cgit_repo->clone_url || cgit_clone_prefix) { | 507 | if (ctx->repo->clone_url || ctx->cfg.clone_prefix) { |
505 | html("<h1>clone</h1>\n"); | 508 | html("<h1>clone</h1>\n"); |
506 | if (cgit_repo->clone_url) | 509 | if (ctx->repo->clone_url) |
507 | url = cgit_repo->clone_url; | 510 | url = ctx->repo->clone_url; |
508 | else | 511 | else |
509 | url = fmt("%s%s", cgit_clone_prefix, | 512 | url = fmt("%s%s", ctx->cfg.clone_prefix, |
510 | cgit_repo->url); | 513 | ctx->repo->url); |
511 | html("<a class='menu' href='"); | 514 | html("<a class='menu' href='"); |
512 | html_attr(url); | 515 | html_attr(url); |
513 | html("' title='"); | 516 | html("' title='"); |
514 | html_attr(url); | 517 | html_attr(url); |
515 | html("'>\n"); | 518 | html("'>\n"); |
516 | html_txt(strrpart(url, 20)); | 519 | html_txt(strrpart(url, 20)); |
517 | html("</a>\n"); | 520 | html("</a>\n"); |
518 | } | 521 | } |
519 | 522 | ||
520 | html("<h1>branch</h1>\n"); | 523 | html("<h1>branch</h1>\n"); |
521 | html("<form method='get' action=''>\n"); | 524 | html("<form method='get' action=''>\n"); |
522 | add_hidden_formfields(0, 1, cgit_query_page); | 525 | add_hidden_formfields(0, 1, ctx->qry.page); |
523 | // 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'>"); |
524 | html("<select name='h' onchange='this.form.submit();'>\n"); | 527 | html("<select name='h' onchange='this.form.submit();'>\n"); |
525 | for_each_branch_ref(print_branch_option, cgit_query_head); | 528 | for_each_branch_ref(print_branch_option, ctx->qry.head); |
526 | html("</select>\n"); | 529 | html("</select>\n"); |
527 | // html("</td><td>"); | 530 | // html("</td><td>"); |
528 | 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"); |
529 | // html("</td></tr></table>"); | 532 | // html("</td></tr></table>"); |
530 | html("</form>\n"); | 533 | html("</form>\n"); |
531 | 534 | ||
532 | html("<h1>search</h1>\n"); | 535 | html("<h1>search</h1>\n"); |
533 | html("<form method='get' action='"); | 536 | html("<form method='get' action='"); |
534 | if (cgit_virtual_root) | 537 | if (ctx->cfg.virtual_root) |
535 | html_attr(cgit_fileurl(cgit_query_repo, "log", | 538 | html_attr(cgit_fileurl(ctx->qry.repo, "log", |
536 | cgit_query_path, NULL)); | 539 | ctx->qry.path, NULL)); |
537 | html("'>\n"); | 540 | html("'>\n"); |
538 | add_hidden_formfields(1, 0, "log"); | 541 | add_hidden_formfields(1, 0, "log"); |
539 | html("<select name='qt'>\n"); | 542 | html("<select name='qt'>\n"); |
540 | html_option("grep", "log msg", cgit_query_grep); | 543 | html_option("grep", "log msg", ctx->qry.grep); |
541 | html_option("author", "author", cgit_query_grep); | 544 | html_option("author", "author", ctx->qry.grep); |
542 | html_option("committer", "committer", cgit_query_grep); | 545 | html_option("committer", "committer", ctx->qry.grep); |
543 | html("</select>\n"); | 546 | html("</select>\n"); |
544 | html("<input class='txt' type='text' name='q' value='"); | 547 | html("<input class='txt' type='text' name='q' value='"); |
545 | html_attr(cgit_query_search); | 548 | html_attr(ctx->qry.search); |
546 | html("'/>\n"); | 549 | html("'/>\n"); |
547 | html("</form>\n"); | 550 | html("</form>\n"); |
548 | } else { | 551 | } else { |
549 | if (!cgit_index_info || html_include(cgit_index_info)) | 552 | if (!ctx->cfg.index_info || html_include(ctx->cfg.index_info)) |
550 | html(default_info); | 553 | html(default_info); |
551 | } | 554 | } |
552 | 555 | ||
553 | html("</td></tr></table></td>\n"); | 556 | html("</td></tr></table></td>\n"); |
554 | 557 | ||
555 | html("<td id='content'>\n"); | 558 | html("<td id='content'>\n"); |
556 | } | 559 | } |
557 | 560 | ||
558 | 561 | void cgit_print_filemode(unsigned short mode) | |
559 | void cgit_print_snapshot_start(const char *mimetype, const char *filename, | ||
560 | struct cacheitem *item) | ||
561 | { | 562 | { |
562 | htmlf("Content-Type: %s\n", mimetype); | 563 | if (S_ISDIR(mode)) |
563 | htmlf("Content-Disposition: inline; filename=\"%s\"\n", filename); | 564 | html("d"); |
564 | htmlf("Last-Modified: %s\n", http_date(item->st.st_mtime)); | 565 | else if (S_ISLNK(mode)) |
565 | htmlf("Expires: %s\n", http_date(item->st.st_mtime + | 566 | html("l"); |
566 | ttl_seconds(item->ttl))); | 567 | else if (S_ISGITLINK(mode)) |
567 | html("\n"); | 568 | html("m"); |
569 | else | ||
570 | html("-"); | ||
571 | html_fileperm(mode >> 6); | ||
572 | html_fileperm(mode >> 3); | ||
573 | html_fileperm(mode); | ||
568 | } | 574 | } |
569 | 575 | ||
570 | /* vim:set sw=8: */ | 576 | void cgit_print_snapshot_links(const char *repo, const char *head, |
577 | const char *hex, int snapshots) | ||
578 | { | ||
579 | const struct cgit_snapshot_format* f; | ||
580 | char *filename; | ||
581 | |||
582 | for (f = cgit_snapshot_formats; f->suffix; f++) { | ||
583 | if (!(snapshots & f->bit)) | ||
584 | continue; | ||
585 | filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, | ||
586 | f->suffix); | ||
587 | cgit_snapshot_link(filename, NULL, NULL, (char *)head, | ||
588 | (char *)hex, filename); | ||
589 | html("<br/>"); | ||
590 | } | ||
591 | } | ||