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