summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2008-04-28 22:55:34 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-04-28 22:55:34 (UTC)
commit4c9916043d6aabb8278e0d78f5fa71a59f82889d (patch) (unidiff)
tree02259d3ef9c31379c2733d1944412a9015e2f311
parent8062817885495b07cf70304c484e8f18c78ab2b5 (diff)
downloadcgit-4c9916043d6aabb8278e0d78f5fa71a59f82889d.zip
cgit-4c9916043d6aabb8278e0d78f5fa71a59f82889d.tar.gz
cgit-4c9916043d6aabb8278e0d78f5fa71a59f82889d.tar.bz2
Re-enable 'index-info' and add support for 'root-desc' in cgitrc
The 'index-info' option got lost when the layout was converted from sidebar to old-fashioned header (noticed by Harley Laue, thanks!), and this commit re-enables it. But there is now also an alternative in the 'root-desc' option; where 'index-info' specifies a file to include, 'root-desc' specifies the text literally. This might be nicer for the one-liner descriptions which these options typically provides. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c3
-rw-r--r--cgit.h1
-rw-r--r--ui-shared.c5
3 files changed, 8 insertions, 1 deletions
diff --git a/cgit.c b/cgit.c
index 38b0ba5..bbde64b 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,461 +1,464 @@
1/* cgit.c: cgi for the git scm 1/* cgit.c: cgi for the git scm
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 "cache.h" 10#include "cache.h"
11#include "cmd.h" 11#include "cmd.h"
12#include "configfile.h" 12#include "configfile.h"
13#include "html.h" 13#include "html.h"
14#include "ui-shared.h" 14#include "ui-shared.h"
15 15
16const char *cgit_version = CGIT_VERSION; 16const char *cgit_version = CGIT_VERSION;
17 17
18void config_cb(const char *name, const char *value) 18void config_cb(const char *name, const char *value)
19{ 19{
20 if (!strcmp(name, "root-title")) 20 if (!strcmp(name, "root-title"))
21 ctx.cfg.root_title = xstrdup(value); 21 ctx.cfg.root_title = xstrdup(value);
22 else if (!strcmp(name, "root-desc"))
23 ctx.cfg.root_desc = xstrdup(value);
22 else if (!strcmp(name, "css")) 24 else if (!strcmp(name, "css"))
23 ctx.cfg.css = xstrdup(value); 25 ctx.cfg.css = xstrdup(value);
24 else if (!strcmp(name, "logo")) 26 else if (!strcmp(name, "logo"))
25 ctx.cfg.logo = xstrdup(value); 27 ctx.cfg.logo = xstrdup(value);
26 else if (!strcmp(name, "index-header")) 28 else if (!strcmp(name, "index-header"))
27 ctx.cfg.index_header = xstrdup(value); 29 ctx.cfg.index_header = xstrdup(value);
28 else if (!strcmp(name, "index-info")) 30 else if (!strcmp(name, "index-info"))
29 ctx.cfg.index_info = xstrdup(value); 31 ctx.cfg.index_info = xstrdup(value);
30 else if (!strcmp(name, "logo-link")) 32 else if (!strcmp(name, "logo-link"))
31 ctx.cfg.logo_link = xstrdup(value); 33 ctx.cfg.logo_link = xstrdup(value);
32 else if (!strcmp(name, "module-link")) 34 else if (!strcmp(name, "module-link"))
33 ctx.cfg.module_link = xstrdup(value); 35 ctx.cfg.module_link = xstrdup(value);
34 else if (!strcmp(name, "virtual-root")) { 36 else if (!strcmp(name, "virtual-root")) {
35 ctx.cfg.virtual_root = trim_end(value, '/'); 37 ctx.cfg.virtual_root = trim_end(value, '/');
36 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 38 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
37 ctx.cfg.virtual_root = ""; 39 ctx.cfg.virtual_root = "";
38 } else if (!strcmp(name, "nocache")) 40 } else if (!strcmp(name, "nocache"))
39 ctx.cfg.nocache = atoi(value); 41 ctx.cfg.nocache = atoi(value);
40 else if (!strcmp(name, "snapshots")) 42 else if (!strcmp(name, "snapshots"))
41 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 43 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
42 else if (!strcmp(name, "enable-index-links")) 44 else if (!strcmp(name, "enable-index-links"))
43 ctx.cfg.enable_index_links = atoi(value); 45 ctx.cfg.enable_index_links = atoi(value);
44 else if (!strcmp(name, "enable-log-filecount")) 46 else if (!strcmp(name, "enable-log-filecount"))
45 ctx.cfg.enable_log_filecount = atoi(value); 47 ctx.cfg.enable_log_filecount = atoi(value);
46 else if (!strcmp(name, "enable-log-linecount")) 48 else if (!strcmp(name, "enable-log-linecount"))
47 ctx.cfg.enable_log_linecount = atoi(value); 49 ctx.cfg.enable_log_linecount = atoi(value);
48 else if (!strcmp(name, "cache-root")) 50 else if (!strcmp(name, "cache-root"))
49 ctx.cfg.cache_root = xstrdup(value); 51 ctx.cfg.cache_root = xstrdup(value);
50 else if (!strcmp(name, "cache-root-ttl")) 52 else if (!strcmp(name, "cache-root-ttl"))
51 ctx.cfg.cache_root_ttl = atoi(value); 53 ctx.cfg.cache_root_ttl = atoi(value);
52 else if (!strcmp(name, "cache-repo-ttl")) 54 else if (!strcmp(name, "cache-repo-ttl"))
53 ctx.cfg.cache_repo_ttl = atoi(value); 55 ctx.cfg.cache_repo_ttl = atoi(value);
54 else if (!strcmp(name, "cache-static-ttl")) 56 else if (!strcmp(name, "cache-static-ttl"))
55 ctx.cfg.cache_static_ttl = atoi(value); 57 ctx.cfg.cache_static_ttl = atoi(value);
56 else if (!strcmp(name, "cache-dynamic-ttl")) 58 else if (!strcmp(name, "cache-dynamic-ttl"))
57 ctx.cfg.cache_dynamic_ttl = atoi(value); 59 ctx.cfg.cache_dynamic_ttl = atoi(value);
58 else if (!strcmp(name, "max-message-length")) 60 else if (!strcmp(name, "max-message-length"))
59 ctx.cfg.max_msg_len = atoi(value); 61 ctx.cfg.max_msg_len = atoi(value);
60 else if (!strcmp(name, "max-repodesc-length")) 62 else if (!strcmp(name, "max-repodesc-length"))
61 ctx.cfg.max_repodesc_len = atoi(value); 63 ctx.cfg.max_repodesc_len = atoi(value);
62 else if (!strcmp(name, "max-commit-count")) 64 else if (!strcmp(name, "max-commit-count"))
63 ctx.cfg.max_commit_count = atoi(value); 65 ctx.cfg.max_commit_count = atoi(value);
64 else if (!strcmp(name, "summary-log")) 66 else if (!strcmp(name, "summary-log"))
65 ctx.cfg.summary_log = atoi(value); 67 ctx.cfg.summary_log = atoi(value);
66 else if (!strcmp(name, "summary-branches")) 68 else if (!strcmp(name, "summary-branches"))
67 ctx.cfg.summary_branches = atoi(value); 69 ctx.cfg.summary_branches = atoi(value);
68 else if (!strcmp(name, "summary-tags")) 70 else if (!strcmp(name, "summary-tags"))
69 ctx.cfg.summary_tags = atoi(value); 71 ctx.cfg.summary_tags = atoi(value);
70 else if (!strcmp(name, "agefile")) 72 else if (!strcmp(name, "agefile"))
71 ctx.cfg.agefile = xstrdup(value); 73 ctx.cfg.agefile = xstrdup(value);
72 else if (!strcmp(name, "renamelimit")) 74 else if (!strcmp(name, "renamelimit"))
73 ctx.cfg.renamelimit = atoi(value); 75 ctx.cfg.renamelimit = atoi(value);
74 else if (!strcmp(name, "robots")) 76 else if (!strcmp(name, "robots"))
75 ctx.cfg.robots = xstrdup(value); 77 ctx.cfg.robots = xstrdup(value);
76 else if (!strcmp(name, "clone-prefix")) 78 else if (!strcmp(name, "clone-prefix"))
77 ctx.cfg.clone_prefix = xstrdup(value); 79 ctx.cfg.clone_prefix = xstrdup(value);
78 else if (!strcmp(name, "repo.group")) 80 else if (!strcmp(name, "repo.group"))
79 ctx.cfg.repo_group = xstrdup(value); 81 ctx.cfg.repo_group = xstrdup(value);
80 else if (!strcmp(name, "repo.url")) 82 else if (!strcmp(name, "repo.url"))
81 ctx.repo = cgit_add_repo(value); 83 ctx.repo = cgit_add_repo(value);
82 else if (!strcmp(name, "repo.name")) 84 else if (!strcmp(name, "repo.name"))
83 ctx.repo->name = xstrdup(value); 85 ctx.repo->name = xstrdup(value);
84 else if (ctx.repo && !strcmp(name, "repo.path")) 86 else if (ctx.repo && !strcmp(name, "repo.path"))
85 ctx.repo->path = trim_end(value, '/'); 87 ctx.repo->path = trim_end(value, '/');
86 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 88 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
87 ctx.repo->clone_url = xstrdup(value); 89 ctx.repo->clone_url = xstrdup(value);
88 else if (ctx.repo && !strcmp(name, "repo.desc")) 90 else if (ctx.repo && !strcmp(name, "repo.desc"))
89 ctx.repo->desc = xstrdup(value); 91 ctx.repo->desc = xstrdup(value);
90 else if (ctx.repo && !strcmp(name, "repo.owner")) 92 else if (ctx.repo && !strcmp(name, "repo.owner"))
91 ctx.repo->owner = xstrdup(value); 93 ctx.repo->owner = xstrdup(value);
92 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 94 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
93 ctx.repo->defbranch = xstrdup(value); 95 ctx.repo->defbranch = xstrdup(value);
94 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 96 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
95 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 97 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
96 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 98 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
97 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 99 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
98 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 100 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
99 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 101 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
100 else if (ctx.repo && !strcmp(name, "repo.module-link")) 102 else if (ctx.repo && !strcmp(name, "repo.module-link"))
101 ctx.repo->module_link= xstrdup(value); 103 ctx.repo->module_link= xstrdup(value);
102 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 104 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
103 if (*value == '/') 105 if (*value == '/')
104 ctx.repo->readme = xstrdup(value); 106 ctx.repo->readme = xstrdup(value);
105 else 107 else
106 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 108 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
107 } else if (!strcmp(name, "include")) 109 } else if (!strcmp(name, "include"))
108 parse_configfile(value, config_cb); 110 parse_configfile(value, config_cb);
109} 111}
110 112
111static void querystring_cb(const char *name, const char *value) 113static void querystring_cb(const char *name, const char *value)
112{ 114{
113 if (!strcmp(name,"r")) { 115 if (!strcmp(name,"r")) {
114 ctx.qry.repo = xstrdup(value); 116 ctx.qry.repo = xstrdup(value);
115 ctx.repo = cgit_get_repoinfo(value); 117 ctx.repo = cgit_get_repoinfo(value);
116 } else if (!strcmp(name, "p")) { 118 } else if (!strcmp(name, "p")) {
117 ctx.qry.page = xstrdup(value); 119 ctx.qry.page = xstrdup(value);
118 } else if (!strcmp(name, "url")) { 120 } else if (!strcmp(name, "url")) {
119 cgit_parse_url(value); 121 cgit_parse_url(value);
120 } else if (!strcmp(name, "qt")) { 122 } else if (!strcmp(name, "qt")) {
121 ctx.qry.grep = xstrdup(value); 123 ctx.qry.grep = xstrdup(value);
122 } else if (!strcmp(name, "q")) { 124 } else if (!strcmp(name, "q")) {
123 ctx.qry.search = xstrdup(value); 125 ctx.qry.search = xstrdup(value);
124 } else if (!strcmp(name, "h")) { 126 } else if (!strcmp(name, "h")) {
125 ctx.qry.head = xstrdup(value); 127 ctx.qry.head = xstrdup(value);
126 ctx.qry.has_symref = 1; 128 ctx.qry.has_symref = 1;
127 } else if (!strcmp(name, "id")) { 129 } else if (!strcmp(name, "id")) {
128 ctx.qry.sha1 = xstrdup(value); 130 ctx.qry.sha1 = xstrdup(value);
129 ctx.qry.has_sha1 = 1; 131 ctx.qry.has_sha1 = 1;
130 } else if (!strcmp(name, "id2")) { 132 } else if (!strcmp(name, "id2")) {
131 ctx.qry.sha2 = xstrdup(value); 133 ctx.qry.sha2 = xstrdup(value);
132 ctx.qry.has_sha1 = 1; 134 ctx.qry.has_sha1 = 1;
133 } else if (!strcmp(name, "ofs")) { 135 } else if (!strcmp(name, "ofs")) {
134 ctx.qry.ofs = atoi(value); 136 ctx.qry.ofs = atoi(value);
135 } else if (!strcmp(name, "path")) { 137 } else if (!strcmp(name, "path")) {
136 ctx.qry.path = trim_end(value, '/'); 138 ctx.qry.path = trim_end(value, '/');
137 } else if (!strcmp(name, "name")) { 139 } else if (!strcmp(name, "name")) {
138 ctx.qry.name = xstrdup(value); 140 ctx.qry.name = xstrdup(value);
139 } 141 }
140} 142}
141 143
142static void prepare_context(struct cgit_context *ctx) 144static void prepare_context(struct cgit_context *ctx)
143{ 145{
144 memset(ctx, 0, sizeof(ctx)); 146 memset(ctx, 0, sizeof(ctx));
145 ctx->cfg.agefile = "info/web/last-modified"; 147 ctx->cfg.agefile = "info/web/last-modified";
146 ctx->cfg.cache_dynamic_ttl = 5; 148 ctx->cfg.cache_dynamic_ttl = 5;
147 ctx->cfg.cache_max_create_time = 5; 149 ctx->cfg.cache_max_create_time = 5;
148 ctx->cfg.cache_repo_ttl = 5; 150 ctx->cfg.cache_repo_ttl = 5;
149 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 151 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
150 ctx->cfg.cache_root_ttl = 5; 152 ctx->cfg.cache_root_ttl = 5;
151 ctx->cfg.cache_static_ttl = -1; 153 ctx->cfg.cache_static_ttl = -1;
152 ctx->cfg.css = "/cgit.css"; 154 ctx->cfg.css = "/cgit.css";
153 ctx->cfg.logo = "/git-logo.png"; 155 ctx->cfg.logo = "/git-logo.png";
154 ctx->cfg.max_commit_count = 50; 156 ctx->cfg.max_commit_count = 50;
155 ctx->cfg.max_lock_attempts = 5; 157 ctx->cfg.max_lock_attempts = 5;
156 ctx->cfg.max_msg_len = 60; 158 ctx->cfg.max_msg_len = 60;
157 ctx->cfg.max_repodesc_len = 60; 159 ctx->cfg.max_repodesc_len = 60;
158 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 160 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
159 ctx->cfg.renamelimit = -1; 161 ctx->cfg.renamelimit = -1;
160 ctx->cfg.robots = "index, nofollow"; 162 ctx->cfg.robots = "index, nofollow";
161 ctx->cfg.root_title = "Git repository browser"; 163 ctx->cfg.root_title = "Git repository browser";
164 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
162 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 165 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
163 ctx->page.mimetype = "text/html"; 166 ctx->page.mimetype = "text/html";
164 ctx->page.charset = PAGE_ENCODING; 167 ctx->page.charset = PAGE_ENCODING;
165 ctx->page.filename = NULL; 168 ctx->page.filename = NULL;
166} 169}
167 170
168static int cgit_prepare_cache(struct cacheitem *item) 171static int cgit_prepare_cache(struct cacheitem *item)
169{ 172{
170 if (!ctx.repo && ctx.qry.repo) { 173 if (!ctx.repo && ctx.qry.repo) {
171 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, 174 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
172 "Bad request"); 175 "Bad request");
173 cgit_print_http_headers(&ctx); 176 cgit_print_http_headers(&ctx);
174 cgit_print_docstart(&ctx); 177 cgit_print_docstart(&ctx);
175 cgit_print_pageheader(&ctx); 178 cgit_print_pageheader(&ctx);
176 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); 179 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
177 cgit_print_docend(); 180 cgit_print_docend();
178 return 0; 181 return 0;
179 } 182 }
180 183
181 if (!ctx.repo) { 184 if (!ctx.repo) {
182 item->name = xstrdup(fmt("%s/index.%s.html", 185 item->name = xstrdup(fmt("%s/index.%s.html",
183 ctx.cfg.cache_root, 186 ctx.cfg.cache_root,
184 cache_safe_filename(ctx.qry.raw))); 187 cache_safe_filename(ctx.qry.raw)));
185 item->ttl = ctx.cfg.cache_root_ttl; 188 item->ttl = ctx.cfg.cache_root_ttl;
186 return 1; 189 return 1;
187 } 190 }
188 191
189 if (!ctx.qry.page) { 192 if (!ctx.qry.page) {
190 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, 193 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
191 cache_safe_filename(ctx.repo->url), 194 cache_safe_filename(ctx.repo->url),
192 cache_safe_filename(ctx.qry.raw))); 195 cache_safe_filename(ctx.qry.raw)));
193 item->ttl = ctx.cfg.cache_repo_ttl; 196 item->ttl = ctx.cfg.cache_repo_ttl;
194 } else { 197 } else {
195 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, 198 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
196 cache_safe_filename(ctx.repo->url), 199 cache_safe_filename(ctx.repo->url),
197 ctx.qry.page, 200 ctx.qry.page,
198 cache_safe_filename(ctx.qry.raw))); 201 cache_safe_filename(ctx.qry.raw)));
199 if (ctx.qry.has_symref) 202 if (ctx.qry.has_symref)
200 item->ttl = ctx.cfg.cache_dynamic_ttl; 203 item->ttl = ctx.cfg.cache_dynamic_ttl;
201 else if (ctx.qry.has_sha1) 204 else if (ctx.qry.has_sha1)
202 item->ttl = ctx.cfg.cache_static_ttl; 205 item->ttl = ctx.cfg.cache_static_ttl;
203 else 206 else
204 item->ttl = ctx.cfg.cache_repo_ttl; 207 item->ttl = ctx.cfg.cache_repo_ttl;
205 } 208 }
206 return 1; 209 return 1;
207} 210}
208 211
209struct refmatch { 212struct refmatch {
210 char *req_ref; 213 char *req_ref;
211 char *first_ref; 214 char *first_ref;
212 int match; 215 int match;
213}; 216};
214 217
215int find_current_ref(const char *refname, const unsigned char *sha1, 218int find_current_ref(const char *refname, const unsigned char *sha1,
216 int flags, void *cb_data) 219 int flags, void *cb_data)
217{ 220{
218 struct refmatch *info; 221 struct refmatch *info;
219 222
220 info = (struct refmatch *)cb_data; 223 info = (struct refmatch *)cb_data;
221 if (!strcmp(refname, info->req_ref)) 224 if (!strcmp(refname, info->req_ref))
222 info->match = 1; 225 info->match = 1;
223 if (!info->first_ref) 226 if (!info->first_ref)
224 info->first_ref = xstrdup(refname); 227 info->first_ref = xstrdup(refname);
225 return info->match; 228 return info->match;
226} 229}
227 230
228char *find_default_branch(struct cgit_repo *repo) 231char *find_default_branch(struct cgit_repo *repo)
229{ 232{
230 struct refmatch info; 233 struct refmatch info;
231 234
232 info.req_ref = repo->defbranch; 235 info.req_ref = repo->defbranch;
233 info.first_ref = NULL; 236 info.first_ref = NULL;
234 info.match = 0; 237 info.match = 0;
235 for_each_branch_ref(find_current_ref, &info); 238 for_each_branch_ref(find_current_ref, &info);
236 if (info.match) 239 if (info.match)
237 return info.req_ref; 240 return info.req_ref;
238 else 241 else
239 return info.first_ref; 242 return info.first_ref;
240} 243}
241 244
242static int prepare_repo_cmd(struct cgit_context *ctx) 245static int prepare_repo_cmd(struct cgit_context *ctx)
243{ 246{
244 char *tmp; 247 char *tmp;
245 unsigned char sha1[20]; 248 unsigned char sha1[20];
246 int nongit = 0; 249 int nongit = 0;
247 250
248 setenv("GIT_DIR", ctx->repo->path, 1); 251 setenv("GIT_DIR", ctx->repo->path, 1);
249 setup_git_directory_gently(&nongit); 252 setup_git_directory_gently(&nongit);
250 if (nongit) { 253 if (nongit) {
251 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, 254 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
252 "config error"); 255 "config error");
253 tmp = fmt("Not a git repository: '%s'", ctx->repo->path); 256 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
254 ctx->repo = NULL; 257 ctx->repo = NULL;
255 cgit_print_http_headers(ctx); 258 cgit_print_http_headers(ctx);
256 cgit_print_docstart(ctx); 259 cgit_print_docstart(ctx);
257 cgit_print_pageheader(ctx); 260 cgit_print_pageheader(ctx);
258 cgit_print_error(tmp); 261 cgit_print_error(tmp);
259 cgit_print_docend(); 262 cgit_print_docend();
260 return 1; 263 return 1;
261 } 264 }
262 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); 265 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
263 266
264 if (!ctx->qry.head) { 267 if (!ctx->qry.head) {
265 ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); 268 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
266 ctx->repo->defbranch = ctx->qry.head; 269 ctx->repo->defbranch = ctx->qry.head;
267 } 270 }
268 271
269 if (!ctx->qry.head) { 272 if (!ctx->qry.head) {
270 cgit_print_http_headers(ctx); 273 cgit_print_http_headers(ctx);
271 cgit_print_docstart(ctx); 274 cgit_print_docstart(ctx);
272 cgit_print_pageheader(ctx); 275 cgit_print_pageheader(ctx);
273 cgit_print_error("Repository seems to be empty"); 276 cgit_print_error("Repository seems to be empty");
274 cgit_print_docend(); 277 cgit_print_docend();
275 return 1; 278 return 1;
276 } 279 }
277 280
278 if (get_sha1(ctx->qry.head, sha1)) { 281 if (get_sha1(ctx->qry.head, sha1)) {
279 tmp = xstrdup(ctx->qry.head); 282 tmp = xstrdup(ctx->qry.head);
280 ctx->qry.head = ctx->repo->defbranch; 283 ctx->qry.head = ctx->repo->defbranch;
281 cgit_print_http_headers(ctx); 284 cgit_print_http_headers(ctx);
282 cgit_print_docstart(ctx); 285 cgit_print_docstart(ctx);
283 cgit_print_pageheader(ctx); 286 cgit_print_pageheader(ctx);
284 cgit_print_error(fmt("Invalid branch: %s", tmp)); 287 cgit_print_error(fmt("Invalid branch: %s", tmp));
285 cgit_print_docend(); 288 cgit_print_docend();
286 return 1; 289 return 1;
287 } 290 }
288 return 0; 291 return 0;
289} 292}
290 293
291static void process_request(struct cgit_context *ctx) 294static void process_request(struct cgit_context *ctx)
292{ 295{
293 struct cgit_cmd *cmd; 296 struct cgit_cmd *cmd;
294 297
295 cmd = cgit_get_cmd(ctx); 298 cmd = cgit_get_cmd(ctx);
296 if (!cmd) { 299 if (!cmd) {
297 ctx->page.title = "cgit error"; 300 ctx->page.title = "cgit error";
298 ctx->repo = NULL; 301 ctx->repo = NULL;
299 cgit_print_http_headers(ctx); 302 cgit_print_http_headers(ctx);
300 cgit_print_docstart(ctx); 303 cgit_print_docstart(ctx);
301 cgit_print_pageheader(ctx); 304 cgit_print_pageheader(ctx);
302 cgit_print_error("Invalid request"); 305 cgit_print_error("Invalid request");
303 cgit_print_docend(); 306 cgit_print_docend();
304 return; 307 return;
305 } 308 }
306 309
307 if (cmd->want_repo && prepare_repo_cmd(ctx)) 310 if (cmd->want_repo && prepare_repo_cmd(ctx))
308 return; 311 return;
309 312
310 if (cmd->want_layout) { 313 if (cmd->want_layout) {
311 cgit_print_http_headers(ctx); 314 cgit_print_http_headers(ctx);
312 cgit_print_docstart(ctx); 315 cgit_print_docstart(ctx);
313 cgit_print_pageheader(ctx); 316 cgit_print_pageheader(ctx);
314 } 317 }
315 318
316 cmd->fn(ctx); 319 cmd->fn(ctx);
317 320
318 if (cmd->want_layout) 321 if (cmd->want_layout)
319 cgit_print_docend(); 322 cgit_print_docend();
320} 323}
321 324
322static long ttl_seconds(long ttl) 325static long ttl_seconds(long ttl)
323{ 326{
324 if (ttl<0) 327 if (ttl<0)
325 return 60 * 60 * 24 * 365; 328 return 60 * 60 * 24 * 365;
326 else 329 else
327 return ttl * 60; 330 return ttl * 60;
328} 331}
329 332
330static void cgit_fill_cache(struct cacheitem *item, int use_cache) 333static void cgit_fill_cache(struct cacheitem *item, int use_cache)
331{ 334{
332 int stdout2; 335 int stdout2;
333 336
334 if (use_cache) { 337 if (use_cache) {
335 stdout2 = chk_positive(dup(STDOUT_FILENO), 338 stdout2 = chk_positive(dup(STDOUT_FILENO),
336 "Preserving STDOUT"); 339 "Preserving STDOUT");
337 chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); 340 chk_zero(close(STDOUT_FILENO), "Closing STDOUT");
338 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 341 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
339 } 342 }
340 343
341 ctx.page.modified = time(NULL); 344 ctx.page.modified = time(NULL);
342 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); 345 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
343 process_request(&ctx); 346 process_request(&ctx);
344 347
345 if (use_cache) { 348 if (use_cache) {
346 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 349 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
347 chk_positive(dup2(stdout2, STDOUT_FILENO), 350 chk_positive(dup2(stdout2, STDOUT_FILENO),
348 "Restoring original STDOUT"); 351 "Restoring original STDOUT");
349 chk_zero(close(stdout2), "Closing temporary STDOUT"); 352 chk_zero(close(stdout2), "Closing temporary STDOUT");
350 } 353 }
351} 354}
352 355
353static void cgit_check_cache(struct cacheitem *item) 356static void cgit_check_cache(struct cacheitem *item)
354{ 357{
355 int i = 0; 358 int i = 0;
356 359
357 top: 360 top:
358 if (++i > ctx.cfg.max_lock_attempts) { 361 if (++i > ctx.cfg.max_lock_attempts) {
359 die("cgit_refresh_cache: unable to lock %s: %s", 362 die("cgit_refresh_cache: unable to lock %s: %s",
360 item->name, strerror(errno)); 363 item->name, strerror(errno));
361 } 364 }
362 if (!cache_exist(item)) { 365 if (!cache_exist(item)) {
363 if (!cache_lock(item)) { 366 if (!cache_lock(item)) {
364 sleep(1); 367 sleep(1);
365 goto top; 368 goto top;
366 } 369 }
367 if (!cache_exist(item)) { 370 if (!cache_exist(item)) {
368 cgit_fill_cache(item, 1); 371 cgit_fill_cache(item, 1);
369 cache_unlock(item); 372 cache_unlock(item);
370 } else { 373 } else {
371 cache_cancel_lock(item); 374 cache_cancel_lock(item);
372 } 375 }
373 } else if (cache_expired(item) && cache_lock(item)) { 376 } else if (cache_expired(item) && cache_lock(item)) {
374 if (cache_expired(item)) { 377 if (cache_expired(item)) {
375 cgit_fill_cache(item, 1); 378 cgit_fill_cache(item, 1);
376 cache_unlock(item); 379 cache_unlock(item);
377 } else { 380 } else {
378 cache_cancel_lock(item); 381 cache_cancel_lock(item);
379 } 382 }
380 } 383 }
381} 384}
382 385
383static void cgit_print_cache(struct cacheitem *item) 386static void cgit_print_cache(struct cacheitem *item)
384{ 387{
385 static char buf[4096]; 388 static char buf[4096];
386 ssize_t i; 389 ssize_t i;
387 390
388 int fd = open(item->name, O_RDONLY); 391 int fd = open(item->name, O_RDONLY);
389 if (fd<0) 392 if (fd<0)
390 die("Unable to open cached file %s", item->name); 393 die("Unable to open cached file %s", item->name);
391 394
392 while((i=read(fd, buf, sizeof(buf))) > 0) 395 while((i=read(fd, buf, sizeof(buf))) > 0)
393 write(STDOUT_FILENO, buf, i); 396 write(STDOUT_FILENO, buf, i);
394 397
395 close(fd); 398 close(fd);
396} 399}
397 400
398static void cgit_parse_args(int argc, const char **argv) 401static void cgit_parse_args(int argc, const char **argv)
399{ 402{
400 int i; 403 int i;
401 404
402 for (i = 1; i < argc; i++) { 405 for (i = 1; i < argc; i++) {
403 if (!strncmp(argv[i], "--cache=", 8)) { 406 if (!strncmp(argv[i], "--cache=", 8)) {
404 ctx.cfg.cache_root = xstrdup(argv[i]+8); 407 ctx.cfg.cache_root = xstrdup(argv[i]+8);
405 } 408 }
406 if (!strcmp(argv[i], "--nocache")) { 409 if (!strcmp(argv[i], "--nocache")) {
407 ctx.cfg.nocache = 1; 410 ctx.cfg.nocache = 1;
408 } 411 }
409 if (!strncmp(argv[i], "--query=", 8)) { 412 if (!strncmp(argv[i], "--query=", 8)) {
410 ctx.qry.raw = xstrdup(argv[i]+8); 413 ctx.qry.raw = xstrdup(argv[i]+8);
411 } 414 }
412 if (!strncmp(argv[i], "--repo=", 7)) { 415 if (!strncmp(argv[i], "--repo=", 7)) {
413 ctx.qry.repo = xstrdup(argv[i]+7); 416 ctx.qry.repo = xstrdup(argv[i]+7);
414 } 417 }
415 if (!strncmp(argv[i], "--page=", 7)) { 418 if (!strncmp(argv[i], "--page=", 7)) {
416 ctx.qry.page = xstrdup(argv[i]+7); 419 ctx.qry.page = xstrdup(argv[i]+7);
417 } 420 }
418 if (!strncmp(argv[i], "--head=", 7)) { 421 if (!strncmp(argv[i], "--head=", 7)) {
419 ctx.qry.head = xstrdup(argv[i]+7); 422 ctx.qry.head = xstrdup(argv[i]+7);
420 ctx.qry.has_symref = 1; 423 ctx.qry.has_symref = 1;
421 } 424 }
422 if (!strncmp(argv[i], "--sha1=", 7)) { 425 if (!strncmp(argv[i], "--sha1=", 7)) {
423 ctx.qry.sha1 = xstrdup(argv[i]+7); 426 ctx.qry.sha1 = xstrdup(argv[i]+7);
424 ctx.qry.has_sha1 = 1; 427 ctx.qry.has_sha1 = 1;
425 } 428 }
426 if (!strncmp(argv[i], "--ofs=", 6)) { 429 if (!strncmp(argv[i], "--ofs=", 6)) {
427 ctx.qry.ofs = atoi(argv[i]+6); 430 ctx.qry.ofs = atoi(argv[i]+6);
428 } 431 }
429 } 432 }
430} 433}
431 434
432int main(int argc, const char **argv) 435int main(int argc, const char **argv)
433{ 436{
434 struct cacheitem item; 437 struct cacheitem item;
435 const char *cgit_config_env = getenv("CGIT_CONFIG"); 438 const char *cgit_config_env = getenv("CGIT_CONFIG");
436 439
437 prepare_context(&ctx); 440 prepare_context(&ctx);
438 item.st.st_mtime = time(NULL); 441 item.st.st_mtime = time(NULL);
439 cgit_repolist.length = 0; 442 cgit_repolist.length = 0;
440 cgit_repolist.count = 0; 443 cgit_repolist.count = 0;
441 cgit_repolist.repos = NULL; 444 cgit_repolist.repos = NULL;
442 445
443 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 446 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
444 config_cb); 447 config_cb);
445 ctx.repo = NULL; 448 ctx.repo = NULL;
446 if (getenv("SCRIPT_NAME")) 449 if (getenv("SCRIPT_NAME"))
447 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 450 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
448 if (getenv("QUERY_STRING")) 451 if (getenv("QUERY_STRING"))
449 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 452 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
450 cgit_parse_args(argc, argv); 453 cgit_parse_args(argc, argv);
451 http_parse_querystring(ctx.qry.raw, querystring_cb); 454 http_parse_querystring(ctx.qry.raw, querystring_cb);
452 if (!cgit_prepare_cache(&item)) 455 if (!cgit_prepare_cache(&item))
453 return 0; 456 return 0;
454 if (ctx.cfg.nocache) { 457 if (ctx.cfg.nocache) {
455 cgit_fill_cache(&item, 0); 458 cgit_fill_cache(&item, 0);
456 } else { 459 } else {
457 cgit_check_cache(&item); 460 cgit_check_cache(&item);
458 cgit_print_cache(&item); 461 cgit_print_cache(&item);
459 } 462 }
460 return 0; 463 return 0;
461} 464}
diff --git a/cgit.h b/cgit.h
index a3b6535..7761b6e 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,231 +1,232 @@
1#ifndef CGIT_H 1#ifndef CGIT_H
2#define CGIT_H 2#define CGIT_H
3 3
4 4
5#include <git-compat-util.h> 5#include <git-compat-util.h>
6#include <cache.h> 6#include <cache.h>
7#include <grep.h> 7#include <grep.h>
8#include <object.h> 8#include <object.h>
9#include <tree.h> 9#include <tree.h>
10#include <commit.h> 10#include <commit.h>
11#include <tag.h> 11#include <tag.h>
12#include <diff.h> 12#include <diff.h>
13#include <diffcore.h> 13#include <diffcore.h>
14#include <refs.h> 14#include <refs.h>
15#include <revision.h> 15#include <revision.h>
16#include <log-tree.h> 16#include <log-tree.h>
17#include <archive.h> 17#include <archive.h>
18#include <xdiff/xdiff.h> 18#include <xdiff/xdiff.h>
19#include <utf8.h> 19#include <utf8.h>
20 20
21 21
22/* 22/*
23 * Dateformats used on misc. pages 23 * Dateformats used on misc. pages
24 */ 24 */
25#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" 25#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S"
26#define FMT_SHORTDATE "%Y-%m-%d" 26#define FMT_SHORTDATE "%Y-%m-%d"
27 27
28 28
29/* 29/*
30 * Limits used for relative dates 30 * Limits used for relative dates
31 */ 31 */
32#define TM_MIN 60 32#define TM_MIN 60
33#define TM_HOUR (TM_MIN * 60) 33#define TM_HOUR (TM_MIN * 60)
34#define TM_DAY (TM_HOUR * 24) 34#define TM_DAY (TM_HOUR * 24)
35#define TM_WEEK (TM_DAY * 7) 35#define TM_WEEK (TM_DAY * 7)
36#define TM_YEAR (TM_DAY * 365) 36#define TM_YEAR (TM_DAY * 365)
37#define TM_MONTH (TM_YEAR / 12.0) 37#define TM_MONTH (TM_YEAR / 12.0)
38 38
39 39
40/* 40/*
41 * Default encoding 41 * Default encoding
42 */ 42 */
43#define PAGE_ENCODING "UTF-8" 43#define PAGE_ENCODING "UTF-8"
44 44
45typedef void (*configfn)(const char *name, const char *value); 45typedef void (*configfn)(const char *name, const char *value);
46typedef void (*filepair_fn)(struct diff_filepair *pair); 46typedef void (*filepair_fn)(struct diff_filepair *pair);
47typedef void (*linediff_fn)(char *line, int len); 47typedef void (*linediff_fn)(char *line, int len);
48 48
49struct cgit_repo { 49struct cgit_repo {
50 char *url; 50 char *url;
51 char *name; 51 char *name;
52 char *path; 52 char *path;
53 char *desc; 53 char *desc;
54 char *owner; 54 char *owner;
55 char *defbranch; 55 char *defbranch;
56 char *group; 56 char *group;
57 char *module_link; 57 char *module_link;
58 char *readme; 58 char *readme;
59 char *clone_url; 59 char *clone_url;
60 int snapshots; 60 int snapshots;
61 int enable_log_filecount; 61 int enable_log_filecount;
62 int enable_log_linecount; 62 int enable_log_linecount;
63}; 63};
64 64
65struct cgit_repolist { 65struct cgit_repolist {
66 int length; 66 int length;
67 int count; 67 int count;
68 struct cgit_repo *repos; 68 struct cgit_repo *repos;
69}; 69};
70 70
71struct commitinfo { 71struct commitinfo {
72 struct commit *commit; 72 struct commit *commit;
73 char *author; 73 char *author;
74 char *author_email; 74 char *author_email;
75 unsigned long author_date; 75 unsigned long author_date;
76 char *committer; 76 char *committer;
77 char *committer_email; 77 char *committer_email;
78 unsigned long committer_date; 78 unsigned long committer_date;
79 char *subject; 79 char *subject;
80 char *msg; 80 char *msg;
81 char *msg_encoding; 81 char *msg_encoding;
82}; 82};
83 83
84struct taginfo { 84struct taginfo {
85 char *tagger; 85 char *tagger;
86 char *tagger_email; 86 char *tagger_email;
87 int tagger_date; 87 int tagger_date;
88 char *msg; 88 char *msg;
89}; 89};
90 90
91struct refinfo { 91struct refinfo {
92 const char *refname; 92 const char *refname;
93 struct object *object; 93 struct object *object;
94 union { 94 union {
95 struct taginfo *tag; 95 struct taginfo *tag;
96 struct commitinfo *commit; 96 struct commitinfo *commit;
97 }; 97 };
98}; 98};
99 99
100struct reflist { 100struct reflist {
101 struct refinfo **refs; 101 struct refinfo **refs;
102 int alloc; 102 int alloc;
103 int count; 103 int count;
104}; 104};
105 105
106struct cgit_query { 106struct cgit_query {
107 int has_symref; 107 int has_symref;
108 int has_sha1; 108 int has_sha1;
109 char *raw; 109 char *raw;
110 char *repo; 110 char *repo;
111 char *page; 111 char *page;
112 char *search; 112 char *search;
113 char *grep; 113 char *grep;
114 char *head; 114 char *head;
115 char *sha1; 115 char *sha1;
116 char *sha2; 116 char *sha2;
117 char *path; 117 char *path;
118 char *name; 118 char *name;
119 int ofs; 119 int ofs;
120}; 120};
121 121
122struct cgit_config { 122struct cgit_config {
123 char *agefile; 123 char *agefile;
124 char *cache_root; 124 char *cache_root;
125 char *clone_prefix; 125 char *clone_prefix;
126 char *css; 126 char *css;
127 char *index_header; 127 char *index_header;
128 char *index_info; 128 char *index_info;
129 char *logo; 129 char *logo;
130 char *logo_link; 130 char *logo_link;
131 char *module_link; 131 char *module_link;
132 char *repo_group; 132 char *repo_group;
133 char *robots; 133 char *robots;
134 char *root_title; 134 char *root_title;
135 char *root_desc;
135 char *script_name; 136 char *script_name;
136 char *virtual_root; 137 char *virtual_root;
137 int cache_dynamic_ttl; 138 int cache_dynamic_ttl;
138 int cache_max_create_time; 139 int cache_max_create_time;
139 int cache_repo_ttl; 140 int cache_repo_ttl;
140 int cache_root_ttl; 141 int cache_root_ttl;
141 int cache_static_ttl; 142 int cache_static_ttl;
142 int enable_index_links; 143 int enable_index_links;
143 int enable_log_filecount; 144 int enable_log_filecount;
144 int enable_log_linecount; 145 int enable_log_linecount;
145 int max_commit_count; 146 int max_commit_count;
146 int max_lock_attempts; 147 int max_lock_attempts;
147 int max_msg_len; 148 int max_msg_len;
148 int max_repodesc_len; 149 int max_repodesc_len;
149 int nocache; 150 int nocache;
150 int renamelimit; 151 int renamelimit;
151 int snapshots; 152 int snapshots;
152 int summary_branches; 153 int summary_branches;
153 int summary_log; 154 int summary_log;
154 int summary_tags; 155 int summary_tags;
155}; 156};
156 157
157struct cgit_page { 158struct cgit_page {
158 time_t modified; 159 time_t modified;
159 time_t expires; 160 time_t expires;
160 char *mimetype; 161 char *mimetype;
161 char *charset; 162 char *charset;
162 char *filename; 163 char *filename;
163 char *title; 164 char *title;
164}; 165};
165 166
166struct cgit_context { 167struct cgit_context {
167 struct cgit_query qry; 168 struct cgit_query qry;
168 struct cgit_config cfg; 169 struct cgit_config cfg;
169 struct cgit_repo *repo; 170 struct cgit_repo *repo;
170 struct cgit_page page; 171 struct cgit_page page;
171}; 172};
172 173
173struct cgit_snapshot_format { 174struct cgit_snapshot_format {
174 const char *suffix; 175 const char *suffix;
175 const char *mimetype; 176 const char *mimetype;
176 write_archive_fn_t write_func; 177 write_archive_fn_t write_func;
177 int bit; 178 int bit;
178}; 179};
179 180
180extern const char *cgit_version; 181extern const char *cgit_version;
181 182
182extern struct cgit_repolist cgit_repolist; 183extern struct cgit_repolist cgit_repolist;
183extern struct cgit_context ctx; 184extern struct cgit_context ctx;
184extern const struct cgit_snapshot_format cgit_snapshot_formats[]; 185extern const struct cgit_snapshot_format cgit_snapshot_formats[];
185 186
186extern struct cgit_repo *cgit_add_repo(const char *url); 187extern struct cgit_repo *cgit_add_repo(const char *url);
187extern struct cgit_repo *cgit_get_repoinfo(const char *url); 188extern struct cgit_repo *cgit_get_repoinfo(const char *url);
188extern void cgit_repo_config_cb(const char *name, const char *value); 189extern void cgit_repo_config_cb(const char *name, const char *value);
189 190
190extern int chk_zero(int result, char *msg); 191extern int chk_zero(int result, char *msg);
191extern int chk_positive(int result, char *msg); 192extern int chk_positive(int result, char *msg);
192extern int chk_non_negative(int result, char *msg); 193extern int chk_non_negative(int result, char *msg);
193 194
194extern char *trim_end(const char *str, char c); 195extern char *trim_end(const char *str, char c);
195extern char *strlpart(char *txt, int maxlen); 196extern char *strlpart(char *txt, int maxlen);
196extern char *strrpart(char *txt, int maxlen); 197extern char *strrpart(char *txt, int maxlen);
197 198
198extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 199extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
199extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 200extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
200 int flags, void *cb_data); 201 int flags, void *cb_data);
201 202
202extern void *cgit_free_commitinfo(struct commitinfo *info); 203extern void *cgit_free_commitinfo(struct commitinfo *info);
203 204
204extern int cgit_diff_files(const unsigned char *old_sha1, 205extern int cgit_diff_files(const unsigned char *old_sha1,
205 const unsigned char *new_sha1, 206 const unsigned char *new_sha1,
206 linediff_fn fn); 207 linediff_fn fn);
207 208
208extern void cgit_diff_tree(const unsigned char *old_sha1, 209extern void cgit_diff_tree(const unsigned char *old_sha1,
209 const unsigned char *new_sha1, 210 const unsigned char *new_sha1,
210 filepair_fn fn, const char *prefix); 211 filepair_fn fn, const char *prefix);
211 212
212extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 213extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
213 214
214extern char *fmt(const char *format,...); 215extern char *fmt(const char *format,...);
215 216
216extern struct commitinfo *cgit_parse_commit(struct commit *commit); 217extern struct commitinfo *cgit_parse_commit(struct commit *commit);
217extern struct taginfo *cgit_parse_tag(struct tag *tag); 218extern struct taginfo *cgit_parse_tag(struct tag *tag);
218extern void cgit_parse_url(const char *url); 219extern void cgit_parse_url(const char *url);
219 220
220extern const char *cgit_repobasename(const char *reponame); 221extern const char *cgit_repobasename(const char *reponame);
221 222
222extern int cgit_parse_snapshots_mask(const char *str); 223extern int cgit_parse_snapshots_mask(const char *str);
223 224
224/* libgit.a either links against or compiles its own implementation of 225/* libgit.a either links against or compiles its own implementation of
225 * strcasestr(), and we'd like to reuse it. Simply re-declaring it 226 * strcasestr(), and we'd like to reuse it. Simply re-declaring it
226 * seems to do the trick. 227 * seems to do the trick.
227 */ 228 */
228extern char *strcasestr(const char *haystack, const char *needle); 229extern char *strcasestr(const char *haystack, const char *needle);
229 230
230 231
231#endif /* CGIT_H */ 232#endif /* CGIT_H */
diff --git a/ui-shared.c b/ui-shared.c
index 8a804c2..83758f7 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,596 +1,599 @@
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 char *repolink(char *title, char *class, char *page, char *head, 117static char *repolink(char *title, char *class, char *page, char *head,
118 char *path) 118 char *path)
119{ 119{
120 char *delim = "?"; 120 char *delim = "?";
121 121
122 html("<a"); 122 html("<a");
123 if (title) { 123 if (title) {
124 html(" title='"); 124 html(" title='");
125 html_attr(title); 125 html_attr(title);
126 html("'"); 126 html("'");
127 } 127 }
128 if (class) { 128 if (class) {
129 html(" class='"); 129 html(" class='");
130 html_attr(class); 130 html_attr(class);
131 html("'"); 131 html("'");
132 } 132 }
133 html(" href='"); 133 html(" href='");
134 if (ctx.cfg.virtual_root) { 134 if (ctx.cfg.virtual_root) {
135 html_attr(ctx.cfg.virtual_root); 135 html_attr(ctx.cfg.virtual_root);
136 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 136 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
137 html("/"); 137 html("/");
138 html_attr(ctx.repo->url); 138 html_attr(ctx.repo->url);
139 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 139 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
140 html("/"); 140 html("/");
141 if (page) { 141 if (page) {
142 html(page); 142 html(page);
143 html("/"); 143 html("/");
144 if (path) 144 if (path)
145 html_attr(path); 145 html_attr(path);
146 } 146 }
147 } else { 147 } else {
148 html(ctx.cfg.script_name); 148 html(ctx.cfg.script_name);
149 html("?url="); 149 html("?url=");
150 html_attr(ctx.repo->url); 150 html_attr(ctx.repo->url);
151 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 151 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
152 html("/"); 152 html("/");
153 if (page) { 153 if (page) {
154 html(page); 154 html(page);
155 html("/"); 155 html("/");
156 if (path) 156 if (path)
157 html_attr(path); 157 html_attr(path);
158 } 158 }
159 delim = "&amp;"; 159 delim = "&amp;";
160 } 160 }
161 if (head && strcmp(head, ctx.repo->defbranch)) { 161 if (head && strcmp(head, ctx.repo->defbranch)) {
162 html(delim); 162 html(delim);
163 html("h="); 163 html("h=");
164 html_attr(head); 164 html_attr(head);
165 delim = "&amp;"; 165 delim = "&amp;";
166 } 166 }
167 return fmt("%s", delim); 167 return fmt("%s", delim);
168} 168}
169 169
170static void reporevlink(char *page, char *name, char *title, char *class, 170static void reporevlink(char *page, char *name, char *title, char *class,
171 char *head, char *rev, char *path) 171 char *head, char *rev, char *path)
172{ 172{
173 char *delim; 173 char *delim;
174 174
175 delim = repolink(title, class, page, head, path); 175 delim = repolink(title, class, page, head, path);
176 if (rev && strcmp(rev, ctx.qry.head)) { 176 if (rev && strcmp(rev, ctx.qry.head)) {
177 html(delim); 177 html(delim);
178 html("id="); 178 html("id=");
179 html_attr(rev); 179 html_attr(rev);
180 } 180 }
181 html("'>"); 181 html("'>");
182 html_txt(name); 182 html_txt(name);
183 html("</a>"); 183 html("</a>");
184} 184}
185 185
186void cgit_tree_link(char *name, char *title, char *class, char *head, 186void cgit_tree_link(char *name, char *title, char *class, char *head,
187 char *rev, char *path) 187 char *rev, char *path)
188{ 188{
189 reporevlink("tree", name, title, class, head, rev, path); 189 reporevlink("tree", name, title, class, head, rev, path);
190} 190}
191 191
192void cgit_log_link(char *name, char *title, char *class, char *head, 192void cgit_log_link(char *name, char *title, char *class, char *head,
193 char *rev, char *path, int ofs, char *grep, char *pattern) 193 char *rev, char *path, int ofs, char *grep, char *pattern)
194{ 194{
195 char *delim; 195 char *delim;
196 196
197 delim = repolink(title, class, "log", head, path); 197 delim = repolink(title, class, "log", head, path);
198 if (rev && strcmp(rev, ctx.qry.head)) { 198 if (rev && strcmp(rev, ctx.qry.head)) {
199 html(delim); 199 html(delim);
200 html("id="); 200 html("id=");
201 html_attr(rev); 201 html_attr(rev);
202 delim = "&"; 202 delim = "&";
203 } 203 }
204 if (grep && pattern) { 204 if (grep && pattern) {
205 html(delim); 205 html(delim);
206 html("qt="); 206 html("qt=");
207 html_attr(grep); 207 html_attr(grep);
208 delim = "&"; 208 delim = "&";
209 html(delim); 209 html(delim);
210 html("q="); 210 html("q=");
211 html_attr(pattern); 211 html_attr(pattern);
212 } 212 }
213 if (ofs > 0) { 213 if (ofs > 0) {
214 html(delim); 214 html(delim);
215 html("ofs="); 215 html("ofs=");
216 htmlf("%d", ofs); 216 htmlf("%d", ofs);
217 } 217 }
218 html("'>"); 218 html("'>");
219 html_txt(name); 219 html_txt(name);
220 html("</a>"); 220 html("</a>");
221} 221}
222 222
223void cgit_commit_link(char *name, char *title, char *class, char *head, 223void cgit_commit_link(char *name, char *title, char *class, char *head,
224 char *rev) 224 char *rev)
225{ 225{
226 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { 226 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
227 name[ctx.cfg.max_msg_len] = '\0'; 227 name[ctx.cfg.max_msg_len] = '\0';
228 name[ctx.cfg.max_msg_len - 1] = '.'; 228 name[ctx.cfg.max_msg_len - 1] = '.';
229 name[ctx.cfg.max_msg_len - 2] = '.'; 229 name[ctx.cfg.max_msg_len - 2] = '.';
230 name[ctx.cfg.max_msg_len - 3] = '.'; 230 name[ctx.cfg.max_msg_len - 3] = '.';
231 } 231 }
232 reporevlink("commit", name, title, class, head, rev, NULL); 232 reporevlink("commit", name, title, class, head, rev, NULL);
233} 233}
234 234
235void cgit_refs_link(char *name, char *title, char *class, char *head, 235void cgit_refs_link(char *name, char *title, char *class, char *head,
236 char *rev, char *path) 236 char *rev, char *path)
237{ 237{
238 reporevlink("refs", name, title, class, head, rev, path); 238 reporevlink("refs", name, title, class, head, rev, path);
239} 239}
240 240
241void cgit_snapshot_link(char *name, char *title, char *class, char *head, 241void cgit_snapshot_link(char *name, char *title, char *class, char *head,
242 char *rev, char *archivename) 242 char *rev, char *archivename)
243{ 243{
244 reporevlink("snapshot", name, title, class, head, rev, archivename); 244 reporevlink("snapshot", name, title, class, head, rev, archivename);
245} 245}
246 246
247void cgit_diff_link(char *name, char *title, char *class, char *head, 247void cgit_diff_link(char *name, char *title, char *class, char *head,
248 char *new_rev, char *old_rev, char *path) 248 char *new_rev, char *old_rev, char *path)
249{ 249{
250 char *delim; 250 char *delim;
251 251
252 delim = repolink(title, class, "diff", head, path); 252 delim = repolink(title, class, "diff", head, path);
253 if (new_rev && strcmp(new_rev, ctx.qry.head)) { 253 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
254 html(delim); 254 html(delim);
255 html("id="); 255 html("id=");
256 html_attr(new_rev); 256 html_attr(new_rev);
257 delim = "&amp;"; 257 delim = "&amp;";
258 } 258 }
259 if (old_rev) { 259 if (old_rev) {
260 html(delim); 260 html(delim);
261 html("id2="); 261 html("id2=");
262 html_attr(old_rev); 262 html_attr(old_rev);
263 } 263 }
264 html("'>"); 264 html("'>");
265 html_txt(name); 265 html_txt(name);
266 html("</a>"); 266 html("</a>");
267} 267}
268 268
269void cgit_patch_link(char *name, char *title, char *class, char *head, 269void cgit_patch_link(char *name, char *title, char *class, char *head,
270 char *rev) 270 char *rev)
271{ 271{
272 reporevlink("patch", name, title, class, head, rev, NULL); 272 reporevlink("patch", name, title, class, head, rev, NULL);
273} 273}
274 274
275void cgit_object_link(struct object *obj) 275void cgit_object_link(struct object *obj)
276{ 276{
277 char *page, *arg, *url; 277 char *page, *arg, *url;
278 278
279 if (obj->type == OBJ_COMMIT) { 279 if (obj->type == OBJ_COMMIT) {
280 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, 280 cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL,
281 ctx.qry.head, sha1_to_hex(obj->sha1)); 281 ctx.qry.head, sha1_to_hex(obj->sha1));
282 return; 282 return;
283 } else if (obj->type == OBJ_TREE) { 283 } else if (obj->type == OBJ_TREE) {
284 page = "tree"; 284 page = "tree";
285 arg = "id"; 285 arg = "id";
286 } else if (obj->type == OBJ_TAG) { 286 } else if (obj->type == OBJ_TAG) {
287 page = "tag"; 287 page = "tag";
288 arg = "id"; 288 arg = "id";
289 } else { 289 } else {
290 page = "blob"; 290 page = "blob";
291 arg = "id"; 291 arg = "id";
292 } 292 }
293 293
294 url = cgit_pageurl(ctx.qry.repo, page, 294 url = cgit_pageurl(ctx.qry.repo, page,
295 fmt("%s=%s", arg, sha1_to_hex(obj->sha1))); 295 fmt("%s=%s", arg, sha1_to_hex(obj->sha1)));
296 html_link_open(url, NULL, NULL); 296 html_link_open(url, NULL, NULL);
297 htmlf("%s %s", typename(obj->type), 297 htmlf("%s %s", typename(obj->type),
298 sha1_to_hex(obj->sha1)); 298 sha1_to_hex(obj->sha1));
299 html_link_close(); 299 html_link_close();
300} 300}
301 301
302void cgit_print_date(time_t secs, char *format) 302void cgit_print_date(time_t secs, char *format)
303{ 303{
304 char buf[64]; 304 char buf[64];
305 struct tm *time; 305 struct tm *time;
306 306
307 if (!secs) 307 if (!secs)
308 return; 308 return;
309 time = gmtime(&secs); 309 time = gmtime(&secs);
310 strftime(buf, sizeof(buf)-1, format, time); 310 strftime(buf, sizeof(buf)-1, format, time);
311 html_txt(buf); 311 html_txt(buf);
312} 312}
313 313
314void cgit_print_age(time_t t, time_t max_relative, char *format) 314void cgit_print_age(time_t t, time_t max_relative, char *format)
315{ 315{
316 time_t now, secs; 316 time_t now, secs;
317 317
318 if (!t) 318 if (!t)
319 return; 319 return;
320 time(&now); 320 time(&now);
321 secs = now - t; 321 secs = now - t;
322 322
323 if (secs > max_relative && max_relative >= 0) { 323 if (secs > max_relative && max_relative >= 0) {
324 cgit_print_date(t, format); 324 cgit_print_date(t, format);
325 return; 325 return;
326 } 326 }
327 327
328 if (secs < TM_HOUR * 2) { 328 if (secs < TM_HOUR * 2) {
329 htmlf("<span class='age-mins'>%.0f min.</span>", 329 htmlf("<span class='age-mins'>%.0f min.</span>",
330 secs * 1.0 / TM_MIN); 330 secs * 1.0 / TM_MIN);
331 return; 331 return;
332 } 332 }
333 if (secs < TM_DAY * 2) { 333 if (secs < TM_DAY * 2) {
334 htmlf("<span class='age-hours'>%.0f hours</span>", 334 htmlf("<span class='age-hours'>%.0f hours</span>",
335 secs * 1.0 / TM_HOUR); 335 secs * 1.0 / TM_HOUR);
336 return; 336 return;
337 } 337 }
338 if (secs < TM_WEEK * 2) { 338 if (secs < TM_WEEK * 2) {
339 htmlf("<span class='age-days'>%.0f days</span>", 339 htmlf("<span class='age-days'>%.0f days</span>",
340 secs * 1.0 / TM_DAY); 340 secs * 1.0 / TM_DAY);
341 return; 341 return;
342 } 342 }
343 if (secs < TM_MONTH * 2) { 343 if (secs < TM_MONTH * 2) {
344 htmlf("<span class='age-weeks'>%.0f weeks</span>", 344 htmlf("<span class='age-weeks'>%.0f weeks</span>",
345 secs * 1.0 / TM_WEEK); 345 secs * 1.0 / TM_WEEK);
346 return; 346 return;
347 } 347 }
348 if (secs < TM_YEAR * 2) { 348 if (secs < TM_YEAR * 2) {
349 htmlf("<span class='age-months'>%.0f months</span>", 349 htmlf("<span class='age-months'>%.0f months</span>",
350 secs * 1.0 / TM_MONTH); 350 secs * 1.0 / TM_MONTH);
351 return; 351 return;
352 } 352 }
353 htmlf("<span class='age-years'>%.0f years</span>", 353 htmlf("<span class='age-years'>%.0f years</span>",
354 secs * 1.0 / TM_YEAR); 354 secs * 1.0 / TM_YEAR);
355} 355}
356 356
357void cgit_print_http_headers(struct cgit_context *ctx) 357void cgit_print_http_headers(struct cgit_context *ctx)
358{ 358{
359 if (ctx->page.mimetype && ctx->page.charset) 359 if (ctx->page.mimetype && ctx->page.charset)
360 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, 360 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
361 ctx->page.charset); 361 ctx->page.charset);
362 else if (ctx->page.mimetype) 362 else if (ctx->page.mimetype)
363 htmlf("Content-Type: %s\n", ctx->page.mimetype); 363 htmlf("Content-Type: %s\n", ctx->page.mimetype);
364 if (ctx->page.filename) 364 if (ctx->page.filename)
365 htmlf("Content-Disposition: inline; filename=\"%s\"\n", 365 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
366 ctx->page.filename); 366 ctx->page.filename);
367 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); 367 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
368 htmlf("Expires: %s\n", http_date(ctx->page.expires)); 368 htmlf("Expires: %s\n", http_date(ctx->page.expires));
369 html("\n"); 369 html("\n");
370} 370}
371 371
372void cgit_print_docstart(struct cgit_context *ctx) 372void cgit_print_docstart(struct cgit_context *ctx)
373{ 373{
374 html(cgit_doctype); 374 html(cgit_doctype);
375 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 375 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
376 html("<head>\n"); 376 html("<head>\n");
377 html("<title>"); 377 html("<title>");
378 html_txt(ctx->page.title); 378 html_txt(ctx->page.title);
379 html("</title>\n"); 379 html("</title>\n");
380 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 380 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
381 if (ctx->cfg.robots && *ctx->cfg.robots) 381 if (ctx->cfg.robots && *ctx->cfg.robots)
382 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 382 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
383 html("<link rel='stylesheet' type='text/css' href='"); 383 html("<link rel='stylesheet' type='text/css' href='");
384 html_attr(ctx->cfg.css); 384 html_attr(ctx->cfg.css);
385 html("'/>\n"); 385 html("'/>\n");
386 html("</head>\n"); 386 html("</head>\n");
387 html("<body>\n"); 387 html("<body>\n");
388} 388}
389 389
390void cgit_print_docend() 390void cgit_print_docend()
391{ 391{
392 html("</div>\n</body>\n</html>\n"); 392 html("</div>\n</body>\n</html>\n");
393} 393}
394 394
395int print_branch_option(const char *refname, const unsigned char *sha1, 395int print_branch_option(const char *refname, const unsigned char *sha1,
396 int flags, void *cb_data) 396 int flags, void *cb_data)
397{ 397{
398 char *name = (char *)refname; 398 char *name = (char *)refname;
399 html_option(name, name, ctx.qry.head); 399 html_option(name, name, ctx.qry.head);
400 return 0; 400 return 0;
401} 401}
402 402
403int print_archive_ref(const char *refname, const unsigned char *sha1, 403int print_archive_ref(const char *refname, const unsigned char *sha1,
404 int flags, void *cb_data) 404 int flags, void *cb_data)
405{ 405{
406 struct tag *tag; 406 struct tag *tag;
407 struct taginfo *info; 407 struct taginfo *info;
408 struct object *obj; 408 struct object *obj;
409 char buf[256], *url; 409 char buf[256], *url;
410 unsigned char fileid[20]; 410 unsigned char fileid[20];
411 int *header = (int *)cb_data; 411 int *header = (int *)cb_data;
412 412
413 if (prefixcmp(refname, "refs/archives")) 413 if (prefixcmp(refname, "refs/archives"))
414 return 0; 414 return 0;
415 strncpy(buf, refname+14, sizeof(buf)); 415 strncpy(buf, refname+14, sizeof(buf));
416 obj = parse_object(sha1); 416 obj = parse_object(sha1);
417 if (!obj) 417 if (!obj)
418 return 1; 418 return 1;
419 if (obj->type == OBJ_TAG) { 419 if (obj->type == OBJ_TAG) {
420 tag = lookup_tag(sha1); 420 tag = lookup_tag(sha1);
421 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 421 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
422 return 0; 422 return 0;
423 hashcpy(fileid, tag->tagged->sha1); 423 hashcpy(fileid, tag->tagged->sha1);
424 } else if (obj->type != OBJ_BLOB) { 424 } else if (obj->type != OBJ_BLOB) {
425 return 0; 425 return 0;
426 } else { 426 } else {
427 hashcpy(fileid, sha1); 427 hashcpy(fileid, sha1);
428 } 428 }
429 if (!*header) { 429 if (!*header) {
430 html("<h1>download</h1>\n"); 430 html("<h1>download</h1>\n");
431 *header = 1; 431 *header = 1;
432 } 432 }
433 url = cgit_pageurl(ctx.qry.repo, "blob", 433 url = cgit_pageurl(ctx.qry.repo, "blob",
434 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 434 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
435 buf)); 435 buf));
436 html_link_open(url, NULL, "menu"); 436 html_link_open(url, NULL, "menu");
437 html_txt(strlpart(buf, 20)); 437 html_txt(strlpart(buf, 20));
438 html_link_close(); 438 html_link_close();
439 return 0; 439 return 0;
440} 440}
441 441
442void add_hidden_formfields(int incl_head, int incl_search, char *page) 442void add_hidden_formfields(int incl_head, int incl_search, char *page)
443{ 443{
444 char *url; 444 char *url;
445 445
446 if (!ctx.cfg.virtual_root) { 446 if (!ctx.cfg.virtual_root) {
447 url = fmt("%s/%s", ctx.qry.repo, page); 447 url = fmt("%s/%s", ctx.qry.repo, page);
448 if (ctx.qry.path) 448 if (ctx.qry.path)
449 url = fmt("%s/%s", url, ctx.qry.path); 449 url = fmt("%s/%s", url, ctx.qry.path);
450 html_hidden("url", url); 450 html_hidden("url", url);
451 } 451 }
452 452
453 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch)) 453 if (incl_head && strcmp(ctx.qry.head, ctx.repo->defbranch))
454 html_hidden("h", ctx.qry.head); 454 html_hidden("h", ctx.qry.head);
455 455
456 if (ctx.qry.sha1) 456 if (ctx.qry.sha1)
457 html_hidden("id", ctx.qry.sha1); 457 html_hidden("id", ctx.qry.sha1);
458 if (ctx.qry.sha2) 458 if (ctx.qry.sha2)
459 html_hidden("id2", ctx.qry.sha2); 459 html_hidden("id2", ctx.qry.sha2);
460 460
461 if (incl_search) { 461 if (incl_search) {
462 if (ctx.qry.grep) 462 if (ctx.qry.grep)
463 html_hidden("qt", ctx.qry.grep); 463 html_hidden("qt", ctx.qry.grep);
464 if (ctx.qry.search) 464 if (ctx.qry.search)
465 html_hidden("q", ctx.qry.search); 465 html_hidden("q", ctx.qry.search);
466 } 466 }
467} 467}
468 468
469char *hc(struct cgit_cmd *cmd, const char *page) 469char *hc(struct cgit_cmd *cmd, const char *page)
470{ 470{
471 return (strcmp(cmd->name, page) ? NULL : "active"); 471 return (strcmp(cmd->name, page) ? NULL : "active");
472} 472}
473 473
474void cgit_print_pageheader(struct cgit_context *ctx) 474void cgit_print_pageheader(struct cgit_context *ctx)
475{ 475{
476 struct cgit_cmd *cmd = cgit_get_cmd(ctx); 476 struct cgit_cmd *cmd = cgit_get_cmd(ctx);
477 477
478 html("<table id='header'>\n"); 478 html("<table id='header'>\n");
479 html("<tr>\n"); 479 html("<tr>\n");
480 html("<td class='logo' rowspan='2'><a href='"); 480 html("<td class='logo' rowspan='2'><a href='");
481 if (ctx->cfg.logo_link) 481 if (ctx->cfg.logo_link)
482 html_attr(ctx->cfg.logo_link); 482 html_attr(ctx->cfg.logo_link);
483 else 483 else
484 html_attr(cgit_rooturl()); 484 html_attr(cgit_rooturl());
485 html("'><img src='"); 485 html("'><img src='");
486 html_attr(ctx->cfg.logo); 486 html_attr(ctx->cfg.logo);
487 html("' alt='cgit logo'/></a></td>\n"); 487 html("' alt='cgit logo'/></a></td>\n");
488 488
489 html("<td class='main'>"); 489 html("<td class='main'>");
490 if (ctx->repo) { 490 if (ctx->repo) {
491 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"), 491 reporevlink(NULL, ctx->repo->name, NULL, hc(cmd, "summary"),
492 ctx->qry.head, NULL, NULL); 492 ctx->qry.head, NULL, NULL);
493 html(" : "); 493 html(" : ");
494 html_txt(ctx->qry.page); 494 html_txt(ctx->qry.page);
495 html("</td><td class='form'>"); 495 html("</td><td class='form'>");
496 html("<form method='get' action=''>\n"); 496 html("<form method='get' action=''>\n");
497 add_hidden_formfields(0, 1, ctx->qry.page); 497 add_hidden_formfields(0, 1, ctx->qry.page);
498 html("<select name='h' onchange='this.form.submit();'>\n"); 498 html("<select name='h' onchange='this.form.submit();'>\n");
499 for_each_branch_ref(print_branch_option, ctx->qry.head); 499 for_each_branch_ref(print_branch_option, ctx->qry.head);
500 html("</select> "); 500 html("</select> ");
501 html("<input type='submit' name='' value='switch'/>"); 501 html("<input type='submit' name='' value='switch'/>");
502 html("</form>"); 502 html("</form>");
503 } else 503 } else
504 html_txt(ctx->cfg.root_title); 504 html_txt(ctx->cfg.root_title);
505 html("</td></tr>\n"); 505 html("</td></tr>\n");
506 506
507 html("<tr><td class='sub'"); 507 html("<tr><td class='sub'");
508 if (ctx->repo) { 508 if (ctx->repo) {
509 html(" colspan='2'>"); 509 html(" colspan='2'>");
510 html_txt(ctx->repo->desc); 510 html_txt(ctx->repo->desc);
511 } else { 511 } else {
512 html(">"); 512 html(">");
513 html_txt("a fast webinterface for the git dscm"); 513 if (ctx->cfg.root_desc)
514 html_txt(ctx->cfg.root_desc);
515 else if (ctx->cfg.index_info)
516 html_include(ctx->cfg.index_info);
514 } 517 }
515 html("</td></tr></table>\n"); 518 html("</td></tr></table>\n");
516 519
517 html("<table class='tabs'><tr><td>\n"); 520 html("<table class='tabs'><tr><td>\n");
518 if (ctx->repo) { 521 if (ctx->repo) {
519 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"), 522 reporevlink(NULL, "summary", NULL, hc(cmd, "summary"),
520 ctx->qry.head, NULL, NULL); 523 ctx->qry.head, NULL, NULL);
521 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head, 524 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,
522 ctx->qry.sha1, NULL); 525 ctx->qry.sha1, NULL);
523 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head, 526 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head,
524 NULL, NULL, 0, NULL, NULL); 527 NULL, NULL, 0, NULL, NULL);
525 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head, 528 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,
526 ctx->qry.sha1, NULL); 529 ctx->qry.sha1, NULL);
527 cgit_commit_link("commit", NULL, hc(cmd, "commit"), 530 cgit_commit_link("commit", NULL, hc(cmd, "commit"),
528 ctx->qry.head, ctx->qry.sha1); 531 ctx->qry.head, ctx->qry.sha1);
529 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head, 532 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,
530 ctx->qry.sha1, ctx->qry.sha2, NULL); 533 ctx->qry.sha1, ctx->qry.sha2, NULL);
531 html("</td><td class='form'>"); 534 html("</td><td class='form'>");
532 html("<form class='right' method='get' action='"); 535 html("<form class='right' method='get' action='");
533 if (ctx->cfg.virtual_root) 536 if (ctx->cfg.virtual_root)
534 html_attr(cgit_fileurl(ctx->qry.repo, "log", 537 html_attr(cgit_fileurl(ctx->qry.repo, "log",
535 ctx->qry.path, NULL)); 538 ctx->qry.path, NULL));
536 html("'>\n"); 539 html("'>\n");
537 add_hidden_formfields(1, 0, "log"); 540 add_hidden_formfields(1, 0, "log");
538 html("<select name='qt'>\n"); 541 html("<select name='qt'>\n");
539 html_option("grep", "log msg", ctx->qry.grep); 542 html_option("grep", "log msg", ctx->qry.grep);
540 html_option("author", "author", ctx->qry.grep); 543 html_option("author", "author", ctx->qry.grep);
541 html_option("committer", "committer", ctx->qry.grep); 544 html_option("committer", "committer", ctx->qry.grep);
542 html("</select>\n"); 545 html("</select>\n");
543 html("<input class='txt' type='text' size='10' name='q' value='"); 546 html("<input class='txt' type='text' size='10' name='q' value='");
544 html_attr(ctx->qry.search); 547 html_attr(ctx->qry.search);
545 html("'/>\n"); 548 html("'/>\n");
546 html("<input type='submit' value='search'/>\n"); 549 html("<input type='submit' value='search'/>\n");
547 html("</form>\n"); 550 html("</form>\n");
548 } else { 551 } else {
549 html("<a class='active' href='"); 552 html("<a class='active' href='");
550 html_attr(cgit_rooturl()); 553 html_attr(cgit_rooturl());
551 html("'>index</a>\n"); 554 html("'>index</a>\n");
552 html("</td><td class='form'>"); 555 html("</td><td class='form'>");
553 html("<form method='get' action='"); 556 html("<form method='get' action='");
554 html_attr(cgit_rooturl()); 557 html_attr(cgit_rooturl());
555 html("'>\n"); 558 html("'>\n");
556 html("<input type='text' name='q' size='10' value='"); 559 html("<input type='text' name='q' size='10' value='");
557 html_attr(ctx->qry.search); 560 html_attr(ctx->qry.search);
558 html("'/>\n"); 561 html("'/>\n");
559 html("<input type='submit' value='search'/>\n"); 562 html("<input type='submit' value='search'/>\n");
560 html("</form>"); 563 html("</form>");
561 } 564 }
562 html("</td></tr></table>\n"); 565 html("</td></tr></table>\n");
563 html("<div class='content'>"); 566 html("<div class='content'>");
564} 567}
565 568
566void cgit_print_filemode(unsigned short mode) 569void cgit_print_filemode(unsigned short mode)
567{ 570{
568 if (S_ISDIR(mode)) 571 if (S_ISDIR(mode))
569 html("d"); 572 html("d");
570 else if (S_ISLNK(mode)) 573 else if (S_ISLNK(mode))
571 html("l"); 574 html("l");
572 else if (S_ISGITLINK(mode)) 575 else if (S_ISGITLINK(mode))
573 html("m"); 576 html("m");
574 else 577 else
575 html("-"); 578 html("-");
576 html_fileperm(mode >> 6); 579 html_fileperm(mode >> 6);
577 html_fileperm(mode >> 3); 580 html_fileperm(mode >> 3);
578 html_fileperm(mode); 581 html_fileperm(mode);
579} 582}
580 583
581void cgit_print_snapshot_links(const char *repo, const char *head, 584void cgit_print_snapshot_links(const char *repo, const char *head,
582 const char *hex, int snapshots) 585 const char *hex, int snapshots)
583{ 586{
584 const struct cgit_snapshot_format* f; 587 const struct cgit_snapshot_format* f;
585 char *filename; 588 char *filename;
586 589
587 for (f = cgit_snapshot_formats; f->suffix; f++) { 590 for (f = cgit_snapshot_formats; f->suffix; f++) {
588 if (!(snapshots & f->bit)) 591 if (!(snapshots & f->bit))
589 continue; 592 continue;
590 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 593 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
591 f->suffix); 594 f->suffix);
592 cgit_snapshot_link(filename, NULL, NULL, (char *)head, 595 cgit_snapshot_link(filename, NULL, NULL, (char *)head,
593 (char *)hex, filename); 596 (char *)hex, filename);
594 html("<br/>"); 597 html("<br/>");
595 } 598 }
596} 599}