]> git.cameronkatri.com Git - cgit.git/blob - ui-shared.c
ui-stats: make cgit_period definitions 'static const'
[cgit.git] / ui-shared.c
1 /* ui-shared.c: common web output functions
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "ui-shared.h"
11 #include "cmd.h"
12 #include "html.h"
13
14 static const char cgit_doctype[] =
15 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
16 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
17
18 static char *http_date(time_t t)
19 {
20 static char day[][4] =
21 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
22 static char month[][4] =
23 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
24 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
25 struct tm *tm = gmtime(&t);
26 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
27 tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
28 tm->tm_hour, tm->tm_min, tm->tm_sec);
29 }
30
31 void cgit_print_error(const char *fmt, ...)
32 {
33 va_list ap;
34 va_start(ap, fmt);
35 cgit_vprint_error(fmt, ap);
36 va_end(ap);
37 }
38
39 void cgit_vprint_error(const char *fmt, va_list ap)
40 {
41 va_list cp;
42 html("<div class='error'>");
43 va_copy(cp, ap);
44 html_vtxtf(fmt, cp);
45 va_end(cp);
46 html("</div>\n");
47 }
48
49 const char *cgit_httpscheme(void)
50 {
51 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
52 return "https://";
53 else
54 return "http://";
55 }
56
57 const char *cgit_hosturl(void)
58 {
59 if (ctx.env.http_host)
60 return ctx.env.http_host;
61 if (!ctx.env.server_name)
62 return NULL;
63 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
64 return ctx.env.server_name;
65 return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
66 }
67
68 const char *cgit_currenturl(void)
69 {
70 if (!ctx.qry.url)
71 return cgit_rooturl();
72 return ctx.qry.url;
73 }
74
75 const char *cgit_rooturl(void)
76 {
77 if (ctx.cfg.virtual_root)
78 return ctx.cfg.virtual_root;
79 else
80 return ctx.cfg.script_name;
81 }
82
83 const char *cgit_loginurl(void)
84 {
85 static const char *login_url = 0;
86 if (!login_url)
87 login_url = fmtalloc("%s?p=login", cgit_rooturl());
88 return login_url;
89 }
90
91 char *cgit_repourl(const char *reponame)
92 {
93 if (ctx.cfg.virtual_root)
94 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
95 else
96 return fmtalloc("?r=%s", reponame);
97 }
98
99 char *cgit_fileurl(const char *reponame, const char *pagename,
100 const char *filename, const char *query)
101 {
102 struct strbuf sb = STRBUF_INIT;
103 char *delim;
104
105 if (ctx.cfg.virtual_root) {
106 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
107 pagename, (filename ? filename:""));
108 delim = "?";
109 } else {
110 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
111 (filename ? filename : ""));
112 delim = "&amp;";
113 }
114 if (query)
115 strbuf_addf(&sb, "%s%s", delim, query);
116 return strbuf_detach(&sb, NULL);
117 }
118
119 char *cgit_pageurl(const char *reponame, const char *pagename,
120 const char *query)
121 {
122 return cgit_fileurl(reponame, pagename, 0, query);
123 }
124
125 const char *cgit_repobasename(const char *reponame)
126 {
127 /* I assume we don't need to store more than one repo basename */
128 static char rvbuf[1024];
129 int p;
130 const char *rv;
131 strncpy(rvbuf, reponame, sizeof(rvbuf));
132 if (rvbuf[sizeof(rvbuf)-1])
133 die("cgit_repobasename: truncated repository name '%s'", reponame);
134 p = strlen(rvbuf)-1;
135 /* strip trailing slashes */
136 while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
137 /* strip trailing .git */
138 if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
139 p -= 3; rvbuf[p--] = 0;
140 }
141 /* strip more trailing slashes if any */
142 while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
143 /* find last slash in the remaining string */
144 rv = strrchr(rvbuf,'/');
145 if (rv)
146 return ++rv;
147 return rvbuf;
148 }
149
150 static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
151 {
152 char *delim = "?";
153
154 if (always_root || page)
155 html_attr(cgit_rooturl());
156 else
157 html_attr(cgit_currenturl());
158
159 if (page) {
160 htmlf("?p=%s", page);
161 delim = "&amp;";
162 }
163 if (search) {
164 html(delim);
165 html("q=");
166 html_attr(search);
167 delim = "&amp;";
168 }
169 if (sort) {
170 html(delim);
171 html("s=");
172 html_attr(sort);
173 delim = "&amp;";
174 }
175 if (ofs) {
176 html(delim);
177 htmlf("ofs=%d", ofs);
178 }
179 }
180
181 static void site_link(const char *page, const char *name, const char *title,
182 const char *class, const char *search, const char *sort, int ofs, int always_root)
183 {
184 html("<a");
185 if (title) {
186 html(" title='");
187 html_attr(title);
188 html("'");
189 }
190 if (class) {
191 html(" class='");
192 html_attr(class);
193 html("'");
194 }
195 html(" href='");
196 site_url(page, search, sort, ofs, always_root);
197 html("'>");
198 html_txt(name);
199 html("</a>");
200 }
201
202 void cgit_index_link(const char *name, const char *title, const char *class,
203 const char *pattern, const char *sort, int ofs, int always_root)
204 {
205 site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
206 }
207
208 static char *repolink(const char *title, const char *class, const char *page,
209 const char *head, const char *path)
210 {
211 char *delim = "?";
212
213 html("<a");
214 if (title) {
215 html(" title='");
216 html_attr(title);
217 html("'");
218 }
219 if (class) {
220 html(" class='");
221 html_attr(class);
222 html("'");
223 }
224 html(" href='");
225 if (ctx.cfg.virtual_root) {
226 html_url_path(ctx.cfg.virtual_root);
227 html_url_path(ctx.repo->url);
228 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
229 html("/");
230 if (page) {
231 html_url_path(page);
232 html("/");
233 if (path)
234 html_url_path(path);
235 }
236 } else {
237 html_url_path(ctx.cfg.script_name);
238 html("?url=");
239 html_url_arg(ctx.repo->url);
240 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
241 html("/");
242 if (page) {
243 html_url_arg(page);
244 html("/");
245 if (path)
246 html_url_arg(path);
247 }
248 delim = "&amp;";
249 }
250 if (head && strcmp(head, ctx.repo->defbranch)) {
251 html(delim);
252 html("h=");
253 html_url_arg(head);
254 delim = "&amp;";
255 }
256 return fmt("%s", delim);
257 }
258
259 static void reporevlink(const char *page, const char *name, const char *title,
260 const char *class, const char *head, const char *rev,
261 const char *path)
262 {
263 char *delim;
264
265 delim = repolink(title, class, page, head, path);
266 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
267 html(delim);
268 html("id=");
269 html_url_arg(rev);
270 }
271 html("'>");
272 html_txt(name);
273 html("</a>");
274 }
275
276 void cgit_summary_link(const char *name, const char *title, const char *class,
277 const char *head)
278 {
279 reporevlink(NULL, name, title, class, head, NULL, NULL);
280 }
281
282 void cgit_tag_link(const char *name, const char *title, const char *class,
283 const char *tag)
284 {
285 reporevlink("tag", name, title, class, tag, NULL, NULL);
286 }
287
288 void cgit_tree_link(const char *name, const char *title, const char *class,
289 const char *head, const char *rev, const char *path)
290 {
291 reporevlink("tree", name, title, class, head, rev, path);
292 }
293
294 void cgit_plain_link(const char *name, const char *title, const char *class,
295 const char *head, const char *rev, const char *path)
296 {
297 reporevlink("plain", name, title, class, head, rev, path);
298 }
299
300 void cgit_log_link(const char *name, const char *title, const char *class,
301 const char *head, const char *rev, const char *path,
302 int ofs, const char *grep, const char *pattern, int showmsg)
303 {
304 char *delim;
305
306 delim = repolink(title, class, "log", head, path);
307 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
308 html(delim);
309 html("id=");
310 html_url_arg(rev);
311 delim = "&amp;";
312 }
313 if (grep && pattern) {
314 html(delim);
315 html("qt=");
316 html_url_arg(grep);
317 delim = "&amp;";
318 html(delim);
319 html("q=");
320 html_url_arg(pattern);
321 }
322 if (ofs > 0) {
323 html(delim);
324 html("ofs=");
325 htmlf("%d", ofs);
326 delim = "&amp;";
327 }
328 if (showmsg) {
329 html(delim);
330 html("showmsg=1");
331 }
332 html("'>");
333 html_txt(name);
334 html("</a>");
335 }
336
337 void cgit_commit_link(char *name, const char *title, const char *class,
338 const char *head, const char *rev, const char *path)
339 {
340 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
341 name[ctx.cfg.max_msg_len] = '\0';
342 name[ctx.cfg.max_msg_len - 1] = '.';
343 name[ctx.cfg.max_msg_len - 2] = '.';
344 name[ctx.cfg.max_msg_len - 3] = '.';
345 }
346
347 char *delim;
348
349 delim = repolink(title, class, "commit", head, path);
350 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
351 html(delim);
352 html("id=");
353 html_url_arg(rev);
354 delim = "&amp;";
355 }
356 if (ctx.qry.difftype) {
357 html(delim);
358 htmlf("dt=%d", ctx.qry.difftype);
359 delim = "&amp;";
360 }
361 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
362 html(delim);
363 html("context=");
364 htmlf("%d", ctx.qry.context);
365 delim = "&amp;";
366 }
367 if (ctx.qry.ignorews) {
368 html(delim);
369 html("ignorews=1");
370 delim = "&amp;";
371 }
372 html("'>");
373 if (name[0] != '\0')
374 html_txt(name);
375 else
376 html_txt("(no commit message)");
377 html("</a>");
378 }
379
380 void cgit_refs_link(const char *name, const char *title, const char *class,
381 const char *head, const char *rev, const char *path)
382 {
383 reporevlink("refs", name, title, class, head, rev, path);
384 }
385
386 void cgit_snapshot_link(const char *name, const char *title, const char *class,
387 const char *head, const char *rev,
388 const char *archivename)
389 {
390 reporevlink("snapshot", name, title, class, head, rev, archivename);
391 }
392
393 void cgit_diff_link(const char *name, const char *title, const char *class,
394 const char *head, const char *new_rev, const char *old_rev,
395 const char *path)
396 {
397 char *delim;
398
399 delim = repolink(title, class, "diff", head, path);
400 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
401 html(delim);
402 html("id=");
403 html_url_arg(new_rev);
404 delim = "&amp;";
405 }
406 if (old_rev) {
407 html(delim);
408 html("id2=");
409 html_url_arg(old_rev);
410 delim = "&amp;";
411 }
412 if (ctx.qry.difftype) {
413 html(delim);
414 htmlf("dt=%d", ctx.qry.difftype);
415 delim = "&amp;";
416 }
417 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
418 html(delim);
419 html("context=");
420 htmlf("%d", ctx.qry.context);
421 delim = "&amp;";
422 }
423 if (ctx.qry.ignorews) {
424 html(delim);
425 html("ignorews=1");
426 delim = "&amp;";
427 }
428 html("'>");
429 html_txt(name);
430 html("</a>");
431 }
432
433 void cgit_patch_link(const char *name, const char *title, const char *class,
434 const char *head, const char *rev, const char *path)
435 {
436 reporevlink("patch", name, title, class, head, rev, path);
437 }
438
439 void cgit_stats_link(const char *name, const char *title, const char *class,
440 const char *head, const char *path)
441 {
442 reporevlink("stats", name, title, class, head, NULL, path);
443 }
444
445 static void cgit_self_link(char *name, const char *title, const char *class)
446 {
447 if (!strcmp(ctx.qry.page, "repolist"))
448 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
449 ctx.qry.ofs, 1);
450 else if (!strcmp(ctx.qry.page, "summary"))
451 cgit_summary_link(name, title, class, ctx.qry.head);
452 else if (!strcmp(ctx.qry.page, "tag"))
453 cgit_tag_link(name, title, class, ctx.qry.has_sha1 ?
454 ctx.qry.sha1 : ctx.qry.head);
455 else if (!strcmp(ctx.qry.page, "tree"))
456 cgit_tree_link(name, title, class, ctx.qry.head,
457 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
458 ctx.qry.path);
459 else if (!strcmp(ctx.qry.page, "plain"))
460 cgit_plain_link(name, title, class, ctx.qry.head,
461 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
462 ctx.qry.path);
463 else if (!strcmp(ctx.qry.page, "log"))
464 cgit_log_link(name, title, class, ctx.qry.head,
465 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
466 ctx.qry.path, ctx.qry.ofs,
467 ctx.qry.grep, ctx.qry.search,
468 ctx.qry.showmsg);
469 else if (!strcmp(ctx.qry.page, "commit"))
470 cgit_commit_link(name, title, class, ctx.qry.head,
471 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
472 ctx.qry.path);
473 else if (!strcmp(ctx.qry.page, "patch"))
474 cgit_patch_link(name, title, class, ctx.qry.head,
475 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
476 ctx.qry.path);
477 else if (!strcmp(ctx.qry.page, "refs"))
478 cgit_refs_link(name, title, class, ctx.qry.head,
479 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
480 ctx.qry.path);
481 else if (!strcmp(ctx.qry.page, "snapshot"))
482 cgit_snapshot_link(name, title, class, ctx.qry.head,
483 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
484 ctx.qry.path);
485 else if (!strcmp(ctx.qry.page, "diff"))
486 cgit_diff_link(name, title, class, ctx.qry.head,
487 ctx.qry.sha1, ctx.qry.sha2,
488 ctx.qry.path);
489 else if (!strcmp(ctx.qry.page, "stats"))
490 cgit_stats_link(name, title, class, ctx.qry.head,
491 ctx.qry.path);
492 else {
493 /* Don't known how to make link for this page */
494 repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
495 html("><!-- cgit_self_link() doesn't know how to make link for page '");
496 html_txt(ctx.qry.page);
497 html("' -->");
498 html_txt(name);
499 html("</a>");
500 }
501 }
502
503 void cgit_object_link(struct object *obj)
504 {
505 char *page, *shortrev, *fullrev, *name;
506
507 fullrev = sha1_to_hex(obj->sha1);
508 shortrev = xstrdup(fullrev);
509 shortrev[10] = '\0';
510 if (obj->type == OBJ_COMMIT) {
511 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
512 ctx.qry.head, fullrev, NULL);
513 return;
514 } else if (obj->type == OBJ_TREE)
515 page = "tree";
516 else if (obj->type == OBJ_TAG)
517 page = "tag";
518 else
519 page = "blob";
520 name = fmt("%s %s...", typename(obj->type), shortrev);
521 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
522 }
523
524 static struct string_list_item *lookup_path(struct string_list *list,
525 const char *path)
526 {
527 struct string_list_item *item;
528
529 while (path && path[0]) {
530 if ((item = string_list_lookup(list, path)))
531 return item;
532 if (!(path = strchr(path, '/')))
533 break;
534 path++;
535 }
536 return NULL;
537 }
538
539 void cgit_submodule_link(const char *class, char *path, const char *rev)
540 {
541 struct string_list *list;
542 struct string_list_item *item;
543 char tail, *dir;
544 size_t len;
545
546 len = 0;
547 tail = 0;
548 list = &ctx.repo->submodules;
549 item = lookup_path(list, path);
550 if (!item) {
551 len = strlen(path);
552 tail = path[len - 1];
553 if (tail == '/') {
554 path[len - 1] = 0;
555 item = lookup_path(list, path);
556 }
557 }
558 html("<a ");
559 if (class)
560 htmlf("class='%s' ", class);
561 html("href='");
562 if (item) {
563 html_attrf(item->util, rev);
564 } else if (ctx.repo->module_link) {
565 dir = strrchr(path, '/');
566 if (dir)
567 dir++;
568 else
569 dir = path;
570 html_attrf(ctx.repo->module_link, dir, rev);
571 } else {
572 html("#");
573 }
574 html("'>");
575 html_txt(path);
576 html("</a>");
577 html_txtf(" @ %.7s", rev);
578 if (item && tail)
579 path[len - 1] = tail;
580 }
581
582 void cgit_print_date(time_t secs, const char *format, int local_time)
583 {
584 char buf[64];
585 struct tm *time;
586
587 if (!secs)
588 return;
589 if (local_time)
590 time = localtime(&secs);
591 else
592 time = gmtime(&secs);
593 strftime(buf, sizeof(buf)-1, format, time);
594 html_txt(buf);
595 }
596
597 static void print_rel_date(time_t t, double value,
598 const char *class, const char *suffix)
599 {
600 char buf[64];
601 struct tm *time;
602
603 if (ctx.cfg.local_time)
604 time = localtime(&t);
605 else
606 time = gmtime(&t);
607 strftime(buf, sizeof(buf) - 1, FMT_LONGDATE, time);
608
609 htmlf("<span class='%s' title='", class);
610 html_attr(buf);
611 htmlf("'>%.0f %s</span>", value, suffix);
612 }
613
614 void cgit_print_age(time_t t, time_t max_relative, const char *format)
615 {
616 time_t now, secs;
617
618 if (!t)
619 return;
620 time(&now);
621 secs = now - t;
622 if (secs < 0)
623 secs = 0;
624
625 if (secs > max_relative && max_relative >= 0) {
626 cgit_print_date(t, format, ctx.cfg.local_time);
627 return;
628 }
629
630 if (secs < TM_HOUR * 2) {
631 print_rel_date(t, secs * 1.0 / TM_MIN, "age-mins", "min.");
632 return;
633 }
634 if (secs < TM_DAY * 2) {
635 print_rel_date(t, secs * 1.0 / TM_HOUR, "age-hours", "hours");
636 return;
637 }
638 if (secs < TM_WEEK * 2) {
639 print_rel_date(t, secs * 1.0 / TM_DAY, "age-days", "days");
640 return;
641 }
642 if (secs < TM_MONTH * 2) {
643 print_rel_date(t, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
644 return;
645 }
646 if (secs < TM_YEAR * 2) {
647 print_rel_date(t, secs * 1.0 / TM_MONTH, "age-months", "months");
648 return;
649 }
650 print_rel_date(t, secs * 1.0 / TM_YEAR, "age-years", "years");
651 }
652
653 void cgit_print_http_headers(void)
654 {
655 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
656 return;
657
658 if (ctx.page.status)
659 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
660 if (ctx.page.mimetype && ctx.page.charset)
661 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
662 ctx.page.charset);
663 else if (ctx.page.mimetype)
664 htmlf("Content-Type: %s\n", ctx.page.mimetype);
665 if (ctx.page.size)
666 htmlf("Content-Length: %zd\n", ctx.page.size);
667 if (ctx.page.filename)
668 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
669 ctx.page.filename);
670 if (!ctx.env.authenticated)
671 html("Cache-Control: no-cache, no-store\n");
672 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
673 htmlf("Expires: %s\n", http_date(ctx.page.expires));
674 if (ctx.page.etag)
675 htmlf("ETag: \"%s\"\n", ctx.page.etag);
676 html("\n");
677 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
678 exit(0);
679 }
680
681 static void print_rel_vcs_link(const char *url)
682 {
683 html("<link rel='vcs-git' href='");
684 html_attr(url);
685 html("' title='");
686 html_attr(ctx.repo->name);
687 html(" Git repository'/>\n");
688 }
689
690 void cgit_print_docstart(void)
691 {
692 if (ctx.cfg.embedded) {
693 if (ctx.cfg.header)
694 html_include(ctx.cfg.header);
695 return;
696 }
697
698 const char *host = cgit_hosturl();
699 html(cgit_doctype);
700 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
701 html("<head>\n");
702 html("<title>");
703 html_txt(ctx.page.title);
704 html("</title>\n");
705 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
706 if (ctx.cfg.robots && *ctx.cfg.robots)
707 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
708 html("<link rel='stylesheet' type='text/css' href='");
709 html_attr(ctx.cfg.css);
710 html("'/>\n");
711 if (ctx.cfg.favicon) {
712 html("<link rel='shortcut icon' href='");
713 html_attr(ctx.cfg.favicon);
714 html("'/>\n");
715 }
716 if (host && ctx.repo && ctx.qry.head) {
717 struct strbuf sb = STRBUF_INIT;
718 strbuf_addf(&sb, "h=%s", ctx.qry.head);
719
720 html("<link rel='alternate' title='Atom feed' href='");
721 html(cgit_httpscheme());
722 html_attr(cgit_hosturl());
723 html_attr(cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
724 sb.buf));
725 html("' type='application/atom+xml'/>\n");
726 strbuf_release(&sb);
727 }
728 if (ctx.repo)
729 cgit_add_clone_urls(print_rel_vcs_link);
730 if (ctx.cfg.head_include)
731 html_include(ctx.cfg.head_include);
732 html("</head>\n");
733 html("<body>\n");
734 if (ctx.cfg.header)
735 html_include(ctx.cfg.header);
736 }
737
738 void cgit_print_docend(void)
739 {
740 html("</div> <!-- class=content -->\n");
741 if (ctx.cfg.embedded) {
742 html("</div> <!-- id=cgit -->\n");
743 if (ctx.cfg.footer)
744 html_include(ctx.cfg.footer);
745 return;
746 }
747 if (ctx.cfg.footer)
748 html_include(ctx.cfg.footer);
749 else {
750 htmlf("<div class='footer'>generated by <a href='http://git.zx2c4.com/cgit/about/'>cgit %s</a> at ",
751 cgit_version);
752 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
753 html("</div>\n");
754 }
755 html("</div> <!-- id=cgit -->\n");
756 html("</body>\n</html>\n");
757 }
758
759 static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
760 {
761 struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
762 int i;
763
764 for (i = 0; url_list[i]; i++) {
765 strbuf_rtrim(url_list[i]);
766 if (url_list[i]->len == 0)
767 continue;
768 if (suffix && *suffix)
769 strbuf_addf(url_list[i], "/%s", suffix);
770 fn(url_list[i]->buf);
771 }
772
773 strbuf_list_free(url_list);
774 }
775
776 void cgit_add_clone_urls(void (*fn)(const char *))
777 {
778 if (ctx.repo->clone_url)
779 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
780 else if (ctx.cfg.clone_prefix)
781 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
782 }
783
784 static int print_branch_option(const char *refname, const unsigned char *sha1,
785 int flags, void *cb_data)
786 {
787 char *name = (char *)refname;
788 html_option(name, name, ctx.qry.head);
789 return 0;
790 }
791
792 void cgit_add_hidden_formfields(int incl_head, int incl_search,
793 const char *page)
794 {
795 if (!ctx.cfg.virtual_root) {
796 struct strbuf url = STRBUF_INIT;
797
798 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
799 if (ctx.qry.vpath)
800 strbuf_addf(&url, "/%s", ctx.qry.vpath);
801 html_hidden("url", url.buf);
802 strbuf_release(&url);
803 }
804
805 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
806 strcmp(ctx.qry.head, ctx.repo->defbranch))
807 html_hidden("h", ctx.qry.head);
808
809 if (ctx.qry.sha1)
810 html_hidden("id", ctx.qry.sha1);
811 if (ctx.qry.sha2)
812 html_hidden("id2", ctx.qry.sha2);
813 if (ctx.qry.showmsg)
814 html_hidden("showmsg", "1");
815
816 if (incl_search) {
817 if (ctx.qry.grep)
818 html_hidden("qt", ctx.qry.grep);
819 if (ctx.qry.search)
820 html_hidden("q", ctx.qry.search);
821 }
822 }
823
824 static const char *hc(const char *page)
825 {
826 return strcmp(ctx.qry.page, page) ? NULL : "active";
827 }
828
829 static void cgit_print_path_crumbs(char *path)
830 {
831 char *old_path = ctx.qry.path;
832 char *p = path, *q, *end = path + strlen(path);
833
834 ctx.qry.path = NULL;
835 cgit_self_link("root", NULL, NULL);
836 ctx.qry.path = p = path;
837 while (p < end) {
838 if (!(q = strchr(p, '/')))
839 q = end;
840 *q = '\0';
841 html_txt("/");
842 cgit_self_link(p, NULL, NULL);
843 if (q < end)
844 *q = '/';
845 p = q + 1;
846 }
847 ctx.qry.path = old_path;
848 }
849
850 static void print_header(void)
851 {
852 char *logo = NULL, *logo_link = NULL;
853
854 html("<table id='header'>\n");
855 html("<tr>\n");
856
857 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
858 logo = ctx.repo->logo;
859 else
860 logo = ctx.cfg.logo;
861 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
862 logo_link = ctx.repo->logo_link;
863 else
864 logo_link = ctx.cfg.logo_link;
865 if (logo && *logo) {
866 html("<td class='logo' rowspan='2'><a href='");
867 if (logo_link && *logo_link)
868 html_attr(logo_link);
869 else
870 html_attr(cgit_rooturl());
871 html("'><img src='");
872 html_attr(logo);
873 html("' alt='cgit logo'/></a></td>\n");
874 }
875
876 html("<td class='main'>");
877 if (ctx.repo) {
878 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
879 html(" : ");
880 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
881 if (ctx.env.authenticated) {
882 html("</td><td class='form'>");
883 html("<form method='get' action=''>\n");
884 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
885 html("<select name='h' onchange='this.form.submit();'>\n");
886 for_each_branch_ref(print_branch_option, ctx.qry.head);
887 html("</select> ");
888 html("<input type='submit' name='' value='switch'/>");
889 html("</form>");
890 }
891 } else
892 html_txt(ctx.cfg.root_title);
893 html("</td></tr>\n");
894
895 html("<tr><td class='sub'>");
896 if (ctx.repo) {
897 html_txt(ctx.repo->desc);
898 html("</td><td class='sub right'>");
899 html_txt(ctx.repo->owner);
900 } else {
901 if (ctx.cfg.root_desc)
902 html_txt(ctx.cfg.root_desc);
903 else if (ctx.cfg.index_info)
904 html_include(ctx.cfg.index_info);
905 }
906 html("</td></tr></table>\n");
907 }
908
909 void cgit_print_pageheader(void)
910 {
911 html("<div id='cgit'>");
912 if (!ctx.env.authenticated || !ctx.cfg.noheader)
913 print_header();
914
915 html("<table class='tabs'><tr><td>\n");
916 if (ctx.env.authenticated && ctx.repo) {
917 if (ctx.repo->readme.nr)
918 reporevlink("about", "about", NULL,
919 hc("about"), ctx.qry.head, NULL,
920 NULL);
921 cgit_summary_link("summary", NULL, hc("summary"),
922 ctx.qry.head);
923 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
924 ctx.qry.sha1, NULL);
925 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
926 NULL, ctx.qry.vpath, 0, NULL, NULL,
927 ctx.qry.showmsg);
928 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
929 ctx.qry.sha1, ctx.qry.vpath);
930 cgit_commit_link("commit", NULL, hc("commit"),
931 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath);
932 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
933 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath);
934 if (ctx.repo->max_stats)
935 cgit_stats_link("stats", NULL, hc("stats"),
936 ctx.qry.head, ctx.qry.vpath);
937 html("</td><td class='form'>");
938 html("<form class='right' method='get' action='");
939 if (ctx.cfg.virtual_root)
940 html_url_path(cgit_fileurl(ctx.qry.repo, "log",
941 ctx.qry.vpath, NULL));
942 html("'>\n");
943 cgit_add_hidden_formfields(1, 0, "log");
944 html("<select name='qt'>\n");
945 html_option("grep", "log msg", ctx.qry.grep);
946 html_option("author", "author", ctx.qry.grep);
947 html_option("committer", "committer", ctx.qry.grep);
948 html_option("range", "range", ctx.qry.grep);
949 html("</select>\n");
950 html("<input class='txt' type='text' size='10' name='q' value='");
951 html_attr(ctx.qry.search);
952 html("'/>\n");
953 html("<input type='submit' value='search'/>\n");
954 html("</form>\n");
955 } else if (ctx.env.authenticated) {
956 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
957 if (ctx.cfg.root_readme)
958 site_link("about", "about", NULL, hc("about"),
959 NULL, NULL, 0, 1);
960 html("</td><td class='form'>");
961 html("<form method='get' action='");
962 html_attr(cgit_currenturl());
963 html("'>\n");
964 html("<input type='text' name='q' size='10' value='");
965 html_attr(ctx.qry.search);
966 html("'/>\n");
967 html("<input type='submit' value='search'/>\n");
968 html("</form>");
969 }
970 html("</td></tr></table>\n");
971 if (ctx.env.authenticated && ctx.qry.vpath) {
972 html("<div class='path'>");
973 html("path: ");
974 cgit_print_path_crumbs(ctx.qry.vpath);
975 html("</div>");
976 }
977 html("<div class='content'>");
978 }
979
980 void cgit_print_filemode(unsigned short mode)
981 {
982 if (S_ISDIR(mode))
983 html("d");
984 else if (S_ISLNK(mode))
985 html("l");
986 else if (S_ISGITLINK(mode))
987 html("m");
988 else
989 html("-");
990 html_fileperm(mode >> 6);
991 html_fileperm(mode >> 3);
992 html_fileperm(mode);
993 }
994
995 void cgit_print_snapshot_links(const char *repo, const char *head,
996 const char *hex, int snapshots)
997 {
998 const struct cgit_snapshot_format* f;
999 struct strbuf filename = STRBUF_INIT;
1000 size_t prefixlen;
1001 unsigned char sha1[20];
1002
1003 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
1004 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
1005 hex++;
1006 strbuf_addf(&filename, "%s-%s", cgit_repobasename(repo), hex);
1007 prefixlen = filename.len;
1008 for (f = cgit_snapshot_formats; f->suffix; f++) {
1009 if (!(snapshots & f->bit))
1010 continue;
1011 strbuf_setlen(&filename, prefixlen);
1012 strbuf_addstr(&filename, f->suffix);
1013 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1014 filename.buf);
1015 html("<br/>");
1016 }
1017 strbuf_release(&filename);
1018 }