blob: d4fb3d9e869139296f5e776bee1466d63dfc5158 [file] [log] [blame]
Lars Hjemli5a106eb2006-12-11 16:38:30 +01001/* ui-shared.c: common web output functions
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
Lars Hjemlif1355692008-04-12 15:53:31 +020010#include "cmd.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemli5a106eb2006-12-11 16:38:30 +010012
13const char cgit_doctype[] =
14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16
17static char *http_date(time_t t)
18{
Lars Hjemli6fb7d092007-05-15 00:22:03 +020019 static char day[][4] =
Lars Hjemli5a106eb2006-12-11 16:38:30 +010020 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
Lars Hjemli6fb7d092007-05-15 00:22:03 +020021 static char month[][4] =
Lars Hjemli5a106eb2006-12-11 16:38:30 +010022 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
Danijel TaĊĦove34a3b52009-11-02 22:10:04 +010023 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
Lars Hjemli5a106eb2006-12-11 16:38:30 +010024 struct tm *tm = gmtime(&t);
25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
Lukas Fleischer53bc7472013-03-03 16:04:29 +010026 tm->tm_mday, month[tm->tm_mon], 1900 + tm->tm_year,
Lars Hjemli5a106eb2006-12-11 16:38:30 +010027 tm->tm_hour, tm->tm_min, tm->tm_sec);
28}
29
Johan Herlandc3f23d42010-06-10 01:09:24 +020030void cgit_print_error(const char *msg)
Lars Hjemli5a106eb2006-12-11 16:38:30 +010031{
32 html("<div class='error'>");
33 html_txt(msg);
34 html("</div>\n");
35}
Lars Hjemli74620f12006-12-11 16:48:03 +010036
Diego Ongaro87a89ae2009-06-10 18:09:55 -050037char *cgit_httpscheme()
38{
Lars Hjemli60a26272009-08-10 08:21:09 +020039 if (ctx.env.https && !strcmp(ctx.env.https, "on"))
Diego Ongaro87a89ae2009-06-10 18:09:55 -050040 return "https://";
41 else
42 return "http://";
43}
44
Lars Hjemlib2a3d312008-05-21 08:17:54 +020045char *cgit_hosturl()
46{
Lars Hjemli60a26272009-08-10 08:21:09 +020047 if (ctx.env.http_host)
48 return ctx.env.http_host;
49 if (!ctx.env.server_name)
50 return NULL;
51 if (!ctx.env.server_port || atoi(ctx.env.server_port) == 80)
52 return ctx.env.server_name;
53 return xstrdup(fmt("%s:%s", ctx.env.server_name, ctx.env.server_port));
Lars Hjemlib2a3d312008-05-21 08:17:54 +020054}
55
Lars Hjemli66cacd02007-02-17 13:46:18 +010056char *cgit_rooturl()
57{
Lars Hjemlib228d4f2008-02-16 13:07:13 +010058 if (ctx.cfg.virtual_root)
59 return fmt("%s/", ctx.cfg.virtual_root);
Lars Hjemli66cacd02007-02-17 13:46:18 +010060 else
Lars Hjemlib228d4f2008-02-16 13:07:13 +010061 return ctx.cfg.script_name;
Lars Hjemli66cacd02007-02-17 13:46:18 +010062}
63
Lars Hjemli74620f12006-12-11 16:48:03 +010064char *cgit_repourl(const char *reponame)
65{
Lars Hjemlib228d4f2008-02-16 13:07:13 +010066 if (ctx.cfg.virtual_root) {
67 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
Lars Hjemli74620f12006-12-11 16:48:03 +010068 } else {
69 return fmt("?r=%s", reponame);
70 }
71}
72
Michael Krelin0df096f2007-07-21 13:13:40 +020073char *cgit_fileurl(const char *reponame, const char *pagename,
74 const char *filename, const char *query)
Lars Hjemli74620f12006-12-11 16:48:03 +010075{
Lars Hjemli68cf9b42007-11-03 11:15:56 +010076 char *tmp;
77 char *delim;
78
Lars Hjemlib228d4f2008-02-16 13:07:13 +010079 if (ctx.cfg.virtual_root) {
80 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
Lars Hjemli68cf9b42007-11-03 11:15:56 +010081 pagename, (filename ? filename:""));
82 delim = "?";
Lars Hjemli74620f12006-12-11 16:48:03 +010083 } else {
Lars Hjemli68cf9b42007-11-03 11:15:56 +010084 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
85 (filename ? filename : ""));
William Bellc366bd62012-10-09 20:45:58 +020086 delim = "&amp;";
Lars Hjemli74620f12006-12-11 16:48:03 +010087 }
Lars Hjemli68cf9b42007-11-03 11:15:56 +010088 if (query)
89 tmp = fmt("%s%s%s", tmp, delim, query);
90 return tmp;
Lars Hjemli74620f12006-12-11 16:48:03 +010091}
92
Michael Krelin0df096f2007-07-21 13:13:40 +020093char *cgit_pageurl(const char *reponame, const char *pagename,
94 const char *query)
95{
Lukas Fleischer53bc7472013-03-03 16:04:29 +010096 return cgit_fileurl(reponame, pagename, 0, query);
Michael Krelin0df096f2007-07-21 13:13:40 +020097}
98
Michael Krelin1cb8bed2007-07-21 15:24:07 +020099const char *cgit_repobasename(const char *reponame)
100{
101 /* I assume we don't need to store more than one repo basename */
102 static char rvbuf[1024];
103 int p;
104 const char *rv;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100105 strncpy(rvbuf, reponame, sizeof(rvbuf));
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500106 if (rvbuf[sizeof(rvbuf)-1])
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200107 die("cgit_repobasename: truncated repository name '%s'", reponame);
108 p = strlen(rvbuf)-1;
109 /* strip trailing slashes */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500110 while (p && rvbuf[p] == '/') rvbuf[p--] = 0;
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200111 /* strip trailing .git */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500112 if (p >= 3 && !strncmp(&rvbuf[p-3], ".git", 4)) {
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200113 p -= 3; rvbuf[p--] = 0;
114 }
115 /* strip more trailing slashes if any */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500116 while ( p && rvbuf[p] == '/') rvbuf[p--] = 0;
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200117 /* find last slash in the remaining string */
118 rv = strrchr(rvbuf,'/');
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500119 if (rv)
Michael Krelin1cb8bed2007-07-21 15:24:07 +0200120 return ++rv;
121 return rvbuf;
122}
Michael Krelin0df096f2007-07-21 13:13:40 +0200123
Tobias Grimm7530d942011-07-31 02:44:05 +0200124static void site_url(const char *page, const char *search, const char *sort, int ofs)
Lars Hjemli71adba12008-04-29 01:09:41 +0200125{
126 char *delim = "?";
127
128 if (ctx.cfg.virtual_root) {
129 html_attr(ctx.cfg.virtual_root);
130 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
131 html("/");
132 } else
133 html(ctx.cfg.script_name);
134
135 if (page) {
136 htmlf("?p=%s", page);
William Bellc366bd62012-10-09 20:45:58 +0200137 delim = "&amp;";
Lars Hjemli71adba12008-04-29 01:09:41 +0200138 }
139 if (search) {
140 html(delim);
141 html("q=");
142 html_attr(search);
William Bellc366bd62012-10-09 20:45:58 +0200143 delim = "&amp;";
Lars Hjemli141f1c32008-05-03 10:37:02 +0200144 }
Tobias Grimm7530d942011-07-31 02:44:05 +0200145 if (sort) {
146 html(delim);
147 html("s=");
148 html_attr(sort);
William Bellc366bd62012-10-09 20:45:58 +0200149 delim = "&amp;";
Tobias Grimm7530d942011-07-31 02:44:05 +0200150 }
Lars Hjemli141f1c32008-05-03 10:37:02 +0200151 if (ofs) {
152 html(delim);
153 htmlf("ofs=%d", ofs);
Lars Hjemli71adba12008-04-29 01:09:41 +0200154 }
155}
156
Johan Herlandc3f23d42010-06-10 01:09:24 +0200157static void site_link(const char *page, const char *name, const char *title,
Tobias Grimm7530d942011-07-31 02:44:05 +0200158 const char *class, const char *search, const char *sort, int ofs)
Lars Hjemli71adba12008-04-29 01:09:41 +0200159{
160 html("<a");
161 if (title) {
162 html(" title='");
163 html_attr(title);
164 html("'");
165 }
166 if (class) {
167 html(" class='");
168 html_attr(class);
169 html("'");
170 }
171 html(" href='");
Tobias Grimm7530d942011-07-31 02:44:05 +0200172 site_url(page, search, sort, ofs);
Lars Hjemli71adba12008-04-29 01:09:41 +0200173 html("'>");
174 html_txt(name);
175 html("</a>");
176}
177
Johan Herlandc3f23d42010-06-10 01:09:24 +0200178void cgit_index_link(const char *name, const char *title, const char *class,
Tobias Grimm7530d942011-07-31 02:44:05 +0200179 const char *pattern, const char *sort, int ofs)
Lars Hjemli141f1c32008-05-03 10:37:02 +0200180{
Tobias Grimm7530d942011-07-31 02:44:05 +0200181 site_link(NULL, name, title, class, pattern, sort, ofs);
Lars Hjemli141f1c32008-05-03 10:37:02 +0200182}
183
Johan Herlandc3f23d42010-06-10 01:09:24 +0200184static char *repolink(const char *title, const char *class, const char *page,
185 const char *head, const char *path)
Lars Hjemli44947bf2007-06-17 01:23:08 +0200186{
187 char *delim = "?";
188
189 html("<a");
190 if (title) {
191 html(" title='");
192 html_attr(title);
193 html("'");
194 }
195 if (class) {
196 html(" class='");
197 html_attr(class);
198 html("'");
199 }
200 html(" href='");
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100201 if (ctx.cfg.virtual_root) {
Lars Hjemli44b208a2008-10-05 16:54:44 +0200202 html_url_path(ctx.cfg.virtual_root);
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100203 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
Lars Hjemli44947bf2007-06-17 01:23:08 +0200204 html("/");
Lars Hjemli44b208a2008-10-05 16:54:44 +0200205 html_url_path(ctx.repo->url);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100206 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
Lars Hjemli44947bf2007-06-17 01:23:08 +0200207 html("/");
Lars Hjemlib8be0282007-06-18 00:18:42 +0200208 if (page) {
Lars Hjemli44b208a2008-10-05 16:54:44 +0200209 html_url_path(page);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200210 html("/");
211 if (path)
Lars Hjemli44b208a2008-10-05 16:54:44 +0200212 html_url_path(path);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200213 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200214 } else {
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100215 html(ctx.cfg.script_name);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200216 html("?url=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200217 html_url_arg(ctx.repo->url);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100218 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
Lars Hjemli44947bf2007-06-17 01:23:08 +0200219 html("/");
Lars Hjemlib8be0282007-06-18 00:18:42 +0200220 if (page) {
Lars Hjemlib5751152008-10-05 12:52:25 +0200221 html_url_arg(page);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200222 html("/");
223 if (path)
Lars Hjemlib5751152008-10-05 12:52:25 +0200224 html_url_arg(path);
Lars Hjemlib8be0282007-06-18 00:18:42 +0200225 }
Lars Hjemli44947bf2007-06-17 01:23:08 +0200226 delim = "&amp;";
227 }
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100228 if (head && strcmp(head, ctx.repo->defbranch)) {
Lars Hjemli44947bf2007-06-17 01:23:08 +0200229 html(delim);
230 html("h=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200231 html_url_arg(head);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200232 delim = "&amp;";
233 }
234 return fmt("%s", delim);
235}
236
Johan Herlandc3f23d42010-06-10 01:09:24 +0200237static void reporevlink(const char *page, const char *name, const char *title,
238 const char *class, const char *head, const char *rev,
239 const char *path)
Lars Hjemli44947bf2007-06-17 01:23:08 +0200240{
241 char *delim;
242
Lars Hjemli48c487d2007-06-17 13:57:51 +0200243 delim = repolink(title, class, page, head, path);
Florian Pritz8d946072010-02-01 17:55:37 +0100244 if (rev && ctx.qry.head != NULL && strcmp(rev, ctx.qry.head)) {
Lars Hjemli44947bf2007-06-17 01:23:08 +0200245 html(delim);
246 html("id=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200247 html_url_arg(rev);
Lars Hjemli44947bf2007-06-17 01:23:08 +0200248 }
249 html("'>");
250 html_txt(name);
251 html("</a>");
252}
Lars Hjemli148fb962006-12-16 00:33:28 +0100253
Johan Herlandc3f23d42010-06-10 01:09:24 +0200254void cgit_summary_link(const char *name, const char *title, const char *class,
255 const char *head)
Lars Hjemlie9d3bd52008-10-05 16:55:50 +0200256{
257 reporevlink(NULL, name, title, class, head, NULL, NULL);
258}
259
Johan Herlandc3f23d42010-06-10 01:09:24 +0200260void cgit_tag_link(const char *name, const char *title, const char *class,
261 const char *head, const char *rev)
Lars Hjemlicf61ad42008-10-05 21:18:45 +0200262{
263 reporevlink("tag", name, title, class, head, rev, NULL);
264}
265
Johan Herlandc3f23d42010-06-10 01:09:24 +0200266void cgit_tree_link(const char *name, const char *title, const char *class,
267 const char *head, const char *rev, const char *path)
Lars Hjemli48c487d2007-06-17 13:57:51 +0200268{
269 reporevlink("tree", name, title, class, head, rev, path);
270}
271
Johan Herlandc3f23d42010-06-10 01:09:24 +0200272void cgit_plain_link(const char *name, const char *title, const char *class,
273 const char *head, const char *rev, const char *path)
Lars Hjemli65b7b872008-08-06 11:07:13 +0200274{
275 reporevlink("plain", name, title, class, head, rev, path);
276}
277
Johan Herlandc3f23d42010-06-10 01:09:24 +0200278void cgit_log_link(const char *name, const char *title, const char *class,
279 const char *head, const char *rev, const char *path,
280 int ofs, const char *grep, const char *pattern, int showmsg)
Lars Hjemli48c487d2007-06-17 13:57:51 +0200281{
Lars Hjemli103940f2007-06-29 20:27:41 +0200282 char *delim;
283
284 delim = repolink(title, class, "log", head, path);
Eric Wong21418ec2012-01-04 09:01:51 +0000285 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
Lars Hjemli103940f2007-06-29 20:27:41 +0200286 html(delim);
287 html("id=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200288 html_url_arg(rev);
William Bellc366bd62012-10-09 20:45:58 +0200289 delim = "&amp;";
Lars Hjemli103940f2007-06-29 20:27:41 +0200290 }
Lars Hjemli51140312007-11-03 10:42:37 +0100291 if (grep && pattern) {
292 html(delim);
293 html("qt=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200294 html_url_arg(grep);
William Bellc366bd62012-10-09 20:45:58 +0200295 delim = "&amp;";
Lars Hjemli51140312007-11-03 10:42:37 +0100296 html(delim);
297 html("q=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200298 html_url_arg(pattern);
Lars Hjemli51140312007-11-03 10:42:37 +0100299 }
Lars Hjemli103940f2007-06-29 20:27:41 +0200300 if (ofs > 0) {
301 html(delim);
302 html("ofs=");
303 htmlf("%d", ofs);
William Bellc366bd62012-10-09 20:45:58 +0200304 delim = "&amp;";
Lars Hjemli0274b572008-11-29 18:39:41 +0100305 }
306 if (showmsg) {
307 html(delim);
308 html("showmsg=1");
Lars Hjemli103940f2007-06-29 20:27:41 +0200309 }
310 html("'>");
311 html_txt(name);
312 html("</a>");
Lars Hjemli48c487d2007-06-17 13:57:51 +0200313}
314
Johan Herlandc3f23d42010-06-10 01:09:24 +0200315void cgit_commit_link(char *name, const char *title, const char *class,
Johan Herland685872b2010-06-10 01:09:35 +0200316 const char *head, const char *rev, const char *path,
317 int toggle_ssdiff)
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200318{
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100319 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
320 name[ctx.cfg.max_msg_len] = '\0';
321 name[ctx.cfg.max_msg_len - 1] = '.';
322 name[ctx.cfg.max_msg_len - 2] = '.';
323 name[ctx.cfg.max_msg_len - 3] = '.';
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200324 }
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200325
326 char *delim;
327
Johan Herland685872b2010-06-10 01:09:35 +0200328 delim = repolink(title, class, "commit", head, path);
Eric Wong21418ec2012-01-04 09:01:51 +0000329 if (rev && ctx.qry.head && strcmp(rev, ctx.qry.head)) {
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200330 html(delim);
331 html("id=");
332 html_url_arg(rev);
333 delim = "&amp;";
334 }
335 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
336 html(delim);
337 html("ss=1");
Johan Herlandd20313e2010-06-10 20:15:51 +0200338 delim = "&amp;";
339 }
340 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
341 html(delim);
342 html("context=");
343 htmlf("%d", ctx.qry.context);
344 delim = "&amp;";
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200345 }
Johan Herland72ef9132010-06-24 17:53:20 +0200346 if (ctx.qry.ignorews) {
347 html(delim);
348 html("ignorews=1");
349 delim = "&amp;";
350 }
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200351 html("'>");
Christian Frankefe1bb0e2012-10-28 18:36:08 +0100352 if (name[0] != '\0')
353 html_txt(name);
354 else
355 html_txt("(no commit message)");
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200356 html("</a>");
Lars Hjemli42a7eb92007-06-17 14:53:02 +0200357}
358
Johan Herlandc3f23d42010-06-10 01:09:24 +0200359void cgit_refs_link(const char *name, const char *title, const char *class,
360 const char *head, const char *rev, const char *path)
Lars Hjemliac1f4932007-10-27 10:47:44 +0200361{
362 reporevlink("refs", name, title, class, head, rev, path);
363}
364
Johan Herlandc3f23d42010-06-10 01:09:24 +0200365void cgit_snapshot_link(const char *name, const char *title, const char *class,
366 const char *head, const char *rev,
367 const char *archivename)
Lars Hjemlieb453422007-07-23 00:11:15 +0200368{
369 reporevlink("snapshot", name, title, class, head, rev, archivename);
370}
371
Johan Herlandc3f23d42010-06-10 01:09:24 +0200372void cgit_diff_link(const char *name, const char *title, const char *class,
373 const char *head, const char *new_rev, const char *old_rev,
374 const char *path, int toggle_ssdiff)
Lars Hjemli4a0be582007-06-17 18:12:03 +0200375{
376 char *delim;
377
378 delim = repolink(title, class, "diff", head, path);
Florian Pritz8d946072010-02-01 17:55:37 +0100379 if (new_rev && ctx.qry.head != NULL && strcmp(new_rev, ctx.qry.head)) {
Lars Hjemli4a0be582007-06-17 18:12:03 +0200380 html(delim);
381 html("id=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200382 html_url_arg(new_rev);
Lars Hjemli4a0be582007-06-17 18:12:03 +0200383 delim = "&amp;";
384 }
385 if (old_rev) {
386 html(delim);
387 html("id2=");
Lars Hjemlib5751152008-10-05 12:52:25 +0200388 html_url_arg(old_rev);
Ragnar Ouchterlonyc358aa32009-09-14 20:19:02 +0200389 delim = "&amp;";
390 }
391 if ((ctx.qry.ssdiff && !toggle_ssdiff) || (!ctx.qry.ssdiff && toggle_ssdiff)) {
392 html(delim);
393 html("ss=1");
Johan Herlandd20313e2010-06-10 20:15:51 +0200394 delim = "&amp;";
395 }
396 if (ctx.qry.context > 0 && ctx.qry.context != 3) {
397 html(delim);
398 html("context=");
399 htmlf("%d", ctx.qry.context);
400 delim = "&amp;";
Lars Hjemli4a0be582007-06-17 18:12:03 +0200401 }
Johan Herland72ef9132010-06-24 17:53:20 +0200402 if (ctx.qry.ignorews) {
403 html(delim);
404 html("ignorews=1");
405 delim = "&amp;";
406 }
Lars Hjemli4a0be582007-06-17 18:12:03 +0200407 html("'>");
408 html_txt(name);
409 html("</a>");
410}
411
Johan Herlandc3f23d42010-06-10 01:09:24 +0200412void cgit_patch_link(const char *name, const char *title, const char *class,
Johan Herlandeac1b672010-06-10 01:09:33 +0200413 const char *head, const char *rev, const char *path)
Lars Hjemli620bb3e2007-12-10 21:47:29 +0100414{
Johan Herlandeac1b672010-06-10 01:09:33 +0200415 reporevlink("patch", name, title, class, head, rev, path);
Lars Hjemli620bb3e2007-12-10 21:47:29 +0100416}
417
Johan Herlandc3f23d42010-06-10 01:09:24 +0200418void cgit_stats_link(const char *name, const char *title, const char *class,
419 const char *head, const char *path)
Lars Hjemlieaf2d252008-12-07 13:34:16 +0100420{
421 reporevlink("stats", name, title, class, head, NULL, path);
422}
423
Lukas Fleischerbafab422013-03-04 08:52:33 +0100424static void cgit_self_link(char *name, const char *title, const char *class,
425 struct cgit_context *ctx)
Johan Herland24fd7e52010-06-10 01:09:29 +0200426{
427 if (!strcmp(ctx->qry.page, "repolist"))
John Keeping6d8a7892013-03-06 20:51:54 +0000428 cgit_index_link(name, title, class, ctx->qry.search, ctx->qry.sort,
429 ctx->qry.ofs);
Johan Herland24fd7e52010-06-10 01:09:29 +0200430 else if (!strcmp(ctx->qry.page, "summary"))
John Keeping6d8a7892013-03-06 20:51:54 +0000431 cgit_summary_link(name, title, class, ctx->qry.head);
Johan Herland24fd7e52010-06-10 01:09:29 +0200432 else if (!strcmp(ctx->qry.page, "tag"))
John Keeping6d8a7892013-03-06 20:51:54 +0000433 cgit_tag_link(name, title, class, ctx->qry.head,
434 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL);
Johan Herland24fd7e52010-06-10 01:09:29 +0200435 else if (!strcmp(ctx->qry.page, "tree"))
John Keeping6d8a7892013-03-06 20:51:54 +0000436 cgit_tree_link(name, title, class, ctx->qry.head,
437 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
438 ctx->qry.path);
Johan Herland24fd7e52010-06-10 01:09:29 +0200439 else if (!strcmp(ctx->qry.page, "plain"))
John Keeping6d8a7892013-03-06 20:51:54 +0000440 cgit_plain_link(name, title, class, ctx->qry.head,
441 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
442 ctx->qry.path);
Johan Herland24fd7e52010-06-10 01:09:29 +0200443 else if (!strcmp(ctx->qry.page, "log"))
John Keeping6d8a7892013-03-06 20:51:54 +0000444 cgit_log_link(name, title, class, ctx->qry.head,
445 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
446 ctx->qry.path, ctx->qry.ofs,
447 ctx->qry.grep, ctx->qry.search,
448 ctx->qry.showmsg);
Johan Herland24fd7e52010-06-10 01:09:29 +0200449 else if (!strcmp(ctx->qry.page, "commit"))
John Keeping6d8a7892013-03-06 20:51:54 +0000450 cgit_commit_link(name, title, class, ctx->qry.head,
451 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
452 ctx->qry.path, 0);
Johan Herland24fd7e52010-06-10 01:09:29 +0200453 else if (!strcmp(ctx->qry.page, "patch"))
John Keeping6d8a7892013-03-06 20:51:54 +0000454 cgit_patch_link(name, title, class, ctx->qry.head,
455 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
456 ctx->qry.path);
Johan Herland24fd7e52010-06-10 01:09:29 +0200457 else if (!strcmp(ctx->qry.page, "refs"))
John Keeping6d8a7892013-03-06 20:51:54 +0000458 cgit_refs_link(name, title, class, ctx->qry.head,
459 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
460 ctx->qry.path);
Johan Herland24fd7e52010-06-10 01:09:29 +0200461 else if (!strcmp(ctx->qry.page, "snapshot"))
John Keeping6d8a7892013-03-06 20:51:54 +0000462 cgit_snapshot_link(name, title, class, ctx->qry.head,
463 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
464 ctx->qry.path);
Johan Herland24fd7e52010-06-10 01:09:29 +0200465 else if (!strcmp(ctx->qry.page, "diff"))
John Keeping6d8a7892013-03-06 20:51:54 +0000466 cgit_diff_link(name, title, class, ctx->qry.head,
467 ctx->qry.sha1, ctx->qry.sha2,
468 ctx->qry.path, 0);
Johan Herland24fd7e52010-06-10 01:09:29 +0200469 else if (!strcmp(ctx->qry.page, "stats"))
John Keeping6d8a7892013-03-06 20:51:54 +0000470 cgit_stats_link(name, title, class, ctx->qry.head,
471 ctx->qry.path);
472 else {
473 /* Don't known how to make link for this page */
474 repolink(title, class, ctx->qry.page, ctx->qry.head, ctx->qry.path);
475 html("><!-- cgit_self_link() doesn't know how to make link for page '");
476 html_txt(ctx->qry.page);
477 html("' -->");
478 html_txt(name);
479 html("</a>");
480 }
Johan Herland24fd7e52010-06-10 01:09:29 +0200481}
482
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200483void cgit_object_link(struct object *obj)
484{
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100485 char *page, *shortrev, *fullrev, *name;
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200486
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100487 fullrev = sha1_to_hex(obj->sha1);
488 shortrev = xstrdup(fullrev);
489 shortrev[10] = '\0';
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200490 if (obj->type == OBJ_COMMIT) {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100491 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
Johan Herland685872b2010-06-10 01:09:35 +0200492 ctx.qry.head, fullrev, NULL, 0);
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200493 return;
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200494 } else if (obj->type == OBJ_TREE)
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200495 page = "tree";
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200496 else if (obj->type == OBJ_TAG)
Lars Hjemlifc5880f2007-10-28 15:40:47 +0100497 page = "tag";
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200498 else
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200499 page = "blob";
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100500 name = fmt("%s %s...", typename(obj->type), shortrev);
501 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200502}
503
Lukas Fleischerbafab422013-03-04 08:52:33 +0100504static struct string_list_item *lookup_path(struct string_list *list,
505 const char *path)
Lars Hjemli6857bec2011-06-15 10:04:13 +0200506{
507 struct string_list_item *item;
508
509 while (path && path[0]) {
510 if ((item = string_list_lookup(list, path)))
511 return item;
512 if (!(path = strchr(path, '/')))
513 break;
514 path++;
515 }
516 return NULL;
517}
518
519void cgit_submodule_link(const char *class, char *path, const char *rev)
520{
521 struct string_list *list;
522 struct string_list_item *item;
523 char tail, *dir;
524 size_t len;
525
Jason A. Donenfeld40e1d9b2013-03-20 20:43:13 +0100526 len = 0;
Lars Hjemli6857bec2011-06-15 10:04:13 +0200527 tail = 0;
528 list = &ctx.repo->submodules;
529 item = lookup_path(list, path);
530 if (!item) {
531 len = strlen(path);
532 tail = path[len - 1];
533 if (tail == '/') {
534 path[len - 1] = 0;
535 item = lookup_path(list, path);
536 }
537 }
538 html("<a ");
539 if (class)
540 htmlf("class='%s' ", class);
541 html("href='");
542 if (item) {
543 html_attr(fmt(item->util, rev));
544 } else if (ctx.repo->module_link) {
545 dir = strrchr(path, '/');
546 if (dir)
547 dir++;
548 else
549 dir = path;
550 html_attr(fmt(ctx.repo->module_link, dir, rev));
551 } else {
552 html("#");
553 }
554 html("'>");
555 html_txt(path);
556 html("</a>");
stfnc0a92e82013-02-27 19:47:17 +0100557 html_txt(fmt(" @ %.7s", rev));
Lars Hjemli6857bec2011-06-15 10:04:13 +0200558 if (item && tail)
559 path[len - 1] = tail;
560}
561
Johan Herlandc3f23d42010-06-10 01:09:24 +0200562void cgit_print_date(time_t secs, const char *format, int local_time)
Lars Hjemli148fb962006-12-16 00:33:28 +0100563{
Lars Hjemli5db39172007-05-22 23:08:46 +0200564 char buf[64];
Lars Hjemli148fb962006-12-16 00:33:28 +0100565 struct tm *time;
566
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +0100567 if (!secs)
568 return;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500569 if (local_time)
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200570 time = localtime(&secs);
571 else
572 time = gmtime(&secs);
Lars Hjemli5db39172007-05-22 23:08:46 +0200573 strftime(buf, sizeof(buf)-1, format, time);
Lars Hjemli148fb962006-12-16 00:33:28 +0100574 html_txt(buf);
Lars Hjemli148fb962006-12-16 00:33:28 +0100575}
576
Johan Herlandc3f23d42010-06-10 01:09:24 +0200577void cgit_print_age(time_t t, time_t max_relative, const char *format)
Lars Hjemli5db39172007-05-22 23:08:46 +0200578{
579 time_t now, secs;
580
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +0100581 if (!t)
582 return;
Lars Hjemli5db39172007-05-22 23:08:46 +0200583 time(&now);
584 secs = now - t;
585
586 if (secs > max_relative && max_relative >= 0) {
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200587 cgit_print_date(t, format, ctx.cfg.local_time);
Lars Hjemli5db39172007-05-22 23:08:46 +0200588 return;
589 }
590
591 if (secs < TM_HOUR * 2) {
592 htmlf("<span class='age-mins'>%.0f min.</span>",
593 secs * 1.0 / TM_MIN);
594 return;
595 }
596 if (secs < TM_DAY * 2) {
597 htmlf("<span class='age-hours'>%.0f hours</span>",
598 secs * 1.0 / TM_HOUR);
599 return;
600 }
601 if (secs < TM_WEEK * 2) {
602 htmlf("<span class='age-days'>%.0f days</span>",
603 secs * 1.0 / TM_DAY);
604 return;
605 }
606 if (secs < TM_MONTH * 2) {
607 htmlf("<span class='age-weeks'>%.0f weeks</span>",
608 secs * 1.0 / TM_WEEK);
609 return;
610 }
611 if (secs < TM_YEAR * 2) {
612 htmlf("<span class='age-months'>%.0f months</span>",
613 secs * 1.0 / TM_MONTH);
614 return;
615 }
616 htmlf("<span class='age-years'>%.0f years</span>",
617 secs * 1.0 / TM_YEAR);
618}
619
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100620void cgit_print_http_headers(struct cgit_context *ctx)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100621{
Lars Hjemli8b2252b2009-08-10 09:20:17 +0200622 if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1"))
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100623 return;
624
Lars Hjemlie429fb02009-06-07 20:43:08 +0200625 if (ctx->page.status)
626 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100627 if (ctx->page.mimetype && ctx->page.charset)
628 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
629 ctx->page.charset);
630 else if (ctx->page.mimetype)
631 htmlf("Content-Type: %s\n", ctx->page.mimetype);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200632 if (ctx->page.size)
Bernhard Reutner-Fischer6bf26582010-12-23 12:47:53 +0100633 htmlf("Content-Length: %zd\n", ctx->page.size);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100634 if (ctx->page.filename)
635 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
636 ctx->page.filename);
637 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
638 htmlf("Expires: %s\n", http_date(ctx->page.expires));
Lars Hjemli488a2142009-02-19 22:38:36 +0100639 if (ctx->page.etag)
640 htmlf("ETag: \"%s\"\n", ctx->page.etag);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100641 html("\n");
Lars Hjemli60a26272009-08-10 08:21:09 +0200642 if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD"))
Lars Hjemli3ff58dd2009-02-19 23:24:15 +0100643 exit(0);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100644}
645
646void cgit_print_docstart(struct cgit_context *ctx)
647{
Lars Hjemli80550bb2009-08-11 10:12:35 +0200648 if (ctx->cfg.embedded) {
649 if (ctx->cfg.header)
650 html_include(ctx->cfg.header);
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100651 return;
Lars Hjemli80550bb2009-08-11 10:12:35 +0200652 }
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100653
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200654 char *host = cgit_hosturl();
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100655 html(cgit_doctype);
Lars Hjemli29154832007-11-11 13:04:28 +0100656 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100657 html("<head>\n");
658 html("<title>");
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100659 html_txt(ctx->page.title);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100660 html("</title>\n");
Lars Hjemlif6925032007-06-18 09:42:10 +0200661 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100662 if (ctx->cfg.robots && *ctx->cfg.robots)
663 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100664 html("<link rel='stylesheet' type='text/css' href='");
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100665 html_attr(ctx->cfg.css);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100666 html("'/>\n");
Lars Hjemli502865a2008-07-19 20:40:30 +0200667 if (ctx->cfg.favicon) {
668 html("<link rel='shortcut icon' href='");
669 html_attr(ctx->cfg.favicon);
670 html("'/>\n");
671 }
John Keeping94b7c762011-11-24 11:54:47 +0000672 if (host && ctx->repo && ctx->qry.head) {
Diego Ongaro694dd432009-06-10 18:18:34 -0500673 html("<link rel='alternate' title='Atom feed' href='");
674 html(cgit_httpscheme());
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200675 html_attr(cgit_hosturl());
Johan Herlandc8e32952010-06-10 01:09:27 +0200676 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200677 fmt("h=%s", ctx->qry.head)));
Mark Lodatob5a3a202009-03-15 00:11:54 -0400678 html("' type='application/atom+xml'/>\n");
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200679 }
Mark Lodatob5a3a202009-03-15 00:11:54 -0400680 if (ctx->cfg.head_include)
681 html_include(ctx->cfg.head_include);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100682 html("</head>\n");
683 html("<body>\n");
Lars Hjemlib1159552009-01-29 21:27:39 +0100684 if (ctx->cfg.header)
685 html_include(ctx->cfg.header);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100686}
687
688void cgit_print_docend()
689{
Lars Hjemli80550bb2009-08-11 10:12:35 +0200690 html("</div> <!-- class=content -->\n");
691 if (ctx.cfg.embedded) {
692 html("</div> <!-- id=cgit -->\n");
693 if (ctx.cfg.footer)
694 html_include(ctx.cfg.footer);
695 return;
696 }
Lars Hjemlide5e9282008-06-26 13:53:30 +0200697 if (ctx.cfg.footer)
698 html_include(ctx.cfg.footer);
699 else {
Lars Hjemlib9aabf02008-10-05 19:09:58 +0200700 htmlf("<div class='footer'>generated by cgit %s at ",
701 cgit_version);
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200702 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
Lars Hjemlide5e9282008-06-26 13:53:30 +0200703 html("</div>\n");
704 }
Lars Hjemli80550bb2009-08-11 10:12:35 +0200705 html("</div> <!-- id=cgit -->\n");
Lars Hjemlide5e9282008-06-26 13:53:30 +0200706 html("</body>\n</html>\n");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100707}
708
Lukas Fleischerbafab422013-03-04 08:52:33 +0100709static int print_branch_option(const char *refname, const unsigned char *sha1,
710 int flags, void *cb_data)
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100711{
712 char *name = (char *)refname;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100713 html_option(name, name, ctx.qry.head);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100714 return 0;
715}
716
Johan Herlandc3f23d42010-06-10 01:09:24 +0200717void cgit_add_hidden_formfields(int incl_head, int incl_search,
718 const char *page)
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100719{
Lars Hjemli68cf9b42007-11-03 11:15:56 +0100720 char *url;
721
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100722 if (!ctx.cfg.virtual_root) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100723 url = fmt("%s/%s", ctx.qry.repo, page);
Johan Herlandc8e32952010-06-10 01:09:27 +0200724 if (ctx.qry.vpath)
725 url = fmt("%s/%s", url, ctx.qry.vpath);
Lars Hjemli68cf9b42007-11-03 11:15:56 +0100726 html_hidden("url", url);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100727 }
728
Lars Hjemli25c84322008-07-27 12:32:08 +0200729 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
730 strcmp(ctx.qry.head, ctx.repo->defbranch))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100731 html_hidden("h", ctx.qry.head);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100732
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100733 if (ctx.qry.sha1)
734 html_hidden("id", ctx.qry.sha1);
735 if (ctx.qry.sha2)
736 html_hidden("id2", ctx.qry.sha2);
Lars Hjemli0274b572008-11-29 18:39:41 +0100737 if (ctx.qry.showmsg)
738 html_hidden("showmsg", "1");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100739
740 if (incl_search) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100741 if (ctx.qry.grep)
742 html_hidden("qt", ctx.qry.grep);
743 if (ctx.qry.search)
744 html_hidden("q", ctx.qry.search);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100745 }
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100746}
747
Johan Herland0e34c6d2010-06-10 01:09:25 +0200748static const char *hc(struct cgit_context *ctx, const char *page)
Lars Hjemlif1355692008-04-12 15:53:31 +0200749{
Johan Herland0e34c6d2010-06-10 01:09:25 +0200750 return strcmp(ctx->qry.page, page) ? NULL : "active";
Lars Hjemlif1355692008-04-12 15:53:31 +0200751}
752
Johan Herland24fd7e52010-06-10 01:09:29 +0200753static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
754{
755 char *old_path = ctx->qry.path;
756 char *p = path, *q, *end = path + strlen(path);
757
758 ctx->qry.path = NULL;
759 cgit_self_link("root", NULL, NULL, ctx);
760 ctx->qry.path = p = path;
761 while (p < end) {
762 if (!(q = strchr(p, '/')))
763 q = end;
764 *q = '\0';
765 html_txt("/");
766 cgit_self_link(p, NULL, NULL, ctx);
767 if (q < end)
768 *q = '/';
769 p = q + 1;
770 }
771 ctx->qry.path = old_path;
772}
773
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200774static void print_header(struct cgit_context *ctx)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100775{
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100776 char *logo = NULL, *logo_link = NULL;
777
Lars Hjemlif1355692008-04-12 15:53:31 +0200778 html("<table id='header'>\n");
779 html("<tr>\n");
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400780
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100781 if (ctx->repo && ctx->repo->logo && *ctx->repo->logo)
782 logo = ctx->repo->logo;
783 else
784 logo = ctx->cfg.logo;
785 if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link)
786 logo_link = ctx->repo->logo_link;
787 else
788 logo_link = ctx->cfg.logo_link;
789 if (logo && *logo) {
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400790 html("<td class='logo' rowspan='2'><a href='");
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100791 if (logo_link && *logo_link)
792 html_attr(logo_link);
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400793 else
794 html_attr(cgit_rooturl());
795 html("'><img src='");
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100796 html_attr(logo);
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400797 html("' alt='cgit logo'/></a></td>\n");
798 }
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200799
Lars Hjemlif1355692008-04-12 15:53:31 +0200800 html("<td class='main'>");
Lars Hjemli7c0d2d92008-04-12 19:59:41 +0200801 if (ctx->repo) {
Tobias Grimm7530d942011-07-31 02:44:05 +0200802 cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
Lars Hjemli17890d02008-05-03 12:44:20 +0200803 html(" : ");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200804 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200805 html("</td><td class='form'>");
806 html("<form method='get' action=''>\n");
Lars Hjemlic3c925f2008-12-07 15:52:35 +0100807 cgit_add_hidden_formfields(0, 1, ctx->qry.page);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200808 html("<select name='h' onchange='this.form.submit();'>\n");
809 for_each_branch_ref(print_branch_option, ctx->qry.head);
810 html("</select> ");
811 html("<input type='submit' name='' value='switch'/>");
812 html("</form>");
Lars Hjemli7c0d2d92008-04-12 19:59:41 +0200813 } else
Lars Hjemlif1355692008-04-12 15:53:31 +0200814 html_txt(ctx->cfg.root_title);
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200815 html("</td></tr>\n");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200816
Lars Hjemli2d6ee032008-07-27 12:22:16 +0200817 html("<tr><td class='sub'>");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200818 if (ctx->repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100819 html_txt(ctx->repo->desc);
Lars Hjemli2d6ee032008-07-27 12:22:16 +0200820 html("</td><td class='sub right'>");
821 html_txt(ctx->repo->owner);
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200822 } else {
Lars Hjemli4c991602008-04-29 00:55:34 +0200823 if (ctx->cfg.root_desc)
824 html_txt(ctx->cfg.root_desc);
825 else if (ctx->cfg.index_info)
826 html_include(ctx->cfg.index_info);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200827 }
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200828 html("</td></tr></table>\n");
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200829}
830
831void cgit_print_pageheader(struct cgit_context *ctx)
832{
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200833 html("<div id='cgit'>");
834 if (!ctx->cfg.noheader)
835 print_header(ctx);
Lars Hjemlif1355692008-04-12 15:53:31 +0200836
837 html("<table class='tabs'><tr><td>\n");
838 if (ctx->repo) {
Johan Herland0e34c6d2010-06-10 01:09:25 +0200839 cgit_summary_link("summary", NULL, hc(ctx, "summary"),
Lars Hjemlicb1cc0d2008-10-06 16:00:42 +0200840 ctx->qry.head);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200841 cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head,
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100842 ctx->qry.sha1, NULL);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200843 cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head,
Johan Herland7fdff242010-06-10 01:09:36 +0200844 NULL, ctx->qry.vpath, 0, NULL, NULL,
845 ctx->qry.showmsg);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200846 cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head,
Johan Herland7fdff242010-06-10 01:09:36 +0200847 ctx->qry.sha1, ctx->qry.vpath);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200848 cgit_commit_link("commit", NULL, hc(ctx, "commit"),
Johan Herland7fdff242010-06-10 01:09:36 +0200849 ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200850 cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head,
Johan Herland7fdff242010-06-10 01:09:36 +0200851 ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0);
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100852 if (ctx->repo->max_stats)
Johan Herland0e34c6d2010-06-10 01:09:25 +0200853 cgit_stats_link("stats", NULL, hc(ctx, "stats"),
Johan Herland7fdff242010-06-10 01:09:36 +0200854 ctx->qry.head, ctx->qry.vpath);
Lars Hjemli71adba12008-04-29 01:09:41 +0200855 if (ctx->repo->readme)
856 reporevlink("about", "about", NULL,
Johan Herland0e34c6d2010-06-10 01:09:25 +0200857 hc(ctx, "about"), ctx->qry.head, NULL,
Lars Hjemli71adba12008-04-29 01:09:41 +0200858 NULL);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200859 html("</td><td class='form'>");
860 html("<form class='right' method='get' action='");
861 if (ctx->cfg.virtual_root)
Lars Hjemli2e884f32008-10-05 19:25:47 +0200862 html_url_path(cgit_fileurl(ctx->qry.repo, "log",
Johan Herlandc8e32952010-06-10 01:09:27 +0200863 ctx->qry.vpath, NULL));
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200864 html("'>\n");
Lars Hjemlic3c925f2008-12-07 15:52:35 +0100865 cgit_add_hidden_formfields(1, 0, "log");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200866 html("<select name='qt'>\n");
867 html_option("grep", "log msg", ctx->qry.grep);
868 html_option("author", "author", ctx->qry.grep);
869 html_option("committer", "committer", ctx->qry.grep);
Lars Hjemlia579fb02010-06-19 14:32:37 +0200870 html_option("range", "range", ctx->qry.grep);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200871 html("</select>\n");
Lars Hjemli536b0542008-04-13 11:57:10 +0200872 html("<input class='txt' type='text' size='10' name='q' value='");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200873 html_attr(ctx->qry.search);
874 html("'/>\n");
875 html("<input type='submit' value='search'/>\n");
876 html("</form>\n");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100877 } else {
Tobias Grimm7530d942011-07-31 02:44:05 +0200878 site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, NULL, 0);
Lars Hjemli71adba12008-04-29 01:09:41 +0200879 if (ctx->cfg.root_readme)
Johan Herland0e34c6d2010-06-10 01:09:25 +0200880 site_link("about", "about", NULL, hc(ctx, "about"),
Tobias Grimm7530d942011-07-31 02:44:05 +0200881 NULL, NULL, 0);
Lars Hjemli536b0542008-04-13 11:57:10 +0200882 html("</td><td class='form'>");
883 html("<form method='get' action='");
884 html_attr(cgit_rooturl());
885 html("'>\n");
886 html("<input type='text' name='q' size='10' value='");
887 html_attr(ctx->qry.search);
888 html("'/>\n");
889 html("<input type='submit' value='search'/>\n");
890 html("</form>");
Lars Hjemlie39d7382006-12-28 02:01:49 +0100891 }
Lars Hjemlif1355692008-04-12 15:53:31 +0200892 html("</td></tr></table>\n");
Johan Herlandc93ef962010-06-10 01:09:28 +0200893 if (ctx->qry.vpath) {
894 html("<div class='path'>");
895 html("path: ");
Johan Herland24fd7e52010-06-10 01:09:29 +0200896 cgit_print_path_crumbs(ctx, ctx->qry.vpath);
Johan Herlandc93ef962010-06-10 01:09:28 +0200897 html("</div>");
898 }
Lars Hjemlif1355692008-04-12 15:53:31 +0200899 html("<div class='content'>");
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100900}
Lars Hjemliab2ab952007-02-08 13:53:13 +0100901
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100902void cgit_print_filemode(unsigned short mode)
903{
904 if (S_ISDIR(mode))
905 html("d");
906 else if (S_ISLNK(mode))
907 html("l");
908 else if (S_ISGITLINK(mode))
909 html("m");
910 else
911 html("-");
912 html_fileperm(mode >> 6);
913 html_fileperm(mode >> 3);
914 html_fileperm(mode);
915}
916
Lars Hjemlif34478c2008-03-24 16:00:27 +0100917void cgit_print_snapshot_links(const char *repo, const char *head,
918 const char *hex, int snapshots)
919{
920 const struct cgit_snapshot_format* f;
Lars Hjemli13032722009-10-16 02:03:32 +0200921 char *prefix;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100922 char *filename;
Lars Hjemli13032722009-10-16 02:03:32 +0200923 unsigned char sha1[20];
Lars Hjemlif34478c2008-03-24 16:00:27 +0100924
Lars Hjemli13032722009-10-16 02:03:32 +0200925 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
926 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
927 hex++;
928 prefix = xstrdup(fmt("%s-%s", cgit_repobasename(repo), hex));
Lars Hjemlif34478c2008-03-24 16:00:27 +0100929 for (f = cgit_snapshot_formats; f->suffix; f++) {
930 if (!(snapshots & f->bit))
931 continue;
Lars Hjemli13032722009-10-16 02:03:32 +0200932 filename = fmt("%s%s", prefix, f->suffix);
Lars Hjemlib9053a42008-12-01 21:50:19 +0100933 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100934 html("<br/>");
935 }
Lukas Fleischer59fe3482013-03-04 13:25:38 +0100936 free(prefix);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100937}