summaryrefslogtreecommitdiffabout
path: root/ui-shared.c
Unidiff
Diffstat (limited to 'ui-shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-shared.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/ui-shared.c b/ui-shared.c
index d08ede9..f366354 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,646 +1,658 @@
1/* ui-shared.c: common web output functions 1/* ui-shared.c: common web output functions
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cmd.h" 10#include "cmd.h"
11#include "html.h" 11#include "html.h"
12 12
13const char cgit_doctype[] = 13const char cgit_doctype[] =
14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" 14"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 15" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
16 16
17static char *http_date(time_t t) 17static char *http_date(time_t t)
18{ 18{
19 static char day[][4] = 19 static char day[][4] =
20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 20 {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
21 static char month[][4] = 21 static char month[][4] =
22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 22 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"}; 23 "Jul", "Aug", "Sep", "Oct", "Now", "Dec"};
24 struct tm *tm = gmtime(&t); 24 struct tm *tm = gmtime(&t);
25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday], 25 return fmt("%s, %02d %s %04d %02d:%02d:%02d GMT", day[tm->tm_wday],
26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year, 26 tm->tm_mday, month[tm->tm_mon], 1900+tm->tm_year,
27 tm->tm_hour, tm->tm_min, tm->tm_sec); 27 tm->tm_hour, tm->tm_min, tm->tm_sec);
28} 28}
29 29
30void cgit_print_error(char *msg) 30void cgit_print_error(char *msg)
31{ 31{
32 html("<div class='error'>"); 32 html("<div class='error'>");
33 html_txt(msg); 33 html_txt(msg);
34 html("</div>\n"); 34 html("</div>\n");
35} 35}
36 36
37char *cgit_rooturl() 37char *cgit_rooturl()
38{ 38{
39 if (ctx.cfg.virtual_root) 39 if (ctx.cfg.virtual_root)
40 return fmt("%s/", ctx.cfg.virtual_root); 40 return fmt("%s/", ctx.cfg.virtual_root);
41 else 41 else
42 return ctx.cfg.script_name; 42 return ctx.cfg.script_name;
43} 43}
44 44
45char *cgit_repourl(const char *reponame) 45char *cgit_repourl(const char *reponame)
46{ 46{
47 if (ctx.cfg.virtual_root) { 47 if (ctx.cfg.virtual_root) {
48 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); 48 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
49 } else { 49 } else {
50 return fmt("?r=%s", reponame); 50 return fmt("?r=%s", reponame);
51 } 51 }
52} 52}
53 53
54char *cgit_fileurl(const char *reponame, const char *pagename, 54char *cgit_fileurl(const char *reponame, const char *pagename,
55 const char *filename, const char *query) 55 const char *filename, const char *query)
56{ 56{
57 char *tmp; 57 char *tmp;
58 char *delim; 58 char *delim;
59 59
60 if (ctx.cfg.virtual_root) { 60 if (ctx.cfg.virtual_root) {
61 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, 61 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
62 pagename, (filename ? filename:"")); 62 pagename, (filename ? filename:""));
63 delim = "?"; 63 delim = "?";
64 } else { 64 } else {
65 tmp = fmt("?url=%s/%s/%s", reponame, pagename, 65 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
66 (filename ? filename : "")); 66 (filename ? filename : ""));
67 delim = "&"; 67 delim = "&";
68 } 68 }
69 if (query) 69 if (query)
70 tmp = fmt("%s%s%s", tmp, delim, query); 70 tmp = fmt("%s%s%s", tmp, delim, query);
71 return tmp; 71 return tmp;
72} 72}
73 73
74char *cgit_pageurl(const char *reponame, const char *pagename, 74char *cgit_pageurl(const char *reponame, const char *pagename,
75 const char *query) 75 const char *query)
76{ 76{
77 return cgit_fileurl(reponame,pagename,0,query); 77 return cgit_fileurl(reponame,pagename,0,query);
78} 78}
79 79
80const char *cgit_repobasename(const char *reponame) 80const char *cgit_repobasename(const char *reponame)
81{ 81{
82 /* I assume we don't need to store more than one repo basename */ 82 /* I assume we don't need to store more than one repo basename */
83 static char rvbuf[1024]; 83 static char rvbuf[1024];
84 int p; 84 int p;
85 const char *rv; 85 const char *rv;
86 strncpy(rvbuf,reponame,sizeof(rvbuf)); 86 strncpy(rvbuf,reponame,sizeof(rvbuf));
87 if(rvbuf[sizeof(rvbuf)-1]) 87 if(rvbuf[sizeof(rvbuf)-1])
88 die("cgit_repobasename: truncated repository name '%s'", reponame); 88 die("cgit_repobasename: truncated repository name '%s'", reponame);
89 p = strlen(rvbuf)-1; 89 p = strlen(rvbuf)-1;
90 /* strip trailing slashes */ 90 /* strip trailing slashes */
91 while(p && rvbuf[p]=='/') rvbuf[p--]=0; 91 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
92 /* strip trailing .git */ 92 /* strip trailing .git */
93 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 93 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
94 p -= 3; rvbuf[p--] = 0; 94 p -= 3; rvbuf[p--] = 0;
95 } 95 }
96 /* strip more trailing slashes if any */ 96 /* strip more trailing slashes if any */
97 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 97 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
98 /* find last slash in the remaining string */ 98 /* find last slash in the remaining string */
99 rv = strrchr(rvbuf,'/'); 99 rv = strrchr(rvbuf,'/');
100 if(rv) 100 if(rv)
101 return ++rv; 101 return ++rv;
102 return rvbuf; 102 return rvbuf;
103} 103}
104 104
105char *cgit_currurl() 105char *cgit_currurl()
106{ 106{
107 if (!ctx.cfg.virtual_root) 107 if (!ctx.cfg.virtual_root)
108 return ctx.cfg.script_name; 108 return ctx.cfg.script_name;
109 else if (ctx.qry.page) 109 else if (ctx.qry.page)
110 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); 110 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
111 else if (ctx.qry.repo) 111 else if (ctx.qry.repo)
112 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); 112 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
113 else 113 else
114 return fmt("%s/", ctx.cfg.virtual_root); 114 return fmt("%s/", ctx.cfg.virtual_root);
115} 115}
116 116
117static void site_url(char *page, char *search) 117static void site_url(char *page, char *search, int ofs)
118{ 118{
119 char *delim = "?"; 119 char *delim = "?";
120 120
121 if (ctx.cfg.virtual_root) { 121 if (ctx.cfg.virtual_root) {
122 html_attr(ctx.cfg.virtual_root); 122 html_attr(ctx.cfg.virtual_root);
123 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 123 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
124 html("/"); 124 html("/");
125 } else 125 } else
126 html(ctx.cfg.script_name); 126 html(ctx.cfg.script_name);
127 127
128 if (page) { 128 if (page) {
129 htmlf("?p=%s", page); 129 htmlf("?p=%s", page);
130 delim = "&"; 130 delim = "&";
131 } 131 }
132 if (search) { 132 if (search) {
133 html(delim); 133 html(delim);
134 html("q="); 134 html("q=");
135 html_attr(search); 135 html_attr(search);
136 delim = "&";
137 }
138 if (ofs) {
139 html(delim);
140 htmlf("ofs=%d", ofs);
136 } 141 }
137} 142}
138 143
139static void site_link(char *page, char *name, char *title, char *class, 144static void site_link(char *page, char *name, char *title, char *class,
140 char *search) 145 char *search, int ofs)
141{ 146{
142 html("<a"); 147 html("<a");
143 if (title) { 148 if (title) {
144 html(" title='"); 149 html(" title='");
145 html_attr(title); 150 html_attr(title);
146 html("'"); 151 html("'");
147 } 152 }
148 if (class) { 153 if (class) {
149 html(" class='"); 154 html(" class='");
150 html_attr(class); 155 html_attr(class);
151 html("'"); 156 html("'");
152 } 157 }
153 html(" href='"); 158 html(" href='");
154 site_url(page, search); 159 site_url(page, search, ofs);
155 html("'>"); 160 html("'>");
156 html_txt(name); 161 html_txt(name);
157 html("</a>"); 162 html("</a>");
158} 163}
159 164
165void cgit_index_link(char *name, char *title, char *class, char *pattern,
166 int ofs)
167{
168 site_link(NULL, name, title, class, pattern, ofs);
169}
170
160static char *repolink(char *title, char *class, char *page, char *head, 171static char *repolink(char *title, char *class, char *page, char *head,
161 char *path) 172 char *path)
162{ 173{
163 char *delim = "?"; 174 char *delim = "?";
164 175
165 html("<a"); 176 html("<a");
166 if (title) { 177 if (title) {
167 html(" title='"); 178 html(" title='");
168 html_attr(title); 179 html_attr(title);
169 html("'"); 180 html("'");
170 } 181 }
171 if (class) { 182 if (class) {
172 html(" class='"); 183 html(" class='");
173 html_attr(class); 184 html_attr(class);
174 html("'"); 185 html("'");
175 } 186 }
176 html(" href='"); 187 html(" href='");
177 if (ctx.cfg.virtual_root) { 188 if (ctx.cfg.virtual_root) {
178 html_attr(ctx.cfg.virtual_root); 189 html_attr(ctx.cfg.virtual_root);
179 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 190 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
180 html("/"); 191 html("/");
181 html_attr(ctx.repo->url); 192 html_attr(ctx.repo->url);
182 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 193 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
183 html("/"); 194 html("/");
184 if (page) { 195 if (page) {
185 html(page); 196 html(page);
186 html("/"); 197 html("/");
187 if (path) 198 if (path)
188 html_attr(path); 199 html_attr(path);
189 } 200 }
190 } else { 201 } else {
191 html(ctx.cfg.script_name); 202 html(ctx.cfg.script_name);
192 html("?url="); 203 html("?url=");
193 html_attr(ctx.repo->url); 204 html_attr(ctx.repo->url);
194 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 205 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
195 html("/"); 206 html("/");
196 if (page) { 207 if (page) {
197 html(page); 208 html(page);
198 html("/"); 209 html("/");
199 if (path) 210 if (path)
200 html_attr(path); 211 html_attr(path);
201 } 212 }
202 delim = "&amp;"; 213 delim = "&amp;";
203 } 214 }
204 if (head && strcmp(head, ctx.repo->defbranch)) { 215 if (head && strcmp(head, ctx.repo->defbranch)) {
205 html(delim); 216 html(delim);
206 html("h="); 217 html("h=");
207 html_attr(head); 218 html_attr(head);
208 delim = "&amp;"; 219 delim = "&amp;";
209 } 220 }
210 return fmt("%s", delim); 221 return fmt("%s", delim);
211} 222}
212 223
213static void reporevlink(char *page, char *name, char *title, char *class, 224static void reporevlink(char *page, char *name, char *title, char *class,
214 char *head, char *rev, char *path) 225 char *head, char *rev, char *path)
215{ 226{
216 char *delim; 227 char *delim;
217 228
218 delim = repolink(title, class, page, head, path); 229 delim = repolink(title, class, page, head, path);
219 if (rev && strcmp(rev, ctx.qry.head)) { 230 if (rev && strcmp(rev, ctx.qry.head)) {
220 html(delim); 231 html(delim);
221 html("id="); 232 html("id=");
222 html_attr(rev); 233 html_attr(rev);
223 } 234 }
224 html("'>"); 235 html("'>");
225 html_txt(name); 236 html_txt(name);
226 html("</a>"); 237 html("</a>");
227} 238}
228 239
229void cgit_tree_link(char *name, char *title, char *class, char *head, 240void cgit_tree_link(char *name, char *title, char *class, char *head,
230 char *rev, char *path) 241 char *rev, char *path)
231{ 242{
232 reporevlink("tree", name, title, class, head, rev, path); 243 reporevlink("tree", name, title, class, head, rev, path);
233} 244}
234 245
235void cgit_log_link(char *name, char *title, char *class, char *head, 246void cgit_log_link(char *name, char *title, char *class, char *head,
236 char *rev, char *path, int ofs, char *grep, char *pattern) 247 char *rev, char *path, int ofs, char *grep, char *pattern)
237{ 248{
238 char *delim; 249 char *delim;
239 250
240 delim = repolink(title, class, "log", head, path); 251 delim = repolink(title, class, "log", head, path);
241 if (rev && strcmp(rev, ctx.qry.head)) { 252 if (rev && strcmp(rev, ctx.qry.head)) {
242 html(delim); 253 html(delim);
243 html("id="); 254 html("id=");
244 html_attr(rev); 255 html_attr(rev);
245 delim = "&"; 256 delim = "&";
246 } 257 }
247 if (grep && pattern) { 258 if (grep && pattern) {
248 html(delim); 259 html(delim);
249 html("qt="); 260 html("qt=");
250 html_attr(grep); 261 html_attr(grep);
251 delim = "&"; 262 delim = "&";
252 html(delim); 263 html(delim);
253 html("q="); 264 html("q=");
254 html_attr(pattern); 265 html_attr(pattern);
255 } 266 }
256 if (ofs > 0) { 267 if (ofs > 0) {
257 html(delim); 268 html(delim);
258 html("ofs="); 269 html("ofs=");
259 htmlf("%d", ofs); 270 htmlf("%d", ofs);
260 } 271 }
261 html("'>"); 272 html("'>");
262 html_txt(name); 273 html_txt(name);
263 html("</a>"); 274 html("</a>");
264} 275}
265 276
266void cgit_commit_link(char *name, char *title, char *class, char *head, 277void cgit_commit_link(char *name, char *title, char *class, char *head,
267 char *rev) 278 char *rev)
268{ 279{
269 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { 280 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
270 name[ctx.cfg.max_msg_len] = '\0'; 281 name[ctx.cfg.max_msg_len] = '\0';
271 name[ctx.cfg.max_msg_len - 1] = '.'; 282 name[ctx.cfg.max_msg_len - 1] = '.';
272 name[ctx.cfg.max_msg_len - 2] = '.'; 283 name[ctx.cfg.max_msg_len - 2] = '.';
273 name[ctx.cfg.max_msg_len - 3] = '.'; 284 name[ctx.cfg.max_msg_len - 3] = '.';
274 } 285 }
275 reporevlink("commit", name, title, class, head, rev, NULL); 286 reporevlink("commit", name, title, class, head, rev, NULL);
276} 287}
277 288
278void cgit_refs_link(char *name, char *title, char *class, char *head, 289void cgit_refs_link(char *name, char *title, char *class, char *head,
279 char *rev, char *path) 290 char *rev, char *path)
280{ 291{
281 reporevlink("refs", name, title, class, head, rev, path); 292 reporevlink("refs", name, title, class, head, rev, path);
282} 293}
283 294
284void cgit_snapshot_link(char *name, char *title, char *class, char *head, 295void cgit_snapshot_link(char *name, char *title, char *class, char *head,
285 char *rev, char *archivename) 296 char *rev, char *archivename)
286{ 297{
287 reporevlink("snapshot", name, title, class, head, rev, archivename); 298 reporevlink("snapshot", name, title, class, head, rev, archivename);
288} 299}
289 300
290void cgit_diff_link(char *name, char *title, char *class, char *head, 301void cgit_diff_link(char *name, char *title, char *class, char *head,
291 char *new_rev, char *old_rev, char *path) 302 char *new_rev, char *old_rev, char *path)
292{ 303{
293 char *delim; 304 char *delim;
294 305
295 delim = repolink(title, class, "diff", head, path); 306 delim = repolink(title, class, "diff", head, path);
296 if (new_rev && strcmp(new_rev, ctx.qry.head)) { 307 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
297 html(delim); 308 html(delim);
298 html("id="); 309 html("id=");
299 html_attr(new_rev); 310 html_attr(new_rev);
300 delim = "&amp;"; 311 delim = "&amp;";
301 } 312 }
302 if (old_rev) { 313 if (old_rev) {
303 html(delim); 314 html(delim);
304 html("id2="); 315 html("id2=");
305 html_attr(old_rev); 316 html_attr(old_rev);
306 } 317 }
307 html("'>"); 318 html("'>");
308 html_txt(name); 319 html_txt(name);
309 html("</a>"); 320 html("</a>");
310} 321}
311 322
312void cgit_patch_link(char *name, char *title, char *class, char *head, 323void cgit_patch_link(char *name, char *title, char *class, char *head,
313 char *rev) 324 char *rev)
314{ 325{
315 reporevlink("patch", name, title, class, head, rev, NULL); 326 reporevlink("patch", name, title, class, head, rev, NULL);
316} 327}
317 328
318void cgit_object_link(struct object *obj) 329void cgit_object_link(struct object *obj)
319{ 330{
320 char *page, *arg, *url; 331 char *page, *arg, *url;
321 332
322 if (obj->type == OBJ_COMMIT) { 333 if (obj->type == OBJ_COMMIT) {
323 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 334 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
324 ctx.qry.head, sha1_to_hex(obj->sha1)); 335 ctx.qry.head, sha1_to_hex(obj->sha1));
325 return; 336 return;
326 } else if (obj->type == OBJ_TREE) { 337 } else if (obj->type == OBJ_TREE) {
327 page = "tree"; 338 page = "tree";
328 arg = "id"; 339 arg = "id";
329 } else if (obj->type == OBJ_TAG) { 340 } else if (obj->type == OBJ_TAG) {
330 page = "tag"; 341 page = "tag";
331 arg = "id"; 342 arg = "id";
332 } else { 343 } else {
333 page = "blob"; 344 page = "blob";
334 arg = "id"; 345 arg = "id";
335 } 346 }
336 347
337 url = cgit_pageurl(ctx.qry.repo, page, 348 url = cgit_pageurl(ctx.qry.repo, page,
338 fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); 349 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
339 html_link_open(url, NULL, NULL); 350 html_link_open(url, NULL, NULL);
340 htmlf("%s %s", typename(obj->type), 351 htmlf("%s %s", typename(obj->type),
341 sha1_to_hex(obj->sha1)); 352 sha1_to_hex(obj->sha1));
342 html_link_close(); 353 html_link_close();
343} 354}
344 355
345void cgit_print_date(time_t secs, char *format) 356void cgit_print_date(time_t secs, char *format)
346{ 357{
347 char buf[64]; 358 char buf[64];
348 struct tm *time; 359 struct tm *time;
349 360
350 if (!secs) 361 if (!secs)
351 return; 362 return;
352 time = gmtime(&secs); 363 time = gmtime(&secs);
353 strftime(buf, sizeof(buf)-1, format, time); 364 strftime(buf, sizeof(buf)-1, format, time);
354 html_txt(buf); 365 html_txt(buf);
355} 366}
356 367
357void cgit_print_age(time_t t, time_t max_relative, char *format) 368void cgit_print_age(time_t t, time_t max_relative, char *format)
358{ 369{
359 time_t now, secs; 370 time_t now, secs;
360 371
361 if (!t) 372 if (!t)
362 return; 373 return;
363 time(&now); 374 time(&now);
364 secs = now - t; 375 secs = now - t;
365 376
366 if (secs > max_relative && max_relative >= 0) { 377 if (secs > max_relative && max_relative >= 0) {
367 cgit_print_date(t, format); 378 cgit_print_date(t, format);
368 return; 379 return;
369 } 380 }
370 381
371 if (secs < TM_HOUR * 2) { 382 if (secs < TM_HOUR * 2) {
372 htmlf("<span class='age-mins'>%.0f min.</span>", 383 htmlf("<span class='age-mins'>%.0f min.</span>",
373 secs * 1.0 / TM_MIN); 384 secs * 1.0 / TM_MIN);
374 return; 385 return;
375 } 386 }
376 if (secs < TM_DAY * 2) { 387 if (secs < TM_DAY * 2) {
377 htmlf("<span class='age-hours'>%.0f hours</span>", 388 htmlf("<span class='age-hours'>%.0f hours</span>",
378 secs * 1.0 / TM_HOUR); 389 secs * 1.0 / TM_HOUR);
379 return; 390 return;
380 } 391 }
381 if (secs < TM_WEEK * 2) { 392 if (secs < TM_WEEK * 2) {
382 htmlf("<span class='age-days'>%.0f days</span>", 393 htmlf("<span class='age-days'>%.0f days</span>",
383 secs * 1.0 / TM_DAY); 394 secs * 1.0 / TM_DAY);
384 return; 395 return;
385 } 396 }
386 if (secs < TM_MONTH * 2) { 397 if (secs < TM_MONTH * 2) {
387 htmlf("<span class='age-weeks'>%.0f weeks</span>", 398 htmlf("<span class='age-weeks'>%.0f weeks</span>",
388 secs * 1.0 / TM_WEEK); 399 secs * 1.0 / TM_WEEK);
389 return; 400 return;
390 } 401 }
391 if (secs < TM_YEAR * 2) { 402 if (secs < TM_YEAR * 2) {
392 htmlf("<span class='age-months'>%.0f months</span>", 403 htmlf("<span class='age-months'>%.0f months</span>",
393 secs * 1.0 / TM_MONTH); 404 secs * 1.0 / TM_MONTH);
394 return; 405 return;
395 } 406 }
396 htmlf("<span class='age-years'>%.0f years</span>", 407 htmlf("<span class='age-years'>%.0f years</span>",
397 secs * 1.0 / TM_YEAR); 408 secs * 1.0 / TM_YEAR);
398} 409}
399 410
400void cgit_print_http_headers(struct cgit_context *ctx) 411void cgit_print_http_headers(struct cgit_context *ctx)
401{ 412{
402 if (ctx->page.mimetype && ctx->page.charset) 413 if (ctx->page.mimetype && ctx->page.charset)
403 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, 414 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
404 ctx->page.charset); 415 ctx->page.charset);
405 else if (ctx->page.mimetype) 416 else if (ctx->page.mimetype)
406 htmlf("Content-Type: %s\n", ctx->page.mimetype); 417 htmlf("Content-Type: %s\n", ctx->page.mimetype);
407 if (ctx->page.filename) 418 if (ctx->page.filename)
408 htmlf("Content-Disposition: inline; filename=\"%s\"\n", 419 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
409 ctx->page.filename); 420 ctx->page.filename);
410 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); 421 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
411 htmlf("Expires: %s\n", http_date(ctx->page.expires)); 422 htmlf("Expires: %s\n", http_date(ctx->page.expires));
412 html("\n"); 423 html("\n");
413} 424}
414 425
415void cgit_print_docstart(struct cgit_context *ctx) 426void cgit_print_docstart(struct cgit_context *ctx)
416{ 427{
417 html(cgit_doctype); 428 html(cgit_doctype);
418 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 429 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
419 html("<head>\n"); 430 html("<head>\n");
420 html("<title>"); 431 html("<title>");
421 html_txt(ctx->page.title); 432 html_txt(ctx->page.title);
422 html("</title>\n"); 433 html("</title>\n");
423 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 434 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
424 if (ctx->cfg.robots && *ctx->cfg.robots) 435 if (ctx->cfg.robots && *ctx->cfg.robots)
425 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 436 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
426 html("<link rel='stylesheet' type='text/css' href='"); 437 html("<link rel='stylesheet' type='text/css' href='");
427 html_attr(ctx->cfg.css); 438 html_attr(ctx->cfg.css);
428 html("'/>\n"); 439 html("'/>\n");
429 html("</head>\n"); 440 html("</head>\n");
430 html("<body>\n"); 441 html("<body>\n");
431} 442}
432 443
433void cgit_print_docend() 444void cgit_print_docend()
434{ 445{
435 html("</div>\n</body>\n</html>\n"); 446 html("</div>\n</body>\n</html>\n");
436} 447}
437 448
438int print_branch_option(const char *refname, const unsigned char *sha1, 449int print_branch_option(const char *refname, const unsigned char *sha1,
439 int flags, void *cb_data) 450 int flags, void *cb_data)
440{ 451{
441 char *name = (char *)refname; 452 char *name = (char *)refname;
442 html_option(name, name, ctx.qry.head); 453 html_option(name, name, ctx.qry.head);
443 return 0; 454 return 0;
444} 455}
445 456
446int print_archive_ref(const char *refname, const unsigned char *sha1, 457int print_archive_ref(const char *refname, const unsigned char *sha1,
447 int flags, void *cb_data) 458 int flags, void *cb_data)
448{ 459{
449 struct tag *tag; 460 struct tag *tag;
450 struct taginfo *info; 461 struct taginfo *info;
451 struct object *obj; 462 struct object *obj;
452 char buf[256], *url; 463 char buf[256], *url;
453 unsigned char fileid[20]; 464 unsigned char fileid[20];
454 int *header = (int *)cb_data; 465 int *header = (int *)cb_data;
455 466
456 if (prefixcmp(refname, "refs/archives")) 467 if (prefixcmp(refname, "refs/archives"))
457 return 0; 468 return 0;
458 strncpy(buf, refname+14, sizeof(buf)); 469 strncpy(buf, refname+14, sizeof(buf));
459 obj = parse_object(sha1); 470 obj = parse_object(sha1);
460 if (!obj) 471 if (!obj)
461 return 1; 472 return 1;
462 if (obj->type == OBJ_TAG) { 473 if (obj->type == OBJ_TAG) {
463 tag = lookup_tag(sha1); 474 tag = lookup_tag(sha1);
464 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 475 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
465 return 0; 476 return 0;
466 hashcpy(fileid, tag->tagged->sha1); 477 hashcpy(fileid, tag->tagged->sha1);
467 } else if (obj->type != OBJ_BLOB) { 478 } else if (obj->type != OBJ_BLOB) {
468 return 0; 479 return 0;
469 } else { 480 } else {
470 hashcpy(fileid, sha1); 481 hashcpy(fileid, sha1);
471 } 482 }
472 if (!*header) { 483 if (!*header) {
473 html("<h1>download</h1>\n"); 484 html("<h1>download</h1>\n");
474 *header = 1; 485 *header = 1;
475 } 486 }
476 url = cgit_pageurl(ctx.qry.repo, "blob", 487 url = cgit_pageurl(ctx.qry.repo, "blob",
477 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 488 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
478 buf)); 489 buf));
479 html_link_open(url, NULL, "menu"); 490 html_link_open(url, NULL, "menu");
480 html_txt(strlpart(buf, 20)); 491 html_txt(strlpart(buf, 20));
481 html_link_close(); 492 html_link_close();
482 return 0; 493 return 0;
483} 494}
484 495
485void add_hidden_formfields(int incl_head, int incl_search, char *page) 496void add_hidden_formfields(int incl_head, int incl_search, char *page)
486{ 497{
487 char *url; 498 char *url;
488 499
489 if (!ctx.cfg.virtual_root) { 500 if (!ctx.cfg.virtual_root) {
490 url = fmt("%s/%s", ctx.qry.repo, page); 501 url = fmt("%s/%s", ctx.qry.repo, page);
491 if (ctx.qry.path) 502 if (ctx.qry.path)
492 url = fmt("%s/%s", url, ctx.qry.path); 503 url = fmt("%s/%s", url, ctx.qry.path);
493 html_hidden("url", url); 504 html_hidden("url", url);
494 } 505 }
495 506
496 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) 507 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
497 html_hidden("h", ctx.qry.head); 508 html_hidden("h", ctx.qry.head);
498 509
499 if (ctx.qry.sha1) 510 if (ctx.qry.sha1)
500 html_hidden("id", ctx.qry.sha1); 511 html_hidden("id", ctx.qry.sha1);
501 if (ctx.qry.sha2) 512 if (ctx.qry.sha2)
502 html_hidden("id2", ctx.qry.sha2); 513 html_hidden("id2", ctx.qry.sha2);
503 514
504 if (incl_search) { 515 if (incl_search) {
505 if (ctx.qry.grep) 516 if (ctx.qry.grep)
506 html_hidden("qt", ctx.qry.grep); 517 html_hidden("qt", ctx.qry.grep);
507 if (ctx.qry.search) 518 if (ctx.qry.search)
508 html_hidden("q", ctx.qry.search); 519 html_hidden("q", ctx.qry.search);
509 } 520 }
510} 521}
511 522
512char *hc(struct cgit_cmd *cmd, const char *page) 523char *hc(struct cgit_cmd *cmd, const char *page)
513{ 524{
514 return (strcmp(cmd->name, page) ? NULL : "active"); 525 return (strcmp(cmd->name, page) ? NULL : "active");
515} 526}
516 527
517void cgit_print_pageheader(struct cgit_context *ctx) 528void cgit_print_pageheader(struct cgit_context *ctx)
518{ 529{
519 struct cgit_cmd *cmd = cgit_get_cmd(ctx); 530 struct cgit_cmd *cmd = cgit_get_cmd(ctx);
520 531
521 html("<table id='header'>\n"); 532 html("<table id='header'>\n");
522 html("<tr>\n"); 533 html("<tr>\n");
523 html("<td class='logo' rowspan='2'><a href='"); 534 html("<td class='logo' rowspan='2'><a href='");
524 if (ctx->cfg.logo_link) 535 if (ctx->cfg.logo_link)
525 html_attr(ctx->cfg.logo_link); 536 html_attr(ctx->cfg.logo_link);
526 else 537 else
527 html_attr(cgit_rooturl()); 538 html_attr(cgit_rooturl());
528 html("'><img src='"); 539 html("'><img src='");
529 html_attr(ctx->cfg.logo); 540 html_attr(ctx->cfg.logo);
530 html("' alt='cgit logo'/></a></td>\n"); 541 html("' alt='cgit logo'/></a></td>\n");
531 542
532 html("<td class='main'>"); 543 html("<td class='main'>");
533 if (ctx->repo) { 544 if (ctx->repo) {
534 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"), 545 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"),
535 ctx->qry.head, NULL, NULL); 546 ctx->qry.head, NULL, NULL);
536 html(" : "); 547 html(" : ");
537 html_txt(ctx->qry.page); 548 html_txt(ctx->qry.page);
538 html("</td><td class='form'>"); 549 html("</td><td class='form'>");
539 html("<form method='get' action=''>\n"); 550 html("<form method='get' action=''>\n");
540 add_hidden_formfields(0, 1, ctx->qry.page); 551 add_hidden_formfields(0, 1, ctx->qry.page);
541 html("<select name='h' onchange='this.form.submit();'>\n"); 552 html("<select name='h' onchange='this.form.submit();'>\n");
542 for_each_branch_ref(print_branch_option, ctx->qry.head); 553 for_each_branch_ref(print_branch_option, ctx->qry.head);
543 html("</select> "); 554 html("</select> ");
544 html("<input type='submit' name='' value='switch'/>"); 555 html("<input type='submit' name='' value='switch'/>");
545 html("</form>"); 556 html("</form>");
546 } else 557 } else
547 html_txt(ctx->cfg.root_title); 558 html_txt(ctx->cfg.root_title);
548 html("</td></tr>\n"); 559 html("</td></tr>\n");
549 560
550 html("<tr><td class='sub'"); 561 html("<tr><td class='sub'");
551 if (ctx->repo) { 562 if (ctx->repo) {
552 html(" colspan='2'>"); 563 html(" colspan='2'>");
553 html_txt(ctx->repo->desc); 564 html_txt(ctx->repo->desc);
554 } else { 565 } else {
555 html(">"); 566 html(">");
556 if (ctx->cfg.root_desc) 567 if (ctx->cfg.root_desc)
557 html_txt(ctx->cfg.root_desc); 568 html_txt(ctx->cfg.root_desc);
558 else if (ctx->cfg.index_info) 569 else if (ctx->cfg.index_info)
559 html_include(ctx->cfg.index_info); 570 html_include(ctx->cfg.index_info);
560 } 571 }
561 html("</td></tr></table>\n"); 572 html("</td></tr></table>\n");
562 573
563 html("<table class='tabs'><tr><td>\n"); 574 html("<table class='tabs'><tr><td>\n");
564 if (ctx->repo) { 575 if (ctx->repo) {
565 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"), 576 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"),
566 ctx->qry.head, NULL, NULL); 577 ctx->qry.head, NULL, NULL);
567 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head, 578 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,
568 ctx->qry.sha1, NULL); 579 ctx->qry.sha1, NULL);
569 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head, 580 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head,
570 NULL, NULL, 0, NULL, NULL); 581 NULL, NULL, 0, NULL, NULL);
571 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head, 582 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,
572 ctx->qry.sha1, NULL); 583 ctx->qry.sha1, NULL);
573 cgit_commit_link("commit", NULL, hc(cmd, "commit"), 584 cgit_commit_link("commit", NULL, hc(cmd, "commit"),
574 ctx->qry.head, ctx->qry.sha1); 585 ctx->qry.head, ctx->qry.sha1);
575 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head, 586 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,
576 ctx->qry.sha1, ctx->qry.sha2, NULL); 587 ctx->qry.sha1, ctx->qry.sha2, NULL);
577 if (ctx->repo->readme) 588 if (ctx->repo->readme)
578 reporevlink("about", "about", NULL, 589 reporevlink("about", "about", NULL,
579 hc(cmd, "about"), ctx->qry.head, NULL, 590 hc(cmd, "about"), ctx->qry.head, NULL,
580 NULL); 591 NULL);
581 html("</td><td class='form'>"); 592 html("</td><td class='form'>");
582 html("<form class='right' method='get' action='"); 593 html("<form class='right' method='get' action='");
583 if (ctx->cfg.virtual_root) 594 if (ctx->cfg.virtual_root)
584 html_attr(cgit_fileurl(ctx->qry.repo, "log", 595 html_attr(cgit_fileurl(ctx->qry.repo, "log",
585 ctx->qry.path, NULL)); 596 ctx->qry.path, NULL));
586 html("'>\n"); 597 html("'>\n");
587 add_hidden_formfields(1, 0, "log"); 598 add_hidden_formfields(1, 0, "log");
588 html("<select name='qt'>\n"); 599 html("<select name='qt'>\n");
589 html_option("grep", "log msg", ctx->qry.grep); 600 html_option("grep", "log msg", ctx->qry.grep);
590 html_option("author", "author", ctx->qry.grep); 601 html_option("author", "author", ctx->qry.grep);
591 html_option("committer", "committer", ctx->qry.grep); 602 html_option("committer", "committer", ctx->qry.grep);
592 html("</select>\n"); 603 html("</select>\n");
593 html("<input class='txt' type='text' size='10' name='q' value='"); 604 html("<input class='txt' type='text' size='10' name='q' value='");
594 html_attr(ctx->qry.search); 605 html_attr(ctx->qry.search);
595 html("'/>\n"); 606 html("'/>\n");
596 html("<input type='submit' value='search'/>\n"); 607 html("<input type='submit' value='search'/>\n");
597 html("</form>\n"); 608 html("</form>\n");
598 } else { 609 } else {
599 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL); 610 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0);
600 if (ctx->cfg.root_readme) 611 if (ctx->cfg.root_readme)
601 site_link("about", "about", NULL, hc(cmd, "about"), NULL); 612 site_link("about", "about", NULL, hc(cmd, "about"),
613 NULL, 0);
602 html("</td><td class='form'>"); 614 html("</td><td class='form'>");
603 html("<form method='get' action='"); 615 html("<form method='get' action='");
604 html_attr(cgit_rooturl()); 616 html_attr(cgit_rooturl());
605 html("'>\n"); 617 html("'>\n");
606 html("<input type='text' name='q' size='10' value='"); 618 html("<input type='text' name='q' size='10' value='");
607 html_attr(ctx->qry.search); 619 html_attr(ctx->qry.search);
608 html("'/>\n"); 620 html("'/>\n");
609 html("<input type='submit' value='search'/>\n"); 621 html("<input type='submit' value='search'/>\n");
610 html("</form>"); 622 html("</form>");
611 } 623 }
612 html("</td></tr></table>\n"); 624 html("</td></tr></table>\n");
613 html("<div class='content'>"); 625 html("<div class='content'>");
614} 626}
615 627
616void cgit_print_filemode(unsigned short mode) 628void cgit_print_filemode(unsigned short mode)
617{ 629{
618 if (S_ISDIR(mode)) 630 if (S_ISDIR(mode))
619 html("d"); 631 html("d");
620 else if (S_ISLNK(mode)) 632 else if (S_ISLNK(mode))
621 html("l"); 633 html("l");
622 else if (S_ISGITLINK(mode)) 634 else if (S_ISGITLINK(mode))
623 html("m"); 635 html("m");
624 else 636 else
625 html("-"); 637 html("-");
626 html_fileperm(mode >> 6); 638 html_fileperm(mode >> 6);
627 html_fileperm(mode >> 3); 639 html_fileperm(mode >> 3);
628 html_fileperm(mode); 640 html_fileperm(mode);
629} 641}
630 642
631void cgit_print_snapshot_links(const char *repo, const char *head, 643void cgit_print_snapshot_links(const char *repo, const char *head,
632 const char *hex, int snapshots) 644 const char *hex, int snapshots)
633{ 645{
634 const struct cgit_snapshot_format* f; 646 const struct cgit_snapshot_format* f;
635 char *filename; 647 char *filename;
636 648
637 for (f = cgit_snapshot_formats; f->suffix; f++) { 649 for (f = cgit_snapshot_formats; f->suffix; f++) {
638 if (!(snapshots & f->bit)) 650 if (!(snapshots & f->bit))
639 continue; 651 continue;
640 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 652 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
641 f->suffix); 653 f->suffix);
642 cgit_snapshot_link(filename, NULL, NULL, (char *)head, 654 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
643 (char *)hex, filename); 655 (char *)hex, filename);
644 html("<br/>"); 656 html("<br/>");
645 } 657 }
646} 658}