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