blob: d8578739576f090256f4b1bbe8232f1ff77f0b36 [file] [log] [blame]
Lars Hjemli5a106eb2006-12-11 16:38:30 +01001/* ui-shared.c: common web output functions
2 *
Jeff Smith1649afd2017-10-01 23:39:09 -05003 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli5a106eb2006-12-11 16:38:30 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-shared.h"
Lars Hjemlif1355692008-04-12 15:53:31 +020011#include "cmd.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010012#include "html.h"
Christian Hessee65ea962018-06-04 22:27:46 +020013#include "version.h"
Lars Hjemli5a106eb2006-12-11 16:38:30 +010014
John Keeping0f23d462015-03-08 16:32:22 +000015static const char cgit_doctype[] =
Juuso Lapinlampi8d05b392016-05-11 18:04:14 +000016"<!DOCTYPE html>\n";
Lars Hjemli5a106eb2006-12-11 16:38:30 +010017
18static char *http_date(time_t t)
19{
Lars Hjemli6fb7d092007-05-15 00:22:03 +020020 static char day[][4] =
Lars Hjemli5a106eb2006-12-11 16:38:30 +010021 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
Lars Hjemli6fb7d092007-05-15 00:22:03 +020022 static char month[][4] =
Lars Hjemli5a106eb2006-12-11 16:38:30 +010023 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
Danijel Tašove34a3b52009-11-02 22:10:04 +010024 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Lars Hjemli5a106eb2006-12-11 16:38:30 +010025 struct tm *tm = gmtime(&t);
26 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
Lukas Fleischer53bc7472013-03-03 16:04:29 +010027 tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
Lars Hjemli5a106eb2006-12-11 16:38:30 +010028 tm->tm_hour, tm->tm_min, tm->tm_sec);
29}
30
John Keepinged5bd302013-04-06 11:23:52 +010031void cgit_print_error(const char *fmt, ...)
Lars Hjemli5a106eb2006-12-11 16:38:30 +010032{
John Keepinged5bd302013-04-06 11:23:52 +010033 va_list ap;
34 va_start(ap, fmt);
35 cgit_vprint_error(fmt, ap);
36 va_end(ap);
37}
38
39void cgit_vprint_error(const char *fmt, va_list ap)
40{
41 va_list cp;
Lars Hjemli5a106eb2006-12-11 16:38:30 +010042 html("<div class='error'>");
John Keepinged5bd302013-04-06 11:23:52 +010043 va_copy(cp, ap);
44 html_vtxtf(fmt, cp);
45 va_end(cp);
Lars Hjemli5a106eb2006-12-11 16:38:30 +010046 html("</div>\n");
47}
Lars Hjemli74620f12006-12-11 16:48:03 +010048
John Keepinge3d3fff2015-03-08 16:32:16 +000049const char *cgit_httpscheme(void)
Diego Ongaro87a89ae2009-06-10 18:09:55 -050050{
Lars Hjemli60a26272009-08-10 08:21:09 +020051 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
Diego Ongaro87a89ae2009-06-10 18:09:55 -050052 return "https://";
53 else
54 return "http://";
55}
56
Christian Hessef77e2a82015-10-09 13:15:50 +020057char *cgit_hosturl(void)
Lars Hjemlib2a3d312008-05-21 08:17:54 +020058{
Lars Hjemli60a26272009-08-10 08:21:09 +020059 if (ctx.env.http_host)
Christian Hessef77e2a82015-10-09 13:15:50 +020060 return xstrdup(ctx.env.http_host);
Lars Hjemli60a26272009-08-10 08:21:09 +020061 if (!ctx.env.server_name)
62 return NULL;
63 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
Christian Hessef77e2a82015-10-09 13:15:50 +020064 return xstrdup(ctx.env.server_name);
John Keepingfb3655d2013-04-06 10:28:57 +010065 return fmtalloc("%s:%s", ctx.env.server_name, ctx.env.server_port);
Lars Hjemlib2a3d312008-05-21 08:17:54 +020066}
67
Christian Hessec5c0eb82015-10-09 13:15:47 +020068char *cgit_currenturl(void)
Jason A. Donenfeld6bcda2f2015-03-03 17:18:42 +010069{
Jason A. Donenfeld8eef4582015-03-08 12:34:07 +010070 const char *root = cgit_rooturl();
71 size_t len = strlen(root);
John Keepingbead27b2016-08-07 16:13:30 +010072
73 if (!ctx.qry.url)
74 return xstrdup(root);
Jason A. Donenfeld8eef4582015-03-08 12:34:07 +010075 if (len && root[len - 1] == '/')
76 return fmtalloc("%s%s", root, ctx.qry.url);
77 return fmtalloc("%s/%s", root, ctx.qry.url);
Jason A. Donenfeld6bcda2f2015-03-03 17:18:42 +010078}
79
John Keepinge3d3fff2015-03-08 16:32:16 +000080const char *cgit_rooturl(void)
Lars Hjemli66cacd02007-02-17 13:46:18 +010081{
Lars Hjemlib228d4f2008-02-16 13:07:13 +010082 if (ctx.cfg.virtual_root)
John Keepingb1f17f12013-04-01 19:03:34 +010083 return ctx.cfg.virtual_root;
Lars Hjemli66cacd02007-02-17 13:46:18 +010084 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +010085 return ctx.cfg.script_name;
Lars Hjemli66cacd02007-02-17 13:46:18 +010086}
87
John Keepinge3d3fff2015-03-08 16:32:16 +000088const char *cgit_loginurl(void)
Jason A. Donenfelda4313262014-01-16 23:21:54 +010089{
John Keeping94e5f212015-03-08 16:32:24 +000090 static const char *login_url;
Jason A. Donenfelda4313262014-01-16 23:21:54 +010091 if (!login_url)
92 login_url = fmtalloc("%s?p=login", cgit_rooturl());
93 return login_url;
94}
95
Lars Hjemli74620f12006-12-11 16:48:03 +010096char *cgit_repourl(const char *reponame)
97{
John Keepingfb3655d2013-04-06 10:28:57 +010098 if (ctx.cfg.virtual_root)
99 return fmtalloc("%s%s/", ctx.cfg.virtual_root, reponame);
100 else
101 return fmtalloc("?r=%s", reponame);
Lars Hjemli74620f12006-12-11 16:48:03 +0100102}
103
Michael Krelin0df096f2007-07-21 13:13:40 +0200104char *cgit_fileurl(const char *reponame, const char *pagename,
105 const char *filename, const char *query)
Lars Hjemli74620f12006-12-11 16:48:03 +0100106{
John Keepingfb3655d2013-04-06 10:28:57 +0100107 struct strbuf sb = STRBUF_INIT;
Lars Hjemli68cf9b42007-11-03 11:15:56 +0100108 char *delim;
109
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100110 if (ctx.cfg.virtual_root) {
John Keepingfb3655d2013-04-06 10:28:57 +0100111 strbuf_addf(&sb, "%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
112 pagename, (filename ? filename:""));
Lars Hjemli68cf9b42007-11-03 11:15:56 +0100113 delim = "?";
Lars Hjemli74620f12006-12-11 16:48:03 +0100114 } else {
John Keepingfb3655d2013-04-06 10:28:57 +0100115 strbuf_addf(&sb, "?url=%s/%s/%s", reponame, pagename,
116 (filename ? filename : ""));
William Bellc366bd62012-10-09 20:45:58 +0200117 delim = "&amp;";
Lars Hjemli74620f12006-12-11 16:48:03 +0100118 }
Lars Hjemli68cf9b42007-11-03 11:15:56 +0100119 if (query)
John Keepingfb3655d2013-04-06 10:28:57 +0100120 strbuf_addf(&sb, "%s%s", delim, query);
121 return strbuf_detach(&sb, NULL);
Lars Hjemli74620f12006-12-11 16:48:03 +0100122}
123
Michael Krelin0df096f2007-07-21 13:13:40 +0200124char *cgit_pageurl(const char *reponame, const char *pagename,
125 const char *query)
126{
John Keepingd34b9672015-03-08 16:32:25 +0000127 return cgit_fileurl(reponame, pagename, NULL, query);
Michael Krelin0df096f2007-07-21 13:13:40 +0200128}
129
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200130const char *cgit_repobasename(const char *reponame)
131{
132 /* I assume we don't need to store more than one repo basename */
133 static char rvbuf[1024];
134 int p;
135 const char *rv;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100136 strncpy(rvbuf, reponame, sizeof(rvbuf));
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500137 if (rvbuf[sizeof(rvbuf)-1])
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200138 die("cgit_repobasename: truncated repository name '%s'", reponame);
139 p = strlen(rvbuf)-1;
140 /* strip trailing slashes */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500141 while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200142 /* strip trailing .git */
Christian Hesse79c985e2014-05-29 17:35:46 +0200143 if (p >= 3 && starts_with(&rvbuf[p-3], ".git")) {
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200144 p -= 3; rvbuf[p--] = 0;
145 }
146 /* strip more trailing slashes if any */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500147 while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200148 /* find last slash in the remaining string */
149 rv = strrchr(rvbuf,'/');
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500150 if (rv)
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200151 return ++rv;
152 return rvbuf;
153}
Michael Krelin0df096f2007-07-21 13:13:40 +0200154
John Keepingc1572bb2018-03-31 14:20:01 +0100155const char *cgit_snapshot_prefix(const struct cgit_repo *repo)
156{
157 if (repo->snapshot_prefix)
158 return repo->snapshot_prefix;
159
160 return cgit_repobasename(repo->url);
161}
162
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100163static void site_url(const char *page, const char *search, const char *sort, int ofs, int always_root)
Lars Hjemli71adba12008-04-29 01:09:41 +0200164{
165 char *delim = "?";
166
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100167 if (always_root || page)
Jason A. Donenfelddb4b7352015-03-03 17:13:52 +0100168 html_attr(cgit_rooturl());
Christian Hesse3e244a02015-10-09 13:15:48 +0200169 else {
170 char *currenturl = cgit_currenturl();
171 html_attr(currenturl);
172 free(currenturl);
173 }
Lars Hjemli71adba12008-04-29 01:09:41 +0200174
175 if (page) {
176 htmlf("?p=%s", page);
William Bellc366bd62012-10-09 20:45:58 +0200177 delim = "&amp;";
Lars Hjemli71adba12008-04-29 01:09:41 +0200178 }
179 if (search) {
180 html(delim);
181 html("q=");
182 html_attr(search);
William Bellc366bd62012-10-09 20:45:58 +0200183 delim = "&amp;";
Lars Hjemli141f1c32008-05-03 10:37:02 +0200184 }
Tobias Grimm7530d942011-07-31 02:44:05 +0200185 if (sort) {
186 html(delim);
187 html("s=");
188 html_attr(sort);
William Bellc366bd62012-10-09 20:45:58 +0200189 delim = "&amp;";
Tobias Grimm7530d942011-07-31 02:44:05 +0200190 }
Lars Hjemli141f1c32008-05-03 10:37:02 +0200191 if (ofs) {
192 html(delim);
193 htmlf("ofs=%d", ofs);
Lars Hjemli71adba12008-04-29 01:09:41 +0200194 }
195}
196
Johan Herlandc3f23d42010-06-10 01:09:24 +0200197static void site_link(const char *page, const char *name, const char *title,
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100198 const char *class, const char *search, const char *sort, int ofs, int always_root)
Lars Hjemli71adba12008-04-29 01:09:41 +0200199{
200 html("<a");
201 if (title) {
202 html(" title='");
203 html_attr(title);
204 html("'");
205 }
206 if (class) {
207 html(" class='");
208 html_attr(class);
209 html("'");
210 }
211 html(" href='");
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100212 site_url(page, search, sort, ofs, always_root);
Lars Hjemli71adba12008-04-29 01:09:41 +0200213 html("'>");
214 html_txt(name);
215 html("</a>");
216}
217
Johan Herlandc3f23d42010-06-10 01:09:24 +0200218void cgit_index_link(const char *name, const char *title, const char *class,
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100219 const char *pattern, const char *sort, int ofs, int always_root)
Lars Hjemli141f1c32008-05-03 10:37:02 +0200220{
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100221 site_link(NULL, name, title, class, pattern, sort, ofs, always_root);
Lars Hjemli141f1c32008-05-03 10:37:02 +0200222}
223
Johan Herlandc3f23d42010-06-10 01:09:24 +0200224static char *repolink(const char *title, const char *class, const char *page,
225 const char *head, const char *path)
Lars Hjemli44947bf2007-06-17 01:23:08 +0200226{
227 char *delim = "?";
228
229 html("<a");
230 if (title) {
231 html(" title='");
232 html_attr(title);
233 html("'");
234 }
235 if (class) {
236 html(" class='");
237 html_attr(class);
238 html("'");
239 }
240 html(" href='");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100241 if (ctx.cfg.virtual_root) {
Lars Hjemli44b208a2008-10-05 16:54:44 +0200242 html_url_path(ctx.cfg.virtual_root);
Lars Hjemli44b208a2008-10-05 16:54:44 +0200243 html_url_path(ctx.repo->url);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100244 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
Lars Hjemli44947bf2007-06-17 01:23:08 +0200245 html("/");
Lars Hjemlib8be0282007-06-18 00:18:42 +0200246 if (page) {
Lars Hjemli44b208a2008-10-05 16:54:44 +0200247 html_url_path(page);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200248 html("/");
249 if (path)
Lars Hjemli44b208a2008-10-05 16:54:44 +0200250 html_url_path(path);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200251 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200252 } else {
John Keepinga45030f2014-01-12 19:45:16 +0000253 html_url_path(ctx.cfg.script_name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200254 html("?url=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200255 html_url_arg(ctx.repo->url);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100256 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
Lars Hjemli44947bf2007-06-17 01:23:08 +0200257 html("/");
Lars Hjemlib8be0282007-06-18 00:18:42 +0200258 if (page) {
Lars Hjemlib5751152008-10-05 12:52:25 +0200259 html_url_arg(page);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200260 html("/");
261 if (path)
Lars Hjemlib5751152008-10-05 12:52:25 +0200262 html_url_arg(path);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200263 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200264 delim = "&amp;";
265 }
Eric Wong590ba452016-07-06 07:08:01 +0000266 if (head && ctx.repo->defbranch && strcmp(head, ctx.repo->defbranch)) {
Lars Hjemli44947bf2007-06-17 01:23:08 +0200267 html(delim);
268 html("h=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200269 html_url_arg(head);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200270 delim = "&amp;";
271 }
272 return fmt("%s", delim);
273}
274
Johan Herlandc3f23d42010-06-10 01:09:24 +0200275static void reporevlink(const char *page, const char *name, const char *title,
276 const char *class, const char *head, const char *rev,
277 const char *path)
Lars Hjemli44947bf2007-06-17 01:23:08 +0200278{
279 char *delim;
280
Lars Hjemli48c487d2007-06-17 13:57:51 +0200281 delim = repolink(title, class, page, head, path);
Florian Pritz8d946072010-02-01 17:55:37 +0100282 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
Lars Hjemli44947bf2007-06-17 01:23:08 +0200283 html(delim);
284 html("id=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200285 html_url_arg(rev);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200286 }
287 html("'>");
288 html_txt(name);
289 html("</a>");
290}
Lars Hjemli148fb962006-12-16 00:33:28 +0100291
Johan Herlandc3f23d42010-06-10 01:09:24 +0200292void cgit_summary_link(const char *name, const char *title, const char *class,
293 const char *head)
Lars Hjemlie9d3bd52008-10-05 16:55:50 +0200294{
295 reporevlink(NULL, name, title, class, head, NULL, NULL);
296}
297
Johan Herlandc3f23d42010-06-10 01:09:24 +0200298void cgit_tag_link(const char *name, const char *title, const char *class,
John Keepingc422b9b2015-01-15 22:18:14 +0000299 const char *tag)
Lars Hjemlicf61ad42008-10-05 21:18:45 +0200300{
John Keepingc422b9b2015-01-15 22:18:14 +0000301 reporevlink("tag", name, title, class, tag, NULL, NULL);
Lars Hjemlicf61ad42008-10-05 21:18:45 +0200302}
303
Johan Herlandc3f23d42010-06-10 01:09:24 +0200304void cgit_tree_link(const char *name, const char *title, const char *class,
305 const char *head, const char *rev, const char *path)
Lars Hjemli48c487d2007-06-17 13:57:51 +0200306{
307 reporevlink("tree", name, title, class, head, rev, path);
308}
309
Johan Herlandc3f23d42010-06-10 01:09:24 +0200310void cgit_plain_link(const char *name, const char *title, const char *class,
311 const char *head, const char *rev, const char *path)
Lars Hjemli65b7b872008-08-06 11:07:13 +0200312{
313 reporevlink("plain", name, title, class, head, rev, path);
314}
315
Jeff Smith1649afd2017-10-01 23:39:09 -0500316void cgit_blame_link(const char *name, const char *title, const char *class,
317 const char *head, const char *rev, const char *path)
318{
319 reporevlink("blame", name, title, class, head, rev, path);
320}
321
Johan Herlandc3f23d42010-06-10 01:09:24 +0200322void cgit_log_link(const char *name, const char *title, const char *class,
323 const char *head, const char *rev, const char *path,
John Keeping30304d82015-08-12 15:55:28 +0100324 int ofs, const char *grep, const char *pattern, int showmsg,
325 int follow)
Lars Hjemli48c487d2007-06-17 13:57:51 +0200326{
Lars Hjemli103940f2007-06-29 20:27:41 +0200327 char *delim;
328
329 delim = repolink(title, class, "log", head, path);
Eric Wong21418ec2012-01-04 09:01:51 +0000330 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
Lars Hjemli103940f2007-06-29 20:27:41 +0200331 html(delim);
332 html("id=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200333 html_url_arg(rev);
William Bellc366bd62012-10-09 20:45:58 +0200334 delim = "&amp;";
Lars Hjemli103940f2007-06-29 20:27:41 +0200335 }
Lars Hjemli51140312007-11-03 10:42:37 +0100336 if (grep && pattern) {
337 html(delim);
338 html("qt=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200339 html_url_arg(grep);
William Bellc366bd62012-10-09 20:45:58 +0200340 delim = "&amp;";
Lars Hjemli51140312007-11-03 10:42:37 +0100341 html(delim);
342 html("q=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200343 html_url_arg(pattern);
Lars Hjemli51140312007-11-03 10:42:37 +0100344 }
Lars Hjemli103940f2007-06-29 20:27:41 +0200345 if (ofs > 0) {
346 html(delim);
347 html("ofs=");
348 htmlf("%d", ofs);
William Bellc366bd62012-10-09 20:45:58 +0200349 delim = "&amp;";
Lars Hjemli0274b572008-11-29 18:39:41 +0100350 }
351 if (showmsg) {
352 html(delim);
353 html("showmsg=1");
John Keeping30304d82015-08-12 15:55:28 +0100354 delim = "&amp;";
355 }
356 if (follow) {
357 html(delim);
358 html("follow=1");
Lars Hjemli103940f2007-06-29 20:27:41 +0200359 }
360 html("'>");
361 html_txt(name);
362 html("</a>");
Lars Hjemli48c487d2007-06-17 13:57:51 +0200363}
364
Jeff Smithf6ffe402017-10-01 23:39:07 -0500365void cgit_commit_link(const char *name, const char *title, const char *class,
John Keepingeeddb5b2014-10-05 10:59:02 +0100366 const char *head, const char *rev, const char *path)
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200367{
John Keepingbead27b2016-08-07 16:13:30 +0100368 char *delim;
369
Johan Herland685872b2010-06-10 01:09:35 +0200370 delim = repolink(title, class, "commit", head, path);
Eric Wong21418ec2012-01-04 09:01:51 +0000371 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200372 html(delim);
373 html("id=");
374 html_url_arg(rev);
375 delim = "&amp;";
376 }
John Keeping18302712014-10-05 10:59:04 +0100377 if (ctx.qry.difftype) {
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200378 html(delim);
John Keeping18302712014-10-05 10:59:04 +0100379 htmlf("dt=%d", ctx.qry.difftype);
Johan Herlandd20313e2010-06-10 20:15:51 +0200380 delim = "&amp;";
381 }
382 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
383 html(delim);
384 html("context=");
385 htmlf("%d", ctx.qry.context);
386 delim = "&amp;";
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200387 }
Johan Herland72ef9132010-06-24 17:53:20 +0200388 if (ctx.qry.ignorews) {
389 html(delim);
390 html("ignorews=1");
391 delim = "&amp;";
392 }
John Keeping30304d82015-08-12 15:55:28 +0100393 if (ctx.qry.follow) {
394 html(delim);
395 html("follow=1");
396 }
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200397 html("'>");
Jeff Smithf6ffe402017-10-01 23:39:07 -0500398 if (name[0] != '\0') {
399 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
400 html_ntxt(name, ctx.cfg.max_msg_len - 3);
401 html("...");
402 } else
403 html_txt(name);
404 } else
Christian Frankefe1bb0e2012-10-28 18:36:08 +0100405 html_txt("(no commit message)");
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200406 html("</a>");
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200407}
408
Johan Herlandc3f23d42010-06-10 01:09:24 +0200409void cgit_refs_link(const char *name, const char *title, const char *class,
410 const char *head, const char *rev, const char *path)
Lars Hjemliac1f4932007-10-27 10:47:44 +0200411{
412 reporevlink("refs", name, title, class, head, rev, path);
413}
414
Johan Herlandc3f23d42010-06-10 01:09:24 +0200415void cgit_snapshot_link(const char *name, const char *title, const char *class,
416 const char *head, const char *rev,
417 const char *archivename)
Lars Hjemlieb453422007-07-23 00:11:15 +0200418{
419 reporevlink("snapshot", name, title, class, head, rev, archivename);
420}
421
Johan Herlandc3f23d42010-06-10 01:09:24 +0200422void cgit_diff_link(const char *name, const char *title, const char *class,
423 const char *head, const char *new_rev, const char *old_rev,
John Keeping03f537f2014-10-05 10:59:03 +0100424 const char *path)
Lars Hjemli4a0be582007-06-17 18:12:03 +0200425{
426 char *delim;
427
428 delim = repolink(title, class, "diff", head, path);
Florian Pritz8d946072010-02-01 17:55:37 +0100429 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
Lars Hjemli4a0be582007-06-17 18:12:03 +0200430 html(delim);
431 html("id=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200432 html_url_arg(new_rev);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200433 delim = "&amp;";
434 }
435 if (old_rev) {
436 html(delim);
437 html("id2=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200438 html_url_arg(old_rev);
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200439 delim = "&amp;";
440 }
John Keeping18302712014-10-05 10:59:04 +0100441 if (ctx.qry.difftype) {
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200442 html(delim);
John Keeping18302712014-10-05 10:59:04 +0100443 htmlf("dt=%d", ctx.qry.difftype);
Johan Herlandd20313e2010-06-10 20:15:51 +0200444 delim = "&amp;";
445 }
446 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
447 html(delim);
448 html("context=");
449 htmlf("%d", ctx.qry.context);
450 delim = "&amp;";
Lars Hjemli4a0be582007-06-17 18:12:03 +0200451 }
Johan Herland72ef9132010-06-24 17:53:20 +0200452 if (ctx.qry.ignorews) {
453 html(delim);
454 html("ignorews=1");
455 delim = "&amp;";
456 }
John Keeping30304d82015-08-12 15:55:28 +0100457 if (ctx.qry.follow) {
458 html(delim);
459 html("follow=1");
460 }
Lars Hjemli4a0be582007-06-17 18:12:03 +0200461 html("'>");
462 html_txt(name);
463 html("</a>");
464}
465
Johan Herlandc3f23d42010-06-10 01:09:24 +0200466void cgit_patch_link(const char *name, const char *title, const char *class,
Johan Herlandeac1b672010-06-10 01:09:33 +0200467 const char *head, const char *rev, const char *path)
Lars Hjemli620bb3e2007-12-10 21:47:29 +0100468{
Johan Herlandeac1b672010-06-10 01:09:33 +0200469 reporevlink("patch", name, title, class, head, rev, path);
Lars Hjemli620bb3e2007-12-10 21:47:29 +0100470}
471
Johan Herlandc3f23d42010-06-10 01:09:24 +0200472void cgit_stats_link(const char *name, const char *title, const char *class,
473 const char *head, const char *path)
Lars Hjemlieaf2d252008-12-07 13:34:16 +0100474{
475 reporevlink("stats", name, title, class, head, NULL, path);
476}
477
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100478static void cgit_self_link(char *name, const char *title, const char *class)
Johan Herland24fd7e52010-06-10 01:09:29 +0200479{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100480 if (!strcmp(ctx.qry.page, "repolist"))
481 cgit_index_link(name, title, class, ctx.qry.search, ctx.qry.sort,
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100482 ctx.qry.ofs, 1);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100483 else if (!strcmp(ctx.qry.page, "summary"))
484 cgit_summary_link(name, title, class, ctx.qry.head);
485 else if (!strcmp(ctx.qry.page, "tag"))
John Keepingc422b9b2015-01-15 22:18:14 +0000486 cgit_tag_link(name, title, class, ctx.qry.has_sha1 ?
487 ctx.qry.sha1 : ctx.qry.head);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100488 else if (!strcmp(ctx.qry.page, "tree"))
489 cgit_tree_link(name, title, class, ctx.qry.head,
490 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
491 ctx.qry.path);
492 else if (!strcmp(ctx.qry.page, "plain"))
493 cgit_plain_link(name, title, class, ctx.qry.head,
494 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
495 ctx.qry.path);
Jeff Smith1649afd2017-10-01 23:39:09 -0500496 else if (!strcmp(ctx.qry.page, "blame"))
497 cgit_blame_link(name, title, class, ctx.qry.head,
498 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
499 ctx.qry.path);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100500 else if (!strcmp(ctx.qry.page, "log"))
501 cgit_log_link(name, title, class, ctx.qry.head,
502 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
503 ctx.qry.path, ctx.qry.ofs,
504 ctx.qry.grep, ctx.qry.search,
John Keeping30304d82015-08-12 15:55:28 +0100505 ctx.qry.showmsg, ctx.qry.follow);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100506 else if (!strcmp(ctx.qry.page, "commit"))
507 cgit_commit_link(name, title, class, ctx.qry.head,
508 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
John Keepingeeddb5b2014-10-05 10:59:02 +0100509 ctx.qry.path);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100510 else if (!strcmp(ctx.qry.page, "patch"))
511 cgit_patch_link(name, title, class, ctx.qry.head,
512 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
513 ctx.qry.path);
514 else if (!strcmp(ctx.qry.page, "refs"))
515 cgit_refs_link(name, title, class, ctx.qry.head,
516 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
517 ctx.qry.path);
518 else if (!strcmp(ctx.qry.page, "snapshot"))
519 cgit_snapshot_link(name, title, class, ctx.qry.head,
520 ctx.qry.has_sha1 ? ctx.qry.sha1 : NULL,
521 ctx.qry.path);
522 else if (!strcmp(ctx.qry.page, "diff"))
523 cgit_diff_link(name, title, class, ctx.qry.head,
524 ctx.qry.sha1, ctx.qry.sha2,
John Keeping03f537f2014-10-05 10:59:03 +0100525 ctx.qry.path);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100526 else if (!strcmp(ctx.qry.page, "stats"))
527 cgit_stats_link(name, title, class, ctx.qry.head,
528 ctx.qry.path);
John Keeping6d8a7892013-03-06 20:51:54 +0000529 else {
530 /* Don't known how to make link for this page */
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100531 repolink(title, class, ctx.qry.page, ctx.qry.head, ctx.qry.path);
John Keeping6d8a7892013-03-06 20:51:54 +0000532 html("><!-- cgit_self_link() doesn't know how to make link for page '");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100533 html_txt(ctx.qry.page);
John Keeping6d8a7892013-03-06 20:51:54 +0000534 html("' -->");
535 html_txt(name);
536 html("</a>");
537 }
Johan Herland24fd7e52010-06-10 01:09:29 +0200538}
539
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200540void cgit_object_link(struct object *obj)
541{
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100542 char *page, *shortrev, *fullrev, *name;
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200543
Christian Hesse559ab5e2016-01-05 07:38:53 +0100544 fullrev = oid_to_hex(&obj->oid);
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100545 shortrev = xstrdup(fullrev);
546 shortrev[10] = '\0';
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200547 if (obj->type == OBJ_COMMIT) {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100548 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
John Keepingeeddb5b2014-10-05 10:59:02 +0100549 ctx.qry.head, fullrev, NULL);
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200550 return;
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200551 } else if (obj->type == OBJ_TREE)
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200552 page = "tree";
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200553 else if (obj->type == OBJ_TAG)
Lars Hjemlifc5880f2007-10-28 15:40:47 +0100554 page = "tag";
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200555 else
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200556 page = "blob";
Christian Hessefb804a32018-05-30 10:28:12 +0200557 name = fmt("%s %s...", type_name(obj->type), shortrev);
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100558 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200559}
560
Lukas Fleischerbafab422013-03-04 08:52:33 +0100561static struct string_list_item *lookup_path(struct string_list *list,
562 const char *path)
Lars Hjemli6857bec2011-06-15 10:04:13 +0200563{
564 struct string_list_item *item;
565
566 while (path && path[0]) {
567 if ((item = string_list_lookup(list, path)))
568 return item;
569 if (!(path = strchr(path, '/')))
570 break;
571 path++;
572 }
573 return NULL;
574}
575
576void cgit_submodule_link(const char *class, char *path, const char *rev)
577{
578 struct string_list *list;
579 struct string_list_item *item;
580 char tail, *dir;
581 size_t len;
582
Jason A. Donenfeld40e1d9b2013-03-20 20:43:13 +0100583 len = 0;
Lars Hjemli6857bec2011-06-15 10:04:13 +0200584 tail = 0;
585 list = &ctx.repo->submodules;
586 item = lookup_path(list, path);
587 if (!item) {
588 len = strlen(path);
589 tail = path[len - 1];
590 if (tail == '/') {
591 path[len - 1] = 0;
592 item = lookup_path(list, path);
593 }
594 }
Lukas Fleischerdb021a12015-03-05 20:41:45 +0100595 if (item || ctx.repo->module_link) {
596 html("<a ");
597 if (class)
598 htmlf("class='%s' ", class);
599 html("href='");
600 if (item) {
601 html_attrf(item->util, rev);
602 } else {
603 dir = strrchr(path, '/');
604 if (dir)
605 dir++;
606 else
607 dir = path;
608 html_attrf(ctx.repo->module_link, dir, rev);
609 }
610 html("'>");
611 html_txt(path);
612 html("</a>");
Lars Hjemli6857bec2011-06-15 10:04:13 +0200613 } else {
Lukas Fleischerdb021a12015-03-05 20:41:45 +0100614 html("<span");
615 if (class)
616 htmlf(" class='%s'", class);
617 html(">");
618 html_txt(path);
619 html("</span>");
Lars Hjemli6857bec2011-06-15 10:04:13 +0200620 }
John Keepingfb3655d2013-04-06 10:28:57 +0100621 html_txtf(" @ %.7s", rev);
Lars Hjemli6857bec2011-06-15 10:04:13 +0200622 if (item && tail)
623 path[len - 1] = tail;
624}
625
John Keeping9c15f3c2016-02-08 15:05:54 +0000626const struct date_mode *cgit_date_mode(enum date_mode_type type)
John Keeping360af462016-01-19 19:33:03 +0000627{
628 static struct date_mode mode;
John Keeping9c15f3c2016-02-08 15:05:54 +0000629 mode.type = type;
John Keeping360af462016-01-19 19:33:03 +0000630 mode.local = ctx.cfg.local_time;
631 return &mode;
632}
633
John Keepingf2a901d2016-01-19 19:33:05 +0000634static void print_rel_date(time_t t, int tz, double value,
John Keepingcaed6cb2014-12-20 13:59:39 +0000635 const char *class, const char *suffix)
636{
John Keepingcaed6cb2014-12-20 13:59:39 +0000637 htmlf("<span class='%s' title='", class);
John Keeping9c15f3c2016-02-08 15:05:54 +0000638 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
John Keepingcaed6cb2014-12-20 13:59:39 +0000639 htmlf("'>%.0f %s</span>", value, suffix);
640}
641
John Keepingf2a901d2016-01-19 19:33:05 +0000642void cgit_print_age(time_t t, int tz, time_t max_relative)
Lars Hjemli5db39172007-05-22 23:08:46 +0200643{
644 time_t now, secs;
645
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +0100646 if (!t)
647 return;
Lars Hjemli5db39172007-05-22 23:08:46 +0200648 time(&now);
649 secs = now - t;
Jason A. Donenfeldbb3cc0d2014-01-17 15:41:41 +0100650 if (secs < 0)
651 secs = 0;
Lars Hjemli5db39172007-05-22 23:08:46 +0200652
653 if (secs > max_relative && max_relative >= 0) {
John Keepinga3606662015-08-13 12:24:34 +0100654 html("<span title='");
John Keeping9c15f3c2016-02-08 15:05:54 +0000655 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
John Keepinga3606662015-08-13 12:24:34 +0100656 html("'>");
John Keeping9c15f3c2016-02-08 15:05:54 +0000657 html_txt(show_date(t, tz, cgit_date_mode(DATE_SHORT)));
John Keepinga3606662015-08-13 12:24:34 +0100658 html("</span>");
Lars Hjemli5db39172007-05-22 23:08:46 +0200659 return;
660 }
661
662 if (secs < TM_HOUR * 2) {
John Keepingf2a901d2016-01-19 19:33:05 +0000663 print_rel_date(t, tz, secs * 1.0 / TM_MIN, "age-mins", "min.");
Lars Hjemli5db39172007-05-22 23:08:46 +0200664 return;
665 }
666 if (secs < TM_DAY * 2) {
John Keepingf2a901d2016-01-19 19:33:05 +0000667 print_rel_date(t, tz, secs * 1.0 / TM_HOUR, "age-hours", "hours");
Lars Hjemli5db39172007-05-22 23:08:46 +0200668 return;
669 }
670 if (secs < TM_WEEK * 2) {
John Keepingf2a901d2016-01-19 19:33:05 +0000671 print_rel_date(t, tz, secs * 1.0 / TM_DAY, "age-days", "days");
Lars Hjemli5db39172007-05-22 23:08:46 +0200672 return;
673 }
674 if (secs < TM_MONTH * 2) {
John Keepingf2a901d2016-01-19 19:33:05 +0000675 print_rel_date(t, tz, secs * 1.0 / TM_WEEK, "age-weeks", "weeks");
Lars Hjemli5db39172007-05-22 23:08:46 +0200676 return;
677 }
678 if (secs < TM_YEAR * 2) {
John Keepingf2a901d2016-01-19 19:33:05 +0000679 print_rel_date(t, tz, secs * 1.0 / TM_MONTH, "age-months", "months");
Lars Hjemli5db39172007-05-22 23:08:46 +0200680 return;
681 }
John Keepingf2a901d2016-01-19 19:33:05 +0000682 print_rel_date(t, tz, secs * 1.0 / TM_YEAR, "age-years", "years");
Lars Hjemli5db39172007-05-22 23:08:46 +0200683}
684
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100685void cgit_print_http_headers(void)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100686{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100687 if (ctx.env.no_http && !strcmp(ctx.env.no_http, "1"))
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100688 return;
689
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100690 if (ctx.page.status)
691 htmlf("Status: %d %s\n", ctx.page.status, ctx.page.statusmsg);
692 if (ctx.page.mimetype && ctx.page.charset)
693 htmlf("Content-Type: %s; charset=%s\n", ctx.page.mimetype,
694 ctx.page.charset);
695 else if (ctx.page.mimetype)
696 htmlf("Content-Type: %s\n", ctx.page.mimetype);
697 if (ctx.page.size)
698 htmlf("Content-Length: %zd\n", ctx.page.size);
Jason A. Donenfeld513b3862016-01-14 14:28:37 +0100699 if (ctx.page.filename) {
700 html("Content-Disposition: inline; filename=\"");
701 html_header_arg_in_quotes(ctx.page.filename);
702 html("\"\n");
703 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100704 if (!ctx.env.authenticated)
Jason A. Donenfeldd6e92002014-01-14 21:49:31 +0100705 html("Cache-Control: no-cache, no-store\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100706 htmlf("Last-Modified: %s\n", http_date(ctx.page.modified));
707 htmlf("Expires: %s\n", http_date(ctx.page.expires));
708 if (ctx.page.etag)
709 htmlf("ETag: \"%s\"\n", ctx.page.etag);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100710 html("\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100711 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
Lars Hjemli3ff58dd2009-02-19 23:24:15 +0100712 exit(0);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100713}
714
Jason A. Donenfeldd7034802015-08-12 14:50:09 +0200715void cgit_redirect(const char *url, bool permanent)
716{
717 htmlf("Status: %d %s\n", permanent ? 301 : 302, permanent ? "Moved" : "Found");
Jason A. Donenfeld42914532016-01-14 14:13:39 +0100718 html("Location: ");
719 html_url_path(url);
720 html("\n\n");
Jason A. Donenfeldd7034802015-08-12 14:50:09 +0200721}
722
John Keeping3c53ebf2014-08-01 22:14:19 +0100723static void print_rel_vcs_link(const char *url)
724{
725 html("<link rel='vcs-git' href='");
726 html_attr(url);
727 html("' title='");
728 html_attr(ctx.repo->name);
729 html(" Git repository'/>\n");
730}
731
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100732void cgit_print_docstart(void)
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100733{
John Keepingbead27b2016-08-07 16:13:30 +0100734 char *host = cgit_hosturl();
735
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100736 if (ctx.cfg.embedded) {
737 if (ctx.cfg.header)
738 html_include(ctx.cfg.header);
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100739 return;
Lars Hjemli80550bb2009-08-11 10:12:35 +0200740 }
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100741
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100742 html(cgit_doctype);
Juuso Lapinlampi8d05b392016-05-11 18:04:14 +0000743 html("<html lang='en'>\n");
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100744 html("<head>\n");
745 html("<title>");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100746 html_txt(ctx.page.title);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100747 html("</title>\n");
Lars Hjemlif6925032007-06-18 09:42:10 +0200748 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100749 if (ctx.cfg.robots && *ctx.cfg.robots)
750 htmlf("<meta name='robots' content='%s'/>\n", ctx.cfg.robots);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100751 html("<link rel='stylesheet' type='text/css' href='");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100752 html_attr(ctx.cfg.css);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100753 html("'/>\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100754 if (ctx.cfg.favicon) {
Lars Hjemli502865a2008-07-19 20:40:30 +0200755 html("<link rel='shortcut icon' href='");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100756 html_attr(ctx.cfg.favicon);
Lars Hjemli502865a2008-07-19 20:40:30 +0200757 html("'/>\n");
758 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100759 if (host && ctx.repo && ctx.qry.head) {
Christian Hesse37fce992015-10-09 13:15:46 +0200760 char *fileurl;
John Keepingfb3655d2013-04-06 10:28:57 +0100761 struct strbuf sb = STRBUF_INIT;
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100762 strbuf_addf(&sb, "h=%s", ctx.qry.head);
John Keepingfb3655d2013-04-06 10:28:57 +0100763
Diego Ongaro694dd432009-06-10 18:18:34 -0500764 html("<link rel='alternate' title='Atom feed' href='");
765 html(cgit_httpscheme());
Christian Hesse6edfc162015-10-09 13:15:51 +0200766 html_attr(host);
Christian Hesse37fce992015-10-09 13:15:46 +0200767 fileurl = cgit_fileurl(ctx.repo->url, "atom", ctx.qry.vpath,
768 sb.buf);
769 html_attr(fileurl);
Mark Lodatob5a3a202009-03-15 00:11:54 -0400770 html("' type='application/atom+xml'/>\n");
John Keepingfb3655d2013-04-06 10:28:57 +0100771 strbuf_release(&sb);
Christian Hesse37fce992015-10-09 13:15:46 +0200772 free(fileurl);
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200773 }
John Keeping3c53ebf2014-08-01 22:14:19 +0100774 if (ctx.repo)
775 cgit_add_clone_urls(print_rel_vcs_link);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100776 if (ctx.cfg.head_include)
777 html_include(ctx.cfg.head_include);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100778 html("</head>\n");
779 html("<body>\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100780 if (ctx.cfg.header)
781 html_include(ctx.cfg.header);
Christian Hesse6edfc162015-10-09 13:15:51 +0200782 free(host);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100783}
784
John Keepinge3d3fff2015-03-08 16:32:16 +0000785void cgit_print_docend(void)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100786{
Lars Hjemli80550bb2009-08-11 10:12:35 +0200787 html("</div> <!-- class=content -->\n");
788 if (ctx.cfg.embedded) {
789 html("</div> <!-- id=cgit -->\n");
790 if (ctx.cfg.footer)
791 html_include(ctx.cfg.footer);
792 return;
793 }
Lars Hjemlide5e9282008-06-26 13:53:30 +0200794 if (ctx.cfg.footer)
795 html_include(ctx.cfg.footer);
796 else {
Christian Hessee65ea962018-06-04 22:27:46 +0200797 htmlf("<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit %s</a> "
798 "(<a href='https://git-scm.com/'>git %s</a>) at ", cgit_version, git_version_string);
John Keeping9c15f3c2016-02-08 15:05:54 +0000799 html_txt(show_date(time(NULL), 0, cgit_date_mode(DATE_ISO8601)));
Lars Hjemlide5e9282008-06-26 13:53:30 +0200800 html("</div>\n");
801 }
Lars Hjemli80550bb2009-08-11 10:12:35 +0200802 html("</div> <!-- id=cgit -->\n");
Lars Hjemlide5e9282008-06-26 13:53:30 +0200803 html("</body>\n</html>\n");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100804}
805
John Keepingaec12042015-08-14 12:47:01 +0100806void cgit_print_error_page(int code, const char *msg, const char *fmt, ...)
807{
808 va_list ap;
John Keepingc5975ae2015-08-14 12:47:22 +0100809 ctx.page.expires = ctx.cfg.cache_dynamic_ttl;
John Keepingaec12042015-08-14 12:47:01 +0100810 ctx.page.status = code;
811 ctx.page.statusmsg = msg;
Juuso Lapinlampi80f12b32016-05-11 17:50:09 +0000812 cgit_print_layout_start();
John Keepingaec12042015-08-14 12:47:01 +0100813 va_start(ap, fmt);
814 cgit_vprint_error(fmt, ap);
815 va_end(ap);
Juuso Lapinlampi80f12b32016-05-11 17:50:09 +0000816 cgit_print_layout_end();
John Keepingaec12042015-08-14 12:47:01 +0100817}
818
John Keeping76498792015-08-14 12:47:11 +0100819void cgit_print_layout_start(void)
820{
821 cgit_print_http_headers();
822 cgit_print_docstart();
823 cgit_print_pageheader();
824}
825
826void cgit_print_layout_end(void)
827{
828 cgit_print_docend();
829}
830
John Keepingbbfa0062014-08-01 22:14:17 +0100831static void add_clone_urls(void (*fn)(const char *), char *txt, char *suffix)
832{
Lukas Fleischer1a9e5662015-02-05 10:11:42 +0100833 struct strbuf **url_list = strbuf_split_str(txt, ' ', 0);
834 int i;
John Keepingbbfa0062014-08-01 22:14:17 +0100835
Lukas Fleischer1a9e5662015-02-05 10:11:42 +0100836 for (i = 0; url_list[i]; i++) {
837 strbuf_rtrim(url_list[i]);
838 if (url_list[i]->len == 0)
839 continue;
840 if (suffix && *suffix)
841 strbuf_addf(url_list[i], "/%s", suffix);
842 fn(url_list[i]->buf);
John Keepingbbfa0062014-08-01 22:14:17 +0100843 }
844
Lukas Fleischer1a9e5662015-02-05 10:11:42 +0100845 strbuf_list_free(url_list);
John Keepingbbfa0062014-08-01 22:14:17 +0100846}
847
848void cgit_add_clone_urls(void (*fn)(const char *))
849{
850 if (ctx.repo->clone_url)
851 add_clone_urls(fn, expand_macros(ctx.repo->clone_url), NULL);
852 else if (ctx.cfg.clone_prefix)
853 add_clone_urls(fn, ctx.cfg.clone_prefix, ctx.repo->url);
854}
855
Christian Hessede83de22015-07-28 10:42:01 +0200856static int print_branch_option(const char *refname, const struct object_id *oid,
Lukas Fleischerbafab422013-03-04 08:52:33 +0100857 int flags, void *cb_data)
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100858{
859 char *name = (char *)refname;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100860 html_option(name, name, ctx.qry.head);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100861 return 0;
862}
863
Johan Herlandc3f23d42010-06-10 01:09:24 +0200864void cgit_add_hidden_formfields(int incl_head, int incl_search,
865 const char *page)
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100866{
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100867 if (!ctx.cfg.virtual_root) {
John Keepingfb3655d2013-04-06 10:28:57 +0100868 struct strbuf url = STRBUF_INIT;
869
870 strbuf_addf(&url, "%s/%s", ctx.qry.repo, page);
Johan Herlandc8e32952010-06-10 01:09:27 +0200871 if (ctx.qry.vpath)
John Keepingfb3655d2013-04-06 10:28:57 +0100872 strbuf_addf(&url, "/%s", ctx.qry.vpath);
873 html_hidden("url", url.buf);
874 strbuf_release(&url);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100875 }
876
Lars Hjemli25c84322008-07-27 12:32:08 +0200877 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
878 strcmp(ctx.qry.head, ctx.repo->defbranch))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100879 html_hidden("h", ctx.qry.head);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100880
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100881 if (ctx.qry.sha1)
882 html_hidden("id", ctx.qry.sha1);
883 if (ctx.qry.sha2)
884 html_hidden("id2", ctx.qry.sha2);
Lars Hjemli0274b572008-11-29 18:39:41 +0100885 if (ctx.qry.showmsg)
886 html_hidden("showmsg", "1");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100887
888 if (incl_search) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100889 if (ctx.qry.grep)
890 html_hidden("qt", ctx.qry.grep);
891 if (ctx.qry.search)
892 html_hidden("q", ctx.qry.search);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100893 }
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100894}
895
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100896static const char *hc(const char *page)
Lars Hjemlif1355692008-04-12 15:53:31 +0200897{
Lukas Fleischerda1b8972015-12-13 01:27:13 +0100898 if (!ctx.qry.page)
899 return NULL;
900
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100901 return strcmp(ctx.qry.page, page) ? NULL : "active";
Lars Hjemlif1355692008-04-12 15:53:31 +0200902}
903
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100904static void cgit_print_path_crumbs(char *path)
Johan Herland24fd7e52010-06-10 01:09:29 +0200905{
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100906 char *old_path = ctx.qry.path;
Johan Herland24fd7e52010-06-10 01:09:29 +0200907 char *p = path, *q, *end = path + strlen(path);
908
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100909 ctx.qry.path = NULL;
910 cgit_self_link("root", NULL, NULL);
911 ctx.qry.path = p = path;
Johan Herland24fd7e52010-06-10 01:09:29 +0200912 while (p < end) {
913 if (!(q = strchr(p, '/')))
914 q = end;
915 *q = '\0';
916 html_txt("/");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100917 cgit_self_link(p, NULL, NULL);
Johan Herland24fd7e52010-06-10 01:09:29 +0200918 if (q < end)
919 *q = '/';
920 p = q + 1;
921 }
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100922 ctx.qry.path = old_path;
Johan Herland24fd7e52010-06-10 01:09:29 +0200923}
924
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100925static void print_header(void)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100926{
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100927 char *logo = NULL, *logo_link = NULL;
928
Lars Hjemlif1355692008-04-12 15:53:31 +0200929 html("<table id='header'>\n");
930 html("<tr>\n");
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400931
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100932 if (ctx.repo && ctx.repo->logo && *ctx.repo->logo)
933 logo = ctx.repo->logo;
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100934 else
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100935 logo = ctx.cfg.logo;
936 if (ctx.repo && ctx.repo->logo_link && *ctx.repo->logo_link)
937 logo_link = ctx.repo->logo_link;
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100938 else
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100939 logo_link = ctx.cfg.logo_link;
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100940 if (logo && *logo) {
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400941 html("<td class='logo' rowspan='2'><a href='");
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100942 if (logo_link && *logo_link)
943 html_attr(logo_link);
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400944 else
945 html_attr(cgit_rooturl());
946 html("'><img src='");
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100947 html_attr(logo);
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400948 html("' alt='cgit logo'/></a></td>\n");
949 }
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200950
Lars Hjemlif1355692008-04-12 15:53:31 +0200951 html("<td class='main'>");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100952 if (ctx.repo) {
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +0100953 cgit_index_link("index", NULL, NULL, NULL, NULL, 0, 1);
Lars Hjemli17890d02008-05-03 12:44:20 +0200954 html(" : ");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100955 cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);
956 if (ctx.env.authenticated) {
Jason A. Donenfeldd6e92002014-01-14 21:49:31 +0100957 html("</td><td class='form'>");
Jason A. Donenfeldc34e2882016-05-12 21:29:40 +0200958 html("<form method='get'>\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100959 cgit_add_hidden_formfields(0, 1, ctx.qry.page);
Jason A. Donenfeldd6e92002014-01-14 21:49:31 +0100960 html("<select name='h' onchange='this.form.submit();'>\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100961 for_each_branch_ref(print_branch_option, ctx.qry.head);
Christian Hessed1ddce92015-03-18 18:08:48 +0100962 if (ctx.repo->enable_remote_branches)
963 for_each_remote_ref(print_branch_option, ctx.qry.head);
Jason A. Donenfeldd6e92002014-01-14 21:49:31 +0100964 html("</select> ");
Juuso Lapinlampi9afda362016-05-11 18:04:18 +0000965 html("<input type='submit' value='switch'/>");
Jason A. Donenfeldd6e92002014-01-14 21:49:31 +0100966 html("</form>");
967 }
Lars Hjemli7c0d2d92008-04-12 19:59:41 +0200968 } else
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100969 html_txt(ctx.cfg.root_title);
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200970 html("</td></tr>\n");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200971
Lars Hjemli2d6ee032008-07-27 12:22:16 +0200972 html("<tr><td class='sub'>");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100973 if (ctx.repo) {
974 html_txt(ctx.repo->desc);
Lars Hjemli2d6ee032008-07-27 12:22:16 +0200975 html("</td><td class='sub right'>");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100976 html_txt(ctx.repo->owner);
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200977 } else {
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100978 if (ctx.cfg.root_desc)
979 html_txt(ctx.cfg.root_desc);
980 else if (ctx.cfg.index_info)
981 html_include(ctx.cfg.index_info);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200982 }
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200983 html("</td></tr></table>\n");
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200984}
985
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100986void cgit_print_pageheader(void)
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200987{
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200988 html("<div id='cgit'>");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100989 if (!ctx.env.authenticated || !ctx.cfg.noheader)
990 print_header();
Lars Hjemlif1355692008-04-12 15:53:31 +0200991
992 html("<table class='tabs'><tr><td>\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100993 if (ctx.env.authenticated && ctx.repo) {
Jason A. Donenfeld3cbbb8e2014-01-17 13:53:37 +0100994 if (ctx.repo->readme.nr)
995 reporevlink("about", "about", NULL,
996 hc("about"), ctx.qry.head, NULL,
997 NULL);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100998 cgit_summary_link("summary", NULL, hc("summary"),
999 ctx.qry.head);
1000 cgit_refs_link("refs", NULL, hc("refs"), ctx.qry.head,
1001 ctx.qry.sha1, NULL);
1002 cgit_log_link("log", NULL, hc("log"), ctx.qry.head,
1003 NULL, ctx.qry.vpath, 0, NULL, NULL,
John Keeping30304d82015-08-12 15:55:28 +01001004 ctx.qry.showmsg, ctx.qry.follow);
Jeff Smith1649afd2017-10-01 23:39:09 -05001005 if (ctx.qry.page && !strcmp(ctx.qry.page, "blame"))
1006 cgit_blame_link("blame", NULL, hc("blame"), ctx.qry.head,
1007 ctx.qry.sha1, ctx.qry.vpath);
1008 else
1009 cgit_tree_link("tree", NULL, hc("tree"), ctx.qry.head,
1010 ctx.qry.sha1, ctx.qry.vpath);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001011 cgit_commit_link("commit", NULL, hc("commit"),
John Keepingeeddb5b2014-10-05 10:59:02 +01001012 ctx.qry.head, ctx.qry.sha1, ctx.qry.vpath);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001013 cgit_diff_link("diff", NULL, hc("diff"), ctx.qry.head,
John Keeping03f537f2014-10-05 10:59:03 +01001014 ctx.qry.sha1, ctx.qry.sha2, ctx.qry.vpath);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001015 if (ctx.repo->max_stats)
1016 cgit_stats_link("stats", NULL, hc("stats"),
1017 ctx.qry.head, ctx.qry.vpath);
Jason A. Donenfeld5f2664f2016-02-22 16:04:15 +01001018 if (ctx.repo->homepage) {
1019 html("<a href='");
1020 html_attr(ctx.repo->homepage);
Jason A. Donenfeldc424b5c2016-02-23 15:35:32 +01001021 html("'>homepage</a>");
Jason A. Donenfeld5f2664f2016-02-22 16:04:15 +01001022 }
Lars Hjemli931fc6d2008-04-13 10:57:11 +02001023 html("</td><td class='form'>");
1024 html("<form class='right' method='get' action='");
Christian Hesse37fce992015-10-09 13:15:46 +02001025 if (ctx.cfg.virtual_root) {
1026 char *fileurl = cgit_fileurl(ctx.qry.repo, "log",
1027 ctx.qry.vpath, NULL);
1028 html_url_path(fileurl);
1029 free(fileurl);
1030 }
Lars Hjemli931fc6d2008-04-13 10:57:11 +02001031 html("'>\n");
Lars Hjemlic3c925f2008-12-07 15:52:35 +01001032 cgit_add_hidden_formfields(1, 0, "log");
Lars Hjemli931fc6d2008-04-13 10:57:11 +02001033 html("<select name='qt'>\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001034 html_option("grep", "log msg", ctx.qry.grep);
1035 html_option("author", "author", ctx.qry.grep);
1036 html_option("committer", "committer", ctx.qry.grep);
1037 html_option("range", "range", ctx.qry.grep);
Lars Hjemli931fc6d2008-04-13 10:57:11 +02001038 html("</select>\n");
Ville Skyttä98abe5b2017-10-14 22:02:16 +03001039 html("<input class='txt' type='search' size='10' name='q' value='");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001040 html_attr(ctx.qry.search);
Lars Hjemli931fc6d2008-04-13 10:57:11 +02001041 html("'/>\n");
1042 html("<input type='submit' value='search'/>\n");
1043 html("</form>\n");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001044 } else if (ctx.env.authenticated) {
Christian Hesse3e244a02015-10-09 13:15:48 +02001045 char *currenturl = cgit_currenturl();
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +01001046 site_link(NULL, "index", NULL, hc("repolist"), NULL, NULL, 0, 1);
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001047 if (ctx.cfg.root_readme)
1048 site_link("about", "about", NULL, hc("about"),
Jason A. Donenfeld2e4a41e2015-03-03 17:23:40 +01001049 NULL, NULL, 0, 1);
Lars Hjemli536b0542008-04-13 11:57:10 +02001050 html("</td><td class='form'>");
1051 html("<form method='get' action='");
Christian Hesse3e244a02015-10-09 13:15:48 +02001052 html_attr(currenturl);
Lars Hjemli536b0542008-04-13 11:57:10 +02001053 html("'>\n");
Ville Skyttä98abe5b2017-10-14 22:02:16 +03001054 html("<input type='search' name='q' size='10' value='");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001055 html_attr(ctx.qry.search);
Lars Hjemli536b0542008-04-13 11:57:10 +02001056 html("'/>\n");
1057 html("<input type='submit' value='search'/>\n");
1058 html("</form>");
Christian Hesse3e244a02015-10-09 13:15:48 +02001059 free(currenturl);
Lars Hjemlie39d7382006-12-28 02:01:49 +01001060 }
Lars Hjemlif1355692008-04-12 15:53:31 +02001061 html("</td></tr></table>\n");
John Keeping1b4ef672017-02-19 12:27:48 +00001062 if (ctx.env.authenticated && ctx.repo && ctx.qry.vpath) {
Johan Herlandc93ef962010-06-10 01:09:28 +02001063 html("<div class='path'>");
1064 html("path: ");
Lukas Fleischerf60ffa12014-01-15 21:53:15 +01001065 cgit_print_path_crumbs(ctx.qry.vpath);
John Keeping30304d82015-08-12 15:55:28 +01001066 if (ctx.cfg.enable_follow_links && !strcmp(ctx.qry.page, "log")) {
1067 html(" (");
1068 ctx.qry.follow = !ctx.qry.follow;
1069 cgit_self_link(ctx.qry.follow ? "follow" : "unfollow",
1070 NULL, NULL);
1071 ctx.qry.follow = !ctx.qry.follow;
1072 html(")");
1073 }
Johan Herlandc93ef962010-06-10 01:09:28 +02001074 html("</div>");
1075 }
Lars Hjemlif1355692008-04-12 15:53:31 +02001076 html("<div class='content'>");
Lars Hjemli5a106eb2006-12-11 16:38:30 +01001077}
Lars Hjemliab2ab952007-02-08 13:53:13 +01001078
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01001079void cgit_print_filemode(unsigned short mode)
1080{
1081 if (S_ISDIR(mode))
1082 html("d");
1083 else if (S_ISLNK(mode))
1084 html("l");
1085 else if (S_ISGITLINK(mode))
1086 html("m");
1087 else
1088 html("-");
1089 html_fileperm(mode >> 6);
1090 html_fileperm(mode >> 3);
1091 html_fileperm(mode);
1092}
1093
Lukas Fleischer9984e7a2016-05-24 18:15:18 +02001094void cgit_compose_snapshot_prefix(struct strbuf *filename, const char *base,
1095 const char *ref)
1096{
Christian Hesse6bef5662016-09-29 22:10:21 +02001097 struct object_id oid;
Lukas Fleischer9984e7a2016-05-24 18:15:18 +02001098
1099 /*
1100 * Prettify snapshot names by stripping leading "v" or "V" if the tag
1101 * name starts with {v,V}[0-9] and the prettify mapping is injective,
1102 * i.e. each stripped tag can be inverted without ambiguities.
1103 */
Christian Hesse6bef5662016-09-29 22:10:21 +02001104 if (get_oid(fmt("refs/tags/%s", ref), &oid) == 0 &&
Lukas Fleischer9984e7a2016-05-24 18:15:18 +02001105 (ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1]) &&
Christian Hesse6bef5662016-09-29 22:10:21 +02001106 ((get_oid(fmt("refs/tags/%s", ref + 1), &oid) == 0) +
1107 (get_oid(fmt("refs/tags/v%s", ref + 1), &oid) == 0) +
1108 (get_oid(fmt("refs/tags/V%s", ref + 1), &oid) == 0) == 1))
Lukas Fleischer9984e7a2016-05-24 18:15:18 +02001109 ref++;
1110
1111 strbuf_addf(filename, "%s-%s", base, ref);
1112}
1113
John Keepingbd1b2812018-03-31 14:05:02 +01001114void cgit_print_snapshot_links(const struct cgit_repo *repo, const char *head,
1115 const char *hex)
Lars Hjemlif34478c2008-03-24 16:00:27 +01001116{
1117 const struct cgit_snapshot_format* f;
John Keepingfb3655d2013-04-06 10:28:57 +01001118 struct strbuf filename = STRBUF_INIT;
1119 size_t prefixlen;
Lars Hjemlif34478c2008-03-24 16:00:27 +01001120
John Keepingc1572bb2018-03-31 14:20:01 +01001121 cgit_compose_snapshot_prefix(&filename, cgit_snapshot_prefix(repo), hex);
John Keepingfb3655d2013-04-06 10:28:57 +01001122 prefixlen = filename.len;
Lars Hjemlif34478c2008-03-24 16:00:27 +01001123 for (f = cgit_snapshot_formats; f->suffix; f++) {
John Keepingbd1b2812018-03-31 14:05:02 +01001124 if (!(repo->snapshots & f->bit))
Lars Hjemlif34478c2008-03-24 16:00:27 +01001125 continue;
John Keepingfb3655d2013-04-06 10:28:57 +01001126 strbuf_setlen(&filename, prefixlen);
1127 strbuf_addstr(&filename, f->suffix);
1128 cgit_snapshot_link(filename.buf, NULL, NULL, NULL, NULL,
1129 filename.buf);
Lars Hjemlif34478c2008-03-24 16:00:27 +01001130 html("<br/>");
1131 }
John Keepingfb3655d2013-04-06 10:28:57 +01001132 strbuf_release(&filename);
Lars Hjemlif34478c2008-03-24 16:00:27 +01001133}
Jeff Smith9337c7e2017-10-01 23:39:06 -05001134
1135void cgit_set_title_from_path(const char *path)
1136{
1137 size_t path_len, path_index, path_last_end;
1138 char *new_title;
1139
1140 if (!path)
1141 return;
1142
1143 path_len = strlen(path);
1144 new_title = xmalloc(path_len + 3 + strlen(ctx.page.title) + 1);
1145 new_title[0] = '\0';
1146
1147 for (path_index = path_len, path_last_end = path_len; path_index-- > 0;) {
1148 if (path[path_index] == '/') {
1149 if (path_index == path_len - 1) {
1150 path_last_end = path_index - 1;
1151 continue;
1152 }
1153 strncat(new_title, &path[path_index + 1], path_last_end - path_index - 1);
1154 strcat(new_title, "\\");
1155 path_last_end = path_index;
1156 }
1157 }
1158 if (path_last_end)
1159 strncat(new_title, path, path_last_end);
1160
1161 strcat(new_title, " - ");
1162 strcat(new_title, ctx.page.title);
1163 ctx.page.title = new_title;
1164}