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