blob: 77a302d0c5bba8a54c29a6551b15cea6f02c9205 [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"))
Tobias Grimm7530d942011-07-31 02:44:05 +0200428 return cgit_index_link(name, title, class, ctx->qry.search, ctx->qry.sort,
Johan Herland24fd7e52010-06-10 01:09:29 +0200429 ctx->qry.ofs);
430 else if (!strcmp(ctx->qry.page, "summary"))
431 return cgit_summary_link(name, title, class, ctx->qry.head);
432 else if (!strcmp(ctx->qry.page, "tag"))
433 return cgit_tag_link(name, title, class, ctx->qry.head,
434 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL);
435 else if (!strcmp(ctx->qry.page, "tree"))
436 return cgit_tree_link(name, title, class, ctx->qry.head,
437 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
438 ctx->qry.path);
439 else if (!strcmp(ctx->qry.page, "plain"))
440 return cgit_plain_link(name, title, class, ctx->qry.head,
441 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
442 ctx->qry.path);
443 else if (!strcmp(ctx->qry.page, "log"))
444 return 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);
449 else if (!strcmp(ctx->qry.page, "commit"))
450 return cgit_commit_link(name, title, class, ctx->qry.head,
451 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
452 ctx->qry.path, 0);
453 else if (!strcmp(ctx->qry.page, "patch"))
454 return cgit_patch_link(name, title, class, ctx->qry.head,
455 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
456 ctx->qry.path);
457 else if (!strcmp(ctx->qry.page, "refs"))
458 return cgit_refs_link(name, title, class, ctx->qry.head,
459 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
460 ctx->qry.path);
461 else if (!strcmp(ctx->qry.page, "snapshot"))
462 return cgit_snapshot_link(name, title, class, ctx->qry.head,
463 ctx->qry.has_sha1 ? ctx->qry.sha1 : NULL,
464 ctx->qry.path);
465 else if (!strcmp(ctx->qry.page, "diff"))
466 return cgit_diff_link(name, title, class, ctx->qry.head,
467 ctx->qry.sha1, ctx->qry.sha2,
468 ctx->qry.path, 0);
469 else if (!strcmp(ctx->qry.page, "stats"))
470 return cgit_stats_link(name, title, class, ctx->qry.head,
471 ctx->qry.path);
472
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}
481
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200482void cgit_object_link(struct object *obj)
483{
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100484 char *page, *shortrev, *fullrev, *name;
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200485
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100486 fullrev = sha1_to_hex(obj->sha1);
487 shortrev = xstrdup(fullrev);
488 shortrev[10] = '\0';
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200489 if (obj->type == OBJ_COMMIT) {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100490 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
Johan Herland685872b2010-06-10 01:09:35 +0200491 ctx.qry.head, fullrev, NULL, 0);
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200492 return;
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200493 } else if (obj->type == OBJ_TREE)
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200494 page = "tree";
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200495 else if (obj->type == OBJ_TAG)
Lars Hjemlifc5880f2007-10-28 15:40:47 +0100496 page = "tag";
Lars Hjemli8b5fc6d2008-10-05 21:12:08 +0200497 else
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200498 page = "blob";
Lars Hjemlic57aceb2008-12-01 21:58:59 +0100499 name = fmt("%s %s...", typename(obj->type), shortrev);
500 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
Lars Hjemli4e9107a2007-07-22 23:42:55 +0200501}
502
Lukas Fleischerbafab422013-03-04 08:52:33 +0100503static struct string_list_item *lookup_path(struct string_list *list,
504 const char *path)
Lars Hjemli6857bec2011-06-15 10:04:13 +0200505{
506 struct string_list_item *item;
507
508 while (path && path[0]) {
509 if ((item = string_list_lookup(list, path)))
510 return item;
511 if (!(path = strchr(path, '/')))
512 break;
513 path++;
514 }
515 return NULL;
516}
517
518void cgit_submodule_link(const char *class, char *path, const char *rev)
519{
520 struct string_list *list;
521 struct string_list_item *item;
522 char tail, *dir;
523 size_t len;
524
525 tail = 0;
526 list = &ctx.repo->submodules;
527 item = lookup_path(list, path);
528 if (!item) {
529 len = strlen(path);
530 tail = path[len - 1];
531 if (tail == '/') {
532 path[len - 1] = 0;
533 item = lookup_path(list, path);
534 }
535 }
536 html("<a ");
537 if (class)
538 htmlf("class='%s' ", class);
539 html("href='");
540 if (item) {
541 html_attr(fmt(item->util, rev));
542 } else if (ctx.repo->module_link) {
543 dir = strrchr(path, '/');
544 if (dir)
545 dir++;
546 else
547 dir = path;
548 html_attr(fmt(ctx.repo->module_link, dir, rev));
549 } else {
550 html("#");
551 }
552 html("'>");
553 html_txt(path);
554 html("</a>");
stfnc0a92e82013-02-27 19:47:17 +0100555 html_txt(fmt(" @ %.7s", rev));
Lars Hjemli6857bec2011-06-15 10:04:13 +0200556 if (item && tail)
557 path[len - 1] = tail;
558}
559
Johan Herlandc3f23d42010-06-10 01:09:24 +0200560void cgit_print_date(time_t secs, const char *format, int local_time)
Lars Hjemli148fb962006-12-16 00:33:28 +0100561{
Lars Hjemli5db39172007-05-22 23:08:46 +0200562 char buf[64];
Lars Hjemli148fb962006-12-16 00:33:28 +0100563 struct tm *time;
564
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +0100565 if (!secs)
566 return;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500567 if (local_time)
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200568 time = localtime(&secs);
569 else
570 time = gmtime(&secs);
Lars Hjemli5db39172007-05-22 23:08:46 +0200571 strftime(buf, sizeof(buf)-1, format, time);
Lars Hjemli148fb962006-12-16 00:33:28 +0100572 html_txt(buf);
Lars Hjemli148fb962006-12-16 00:33:28 +0100573}
574
Johan Herlandc3f23d42010-06-10 01:09:24 +0200575void cgit_print_age(time_t t, time_t max_relative, const char *format)
Lars Hjemli5db39172007-05-22 23:08:46 +0200576{
577 time_t now, secs;
578
Lars Hjemlifc4c4ba2007-12-02 22:11:35 +0100579 if (!t)
580 return;
Lars Hjemli5db39172007-05-22 23:08:46 +0200581 time(&now);
582 secs = now - t;
583
584 if (secs > max_relative && max_relative >= 0) {
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200585 cgit_print_date(t, format, ctx.cfg.local_time);
Lars Hjemli5db39172007-05-22 23:08:46 +0200586 return;
587 }
588
589 if (secs < TM_HOUR * 2) {
590 htmlf("<span class='age-mins'>%.0f min.</span>",
591 secs * 1.0 / TM_MIN);
592 return;
593 }
594 if (secs < TM_DAY * 2) {
595 htmlf("<span class='age-hours'>%.0f hours</span>",
596 secs * 1.0 / TM_HOUR);
597 return;
598 }
599 if (secs < TM_WEEK * 2) {
600 htmlf("<span class='age-days'>%.0f days</span>",
601 secs * 1.0 / TM_DAY);
602 return;
603 }
604 if (secs < TM_MONTH * 2) {
605 htmlf("<span class='age-weeks'>%.0f weeks</span>",
606 secs * 1.0 / TM_WEEK);
607 return;
608 }
609 if (secs < TM_YEAR * 2) {
610 htmlf("<span class='age-months'>%.0f months</span>",
611 secs * 1.0 / TM_MONTH);
612 return;
613 }
614 htmlf("<span class='age-years'>%.0f years</span>",
615 secs * 1.0 / TM_YEAR);
616}
617
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100618void cgit_print_http_headers(struct cgit_context *ctx)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100619{
Lars Hjemli8b2252b2009-08-10 09:20:17 +0200620 if (ctx->env.no_http && !strcmp(ctx->env.no_http, "1"))
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100621 return;
622
Lars Hjemlie429fb02009-06-07 20:43:08 +0200623 if (ctx->page.status)
624 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100625 if (ctx->page.mimetype && ctx->page.charset)
626 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
627 ctx->page.charset);
628 else if (ctx->page.mimetype)
629 htmlf("Content-Type: %s\n", ctx->page.mimetype);
Lars Hjemlie5da4bc2008-08-06 10:53:50 +0200630 if (ctx->page.size)
Bernhard Reutner-Fischer6bf26582010-12-23 12:47:53 +0100631 htmlf("Content-Length: %zd\n", ctx->page.size);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100632 if (ctx->page.filename)
633 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
634 ctx->page.filename);
635 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
636 htmlf("Expires: %s\n", http_date(ctx->page.expires));
Lars Hjemli488a2142009-02-19 22:38:36 +0100637 if (ctx->page.etag)
638 htmlf("ETag: \"%s\"\n", ctx->page.etag);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100639 html("\n");
Lars Hjemli60a26272009-08-10 08:21:09 +0200640 if (ctx->env.request_method && !strcmp(ctx->env.request_method, "HEAD"))
Lars Hjemli3ff58dd2009-02-19 23:24:15 +0100641 exit(0);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100642}
643
644void cgit_print_docstart(struct cgit_context *ctx)
645{
Lars Hjemli80550bb2009-08-11 10:12:35 +0200646 if (ctx->cfg.embedded) {
647 if (ctx->cfg.header)
648 html_include(ctx->cfg.header);
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100649 return;
Lars Hjemli80550bb2009-08-11 10:12:35 +0200650 }
Lars Hjemli0cbb5082009-01-22 23:33:56 +0100651
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200652 char *host = cgit_hosturl();
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100653 html(cgit_doctype);
Lars Hjemli29154832007-11-11 13:04:28 +0100654 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100655 html("<head>\n");
656 html("<title>");
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100657 html_txt(ctx->page.title);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100658 html("</title>\n");
Lars Hjemlif6925032007-06-18 09:42:10 +0200659 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100660 if (ctx->cfg.robots && *ctx->cfg.robots)
661 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100662 html("<link rel='stylesheet' type='text/css' href='");
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100663 html_attr(ctx->cfg.css);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100664 html("'/>\n");
Lars Hjemli502865a2008-07-19 20:40:30 +0200665 if (ctx->cfg.favicon) {
666 html("<link rel='shortcut icon' href='");
667 html_attr(ctx->cfg.favicon);
668 html("'/>\n");
669 }
John Keeping94b7c762011-11-24 11:54:47 +0000670 if (host && ctx->repo && ctx->qry.head) {
Diego Ongaro694dd432009-06-10 18:18:34 -0500671 html("<link rel='alternate' title='Atom feed' href='");
672 html(cgit_httpscheme());
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200673 html_attr(cgit_hosturl());
Johan Herlandc8e32952010-06-10 01:09:27 +0200674 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.vpath,
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200675 fmt("h=%s", ctx->qry.head)));
Mark Lodatob5a3a202009-03-15 00:11:54 -0400676 html("' type='application/atom+xml'/>\n");
Lars Hjemlib2a3d312008-05-21 08:17:54 +0200677 }
Mark Lodatob5a3a202009-03-15 00:11:54 -0400678 if (ctx->cfg.head_include)
679 html_include(ctx->cfg.head_include);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100680 html("</head>\n");
681 html("<body>\n");
Lars Hjemlib1159552009-01-29 21:27:39 +0100682 if (ctx->cfg.header)
683 html_include(ctx->cfg.header);
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100684}
685
686void cgit_print_docend()
687{
Lars Hjemli80550bb2009-08-11 10:12:35 +0200688 html("</div> <!-- class=content -->\n");
689 if (ctx.cfg.embedded) {
690 html("</div> <!-- id=cgit -->\n");
691 if (ctx.cfg.footer)
692 html_include(ctx.cfg.footer);
693 return;
694 }
Lars Hjemlide5e9282008-06-26 13:53:30 +0200695 if (ctx.cfg.footer)
696 html_include(ctx.cfg.footer);
697 else {
Lars Hjemlib9aabf02008-10-05 19:09:58 +0200698 htmlf("<div class='footer'>generated by cgit %s at ",
699 cgit_version);
Stefan Naewe0f0ab142008-08-01 14:54:38 +0200700 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
Lars Hjemlide5e9282008-06-26 13:53:30 +0200701 html("</div>\n");
702 }
Lars Hjemli80550bb2009-08-11 10:12:35 +0200703 html("</div> <!-- id=cgit -->\n");
Lars Hjemlide5e9282008-06-26 13:53:30 +0200704 html("</body>\n</html>\n");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100705}
706
Lukas Fleischerbafab422013-03-04 08:52:33 +0100707static int print_branch_option(const char *refname, const unsigned char *sha1,
708 int flags, void *cb_data)
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100709{
710 char *name = (char *)refname;
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100711 html_option(name, name, ctx.qry.head);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100712 return 0;
713}
714
Johan Herlandc3f23d42010-06-10 01:09:24 +0200715void cgit_add_hidden_formfields(int incl_head, int incl_search,
716 const char *page)
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100717{
Lars Hjemli68cf9b42007-11-03 11:15:56 +0100718 char *url;
719
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100720 if (!ctx.cfg.virtual_root) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100721 url = fmt("%s/%s", ctx.qry.repo, page);
Johan Herlandc8e32952010-06-10 01:09:27 +0200722 if (ctx.qry.vpath)
723 url = fmt("%s/%s", url, ctx.qry.vpath);
Lars Hjemli68cf9b42007-11-03 11:15:56 +0100724 html_hidden("url", url);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100725 }
726
Lars Hjemli25c84322008-07-27 12:32:08 +0200727 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
728 strcmp(ctx.qry.head, ctx.repo->defbranch))
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100729 html_hidden("h", ctx.qry.head);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100730
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100731 if (ctx.qry.sha1)
732 html_hidden("id", ctx.qry.sha1);
733 if (ctx.qry.sha2)
734 html_hidden("id2", ctx.qry.sha2);
Lars Hjemli0274b572008-11-29 18:39:41 +0100735 if (ctx.qry.showmsg)
736 html_hidden("showmsg", "1");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100737
738 if (incl_search) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100739 if (ctx.qry.grep)
740 html_hidden("qt", ctx.qry.grep);
741 if (ctx.qry.search)
742 html_hidden("q", ctx.qry.search);
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100743 }
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100744}
745
Johan Herland0e34c6d2010-06-10 01:09:25 +0200746static const char *hc(struct cgit_context *ctx, const char *page)
Lars Hjemlif1355692008-04-12 15:53:31 +0200747{
Johan Herland0e34c6d2010-06-10 01:09:25 +0200748 return strcmp(ctx->qry.page, page) ? NULL : "active";
Lars Hjemlif1355692008-04-12 15:53:31 +0200749}
750
Johan Herland24fd7e52010-06-10 01:09:29 +0200751static void cgit_print_path_crumbs(struct cgit_context *ctx, char *path)
752{
753 char *old_path = ctx->qry.path;
754 char *p = path, *q, *end = path + strlen(path);
755
756 ctx->qry.path = NULL;
757 cgit_self_link("root", NULL, NULL, ctx);
758 ctx->qry.path = p = path;
759 while (p < end) {
760 if (!(q = strchr(p, '/')))
761 q = end;
762 *q = '\0';
763 html_txt("/");
764 cgit_self_link(p, NULL, NULL, ctx);
765 if (q < end)
766 *q = '/';
767 p = q + 1;
768 }
769 ctx->qry.path = old_path;
770}
771
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200772static void print_header(struct cgit_context *ctx)
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100773{
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100774 char *logo = NULL, *logo_link = NULL;
775
Lars Hjemlif1355692008-04-12 15:53:31 +0200776 html("<table id='header'>\n");
777 html("<tr>\n");
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400778
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100779 if (ctx->repo && ctx->repo->logo && *ctx->repo->logo)
780 logo = ctx->repo->logo;
781 else
782 logo = ctx->cfg.logo;
783 if (ctx->repo && ctx->repo->logo_link && *ctx->repo->logo_link)
784 logo_link = ctx->repo->logo_link;
785 else
786 logo_link = ctx->cfg.logo_link;
787 if (logo && *logo) {
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400788 html("<td class='logo' rowspan='2'><a href='");
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100789 if (logo_link && *logo_link)
790 html_attr(logo_link);
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400791 else
792 html_attr(cgit_rooturl());
793 html("'><img src='");
Bernhard Reutner-Fischer808c6852010-12-23 12:47:54 +0100794 html_attr(logo);
Matthew Metnetsky6421dc32009-06-29 21:27:51 -0400795 html("' alt='cgit logo'/></a></td>\n");
796 }
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200797
Lars Hjemlif1355692008-04-12 15:53:31 +0200798 html("<td class='main'>");
Lars Hjemli7c0d2d92008-04-12 19:59:41 +0200799 if (ctx->repo) {
Tobias Grimm7530d942011-07-31 02:44:05 +0200800 cgit_index_link("index", NULL, NULL, NULL, NULL, 0);
Lars Hjemli17890d02008-05-03 12:44:20 +0200801 html(" : ");
Lars Hjemli49ecbbd2008-10-05 17:16:36 +0200802 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200803 html("</td><td class='form'>");
804 html("<form method='get' action=''>\n");
Lars Hjemlic3c925f2008-12-07 15:52:35 +0100805 cgit_add_hidden_formfields(0, 1, ctx->qry.page);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200806 html("<select name='h' onchange='this.form.submit();'>\n");
807 for_each_branch_ref(print_branch_option, ctx->qry.head);
808 html("</select> ");
809 html("<input type='submit' name='' value='switch'/>");
810 html("</form>");
Lars Hjemli7c0d2d92008-04-12 19:59:41 +0200811 } else
Lars Hjemlif1355692008-04-12 15:53:31 +0200812 html_txt(ctx->cfg.root_title);
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200813 html("</td></tr>\n");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200814
Lars Hjemli2d6ee032008-07-27 12:22:16 +0200815 html("<tr><td class='sub'>");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200816 if (ctx->repo) {
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100817 html_txt(ctx->repo->desc);
Lars Hjemli2d6ee032008-07-27 12:22:16 +0200818 html("</td><td class='sub right'>");
819 html_txt(ctx->repo->owner);
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200820 } else {
Lars Hjemli4c991602008-04-29 00:55:34 +0200821 if (ctx->cfg.root_desc)
822 html_txt(ctx->cfg.root_desc);
823 else if (ctx->cfg.index_info)
824 html_include(ctx->cfg.index_info);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200825 }
Lars Hjemli3cfcb082008-04-15 00:00:11 +0200826 html("</td></tr></table>\n");
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200827}
828
829void cgit_print_pageheader(struct cgit_context *ctx)
830{
Lars Hjemlief0c6aa2009-07-25 12:19:31 +0200831 html("<div id='cgit'>");
832 if (!ctx->cfg.noheader)
833 print_header(ctx);
Lars Hjemlif1355692008-04-12 15:53:31 +0200834
835 html("<table class='tabs'><tr><td>\n");
836 if (ctx->repo) {
Johan Herland0e34c6d2010-06-10 01:09:25 +0200837 cgit_summary_link("summary", NULL, hc(ctx, "summary"),
Lars Hjemlicb1cc0d2008-10-06 16:00:42 +0200838 ctx->qry.head);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200839 cgit_refs_link("refs", NULL, hc(ctx, "refs"), ctx->qry.head,
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100840 ctx->qry.sha1, NULL);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200841 cgit_log_link("log", NULL, hc(ctx, "log"), ctx->qry.head,
Johan Herland7fdff242010-06-10 01:09:36 +0200842 NULL, ctx->qry.vpath, 0, NULL, NULL,
843 ctx->qry.showmsg);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200844 cgit_tree_link("tree", NULL, hc(ctx, "tree"), ctx->qry.head,
Johan Herland7fdff242010-06-10 01:09:36 +0200845 ctx->qry.sha1, ctx->qry.vpath);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200846 cgit_commit_link("commit", NULL, hc(ctx, "commit"),
Johan Herland7fdff242010-06-10 01:09:36 +0200847 ctx->qry.head, ctx->qry.sha1, ctx->qry.vpath, 0);
Johan Herland0e34c6d2010-06-10 01:09:25 +0200848 cgit_diff_link("diff", NULL, hc(ctx, "diff"), ctx->qry.head,
Johan Herland7fdff242010-06-10 01:09:36 +0200849 ctx->qry.sha1, ctx->qry.sha2, ctx->qry.vpath, 0);
Lars Hjemlifb2f3f62008-12-07 13:17:21 +0100850 if (ctx->repo->max_stats)
Johan Herland0e34c6d2010-06-10 01:09:25 +0200851 cgit_stats_link("stats", NULL, hc(ctx, "stats"),
Johan Herland7fdff242010-06-10 01:09:36 +0200852 ctx->qry.head, ctx->qry.vpath);
Lars Hjemli71adba12008-04-29 01:09:41 +0200853 if (ctx->repo->readme)
854 reporevlink("about", "about", NULL,
Johan Herland0e34c6d2010-06-10 01:09:25 +0200855 hc(ctx, "about"), ctx->qry.head, NULL,
Lars Hjemli71adba12008-04-29 01:09:41 +0200856 NULL);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200857 html("</td><td class='form'>");
858 html("<form class='right' method='get' action='");
859 if (ctx->cfg.virtual_root)
Lars Hjemli2e884f32008-10-05 19:25:47 +0200860 html_url_path(cgit_fileurl(ctx->qry.repo, "log",
Johan Herlandc8e32952010-06-10 01:09:27 +0200861 ctx->qry.vpath, NULL));
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200862 html("'>\n");
Lars Hjemlic3c925f2008-12-07 15:52:35 +0100863 cgit_add_hidden_formfields(1, 0, "log");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200864 html("<select name='qt'>\n");
865 html_option("grep", "log msg", ctx->qry.grep);
866 html_option("author", "author", ctx->qry.grep);
867 html_option("committer", "committer", ctx->qry.grep);
Lars Hjemlia579fb02010-06-19 14:32:37 +0200868 html_option("range", "range", ctx->qry.grep);
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200869 html("</select>\n");
Lars Hjemli536b0542008-04-13 11:57:10 +0200870 html("<input class='txt' type='text' size='10' name='q' value='");
Lars Hjemli931fc6d2008-04-13 10:57:11 +0200871 html_attr(ctx->qry.search);
872 html("'/>\n");
873 html("<input type='submit' value='search'/>\n");
874 html("</form>\n");
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100875 } else {
Tobias Grimm7530d942011-07-31 02:44:05 +0200876 site_link(NULL, "index", NULL, hc(ctx, "repolist"), NULL, NULL, 0);
Lars Hjemli71adba12008-04-29 01:09:41 +0200877 if (ctx->cfg.root_readme)
Johan Herland0e34c6d2010-06-10 01:09:25 +0200878 site_link("about", "about", NULL, hc(ctx, "about"),
Tobias Grimm7530d942011-07-31 02:44:05 +0200879 NULL, NULL, 0);
Lars Hjemli536b0542008-04-13 11:57:10 +0200880 html("</td><td class='form'>");
881 html("<form method='get' action='");
882 html_attr(cgit_rooturl());
883 html("'>\n");
884 html("<input type='text' name='q' size='10' value='");
885 html_attr(ctx->qry.search);
886 html("'/>\n");
887 html("<input type='submit' value='search'/>\n");
888 html("</form>");
Lars Hjemlie39d7382006-12-28 02:01:49 +0100889 }
Lars Hjemlif1355692008-04-12 15:53:31 +0200890 html("</td></tr></table>\n");
Johan Herlandc93ef962010-06-10 01:09:28 +0200891 if (ctx->qry.vpath) {
892 html("<div class='path'>");
893 html("path: ");
Johan Herland24fd7e52010-06-10 01:09:29 +0200894 cgit_print_path_crumbs(ctx, ctx->qry.vpath);
Johan Herlandc93ef962010-06-10 01:09:28 +0200895 html("</div>");
896 }
Lars Hjemlif1355692008-04-12 15:53:31 +0200897 html("<div class='content'>");
Lars Hjemli5a106eb2006-12-11 16:38:30 +0100898}
Lars Hjemliab2ab952007-02-08 13:53:13 +0100899
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +0100900void cgit_print_filemode(unsigned short mode)
901{
902 if (S_ISDIR(mode))
903 html("d");
904 else if (S_ISLNK(mode))
905 html("l");
906 else if (S_ISGITLINK(mode))
907 html("m");
908 else
909 html("-");
910 html_fileperm(mode >> 6);
911 html_fileperm(mode >> 3);
912 html_fileperm(mode);
913}
914
Lars Hjemlif34478c2008-03-24 16:00:27 +0100915void cgit_print_snapshot_links(const char *repo, const char *head,
916 const char *hex, int snapshots)
917{
918 const struct cgit_snapshot_format* f;
Lars Hjemli13032722009-10-16 02:03:32 +0200919 char *prefix;
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100920 char *filename;
Lars Hjemli13032722009-10-16 02:03:32 +0200921 unsigned char sha1[20];
Lars Hjemlif34478c2008-03-24 16:00:27 +0100922
Lars Hjemli13032722009-10-16 02:03:32 +0200923 if (get_sha1(fmt("refs/tags/%s", hex), sha1) == 0 &&
924 (hex[0] == 'v' || hex[0] == 'V') && isdigit(hex[1]))
925 hex++;
926 prefix = xstrdup(fmt("%s-%s", cgit_repobasename(repo), hex));
Lars Hjemlif34478c2008-03-24 16:00:27 +0100927 for (f = cgit_snapshot_formats; f->suffix; f++) {
928 if (!(snapshots & f->bit))
929 continue;
Lars Hjemli13032722009-10-16 02:03:32 +0200930 filename = fmt("%s%s", prefix, f->suffix);
Lars Hjemlib9053a42008-12-01 21:50:19 +0100931 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100932 html("<br/>");
933 }
934}