summaryrefslogtreecommitdiffabout
path: root/ui-shared.c
Unidiff
Diffstat (limited to 'ui-shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-shared.c270
1 files changed, 203 insertions, 67 deletions
diff --git a/ui-shared.c b/ui-shared.c
index 8a7cc32..ae29615 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -6,49 +6,49 @@
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#include "cmd.h"
11#include "html.h" 11#include "html.h"
12 12
13const char cgit_doctype[] = 13const char cgit_doctype[] =
14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16 16
17static char *http_date(time_t t) 17static char *http_date(time_t t)
18{ 18{
19 static char day[][4] = 19 static char day[][4] =
20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
21 static char month[][4] = 21 static char month[][4] =
22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
23 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 23 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
24 struct tm *tm = gmtime(&t); 24 struct tm *tm = gmtime(&t);
25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
27 tm->tm_hour, tm->tm_min, tm->tm_sec); 27 tm->tm_hour, tm->tm_min, tm->tm_sec);
28} 28}
29 29
30void cgit_print_error(char *msg) 30void cgit_print_error(const char *msg)
31{ 31{
32 html("<div class='error'>"); 32 html("<div class='error'>");
33 html_txt(msg); 33 html_txt(msg);
34 html("</div>\n"); 34 html("</div>\n");
35} 35}
36 36
37char *cgit_httpscheme() 37char *cgit_httpscheme()
38{ 38{
39 if (ctx.env.https && !strcmp(ctx.env.https, "on")) 39 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
40 return "https://"; 40 return "https://";
41 else 41 else
42 return "http://"; 42 return "http://";
43} 43}
44 44
45char *cgit_hosturl() 45char *cgit_hosturl()
46{ 46{
47 if (ctx.env.http_host) 47 if (ctx.env.http_host)
48 return ctx.env.http_host; 48 return ctx.env.http_host;
49 if (!ctx.env.server_name) 49 if (!ctx.env.server_name)
50 return NULL; 50 return NULL;
51 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80) 51 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
52 return ctx.env.server_name; 52 return ctx.env.server_name;
53 return xstrdup(fmt("%s:%s", ctx.env.server_name, ctx.env.server_port)); 53 return xstrdup(fmt("%s:%s", ctx.env.server_name, ctx.env.server_port));
54} 54}
@@ -112,104 +112,104 @@ const char *cgit_repobasename(const char *reponame)
112 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 112 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
113 p -= 3; rvbuf[p--] = 0; 113 p -= 3; rvbuf[p--] = 0;
114 } 114 }
115 /* strip more trailing slashes if any */ 115 /* strip more trailing slashes if any */
116 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 116 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
117 /* find last slash in the remaining string */ 117 /* find last slash in the remaining string */
118 rv = strrchr(rvbuf,'/'); 118 rv = strrchr(rvbuf,'/');
119 if(rv) 119 if(rv)
120 return ++rv; 120 return ++rv;
121 return rvbuf; 121 return rvbuf;
122} 122}
123 123
124char *cgit_currurl() 124char *cgit_currurl()
125{ 125{
126 if (!ctx.cfg.virtual_root) 126 if (!ctx.cfg.virtual_root)
127 return ctx.cfg.script_name; 127 return ctx.cfg.script_name;
128 else if (ctx.qry.page) 128 else if (ctx.qry.page)
129 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); 129 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
130 else if (ctx.qry.repo) 130 else if (ctx.qry.repo)
131 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); 131 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
132 else 132 else
133 return fmt("%s/", ctx.cfg.virtual_root); 133 return fmt("%s/", ctx.cfg.virtual_root);
134} 134}
135 135
136static void site_url(char *page, char *search, int ofs) 136static void site_url(const char *page, const char *search, int ofs)
137{ 137{
138 char *delim = "?"; 138 char *delim = "?";
139 139
140 if (ctx.cfg.virtual_root) { 140 if (ctx.cfg.virtual_root) {
141 html_attr(ctx.cfg.virtual_root); 141 html_attr(ctx.cfg.virtual_root);
142 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 142 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
143 html("/"); 143 html("/");
144 } else 144 } else
145 html(ctx.cfg.script_name); 145 html(ctx.cfg.script_name);
146 146
147 if (page) { 147 if (page) {
148 htmlf("?p=%s", page); 148 htmlf("?p=%s", page);
149 delim = "&"; 149 delim = "&";
150 } 150 }
151 if (search) { 151 if (search) {
152 html(delim); 152 html(delim);
153 html("q="); 153 html("q=");
154 html_attr(search); 154 html_attr(search);
155 delim = "&"; 155 delim = "&";
156 } 156 }
157 if (ofs) { 157 if (ofs) {
158 html(delim); 158 html(delim);
159 htmlf("ofs=%d", ofs); 159 htmlf("ofs=%d", ofs);
160 } 160 }
161} 161}
162 162
163static void site_link(char *page, char *name, char *title, char *class, 163static void site_link(const char *page, const char *name, const char *title,
164 char *search, int ofs) 164 const char *class, const char *search, int ofs)
165{ 165{
166 html("<a"); 166 html("<a");
167 if (title) { 167 if (title) {
168 html(" title='"); 168 html(" title='");
169 html_attr(title); 169 html_attr(title);
170 html("'"); 170 html("'");
171 } 171 }
172 if (class) { 172 if (class) {
173 html(" class='"); 173 html(" class='");
174 html_attr(class); 174 html_attr(class);
175 html("'"); 175 html("'");
176 } 176 }
177 html(" href='"); 177 html(" href='");
178 site_url(page, search, ofs); 178 site_url(page, search, ofs);
179 html("'>"); 179 html("'>");
180 html_txt(name); 180 html_txt(name);
181 html("</a>"); 181 html("</a>");
182} 182}
183 183
184void cgit_index_link(char *name, char *title, char *class, char *pattern, 184void cgit_index_link(const char *name, const char *title, const char *class,
185 int ofs) 185 const char *pattern, int ofs)
186{ 186{
187 site_link(NULL, name, title, class, pattern, ofs); 187 site_link(NULL, name, title, class, pattern, ofs);
188} 188}
189 189
190static char *repolink(char *title, char *class, char *page, char *head, 190static char *repolink(const char *title, const char *class, const char *page,
191 char *path) 191 const char *head, const char *path)
192{ 192{
193 char *delim = "?"; 193 char *delim = "?";
194 194
195 html("<a"); 195 html("<a");
196 if (title) { 196 if (title) {
197 html(" title='"); 197 html(" title='");
198 html_attr(title); 198 html_attr(title);
199 html("'"); 199 html("'");
200 } 200 }
201 if (class) { 201 if (class) {
202 html(" class='"); 202 html(" class='");
203 html_attr(class); 203 html_attr(class);
204 html("'"); 204 html("'");
205 } 205 }
206 html(" href='"); 206 html(" href='");
207 if (ctx.cfg.virtual_root) { 207 if (ctx.cfg.virtual_root) {
208 html_url_path(ctx.cfg.virtual_root); 208 html_url_path(ctx.cfg.virtual_root);
209 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 209 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
210 html("/"); 210 html("/");
211 html_url_path(ctx.repo->url); 211 html_url_path(ctx.repo->url);
212 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 212 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
213 html("/"); 213 html("/");
214 if (page) { 214 if (page) {
215 html_url_path(page); 215 html_url_path(page);
@@ -219,219 +219,327 @@ static char *repolink(char *title, char *class, char *page, char *head,
219 } 219 }
220 } else { 220 } else {
221 html(ctx.cfg.script_name); 221 html(ctx.cfg.script_name);
222 html("?url="); 222 html("?url=");
223 html_url_arg(ctx.repo->url); 223 html_url_arg(ctx.repo->url);
224 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 224 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
225 html("/"); 225 html("/");
226 if (page) { 226 if (page) {
227 html_url_arg(page); 227 html_url_arg(page);
228 html("/"); 228 html("/");
229 if (path) 229 if (path)
230 html_url_arg(path); 230 html_url_arg(path);
231 } 231 }
232 delim = "&amp;"; 232 delim = "&amp;";
233 } 233 }
234 if (head && strcmp(head, ctx.repo->defbranch)) { 234 if (head && strcmp(head, ctx.repo->defbranch)) {
235 html(delim); 235 html(delim);
236 html("h="); 236 html("h=");
237 html_url_arg(head); 237 html_url_arg(head);
238 delim = "&amp;"; 238 delim = "&amp;";
239 } 239 }
240 return fmt("%s", delim); 240 return fmt("%s", delim);
241} 241}
242 242
243static void reporevlink(char *page, char *name, char *title, char *class, 243static void reporevlink(const char *page, const char *name, const char *title,
244 char *head, char *rev, char *path) 244 const char *class, const char *head, const char *rev,
245 const char *path)
245{ 246{
246 char *delim; 247 char *delim;
247 248
248 delim = repolink(title, class, page, head, path); 249 delim = repolink(title, class, page, head, path);
249 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) { 250 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
250 html(delim); 251 html(delim);
251 html("id="); 252 html("id=");
252 html_url_arg(rev); 253 html_url_arg(rev);
253 } 254 }
254 html("'>"); 255 html("'>");
255 html_txt(name); 256 html_txt(name);
256 html("</a>"); 257 html("</a>");
257} 258}
258 259
259void cgit_summary_link(char *name, char *title, char *class, char *head) 260void cgit_summary_link(const char *name, const char *title, const char *class,
261 const char *head)
260{ 262{
261 reporevlink(NULL, name, title, class, head, NULL, NULL); 263 reporevlink(NULL, name, title, class, head, NULL, NULL);
262} 264}
263 265
264void cgit_tag_link(char *name, char *title, char *class, char *head, 266void cgit_tag_link(const char *name, const char *title, const char *class,
265 char *rev) 267 const char *head, const char *rev)
266{ 268{
267 reporevlink("tag", name, title, class, head, rev, NULL); 269 reporevlink("tag", name, title, class, head, rev, NULL);
268} 270}
269 271
270void cgit_tree_link(char *name, char *title, char *class, char *head, 272void cgit_tree_link(const char *name, const char *title, const char *class,
271 char *rev, char *path) 273 const char *head, const char *rev, const char *path)
272{ 274{
273 reporevlink("tree", name, title, class, head, rev, path); 275 reporevlink("tree", name, title, class, head, rev, path);
274} 276}
275 277
276void cgit_plain_link(char *name, char *title, char *class, char *head, 278void cgit_plain_link(const char *name, const char *title, const char *class,
277 char *rev, char *path) 279 const char *head, const char *rev, const char *path)
278{ 280{
279 reporevlink("plain", name, title, class, head, rev, path); 281 reporevlink("plain", name, title, class, head, rev, path);
280} 282}
281 283
282void cgit_log_link(char *name, char *title, char *class, char *head, 284void cgit_log_link(const char *name, const char *title, const char *class,
283 char *rev, char *path, int ofs, char *grep, char *pattern, 285 const char *head, const char *rev, const char *path,
284 int showmsg) 286 int ofs, const char *grep, const char *pattern, int showmsg)
285{ 287{
286 char *delim; 288 char *delim;
287 289
288 delim = repolink(title, class, "log", head, path); 290 delim = repolink(title, class, "log", head, path);
289 if (rev && strcmp(rev, ctx.qry.head)) { 291 if (rev && strcmp(rev, ctx.qry.head)) {
290 html(delim); 292 html(delim);
291 html("id="); 293 html("id=");
292 html_url_arg(rev); 294 html_url_arg(rev);
293 delim = "&"; 295 delim = "&";
294 } 296 }
295 if (grep && pattern) { 297 if (grep && pattern) {
296 html(delim); 298 html(delim);
297 html("qt="); 299 html("qt=");
298 html_url_arg(grep); 300 html_url_arg(grep);
299 delim = "&"; 301 delim = "&";
300 html(delim); 302 html(delim);
301 html("q="); 303 html("q=");
302 html_url_arg(pattern); 304 html_url_arg(pattern);
303 } 305 }
304 if (ofs > 0) { 306 if (ofs > 0) {
305 html(delim); 307 html(delim);
306 html("ofs="); 308 html("ofs=");
307 htmlf("%d", ofs); 309 htmlf("%d", ofs);
308 delim = "&"; 310 delim = "&";
309 } 311 }
310 if (showmsg) { 312 if (showmsg) {
311 html(delim); 313 html(delim);
312 html("showmsg=1"); 314 html("showmsg=1");
313 } 315 }
314 html("'>"); 316 html("'>");
315 html_txt(name); 317 html_txt(name);
316 html("</a>"); 318 html("</a>");
317} 319}
318 320
319void cgit_commit_link(char *name, char *title, char *class, char *head, 321void cgit_commit_link(char *name, const char *title, const char *class,
320 char *rev) 322 const char *head, const char *rev, const char *path,
323 int toggle_ssdiff)
321{ 324{
322 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { 325 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
323 name[ctx.cfg.max_msg_len] = '\0'; 326 name[ctx.cfg.max_msg_len] = '\0';
324 name[ctx.cfg.max_msg_len - 1] = '.'; 327 name[ctx.cfg.max_msg_len - 1] = '.';
325 name[ctx.cfg.max_msg_len - 2] = '.'; 328 name[ctx.cfg.max_msg_len - 2] = '.';
326 name[ctx.cfg.max_msg_len - 3] = '.'; 329 name[ctx.cfg.max_msg_len - 3] = '.';
327 } 330 }
328 reporevlink("commit", name, title, class, head, rev, NULL); 331
332 char *delim;
333
334 delim = repolink(title, class, "commit", head, path);
335 if (rev && strcmp(rev, ctx.qry.head)) {
336 html(delim);
337 html("id=");
338 html_url_arg(rev);
339 delim = "&amp;";
340 }
341 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
342 html(delim);
343 html("ss=1");
344 delim = "&amp;";
345 }
346 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
347 html(delim);
348 html("context=");
349 htmlf("%d", ctx.qry.context);
350 delim = "&amp;";
351 }
352 if (ctx.qry.ignorews) {
353 html(delim);
354 html("ignorews=1");
355 delim = "&amp;";
356 }
357 html("'>");
358 html_txt(name);
359 html("</a>");
329} 360}
330 361
331void cgit_refs_link(char *name, char *title, char *class, char *head, 362void cgit_refs_link(const char *name, const char *title, const char *class,
332 char *rev, char *path) 363 const char *head, const char *rev, const char *path)
333{ 364{
334 reporevlink("refs", name, title, class, head, rev, path); 365 reporevlink("refs", name, title, class, head, rev, path);
335} 366}
336 367
337void cgit_snapshot_link(char *name, char *title, char *class, char *head, 368void cgit_snapshot_link(const char *name, const char *title, const char *class,
338 char *rev, char *archivename) 369 const char *head, const char *rev,
370 const char *archivename)
339{ 371{
340 reporevlink("snapshot", name, title, class, head, rev, archivename); 372 reporevlink("snapshot", name, title, class, head, rev, archivename);
341} 373}
342 374
343void cgit_diff_link(char *name, char *title, char *class, char *head, 375void cgit_diff_link(const char *name, const char *title, const char *class,
344 char *new_rev, char *old_rev, char *path) 376 const char *head, const char *new_rev, const char *old_rev,
377 const char *path, int toggle_ssdiff)
345{ 378{
346 char *delim; 379 char *delim;
347 380
348 delim = repolink(title, class, "diff", head, path); 381 delim = repolink(title, class, "diff", head, path);
349 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) { 382 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
350 html(delim); 383 html(delim);
351 html("id="); 384 html("id=");
352 html_url_arg(new_rev); 385 html_url_arg(new_rev);
353 delim = "&amp;"; 386 delim = "&amp;";
354 } 387 }
355 if (old_rev) { 388 if (old_rev) {
356 html(delim); 389 html(delim);
357 html("id2="); 390 html("id2=");
358 html_url_arg(old_rev); 391 html_url_arg(old_rev);
392 delim = "&amp;";
393 }
394 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
395 html(delim);
396 html("ss=1");
397 delim = "&amp;";
398 }
399 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
400 html(delim);
401 html("context=");
402 htmlf("%d", ctx.qry.context);
403 delim = "&amp;";
404 }
405 if (ctx.qry.ignorews) {
406 html(delim);
407 html("ignorews=1");
408 delim = "&amp;";
359 } 409 }
360 html("'>"); 410 html("'>");
361 html_txt(name); 411 html_txt(name);
362 html("</a>"); 412 html("</a>");
363} 413}
364 414
365void cgit_patch_link(char *name, char *title, char *class, char *head, 415void cgit_patch_link(const char *name, const char *title, const char *class,
366 char *rev) 416 const char *head, const char *rev, const char *path)
367{ 417{
368 reporevlink("patch", name, title, class, head, rev, NULL); 418 reporevlink("patch", name, title, class, head, rev, path);
369} 419}
370 420
371void cgit_stats_link(char *name, char *title, char *class, char *head, 421void cgit_stats_link(const char *name, const char *title, const char *class,
372 char *path) 422 const char *head, const char *path)
373{ 423{
374 reporevlink("stats", name, title, class, head, NULL, path); 424 reporevlink("stats", name, title, class, head, NULL, path);
375} 425}
376 426
427void cgit_self_link(char *name, const char *title, const char *class,
428 struct cgit_context *ctx)
429{
430 if (!strcmp(ctx->qry.page, "repolist"))
431 return cgit_index_link(name, title, class, ctx->qry.search,
432 ctx->qry.ofs);
433 else if (!strcmp(ctx->qry.page, "summary"))
434 return cgit_summary_link(name, title, class, ctx->qry.head);
435 else if (!strcmp(ctx->qry.page, "tag"))
436 return cgit_tag_link(name, title, class, ctx->qry.head,
437 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL);
438 else if (!strcmp(ctx->qry.page, "tree"))
439 return cgit_tree_link(name, title, class, ctx->qry.head,
440 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
441 ctx->qry.path);
442 else if (!strcmp(ctx->qry.page, "plain"))
443 return cgit_plain_link(name, title, class, ctx->qry.head,
444 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
445 ctx->qry.path);
446 else if (!strcmp(ctx->qry.page, "log"))
447 return cgit_log_link(name, title, class, ctx->qry.head,
448 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
449 ctx->qry.path, ctx->qry.ofs,
450 ctx->qry.grep, ctx->qry.search,
451 ctx->qry.showmsg);
452 else if (!strcmp(ctx->qry.page, "commit"))
453 return cgit_commit_link(name, title, class, ctx->qry.head,
454 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
455 ctx->qry.path, 0);
456 else if (!strcmp(ctx->qry.page, "patch"))
457 return cgit_patch_link(name, title, class, ctx->qry.head,
458 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
459 ctx->qry.path);
460 else if (!strcmp(ctx->qry.page, "refs"))
461 return cgit_refs_link(name, title, class, ctx->qry.head,
462 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
463 ctx->qry.path);
464 else if (!strcmp(ctx->qry.page, "snapshot"))
465 return cgit_snapshot_link(name, title, class, ctx->qry.head,
466 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
467 ctx->qry.path);
468 else if (!strcmp(ctx->qry.page, "diff"))
469 return cgit_diff_link(name, title, class, ctx->qry.head,
470 ctx->qry.sha1, ctx->qry.sha2,
471 ctx->qry.path, 0);
472 else if (!strcmp(ctx->qry.page, "stats"))
473 return cgit_stats_link(name, title, class, ctx->qry.head,
474 ctx->qry.path);
475
476 /* Don't known how to make link for this page */
477 repolink(title, class, ctx->qry.page, ctx->qry.head, ctx->qry.path);
478 html("><!-- cgit_self_link() doesn't know how to make link for page '");
479 html_txt(ctx->qry.page);
480 html("' -->");
481 html_txt(name);
482 html("</a>");
483}
484
377void cgit_object_link(struct object *obj) 485void cgit_object_link(struct object *obj)
378{ 486{
379 char *page, *shortrev, *fullrev, *name; 487 char *page, *shortrev, *fullrev, *name;
380 488
381 fullrev = sha1_to_hex(obj->sha1); 489 fullrev = sha1_to_hex(obj->sha1);
382 shortrev = xstrdup(fullrev); 490 shortrev = xstrdup(fullrev);
383 shortrev[10] = '\0'; 491 shortrev[10] = '\0';
384 if (obj->type == OBJ_COMMIT) { 492 if (obj->type == OBJ_COMMIT) {
385 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL, 493 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
386 ctx.qry.head, fullrev); 494 ctx.qry.head, fullrev, NULL, 0);
387 return; 495 return;
388 } else if (obj->type == OBJ_TREE) 496 } else if (obj->type == OBJ_TREE)
389 page = "tree"; 497 page = "tree";
390 else if (obj->type == OBJ_TAG) 498 else if (obj->type == OBJ_TAG)
391 page = "tag"; 499 page = "tag";
392 else 500 else
393 page = "blob"; 501 page = "blob";
394 name = fmt("%s %s...", typename(obj->type), shortrev); 502 name = fmt("%s %s...", typename(obj->type), shortrev);
395 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL); 503 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
396} 504}
397 505
398void cgit_print_date(time_t secs, char *format, int local_time) 506void cgit_print_date(time_t secs, const char *format, int local_time)
399{ 507{
400 char buf[64]; 508 char buf[64];
401 struct tm *time; 509 struct tm *time;
402 510
403 if (!secs) 511 if (!secs)
404 return; 512 return;
405 if(local_time) 513 if(local_time)
406 time = localtime(&secs); 514 time = localtime(&secs);
407 else 515 else
408 time = gmtime(&secs); 516 time = gmtime(&secs);
409 strftime(buf, sizeof(buf)-1, format, time); 517 strftime(buf, sizeof(buf)-1, format, time);
410 html_txt(buf); 518 html_txt(buf);
411} 519}
412 520
413void cgit_print_age(time_t t, time_t max_relative, char *format) 521void cgit_print_age(time_t t, time_t max_relative, const char *format)
414{ 522{
415 time_t now, secs; 523 time_t now, secs;
416 524
417 if (!t) 525 if (!t)
418 return; 526 return;
419 time(&now); 527 time(&now);
420 secs = now - t; 528 secs = now - t;
421 529
422 if (secs > max_relative && max_relative >= 0) { 530 if (secs > max_relative && max_relative >= 0) {
423 cgit_print_date(t, format, ctx.cfg.local_time); 531 cgit_print_date(t, format, ctx.cfg.local_time);
424 return; 532 return;
425 } 533 }
426 534
427 if (secs < TM_HOUR * 2) { 535 if (secs < TM_HOUR * 2) {
428 htmlf("<span class='age-mins'>%.0f min.</span>", 536 htmlf("<span class='age-mins'>%.0f min.</span>",
429 secs * 1.0 / TM_MIN); 537 secs * 1.0 / TM_MIN);
430 return; 538 return;
431 } 539 }
432 if (secs < TM_DAY * 2) { 540 if (secs < TM_DAY * 2) {
433 htmlf("<span class='age-hours'>%.0f hours</span>", 541 htmlf("<span class='age-hours'>%.0f hours</span>",
434 secs * 1.0 / TM_HOUR); 542 secs * 1.0 / TM_HOUR);
435 return; 543 return;
436 } 544 }
437 if (secs < TM_WEEK * 2) { 545 if (secs < TM_WEEK * 2) {
@@ -488,49 +596,49 @@ void cgit_print_docstart(struct cgit_context *ctx)
488 } 596 }
489 597
490 char *host = cgit_hosturl(); 598 char *host = cgit_hosturl();
491 html(cgit_doctype); 599 html(cgit_doctype);
492 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 600 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
493 html("<head>\n"); 601 html("<head>\n");
494 html("<title>"); 602 html("<title>");
495 html_txt(ctx->page.title); 603 html_txt(ctx->page.title);
496 html("</title>\n"); 604 html("</title>\n");
497 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 605 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
498 if (ctx->cfg.robots && *ctx->cfg.robots) 606 if (ctx->cfg.robots && *ctx->cfg.robots)
499 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 607 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
500 html("<link rel='stylesheet' type='text/css' href='"); 608 html("<link rel='stylesheet' type='text/css' href='");
501 html_attr(ctx->cfg.css); 609 html_attr(ctx->cfg.css);
502 html("'/>\n"); 610 html("'/>\n");
503 if (ctx->cfg.favicon) { 611 if (ctx->cfg.favicon) {
504 html("<link rel='shortcut icon' href='"); 612 html("<link rel='shortcut icon' href='");
505 html_attr(ctx->cfg.favicon); 613 html_attr(ctx->cfg.favicon);
506 html("'/>\n"); 614 html("'/>\n");
507 } 615 }
508 if (host && ctx->repo) { 616 if (host && ctx->repo) {
509 html("<link rel='alternate' title='Atom feed' href='"); 617 html("<link rel='alternate' title='Atom feed' href='");
510 html(cgit_httpscheme()); 618 html(cgit_httpscheme());
511 html_attr(cgit_hosturl()); 619 html_attr(cgit_hosturl());
512 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path, 620 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
513 fmt("h=%s", ctx->qry.head))); 621 fmt("h=%s", ctx->qry.head)));
514 html("' type='application/atom+xml'/>\n"); 622 html("' type='application/atom+xml'/>\n");
515 } 623 }
516 if (ctx->cfg.head_include) 624 if (ctx->cfg.head_include)
517 html_include(ctx->cfg.head_include); 625 html_include(ctx->cfg.head_include);
518 html("</head>\n"); 626 html("</head>\n");
519 html("<body>\n"); 627 html("<body>\n");
520 if (ctx->cfg.header) 628 if (ctx->cfg.header)
521 html_include(ctx->cfg.header); 629 html_include(ctx->cfg.header);
522} 630}
523 631
524void cgit_print_docend() 632void cgit_print_docend()
525{ 633{
526 html("</div> <!-- class=content -->\n"); 634 html("</div> <!-- class=content -->\n");
527 if (ctx.cfg.embedded) { 635 if (ctx.cfg.embedded) {
528 html("</div> <!-- id=cgit -->\n"); 636 html("</div> <!-- id=cgit -->\n");
529 if (ctx.cfg.footer) 637 if (ctx.cfg.footer)
530 html_include(ctx.cfg.footer); 638 html_include(ctx.cfg.footer);
531 return; 639 return;
532 } 640 }
533 if (ctx.cfg.footer) 641 if (ctx.cfg.footer)
534 html_include(ctx.cfg.footer); 642 html_include(ctx.cfg.footer);
535 else { 643 else {
536 htmlf("<div class='footer'>generated by cgit %s at ", 644 htmlf("<div class='footer'>generated by cgit %s at ",
@@ -568,83 +676,103 @@ int print_archive_ref(const char *refname, const unsigned char *sha1,
568 return 1; 676 return 1;
569 if (obj->type == OBJ_TAG) { 677 if (obj->type == OBJ_TAG) {
570 tag = lookup_tag(sha1); 678 tag = lookup_tag(sha1);
571 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 679 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
572 return 0; 680 return 0;
573 hashcpy(fileid, tag->tagged->sha1); 681 hashcpy(fileid, tag->tagged->sha1);
574 } else if (obj->type != OBJ_BLOB) { 682 } else if (obj->type != OBJ_BLOB) {
575 return 0; 683 return 0;
576 } else { 684 } else {
577 hashcpy(fileid, sha1); 685 hashcpy(fileid, sha1);
578 } 686 }
579 if (!*header) { 687 if (!*header) {
580 html("<h1>download</h1>\n"); 688 html("<h1>download</h1>\n");
581 *header = 1; 689 *header = 1;
582 } 690 }
583 url = cgit_pageurl(ctx.qry.repo, "blob", 691 url = cgit_pageurl(ctx.qry.repo, "blob",
584 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 692 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
585 buf)); 693 buf));
586 html_link_open(url, NULL, "menu"); 694 html_link_open(url, NULL, "menu");
587 html_txt(strlpart(buf, 20)); 695 html_txt(strlpart(buf, 20));
588 html_link_close(); 696 html_link_close();
589 return 0; 697 return 0;
590} 698}
591 699
592void cgit_add_hidden_formfields(int incl_head, int incl_search, char *page) 700void cgit_add_hidden_formfields(int incl_head, int incl_search,
701 const char *page)
593{ 702{
594 char *url; 703 char *url;
595 704
596 if (!ctx.cfg.virtual_root) { 705 if (!ctx.cfg.virtual_root) {
597 url = fmt("%s/%s", ctx.qry.repo, page); 706 url = fmt("%s/%s", ctx.qry.repo, page);
598 if (ctx.qry.path) 707 if (ctx.qry.vpath)
599 url = fmt("%s/%s", url, ctx.qry.path); 708 url = fmt("%s/%s", url, ctx.qry.vpath);
600 html_hidden("url", url); 709 html_hidden("url", url);
601 } 710 }
602 711
603 if (incl_head && ctx.qry.head && ctx.repo->defbranch && 712 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
604 strcmp(ctx.qry.head, ctx.repo->defbranch)) 713 strcmp(ctx.qry.head, ctx.repo->defbranch))
605 html_hidden("h", ctx.qry.head); 714 html_hidden("h", ctx.qry.head);
606 715
607 if (ctx.qry.sha1) 716 if (ctx.qry.sha1)
608 html_hidden("id", ctx.qry.sha1); 717 html_hidden("id", ctx.qry.sha1);
609 if (ctx.qry.sha2) 718 if (ctx.qry.sha2)
610 html_hidden("id2", ctx.qry.sha2); 719 html_hidden("id2", ctx.qry.sha2);
611 if (ctx.qry.showmsg) 720 if (ctx.qry.showmsg)
612 html_hidden("showmsg", "1"); 721 html_hidden("showmsg", "1");
613 722
614 if (incl_search) { 723 if (incl_search) {
615 if (ctx.qry.grep) 724 if (ctx.qry.grep)
616 html_hidden("qt", ctx.qry.grep); 725 html_hidden("qt", ctx.qry.grep);
617 if (ctx.qry.search) 726 if (ctx.qry.search)
618 html_hidden("q", ctx.qry.search); 727 html_hidden("q", ctx.qry.search);
619 } 728 }
620} 729}
621 730
622const char *fallback_cmd = "repolist"; 731static const char *hc(struct cgit_context *ctx, const char *page)
732{
733 return strcmp(ctx->qry.page, page) ? NULL : "active";
734}
623 735
624char *hc(struct cgit_cmd *cmd, const char *page) 736static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
625{ 737{
626 return (strcmp(cmd ? cmd->name : fallback_cmd, page) ? NULL : "active"); 738 char *old_path = ctx->qry.path;
739 char *p = path, *q, *end = path + strlen(path);
740
741 ctx->qry.path = NULL;
742 cgit_self_link("root", NULL, NULL, ctx);
743 ctx->qry.path = p = path;
744 while (p < end) {
745 if (!(q = strchr(p, '/')))
746 q = end;
747 *q = '\0';
748 html_txt("/");
749 cgit_self_link(p, NULL, NULL, ctx);
750 if (q < end)
751 *q = '/';
752 p = q + 1;
753 }
754 ctx->qry.path = old_path;
627} 755}
628 756
629static void print_header(struct cgit_context *ctx) 757static void print_header(struct cgit_context *ctx)
630{ 758{
631 html("<table id='header'>\n"); 759 html("<table id='header'>\n");
632 html("<tr>\n"); 760 html("<tr>\n");
633 761
634 if (ctx->cfg.logo && ctx->cfg.logo[0] != 0) { 762 if (ctx->cfg.logo && ctx->cfg.logo[0] != 0) {
635 html("<td class='logo' rowspan='2'><a href='"); 763 html("<td class='logo' rowspan='2'><a href='");
636 if (ctx->cfg.logo_link) 764 if (ctx->cfg.logo_link)
637 html_attr(ctx->cfg.logo_link); 765 html_attr(ctx->cfg.logo_link);
638 else 766 else
639 html_attr(cgit_rooturl()); 767 html_attr(cgit_rooturl());
640 html("'><img src='"); 768 html("'><img src='");
641 html_attr(ctx->cfg.logo); 769 html_attr(ctx->cfg.logo);
642 html("' alt='cgit logo'/></a></td>\n"); 770 html("' alt='cgit logo'/></a></td>\n");
643 } 771 }
644 772
645 html("<td class='main'>"); 773 html("<td class='main'>");
646 if (ctx->repo) { 774 if (ctx->repo) {
647 cgit_index_link("index", NULL, NULL, NULL, 0); 775 cgit_index_link("index", NULL, NULL, NULL, 0);
648 html(" : "); 776 html(" : ");
649 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL); 777 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
650 html("</td><td class='form'>"); 778 html("</td><td class='form'>");
@@ -654,120 +782,128 @@ static void print_header(struct cgit_context *ctx)
654 for_each_branch_ref(print_branch_option, ctx->qry.head); 782 for_each_branch_ref(print_branch_option, ctx->qry.head);
655 html("</select> "); 783 html("</select> ");
656 html("<input type='submit' name='' value='switch'/>"); 784 html("<input type='submit' name='' value='switch'/>");
657 html("</form>"); 785 html("</form>");
658 } else 786 } else
659 html_txt(ctx->cfg.root_title); 787 html_txt(ctx->cfg.root_title);
660 html("</td></tr>\n"); 788 html("</td></tr>\n");
661 789
662 html("<tr><td class='sub'>"); 790 html("<tr><td class='sub'>");
663 if (ctx->repo) { 791 if (ctx->repo) {
664 html_txt(ctx->repo->desc); 792 html_txt(ctx->repo->desc);
665 html("</td><td class='sub right'>"); 793 html("</td><td class='sub right'>");
666 html_txt(ctx->repo->owner); 794 html_txt(ctx->repo->owner);
667 } else { 795 } else {
668 if (ctx->cfg.root_desc) 796 if (ctx->cfg.root_desc)
669 html_txt(ctx->cfg.root_desc); 797 html_txt(ctx->cfg.root_desc);
670 else if (ctx->cfg.index_info) 798 else if (ctx->cfg.index_info)
671 html_include(ctx->cfg.index_info); 799 html_include(ctx->cfg.index_info);
672 } 800 }
673 html("</td></tr></table>\n"); 801 html("</td></tr></table>\n");
674} 802}
675 803
676void cgit_print_pageheader(struct cgit_context *ctx) 804void cgit_print_pageheader(struct cgit_context *ctx)
677{ 805{
678 struct cgit_cmd *cmd = cgit_get_cmd(ctx);
679
680 if (!cmd && ctx->repo)
681 fallback_cmd = "summary";
682
683 html("<div id='cgit'>"); 806 html("<div id='cgit'>");
684 if (!ctx->cfg.noheader) 807 if (!ctx->cfg.noheader)
685 print_header(ctx); 808 print_header(ctx);
686 809
687 html("<table class='tabs'><tr><td>\n"); 810 html("<table class='tabs'><tr><td>\n");
688 if (ctx->repo) { 811 if (ctx->repo) {
689 cgit_summary_link("summary", NULL, hc(cmd, "summary"), 812 cgit_summary_link("summary", NULL, hc(ctx, "summary"),
690 ctx->qry.head); 813 ctx->qry.head);
691 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head, 814 cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head,
692 ctx->qry.sha1, NULL); 815 ctx->qry.sha1, NULL);
693 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head, 816 cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head,
694 NULL, NULL, 0, NULL, NULL, ctx->qry.showmsg); 817 NULL, ctx->qry.vpath, 0, NULL, NULL,
695 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head, 818 ctx->qry.showmsg);
696 ctx->qry.sha1, NULL); 819 cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head,
697 cgit_commit_link("commit", NULL, hc(cmd, "commit"), 820 ctx->qry.sha1, ctx->qry.vpath);
698 ctx->qry.head, ctx->qry.sha1); 821 cgit_commit_link("commit", NULL, hc(ctx, "commit"),
699 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head, 822 ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0);
700 ctx->qry.sha1, ctx->qry.sha2, NULL); 823 cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head,
824 ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0);
701 if (ctx->repo->max_stats) 825 if (ctx->repo->max_stats)
702 cgit_stats_link("stats", NULL, hc(cmd, "stats"), 826 cgit_stats_link("stats", NULL, hc(ctx, "stats"),
703 ctx->qry.head, NULL); 827 ctx->qry.head, ctx->qry.vpath);
704 if (ctx->repo->readme) 828 if (ctx->repo->readme)
705 reporevlink("about", "about", NULL, 829 reporevlink("about", "about", NULL,
706 hc(cmd, "about"), ctx->qry.head, NULL, 830 hc(ctx, "about"), ctx->qry.head, NULL,
707 NULL); 831 NULL);
708 html("</td><td class='form'>"); 832 html("</td><td class='form'>");
709 html("<form class='right' method='get' action='"); 833 html("<form class='right' method='get' action='");
710 if (ctx->cfg.virtual_root) 834 if (ctx->cfg.virtual_root)
711 html_url_path(cgit_fileurl(ctx->qry.repo, "log", 835 html_url_path(cgit_fileurl(ctx->qry.repo, "log",
712 ctx->qry.path, NULL)); 836 ctx->qry.vpath, NULL));
713 html("'>\n"); 837 html("'>\n");
714 cgit_add_hidden_formfields(1, 0, "log"); 838 cgit_add_hidden_formfields(1, 0, "log");
715 html("<select name='qt'>\n"); 839 html("<select name='qt'>\n");
716 html_option("grep", "log msg", ctx->qry.grep); 840 html_option("grep", "log msg", ctx->qry.grep);
717 html_option("author", "author", ctx->qry.grep); 841 html_option("author", "author", ctx->qry.grep);
718 html_option("committer", "committer", ctx->qry.grep); 842 html_option("committer", "committer", ctx->qry.grep);
843 html_option("range", "range", ctx->qry.grep);
719 html("</select>\n"); 844 html("</select>\n");
720 html("<input class='txt' type='text' size='10' name='q' value='"); 845 html("<input class='txt' type='text' size='10' name='q' value='");
721 html_attr(ctx->qry.search); 846 html_attr(ctx->qry.search);
722 html("'/>\n"); 847 html("'/>\n");
723 html("<input type='submit' value='search'/>\n"); 848 html("<input type='submit' value='search'/>\n");
724 html("</form>\n"); 849 html("</form>\n");
725 } else { 850 } else {
726 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0); 851 site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, 0);
727 if (ctx->cfg.root_readme) 852 if (ctx->cfg.root_readme)
728 site_link("about", "about", NULL, hc(cmd, "about"), 853 site_link("about", "about", NULL, hc(ctx, "about"),
729 NULL, 0); 854 NULL, 0);
730 html("</td><td class='form'>"); 855 html("</td><td class='form'>");
731 html("<form method='get' action='"); 856 html("<form method='get' action='");
732 html_attr(cgit_rooturl()); 857 html_attr(cgit_rooturl());
733 html("'>\n"); 858 html("'>\n");
734 html("<input type='text' name='q' size='10' value='"); 859 html("<input type='text' name='q' size='10' value='");
735 html_attr(ctx->qry.search); 860 html_attr(ctx->qry.search);
736 html("'/>\n"); 861 html("'/>\n");
737 html("<input type='submit' value='search'/>\n"); 862 html("<input type='submit' value='search'/>\n");
738 html("</form>"); 863 html("</form>");
739 } 864 }
740 html("</td></tr></table>\n"); 865 html("</td></tr></table>\n");
866 if (ctx->qry.vpath) {
867 html("<div class='path'>");
868 html("path: ");
869 cgit_print_path_crumbs(ctx, ctx->qry.vpath);
870 html("</div>");
871 }
741 html("<div class='content'>"); 872 html("<div class='content'>");
742} 873}
743 874
744void cgit_print_filemode(unsigned short mode) 875void cgit_print_filemode(unsigned short mode)
745{ 876{
746 if (S_ISDIR(mode)) 877 if (S_ISDIR(mode))
747 html("d"); 878 html("d");
748 else if (S_ISLNK(mode)) 879 else if (S_ISLNK(mode))
749 html("l"); 880 html("l");
750 else if (S_ISGITLINK(mode)) 881 else if (S_ISGITLINK(mode))
751 html("m"); 882 html("m");
752 else 883 else
753 html("-"); 884 html("-");
754 html_fileperm(mode >> 6); 885 html_fileperm(mode >> 6);
755 html_fileperm(mode >> 3); 886 html_fileperm(mode >> 3);
756 html_fileperm(mode); 887 html_fileperm(mode);
757} 888}
758 889
759void cgit_print_snapshot_links(const char *repo, const char *head, 890void cgit_print_snapshot_links(const char *repo, const char *head,
760 const char *hex, int snapshots) 891 const char *hex, int snapshots)
761{ 892{
762 const struct cgit_snapshot_format* f; 893 const struct cgit_snapshot_format* f;
894 char *prefix;
763 char *filename; 895 char *filename;
896 unsigned char sha1[20];
764 897
898 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
899 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
900 hex++;
901 prefix = xstrdup(fmt("%s-%s", cgit_repobasename(repo), hex));
765 for (f = cgit_snapshot_formats; f->suffix; f++) { 902 for (f = cgit_snapshot_formats; f->suffix; f++) {
766 if (!(snapshots & f->bit)) 903 if (!(snapshots & f->bit))
767 continue; 904 continue;
768 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 905 filename = fmt("%s%s", prefix, f->suffix);
769 f->suffix);
770 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename); 906 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
771 html("<br/>"); 907 html("<br/>");
772 } 908 }
773} 909}