summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2009-07-25 09:59:22 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2009-07-25 09:59:22 (UTC)
commit681fdc45473143de3f3c5f69fbc7b94f5d6b0b75 (patch) (unidiff)
tree3bfca05875524bee0e5444fb791707bc3e593dbd
parent7e5c048505efe1902fb476cc2cb3160ff7df013d (diff)
parent3ff58ddd51bcbcbc9b7649bad1a39aa98af4b49f (diff)
downloadcgit-681fdc45473143de3f3c5f69fbc7b94f5d6b0b75.zip
cgit-681fdc45473143de3f3c5f69fbc7b94f5d6b0b75.tar.gz
cgit-681fdc45473143de3f3c5f69fbc7b94f5d6b0b75.tar.bz2
Merge branch 'plain-etag'
Conflicts: ui-shared.c
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c4
-rw-r--r--cgit.h1
-rw-r--r--ui-plain.c1
-rw-r--r--ui-shared.c6
4 files changed, 12 insertions, 0 deletions
diff --git a/cgit.c b/cgit.c
index 19adadd..ae20257 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,488 +1,492 @@
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#include "ui-stats.h" 15#include "ui-stats.h"
16#include "scan-tree.h" 16#include "scan-tree.h"
17 17
18const char *cgit_version = CGIT_VERSION; 18const char *cgit_version = CGIT_VERSION;
19 19
20void config_cb(const char *name, const char *value) 20void config_cb(const char *name, const char *value)
21{ 21{
22 if (!strcmp(name, "root-title")) 22 if (!strcmp(name, "root-title"))
23 ctx.cfg.root_title = xstrdup(value); 23 ctx.cfg.root_title = xstrdup(value);
24 else if (!strcmp(name, "root-desc")) 24 else if (!strcmp(name, "root-desc"))
25 ctx.cfg.root_desc = xstrdup(value); 25 ctx.cfg.root_desc = xstrdup(value);
26 else if (!strcmp(name, "root-readme")) 26 else if (!strcmp(name, "root-readme"))
27 ctx.cfg.root_readme = xstrdup(value); 27 ctx.cfg.root_readme = xstrdup(value);
28 else if (!strcmp(name, "css")) 28 else if (!strcmp(name, "css"))
29 ctx.cfg.css = xstrdup(value); 29 ctx.cfg.css = xstrdup(value);
30 else if (!strcmp(name, "favicon")) 30 else if (!strcmp(name, "favicon"))
31 ctx.cfg.favicon = xstrdup(value); 31 ctx.cfg.favicon = xstrdup(value);
32 else if (!strcmp(name, "footer")) 32 else if (!strcmp(name, "footer"))
33 ctx.cfg.footer = xstrdup(value); 33 ctx.cfg.footer = xstrdup(value);
34 else if (!strcmp(name, "header")) 34 else if (!strcmp(name, "header"))
35 ctx.cfg.header = xstrdup(value); 35 ctx.cfg.header = xstrdup(value);
36 else if (!strcmp(name, "logo")) 36 else if (!strcmp(name, "logo"))
37 ctx.cfg.logo = xstrdup(value); 37 ctx.cfg.logo = xstrdup(value);
38 else if (!strcmp(name, "index-header")) 38 else if (!strcmp(name, "index-header"))
39 ctx.cfg.index_header = xstrdup(value); 39 ctx.cfg.index_header = xstrdup(value);
40 else if (!strcmp(name, "index-info")) 40 else if (!strcmp(name, "index-info"))
41 ctx.cfg.index_info = xstrdup(value); 41 ctx.cfg.index_info = xstrdup(value);
42 else if (!strcmp(name, "logo-link")) 42 else if (!strcmp(name, "logo-link"))
43 ctx.cfg.logo_link = xstrdup(value); 43 ctx.cfg.logo_link = xstrdup(value);
44 else if (!strcmp(name, "module-link")) 44 else if (!strcmp(name, "module-link"))
45 ctx.cfg.module_link = xstrdup(value); 45 ctx.cfg.module_link = xstrdup(value);
46 else if (!strcmp(name, "virtual-root")) { 46 else if (!strcmp(name, "virtual-root")) {
47 ctx.cfg.virtual_root = trim_end(value, '/'); 47 ctx.cfg.virtual_root = trim_end(value, '/');
48 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 48 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
49 ctx.cfg.virtual_root = ""; 49 ctx.cfg.virtual_root = "";
50 } else if (!strcmp(name, "nocache")) 50 } else if (!strcmp(name, "nocache"))
51 ctx.cfg.nocache = atoi(value); 51 ctx.cfg.nocache = atoi(value);
52 else if (!strcmp(name, "snapshots")) 52 else if (!strcmp(name, "snapshots"))
53 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 53 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
54 else if (!strcmp(name, "enable-index-links")) 54 else if (!strcmp(name, "enable-index-links"))
55 ctx.cfg.enable_index_links = atoi(value); 55 ctx.cfg.enable_index_links = atoi(value);
56 else if (!strcmp(name, "enable-log-filecount")) 56 else if (!strcmp(name, "enable-log-filecount"))
57 ctx.cfg.enable_log_filecount = atoi(value); 57 ctx.cfg.enable_log_filecount = atoi(value);
58 else if (!strcmp(name, "enable-log-linecount")) 58 else if (!strcmp(name, "enable-log-linecount"))
59 ctx.cfg.enable_log_linecount = atoi(value); 59 ctx.cfg.enable_log_linecount = atoi(value);
60 else if (!strcmp(name, "max-stats")) 60 else if (!strcmp(name, "max-stats"))
61 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL); 61 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
62 else if (!strcmp(name, "cache-size")) 62 else if (!strcmp(name, "cache-size"))
63 ctx.cfg.cache_size = atoi(value); 63 ctx.cfg.cache_size = atoi(value);
64 else if (!strcmp(name, "cache-root")) 64 else if (!strcmp(name, "cache-root"))
65 ctx.cfg.cache_root = xstrdup(value); 65 ctx.cfg.cache_root = xstrdup(value);
66 else if (!strcmp(name, "cache-root-ttl")) 66 else if (!strcmp(name, "cache-root-ttl"))
67 ctx.cfg.cache_root_ttl = atoi(value); 67 ctx.cfg.cache_root_ttl = atoi(value);
68 else if (!strcmp(name, "cache-repo-ttl")) 68 else if (!strcmp(name, "cache-repo-ttl"))
69 ctx.cfg.cache_repo_ttl = atoi(value); 69 ctx.cfg.cache_repo_ttl = atoi(value);
70 else if (!strcmp(name, "cache-static-ttl")) 70 else if (!strcmp(name, "cache-static-ttl"))
71 ctx.cfg.cache_static_ttl = atoi(value); 71 ctx.cfg.cache_static_ttl = atoi(value);
72 else if (!strcmp(name, "cache-dynamic-ttl")) 72 else if (!strcmp(name, "cache-dynamic-ttl"))
73 ctx.cfg.cache_dynamic_ttl = atoi(value); 73 ctx.cfg.cache_dynamic_ttl = atoi(value);
74 else if (!strcmp(name, "max-message-length")) 74 else if (!strcmp(name, "max-message-length"))
75 ctx.cfg.max_msg_len = atoi(value); 75 ctx.cfg.max_msg_len = atoi(value);
76 else if (!strcmp(name, "max-repodesc-length")) 76 else if (!strcmp(name, "max-repodesc-length"))
77 ctx.cfg.max_repodesc_len = atoi(value); 77 ctx.cfg.max_repodesc_len = atoi(value);
78 else if (!strcmp(name, "max-repo-count")) 78 else if (!strcmp(name, "max-repo-count"))
79 ctx.cfg.max_repo_count = atoi(value); 79 ctx.cfg.max_repo_count = atoi(value);
80 else if (!strcmp(name, "max-commit-count")) 80 else if (!strcmp(name, "max-commit-count"))
81 ctx.cfg.max_commit_count = atoi(value); 81 ctx.cfg.max_commit_count = atoi(value);
82 else if (!strcmp(name, "summary-log")) 82 else if (!strcmp(name, "summary-log"))
83 ctx.cfg.summary_log = atoi(value); 83 ctx.cfg.summary_log = atoi(value);
84 else if (!strcmp(name, "summary-branches")) 84 else if (!strcmp(name, "summary-branches"))
85 ctx.cfg.summary_branches = atoi(value); 85 ctx.cfg.summary_branches = atoi(value);
86 else if (!strcmp(name, "summary-tags")) 86 else if (!strcmp(name, "summary-tags"))
87 ctx.cfg.summary_tags = atoi(value); 87 ctx.cfg.summary_tags = atoi(value);
88 else if (!strcmp(name, "agefile")) 88 else if (!strcmp(name, "agefile"))
89 ctx.cfg.agefile = xstrdup(value); 89 ctx.cfg.agefile = xstrdup(value);
90 else if (!strcmp(name, "renamelimit")) 90 else if (!strcmp(name, "renamelimit"))
91 ctx.cfg.renamelimit = atoi(value); 91 ctx.cfg.renamelimit = atoi(value);
92 else if (!strcmp(name, "robots")) 92 else if (!strcmp(name, "robots"))
93 ctx.cfg.robots = xstrdup(value); 93 ctx.cfg.robots = xstrdup(value);
94 else if (!strcmp(name, "clone-prefix")) 94 else if (!strcmp(name, "clone-prefix"))
95 ctx.cfg.clone_prefix = xstrdup(value); 95 ctx.cfg.clone_prefix = xstrdup(value);
96 else if (!strcmp(name, "local-time")) 96 else if (!strcmp(name, "local-time"))
97 ctx.cfg.local_time = atoi(value); 97 ctx.cfg.local_time = atoi(value);
98 else if (!strcmp(name, "repo.group")) 98 else if (!strcmp(name, "repo.group"))
99 ctx.cfg.repo_group = xstrdup(value); 99 ctx.cfg.repo_group = xstrdup(value);
100 else if (!strcmp(name, "repo.url")) 100 else if (!strcmp(name, "repo.url"))
101 ctx.repo = cgit_add_repo(value); 101 ctx.repo = cgit_add_repo(value);
102 else if (!strcmp(name, "repo.name")) 102 else if (!strcmp(name, "repo.name"))
103 ctx.repo->name = xstrdup(value); 103 ctx.repo->name = xstrdup(value);
104 else if (ctx.repo && !strcmp(name, "repo.path")) 104 else if (ctx.repo && !strcmp(name, "repo.path"))
105 ctx.repo->path = trim_end(value, '/'); 105 ctx.repo->path = trim_end(value, '/');
106 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 106 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
107 ctx.repo->clone_url = xstrdup(value); 107 ctx.repo->clone_url = xstrdup(value);
108 else if (ctx.repo && !strcmp(name, "repo.desc")) 108 else if (ctx.repo && !strcmp(name, "repo.desc"))
109 ctx.repo->desc = xstrdup(value); 109 ctx.repo->desc = xstrdup(value);
110 else if (ctx.repo && !strcmp(name, "repo.owner")) 110 else if (ctx.repo && !strcmp(name, "repo.owner"))
111 ctx.repo->owner = xstrdup(value); 111 ctx.repo->owner = xstrdup(value);
112 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 112 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
113 ctx.repo->defbranch = xstrdup(value); 113 ctx.repo->defbranch = xstrdup(value);
114 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 114 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
115 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 115 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
116 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 116 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
117 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 117 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
118 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 118 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
119 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 119 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
120 else if (ctx.repo && !strcmp(name, "repo.max-stats")) 120 else if (ctx.repo && !strcmp(name, "repo.max-stats"))
121 ctx.repo->max_stats = cgit_find_stats_period(value, NULL); 121 ctx.repo->max_stats = cgit_find_stats_period(value, NULL);
122 else if (ctx.repo && !strcmp(name, "repo.module-link")) 122 else if (ctx.repo && !strcmp(name, "repo.module-link"))
123 ctx.repo->module_link= xstrdup(value); 123 ctx.repo->module_link= xstrdup(value);
124 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 124 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
125 if (*value == '/') 125 if (*value == '/')
126 ctx.repo->readme = xstrdup(value); 126 ctx.repo->readme = xstrdup(value);
127 else 127 else
128 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 128 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
129 } else if (!strcmp(name, "include")) 129 } else if (!strcmp(name, "include"))
130 parse_configfile(value, config_cb); 130 parse_configfile(value, config_cb);
131} 131}
132 132
133static void querystring_cb(const char *name, const char *value) 133static void querystring_cb(const char *name, const char *value)
134{ 134{
135 if (!strcmp(name,"r")) { 135 if (!strcmp(name,"r")) {
136 ctx.qry.repo = xstrdup(value); 136 ctx.qry.repo = xstrdup(value);
137 ctx.repo = cgit_get_repoinfo(value); 137 ctx.repo = cgit_get_repoinfo(value);
138 } else if (!strcmp(name, "p")) { 138 } else if (!strcmp(name, "p")) {
139 ctx.qry.page = xstrdup(value); 139 ctx.qry.page = xstrdup(value);
140 } else if (!strcmp(name, "url")) { 140 } else if (!strcmp(name, "url")) {
141 ctx.qry.url = xstrdup(value); 141 ctx.qry.url = xstrdup(value);
142 cgit_parse_url(value); 142 cgit_parse_url(value);
143 } else if (!strcmp(name, "qt")) { 143 } else if (!strcmp(name, "qt")) {
144 ctx.qry.grep = xstrdup(value); 144 ctx.qry.grep = xstrdup(value);
145 } else if (!strcmp(name, "q")) { 145 } else if (!strcmp(name, "q")) {
146 ctx.qry.search = xstrdup(value); 146 ctx.qry.search = xstrdup(value);
147 } else if (!strcmp(name, "h")) { 147 } else if (!strcmp(name, "h")) {
148 ctx.qry.head = xstrdup(value); 148 ctx.qry.head = xstrdup(value);
149 ctx.qry.has_symref = 1; 149 ctx.qry.has_symref = 1;
150 } else if (!strcmp(name, "id")) { 150 } else if (!strcmp(name, "id")) {
151 ctx.qry.sha1 = xstrdup(value); 151 ctx.qry.sha1 = xstrdup(value);
152 ctx.qry.has_sha1 = 1; 152 ctx.qry.has_sha1 = 1;
153 } else if (!strcmp(name, "id2")) { 153 } else if (!strcmp(name, "id2")) {
154 ctx.qry.sha2 = xstrdup(value); 154 ctx.qry.sha2 = xstrdup(value);
155 ctx.qry.has_sha1 = 1; 155 ctx.qry.has_sha1 = 1;
156 } else if (!strcmp(name, "ofs")) { 156 } else if (!strcmp(name, "ofs")) {
157 ctx.qry.ofs = atoi(value); 157 ctx.qry.ofs = atoi(value);
158 } else if (!strcmp(name, "path")) { 158 } else if (!strcmp(name, "path")) {
159 ctx.qry.path = trim_end(value, '/'); 159 ctx.qry.path = trim_end(value, '/');
160 } else if (!strcmp(name, "name")) { 160 } else if (!strcmp(name, "name")) {
161 ctx.qry.name = xstrdup(value); 161 ctx.qry.name = xstrdup(value);
162 } else if (!strcmp(name, "mimetype")) { 162 } else if (!strcmp(name, "mimetype")) {
163 ctx.qry.mimetype = xstrdup(value); 163 ctx.qry.mimetype = xstrdup(value);
164 } else if (!strcmp(name, "s")){ 164 } else if (!strcmp(name, "s")){
165 ctx.qry.sort = xstrdup(value); 165 ctx.qry.sort = xstrdup(value);
166 } else if (!strcmp(name, "showmsg")) { 166 } else if (!strcmp(name, "showmsg")) {
167 ctx.qry.showmsg = atoi(value); 167 ctx.qry.showmsg = atoi(value);
168 } else if (!strcmp(name, "period")) { 168 } else if (!strcmp(name, "period")) {
169 ctx.qry.period = xstrdup(value); 169 ctx.qry.period = xstrdup(value);
170 } 170 }
171} 171}
172 172
173static void prepare_context(struct cgit_context *ctx) 173static void prepare_context(struct cgit_context *ctx)
174{ 174{
175 memset(ctx, 0, sizeof(ctx)); 175 memset(ctx, 0, sizeof(ctx));
176 ctx->cfg.agefile = "info/web/last-modified"; 176 ctx->cfg.agefile = "info/web/last-modified";
177 ctx->cfg.nocache = 0; 177 ctx->cfg.nocache = 0;
178 ctx->cfg.cache_size = 0; 178 ctx->cfg.cache_size = 0;
179 ctx->cfg.cache_dynamic_ttl = 5; 179 ctx->cfg.cache_dynamic_ttl = 5;
180 ctx->cfg.cache_max_create_time = 5; 180 ctx->cfg.cache_max_create_time = 5;
181 ctx->cfg.cache_repo_ttl = 5; 181 ctx->cfg.cache_repo_ttl = 5;
182 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 182 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
183 ctx->cfg.cache_root_ttl = 5; 183 ctx->cfg.cache_root_ttl = 5;
184 ctx->cfg.cache_static_ttl = -1; 184 ctx->cfg.cache_static_ttl = -1;
185 ctx->cfg.css = "/cgit.css"; 185 ctx->cfg.css = "/cgit.css";
186 ctx->cfg.logo = "/git-logo.png"; 186 ctx->cfg.logo = "/git-logo.png";
187 ctx->cfg.local_time = 0; 187 ctx->cfg.local_time = 0;
188 ctx->cfg.max_repo_count = 50; 188 ctx->cfg.max_repo_count = 50;
189 ctx->cfg.max_commit_count = 50; 189 ctx->cfg.max_commit_count = 50;
190 ctx->cfg.max_lock_attempts = 5; 190 ctx->cfg.max_lock_attempts = 5;
191 ctx->cfg.max_msg_len = 80; 191 ctx->cfg.max_msg_len = 80;
192 ctx->cfg.max_repodesc_len = 80; 192 ctx->cfg.max_repodesc_len = 80;
193 ctx->cfg.max_stats = 0; 193 ctx->cfg.max_stats = 0;
194 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 194 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
195 ctx->cfg.renamelimit = -1; 195 ctx->cfg.renamelimit = -1;
196 ctx->cfg.robots = "index, nofollow"; 196 ctx->cfg.robots = "index, nofollow";
197 ctx->cfg.root_title = "Git repository browser"; 197 ctx->cfg.root_title = "Git repository browser";
198 ctx->cfg.root_desc = "a fast webinterface for the git dscm"; 198 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
199 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 199 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
200 ctx->cfg.summary_branches = 10; 200 ctx->cfg.summary_branches = 10;
201 ctx->cfg.summary_log = 10; 201 ctx->cfg.summary_log = 10;
202 ctx->cfg.summary_tags = 10; 202 ctx->cfg.summary_tags = 10;
203 ctx->page.mimetype = "text/html"; 203 ctx->page.mimetype = "text/html";
204 ctx->page.charset = PAGE_ENCODING; 204 ctx->page.charset = PAGE_ENCODING;
205 ctx->page.filename = NULL; 205 ctx->page.filename = NULL;
206 ctx->page.size = 0; 206 ctx->page.size = 0;
207 ctx->page.modified = time(NULL); 207 ctx->page.modified = time(NULL);
208 ctx->page.expires = ctx->page.modified; 208 ctx->page.expires = ctx->page.modified;
209 ctx->page.etag = NULL;
209} 210}
210 211
211struct refmatch { 212struct refmatch {
212 char *req_ref; 213 char *req_ref;
213 char *first_ref; 214 char *first_ref;
214 int match; 215 int match;
215}; 216};
216 217
217int find_current_ref(const char *refname, const unsigned char *sha1, 218int find_current_ref(const char *refname, const unsigned char *sha1,
218 int flags, void *cb_data) 219 int flags, void *cb_data)
219{ 220{
220 struct refmatch *info; 221 struct refmatch *info;
221 222
222 info = (struct refmatch *)cb_data; 223 info = (struct refmatch *)cb_data;
223 if (!strcmp(refname, info->req_ref)) 224 if (!strcmp(refname, info->req_ref))
224 info->match = 1; 225 info->match = 1;
225 if (!info->first_ref) 226 if (!info->first_ref)
226 info->first_ref = xstrdup(refname); 227 info->first_ref = xstrdup(refname);
227 return info->match; 228 return info->match;
228} 229}
229 230
230char *find_default_branch(struct cgit_repo *repo) 231char *find_default_branch(struct cgit_repo *repo)
231{ 232{
232 struct refmatch info; 233 struct refmatch info;
233 char *ref; 234 char *ref;
234 235
235 info.req_ref = repo->defbranch; 236 info.req_ref = repo->defbranch;
236 info.first_ref = NULL; 237 info.first_ref = NULL;
237 info.match = 0; 238 info.match = 0;
238 for_each_branch_ref(find_current_ref, &info); 239 for_each_branch_ref(find_current_ref, &info);
239 if (info.match) 240 if (info.match)
240 ref = info.req_ref; 241 ref = info.req_ref;
241 else 242 else
242 ref = info.first_ref; 243 ref = info.first_ref;
243 if (ref) 244 if (ref)
244 ref = xstrdup(ref); 245 ref = xstrdup(ref);
245 return ref; 246 return ref;
246} 247}
247 248
248static int prepare_repo_cmd(struct cgit_context *ctx) 249static int prepare_repo_cmd(struct cgit_context *ctx)
249{ 250{
250 char *tmp; 251 char *tmp;
251 unsigned char sha1[20]; 252 unsigned char sha1[20];
252 int nongit = 0; 253 int nongit = 0;
253 254
254 setenv("GIT_DIR", ctx->repo->path, 1); 255 setenv("GIT_DIR", ctx->repo->path, 1);
255 setup_git_directory_gently(&nongit); 256 setup_git_directory_gently(&nongit);
256 if (nongit) { 257 if (nongit) {
257 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, 258 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
258 "config error"); 259 "config error");
259 tmp = fmt("Not a git repository: '%s'", ctx->repo->path); 260 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
260 ctx->repo = NULL; 261 ctx->repo = NULL;
261 cgit_print_http_headers(ctx); 262 cgit_print_http_headers(ctx);
262 cgit_print_docstart(ctx); 263 cgit_print_docstart(ctx);
263 cgit_print_pageheader(ctx); 264 cgit_print_pageheader(ctx);
264 cgit_print_error(tmp); 265 cgit_print_error(tmp);
265 cgit_print_docend(); 266 cgit_print_docend();
266 return 1; 267 return 1;
267 } 268 }
268 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); 269 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
269 270
270 if (!ctx->qry.head) { 271 if (!ctx->qry.head) {
271 ctx->qry.nohead = 1; 272 ctx->qry.nohead = 1;
272 ctx->qry.head = find_default_branch(ctx->repo); 273 ctx->qry.head = find_default_branch(ctx->repo);
273 ctx->repo->defbranch = ctx->qry.head; 274 ctx->repo->defbranch = ctx->qry.head;
274 } 275 }
275 276
276 if (!ctx->qry.head) { 277 if (!ctx->qry.head) {
277 cgit_print_http_headers(ctx); 278 cgit_print_http_headers(ctx);
278 cgit_print_docstart(ctx); 279 cgit_print_docstart(ctx);
279 cgit_print_pageheader(ctx); 280 cgit_print_pageheader(ctx);
280 cgit_print_error("Repository seems to be empty"); 281 cgit_print_error("Repository seems to be empty");
281 cgit_print_docend(); 282 cgit_print_docend();
282 return 1; 283 return 1;
283 } 284 }
284 285
285 if (get_sha1(ctx->qry.head, sha1)) { 286 if (get_sha1(ctx->qry.head, sha1)) {
286 tmp = xstrdup(ctx->qry.head); 287 tmp = xstrdup(ctx->qry.head);
287 ctx->qry.head = ctx->repo->defbranch; 288 ctx->qry.head = ctx->repo->defbranch;
288 ctx->page.status = 404; 289 ctx->page.status = 404;
289 ctx->page.statusmsg = "not found"; 290 ctx->page.statusmsg = "not found";
290 cgit_print_http_headers(ctx); 291 cgit_print_http_headers(ctx);
291 cgit_print_docstart(ctx); 292 cgit_print_docstart(ctx);
292 cgit_print_pageheader(ctx); 293 cgit_print_pageheader(ctx);
293 cgit_print_error(fmt("Invalid branch: %s", tmp)); 294 cgit_print_error(fmt("Invalid branch: %s", tmp));
294 cgit_print_docend(); 295 cgit_print_docend();
295 return 1; 296 return 1;
296 } 297 }
297 return 0; 298 return 0;
298} 299}
299 300
300static void process_request(void *cbdata) 301static void process_request(void *cbdata)
301{ 302{
302 struct cgit_context *ctx = cbdata; 303 struct cgit_context *ctx = cbdata;
303 struct cgit_cmd *cmd; 304 struct cgit_cmd *cmd;
304 305
305 cmd = cgit_get_cmd(ctx); 306 cmd = cgit_get_cmd(ctx);
306 if (!cmd) { 307 if (!cmd) {
307 ctx->page.title = "cgit error"; 308 ctx->page.title = "cgit error";
308 cgit_print_http_headers(ctx); 309 cgit_print_http_headers(ctx);
309 cgit_print_docstart(ctx); 310 cgit_print_docstart(ctx);
310 cgit_print_pageheader(ctx); 311 cgit_print_pageheader(ctx);
311 cgit_print_error("Invalid request"); 312 cgit_print_error("Invalid request");
312 cgit_print_docend(); 313 cgit_print_docend();
313 return; 314 return;
314 } 315 }
315 316
316 if (cmd->want_repo && !ctx->repo) { 317 if (cmd->want_repo && !ctx->repo) {
317 cgit_print_http_headers(ctx); 318 cgit_print_http_headers(ctx);
318 cgit_print_docstart(ctx); 319 cgit_print_docstart(ctx);
319 cgit_print_pageheader(ctx); 320 cgit_print_pageheader(ctx);
320 cgit_print_error(fmt("No repository selected")); 321 cgit_print_error(fmt("No repository selected"));
321 cgit_print_docend(); 322 cgit_print_docend();
322 return; 323 return;
323 } 324 }
324 325
325 if (ctx->repo && prepare_repo_cmd(ctx)) 326 if (ctx->repo && prepare_repo_cmd(ctx))
326 return; 327 return;
327 328
328 if (cmd->want_layout) { 329 if (cmd->want_layout) {
329 cgit_print_http_headers(ctx); 330 cgit_print_http_headers(ctx);
330 cgit_print_docstart(ctx); 331 cgit_print_docstart(ctx);
331 cgit_print_pageheader(ctx); 332 cgit_print_pageheader(ctx);
332 } 333 }
333 334
334 cmd->fn(ctx); 335 cmd->fn(ctx);
335 336
336 if (cmd->want_layout) 337 if (cmd->want_layout)
337 cgit_print_docend(); 338 cgit_print_docend();
338} 339}
339 340
340int cmp_repos(const void *a, const void *b) 341int cmp_repos(const void *a, const void *b)
341{ 342{
342 const struct cgit_repo *ra = a, *rb = b; 343 const struct cgit_repo *ra = a, *rb = b;
343 return strcmp(ra->url, rb->url); 344 return strcmp(ra->url, rb->url);
344} 345}
345 346
346void print_repo(struct cgit_repo *repo) 347void print_repo(struct cgit_repo *repo)
347{ 348{
348 printf("repo.url=%s\n", repo->url); 349 printf("repo.url=%s\n", repo->url);
349 printf("repo.name=%s\n", repo->name); 350 printf("repo.name=%s\n", repo->name);
350 printf("repo.path=%s\n", repo->path); 351 printf("repo.path=%s\n", repo->path);
351 if (repo->owner) 352 if (repo->owner)
352 printf("repo.owner=%s\n", repo->owner); 353 printf("repo.owner=%s\n", repo->owner);
353 if (repo->desc) 354 if (repo->desc)
354 printf("repo.desc=%s\n", repo->desc); 355 printf("repo.desc=%s\n", repo->desc);
355 if (repo->readme) 356 if (repo->readme)
356 printf("repo.readme=%s\n", repo->readme); 357 printf("repo.readme=%s\n", repo->readme);
357 printf("\n"); 358 printf("\n");
358} 359}
359 360
360void print_repolist(struct cgit_repolist *list) 361void print_repolist(struct cgit_repolist *list)
361{ 362{
362 int i; 363 int i;
363 364
364 for(i = 0; i < list->count; i++) 365 for(i = 0; i < list->count; i++)
365 print_repo(&list->repos[i]); 366 print_repo(&list->repos[i]);
366} 367}
367 368
368 369
369static void cgit_parse_args(int argc, const char **argv) 370static void cgit_parse_args(int argc, const char **argv)
370{ 371{
371 int i; 372 int i;
372 int scan = 0; 373 int scan = 0;
373 374
374 for (i = 1; i < argc; i++) { 375 for (i = 1; i < argc; i++) {
375 if (!strncmp(argv[i], "--cache=", 8)) { 376 if (!strncmp(argv[i], "--cache=", 8)) {
376 ctx.cfg.cache_root = xstrdup(argv[i]+8); 377 ctx.cfg.cache_root = xstrdup(argv[i]+8);
377 } 378 }
378 if (!strcmp(argv[i], "--nocache")) { 379 if (!strcmp(argv[i], "--nocache")) {
379 ctx.cfg.nocache = 1; 380 ctx.cfg.nocache = 1;
380 } 381 }
381 if (!strncmp(argv[i], "--query=", 8)) { 382 if (!strncmp(argv[i], "--query=", 8)) {
382 ctx.qry.raw = xstrdup(argv[i]+8); 383 ctx.qry.raw = xstrdup(argv[i]+8);
383 } 384 }
384 if (!strncmp(argv[i], "--repo=", 7)) { 385 if (!strncmp(argv[i], "--repo=", 7)) {
385 ctx.qry.repo = xstrdup(argv[i]+7); 386 ctx.qry.repo = xstrdup(argv[i]+7);
386 } 387 }
387 if (!strncmp(argv[i], "--page=", 7)) { 388 if (!strncmp(argv[i], "--page=", 7)) {
388 ctx.qry.page = xstrdup(argv[i]+7); 389 ctx.qry.page = xstrdup(argv[i]+7);
389 } 390 }
390 if (!strncmp(argv[i], "--head=", 7)) { 391 if (!strncmp(argv[i], "--head=", 7)) {
391 ctx.qry.head = xstrdup(argv[i]+7); 392 ctx.qry.head = xstrdup(argv[i]+7);
392 ctx.qry.has_symref = 1; 393 ctx.qry.has_symref = 1;
393 } 394 }
394 if (!strncmp(argv[i], "--sha1=", 7)) { 395 if (!strncmp(argv[i], "--sha1=", 7)) {
395 ctx.qry.sha1 = xstrdup(argv[i]+7); 396 ctx.qry.sha1 = xstrdup(argv[i]+7);
396 ctx.qry.has_sha1 = 1; 397 ctx.qry.has_sha1 = 1;
397 } 398 }
398 if (!strncmp(argv[i], "--ofs=", 6)) { 399 if (!strncmp(argv[i], "--ofs=", 6)) {
399 ctx.qry.ofs = atoi(argv[i]+6); 400 ctx.qry.ofs = atoi(argv[i]+6);
400 } 401 }
401 if (!strncmp(argv[i], "--scan-tree=", 12)) { 402 if (!strncmp(argv[i], "--scan-tree=", 12)) {
402 scan++; 403 scan++;
403 scan_tree(argv[i] + 12); 404 scan_tree(argv[i] + 12);
404 } 405 }
405 } 406 }
406 if (scan) { 407 if (scan) {
407 qsort(cgit_repolist.repos, cgit_repolist.count, 408 qsort(cgit_repolist.repos, cgit_repolist.count,
408 sizeof(struct cgit_repo), cmp_repos); 409 sizeof(struct cgit_repo), cmp_repos);
409 print_repolist(&cgit_repolist); 410 print_repolist(&cgit_repolist);
410 exit(0); 411 exit(0);
411 } 412 }
412} 413}
413 414
414static int calc_ttl() 415static int calc_ttl()
415{ 416{
416 if (!ctx.repo) 417 if (!ctx.repo)
417 return ctx.cfg.cache_root_ttl; 418 return ctx.cfg.cache_root_ttl;
418 419
419 if (!ctx.qry.page) 420 if (!ctx.qry.page)
420 return ctx.cfg.cache_repo_ttl; 421 return ctx.cfg.cache_repo_ttl;
421 422
422 if (ctx.qry.has_symref) 423 if (ctx.qry.has_symref)
423 return ctx.cfg.cache_dynamic_ttl; 424 return ctx.cfg.cache_dynamic_ttl;
424 425
425 if (ctx.qry.has_sha1) 426 if (ctx.qry.has_sha1)
426 return ctx.cfg.cache_static_ttl; 427 return ctx.cfg.cache_static_ttl;
427 428
428 return ctx.cfg.cache_repo_ttl; 429 return ctx.cfg.cache_repo_ttl;
429} 430}
430 431
431int main(int argc, const char **argv) 432int main(int argc, const char **argv)
432{ 433{
433 const char *cgit_config_env = getenv("CGIT_CONFIG"); 434 const char *cgit_config_env = getenv("CGIT_CONFIG");
435 const char *method = getenv("REQUEST_METHOD");
434 const char *path; 436 const char *path;
435 char *qry; 437 char *qry;
436 int err, ttl; 438 int err, ttl;
437 439
438 prepare_context(&ctx); 440 prepare_context(&ctx);
439 cgit_repolist.length = 0; 441 cgit_repolist.length = 0;
440 cgit_repolist.count = 0; 442 cgit_repolist.count = 0;
441 cgit_repolist.repos = NULL; 443 cgit_repolist.repos = NULL;
442 444
443 if (getenv("SCRIPT_NAME")) 445 if (getenv("SCRIPT_NAME"))
444 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 446 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
445 if (getenv("QUERY_STRING")) 447 if (getenv("QUERY_STRING"))
446 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 448 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
447 cgit_parse_args(argc, argv); 449 cgit_parse_args(argc, argv);
448 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 450 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
449 config_cb); 451 config_cb);
450 ctx.repo = NULL; 452 ctx.repo = NULL;
451 http_parse_querystring(ctx.qry.raw, querystring_cb); 453 http_parse_querystring(ctx.qry.raw, querystring_cb);
452 454
453 /* If virtual-root isn't specified in cgitrc, lets pretend 455 /* If virtual-root isn't specified in cgitrc, lets pretend
454 * that virtual-root equals SCRIPT_NAME. 456 * that virtual-root equals SCRIPT_NAME.
455 */ 457 */
456 if (!ctx.cfg.virtual_root) 458 if (!ctx.cfg.virtual_root)
457 ctx.cfg.virtual_root = ctx.cfg.script_name; 459 ctx.cfg.virtual_root = ctx.cfg.script_name;
458 460
459 /* If no url parameter is specified on the querystring, lets 461 /* If no url parameter is specified on the querystring, lets
460 * use PATH_INFO as url. This allows cgit to work with virtual 462 * use PATH_INFO as url. This allows cgit to work with virtual
461 * urls without the need for rewriterules in the webserver (as 463 * urls without the need for rewriterules in the webserver (as
462 * long as PATH_INFO is included in the cache lookup key). 464 * long as PATH_INFO is included in the cache lookup key).
463 */ 465 */
464 path = getenv("PATH_INFO"); 466 path = getenv("PATH_INFO");
465 if (!ctx.qry.url && path) { 467 if (!ctx.qry.url && path) {
466 if (path[0] == '/') 468 if (path[0] == '/')
467 path++; 469 path++;
468 ctx.qry.url = xstrdup(path); 470 ctx.qry.url = xstrdup(path);
469 if (ctx.qry.raw) { 471 if (ctx.qry.raw) {
470 qry = ctx.qry.raw; 472 qry = ctx.qry.raw;
471 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); 473 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
472 free(qry); 474 free(qry);
473 } else 475 } else
474 ctx.qry.raw = ctx.qry.url; 476 ctx.qry.raw = ctx.qry.url;
475 cgit_parse_url(ctx.qry.url); 477 cgit_parse_url(ctx.qry.url);
476 } 478 }
477 479
478 ttl = calc_ttl(); 480 ttl = calc_ttl();
479 ctx.page.expires += ttl*60; 481 ctx.page.expires += ttl*60;
482 if (method && !strcmp(method, "HEAD"))
483 ctx.cfg.nocache = 1;
480 if (ctx.cfg.nocache) 484 if (ctx.cfg.nocache)
481 ctx.cfg.cache_size = 0; 485 ctx.cfg.cache_size = 0;
482 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, 486 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
483 ctx.qry.raw, ttl, process_request, &ctx); 487 ctx.qry.raw, ttl, process_request, &ctx);
484 if (err) 488 if (err)
485 cgit_print_error(fmt("Error processing page: %s (%d)", 489 cgit_print_error(fmt("Error processing page: %s (%d)",
486 strerror(err), err)); 490 strerror(err), err));
487 return err; 491 return err;
488} 492}
diff --git a/cgit.h b/cgit.h
index 00aca4c..07a277a 100644
--- a/cgit.h
+++ b/cgit.h
@@ -1,248 +1,249 @@
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-interface.h> 18#include <xdiff-interface.h>
19#include <xdiff/xdiff.h> 19#include <xdiff/xdiff.h>
20#include <utf8.h> 20#include <utf8.h>
21 21
22 22
23/* 23/*
24 * Dateformats used on misc. pages 24 * Dateformats used on misc. pages
25 */ 25 */
26#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S (%Z)" 26#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S (%Z)"
27#define FMT_SHORTDATE "%Y-%m-%d" 27#define FMT_SHORTDATE "%Y-%m-%d"
28#define FMT_ATOMDATE "%Y-%m-%dT%H:%M:%SZ" 28#define FMT_ATOMDATE "%Y-%m-%dT%H:%M:%SZ"
29 29
30 30
31/* 31/*
32 * Limits used for relative dates 32 * Limits used for relative dates
33 */ 33 */
34#define TM_MIN 60 34#define TM_MIN 60
35#define TM_HOUR (TM_MIN * 60) 35#define TM_HOUR (TM_MIN * 60)
36#define TM_DAY (TM_HOUR * 24) 36#define TM_DAY (TM_HOUR * 24)
37#define TM_WEEK (TM_DAY * 7) 37#define TM_WEEK (TM_DAY * 7)
38#define TM_YEAR (TM_DAY * 365) 38#define TM_YEAR (TM_DAY * 365)
39#define TM_MONTH (TM_YEAR / 12.0) 39#define TM_MONTH (TM_YEAR / 12.0)
40 40
41 41
42/* 42/*
43 * Default encoding 43 * Default encoding
44 */ 44 */
45#define PAGE_ENCODING "UTF-8" 45#define PAGE_ENCODING "UTF-8"
46 46
47typedef void (*configfn)(const char *name, const char *value); 47typedef void (*configfn)(const char *name, const char *value);
48typedef void (*filepair_fn)(struct diff_filepair *pair); 48typedef void (*filepair_fn)(struct diff_filepair *pair);
49typedef void (*linediff_fn)(char *line, int len); 49typedef void (*linediff_fn)(char *line, int len);
50 50
51struct cgit_repo { 51struct cgit_repo {
52 char *url; 52 char *url;
53 char *name; 53 char *name;
54 char *path; 54 char *path;
55 char *desc; 55 char *desc;
56 char *owner; 56 char *owner;
57 char *defbranch; 57 char *defbranch;
58 char *group; 58 char *group;
59 char *module_link; 59 char *module_link;
60 char *readme; 60 char *readme;
61 char *clone_url; 61 char *clone_url;
62 int snapshots; 62 int snapshots;
63 int enable_log_filecount; 63 int enable_log_filecount;
64 int enable_log_linecount; 64 int enable_log_linecount;
65 int max_stats; 65 int max_stats;
66 time_t mtime; 66 time_t mtime;
67}; 67};
68 68
69struct cgit_repolist { 69struct cgit_repolist {
70 int length; 70 int length;
71 int count; 71 int count;
72 struct cgit_repo *repos; 72 struct cgit_repo *repos;
73}; 73};
74 74
75struct commitinfo { 75struct commitinfo {
76 struct commit *commit; 76 struct commit *commit;
77 char *author; 77 char *author;
78 char *author_email; 78 char *author_email;
79 unsigned long author_date; 79 unsigned long author_date;
80 char *committer; 80 char *committer;
81 char *committer_email; 81 char *committer_email;
82 unsigned long committer_date; 82 unsigned long committer_date;
83 char *subject; 83 char *subject;
84 char *msg; 84 char *msg;
85 char *msg_encoding; 85 char *msg_encoding;
86}; 86};
87 87
88struct taginfo { 88struct taginfo {
89 char *tagger; 89 char *tagger;
90 char *tagger_email; 90 char *tagger_email;
91 unsigned long tagger_date; 91 unsigned long tagger_date;
92 char *msg; 92 char *msg;
93}; 93};
94 94
95struct refinfo { 95struct refinfo {
96 const char *refname; 96 const char *refname;
97 struct object *object; 97 struct object *object;
98 union { 98 union {
99 struct taginfo *tag; 99 struct taginfo *tag;
100 struct commitinfo *commit; 100 struct commitinfo *commit;
101 }; 101 };
102}; 102};
103 103
104struct reflist { 104struct reflist {
105 struct refinfo **refs; 105 struct refinfo **refs;
106 int alloc; 106 int alloc;
107 int count; 107 int count;
108}; 108};
109 109
110struct cgit_query { 110struct cgit_query {
111 int has_symref; 111 int has_symref;
112 int has_sha1; 112 int has_sha1;
113 char *raw; 113 char *raw;
114 char *repo; 114 char *repo;
115 char *page; 115 char *page;
116 char *search; 116 char *search;
117 char *grep; 117 char *grep;
118 char *head; 118 char *head;
119 char *sha1; 119 char *sha1;
120 char *sha2; 120 char *sha2;
121 char *path; 121 char *path;
122 char *name; 122 char *name;
123 char *mimetype; 123 char *mimetype;
124 char *url; 124 char *url;
125 char *period; 125 char *period;
126 int ofs; 126 int ofs;
127 int nohead; 127 int nohead;
128 char *sort; 128 char *sort;
129 int showmsg; 129 int showmsg;
130}; 130};
131 131
132struct cgit_config { 132struct cgit_config {
133 char *agefile; 133 char *agefile;
134 char *cache_root; 134 char *cache_root;
135 char *clone_prefix; 135 char *clone_prefix;
136 char *css; 136 char *css;
137 char *favicon; 137 char *favicon;
138 char *footer; 138 char *footer;
139 char *header; 139 char *header;
140 char *index_header; 140 char *index_header;
141 char *index_info; 141 char *index_info;
142 char *logo; 142 char *logo;
143 char *logo_link; 143 char *logo_link;
144 char *module_link; 144 char *module_link;
145 char *repo_group; 145 char *repo_group;
146 char *robots; 146 char *robots;
147 char *root_title; 147 char *root_title;
148 char *root_desc; 148 char *root_desc;
149 char *root_readme; 149 char *root_readme;
150 char *script_name; 150 char *script_name;
151 char *virtual_root; 151 char *virtual_root;
152 int cache_size; 152 int cache_size;
153 int cache_dynamic_ttl; 153 int cache_dynamic_ttl;
154 int cache_max_create_time; 154 int cache_max_create_time;
155 int cache_repo_ttl; 155 int cache_repo_ttl;
156 int cache_root_ttl; 156 int cache_root_ttl;
157 int cache_static_ttl; 157 int cache_static_ttl;
158 int enable_index_links; 158 int enable_index_links;
159 int enable_log_filecount; 159 int enable_log_filecount;
160 int enable_log_linecount; 160 int enable_log_linecount;
161 int local_time; 161 int local_time;
162 int max_repo_count; 162 int max_repo_count;
163 int max_commit_count; 163 int max_commit_count;
164 int max_lock_attempts; 164 int max_lock_attempts;
165 int max_msg_len; 165 int max_msg_len;
166 int max_repodesc_len; 166 int max_repodesc_len;
167 int max_stats; 167 int max_stats;
168 int nocache; 168 int nocache;
169 int renamelimit; 169 int renamelimit;
170 int snapshots; 170 int snapshots;
171 int summary_branches; 171 int summary_branches;
172 int summary_log; 172 int summary_log;
173 int summary_tags; 173 int summary_tags;
174}; 174};
175 175
176struct cgit_page { 176struct cgit_page {
177 time_t modified; 177 time_t modified;
178 time_t expires; 178 time_t expires;
179 size_t size; 179 size_t size;
180 char *mimetype; 180 char *mimetype;
181 char *charset; 181 char *charset;
182 char *filename; 182 char *filename;
183 char *etag;
183 char *title; 184 char *title;
184 int status; 185 int status;
185 char *statusmsg; 186 char *statusmsg;
186}; 187};
187 188
188struct cgit_context { 189struct cgit_context {
189 struct cgit_query qry; 190 struct cgit_query qry;
190 struct cgit_config cfg; 191 struct cgit_config cfg;
191 struct cgit_repo *repo; 192 struct cgit_repo *repo;
192 struct cgit_page page; 193 struct cgit_page page;
193}; 194};
194 195
195struct cgit_snapshot_format { 196struct cgit_snapshot_format {
196 const char *suffix; 197 const char *suffix;
197 const char *mimetype; 198 const char *mimetype;
198 write_archive_fn_t write_func; 199 write_archive_fn_t write_func;
199 int bit; 200 int bit;
200}; 201};
201 202
202extern const char *cgit_version; 203extern const char *cgit_version;
203 204
204extern struct cgit_repolist cgit_repolist; 205extern struct cgit_repolist cgit_repolist;
205extern struct cgit_context ctx; 206extern struct cgit_context ctx;
206extern const struct cgit_snapshot_format cgit_snapshot_formats[]; 207extern const struct cgit_snapshot_format cgit_snapshot_formats[];
207 208
208extern struct cgit_repo *cgit_add_repo(const char *url); 209extern struct cgit_repo *cgit_add_repo(const char *url);
209extern struct cgit_repo *cgit_get_repoinfo(const char *url); 210extern struct cgit_repo *cgit_get_repoinfo(const char *url);
210extern void cgit_repo_config_cb(const char *name, const char *value); 211extern void cgit_repo_config_cb(const char *name, const char *value);
211 212
212extern int chk_zero(int result, char *msg); 213extern int chk_zero(int result, char *msg);
213extern int chk_positive(int result, char *msg); 214extern int chk_positive(int result, char *msg);
214extern int chk_non_negative(int result, char *msg); 215extern int chk_non_negative(int result, char *msg);
215 216
216extern char *trim_end(const char *str, char c); 217extern char *trim_end(const char *str, char c);
217extern char *strlpart(char *txt, int maxlen); 218extern char *strlpart(char *txt, int maxlen);
218extern char *strrpart(char *txt, int maxlen); 219extern char *strrpart(char *txt, int maxlen);
219 220
220extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 221extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
221extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 222extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
222 int flags, void *cb_data); 223 int flags, void *cb_data);
223 224
224extern void *cgit_free_commitinfo(struct commitinfo *info); 225extern void *cgit_free_commitinfo(struct commitinfo *info);
225 226
226extern int cgit_diff_files(const unsigned char *old_sha1, 227extern int cgit_diff_files(const unsigned char *old_sha1,
227 const unsigned char *new_sha1, 228 const unsigned char *new_sha1,
228 unsigned long *old_size, unsigned long *new_size, 229 unsigned long *old_size, unsigned long *new_size,
229 int *binary, linediff_fn fn); 230 int *binary, linediff_fn fn);
230 231
231extern void cgit_diff_tree(const unsigned char *old_sha1, 232extern void cgit_diff_tree(const unsigned char *old_sha1,
232 const unsigned char *new_sha1, 233 const unsigned char *new_sha1,
233 filepair_fn fn, const char *prefix); 234 filepair_fn fn, const char *prefix);
234 235
235extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 236extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
236 237
237extern char *fmt(const char *format,...); 238extern char *fmt(const char *format,...);
238 239
239extern struct commitinfo *cgit_parse_commit(struct commit *commit); 240extern struct commitinfo *cgit_parse_commit(struct commit *commit);
240extern struct taginfo *cgit_parse_tag(struct tag *tag); 241extern struct taginfo *cgit_parse_tag(struct tag *tag);
241extern void cgit_parse_url(const char *url); 242extern void cgit_parse_url(const char *url);
242 243
243extern const char *cgit_repobasename(const char *reponame); 244extern const char *cgit_repobasename(const char *reponame);
244 245
245extern int cgit_parse_snapshots_mask(const char *str); 246extern int cgit_parse_snapshots_mask(const char *str);
246 247
247 248
248#endif /* CGIT_H */ 249#endif /* CGIT_H */
diff --git a/ui-plain.c b/ui-plain.c
index 9a9ae7d..93a3a05 100644
--- a/ui-plain.c
+++ b/ui-plain.c
@@ -1,82 +1,83 @@
1/* ui-plain.c: functions for output of plain blobs by path 1/* ui-plain.c: functions for output of plain blobs by path
2 * 2 *
3 * Copyright (C) 2008 Lars Hjemli 3 * Copyright (C) 2008 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 "html.h" 10#include "html.h"
11#include "ui-shared.h" 11#include "ui-shared.h"
12 12
13char *curr_rev; 13char *curr_rev;
14char *match_path; 14char *match_path;
15int match; 15int match;
16 16
17static void print_object(const unsigned char *sha1, const char *path) 17static void print_object(const unsigned char *sha1, const char *path)
18{ 18{
19 enum object_type type; 19 enum object_type type;
20 char *buf; 20 char *buf;
21 unsigned long size; 21 unsigned long size;
22 22
23 type = sha1_object_info(sha1, &size); 23 type = sha1_object_info(sha1, &size);
24 if (type == OBJ_BAD) { 24 if (type == OBJ_BAD) {
25 html_status(404, "Not found", 0); 25 html_status(404, "Not found", 0);
26 return; 26 return;
27 } 27 }
28 28
29 buf = read_sha1_file(sha1, &type, &size); 29 buf = read_sha1_file(sha1, &type, &size);
30 if (!buf) { 30 if (!buf) {
31 html_status(404, "Not found", 0); 31 html_status(404, "Not found", 0);
32 return; 32 return;
33 } 33 }
34 if (buffer_is_binary(buf, size)) 34 if (buffer_is_binary(buf, size))
35 ctx.page.mimetype = "application/octet-stream"; 35 ctx.page.mimetype = "application/octet-stream";
36 else 36 else
37 ctx.page.mimetype = "text/plain"; 37 ctx.page.mimetype = "text/plain";
38 ctx.page.filename = fmt("%s", path); 38 ctx.page.filename = fmt("%s", path);
39 ctx.page.size = size; 39 ctx.page.size = size;
40 ctx.page.etag = sha1_to_hex(sha1);
40 cgit_print_http_headers(&ctx); 41 cgit_print_http_headers(&ctx);
41 html_raw(buf, size); 42 html_raw(buf, size);
42 match = 1; 43 match = 1;
43} 44}
44 45
45static int walk_tree(const unsigned char *sha1, const char *base, int baselen, 46static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
46 const char *pathname, unsigned mode, int stage, 47 const char *pathname, unsigned mode, int stage,
47 void *cbdata) 48 void *cbdata)
48{ 49{
49 if (S_ISDIR(mode)) 50 if (S_ISDIR(mode))
50 return READ_TREE_RECURSIVE; 51 return READ_TREE_RECURSIVE;
51 52
52 if (S_ISREG(mode)) 53 if (S_ISREG(mode))
53 print_object(sha1, pathname); 54 print_object(sha1, pathname);
54 55
55 return 0; 56 return 0;
56} 57}
57 58
58void cgit_print_plain(struct cgit_context *ctx) 59void cgit_print_plain(struct cgit_context *ctx)
59{ 60{
60 const char *rev = ctx->qry.sha1; 61 const char *rev = ctx->qry.sha1;
61 unsigned char sha1[20]; 62 unsigned char sha1[20];
62 struct commit *commit; 63 struct commit *commit;
63 const char *paths[] = {ctx->qry.path, NULL}; 64 const char *paths[] = {ctx->qry.path, NULL};
64 65
65 if (!rev) 66 if (!rev)
66 rev = ctx->qry.head; 67 rev = ctx->qry.head;
67 68
68 curr_rev = xstrdup(rev); 69 curr_rev = xstrdup(rev);
69 if (get_sha1(rev, sha1)) { 70 if (get_sha1(rev, sha1)) {
70 html_status(404, "Not found", 0); 71 html_status(404, "Not found", 0);
71 return; 72 return;
72 } 73 }
73 commit = lookup_commit_reference(sha1); 74 commit = lookup_commit_reference(sha1);
74 if (!commit || parse_commit(commit)) { 75 if (!commit || parse_commit(commit)) {
75 html_status(404, "Not found", 0); 76 html_status(404, "Not found", 0);
76 return; 77 return;
77 } 78 }
78 match_path = ctx->qry.path; 79 match_path = ctx->qry.path;
79 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); 80 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
80 if (!match) 81 if (!match)
81 html_status(404, "Not found", 0); 82 html_status(404, "Not found", 0);
82} 83}
diff --git a/ui-shared.c b/ui-shared.c
index 29036d0..10be3c0 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -1,753 +1,759 @@
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_httpscheme() 37char *cgit_httpscheme()
38{ 38{
39 char *https; 39 char *https;
40 40
41 https = getenv("HTTPS"); 41 https = getenv("HTTPS");
42 if (https != NULL && strcmp(https, "on") == 0) 42 if (https != NULL && strcmp(https, "on") == 0)
43 return "https://"; 43 return "https://";
44 else 44 else
45 return "http://"; 45 return "http://";
46} 46}
47 47
48char *cgit_hosturl() 48char *cgit_hosturl()
49{ 49{
50 char *host, *port; 50 char *host, *port;
51 51
52 host = getenv("HTTP_HOST"); 52 host = getenv("HTTP_HOST");
53 if (host) { 53 if (host) {
54 host = xstrdup(host); 54 host = xstrdup(host);
55 } else { 55 } else {
56 host = getenv("SERVER_NAME"); 56 host = getenv("SERVER_NAME");
57 if (!host) 57 if (!host)
58 return NULL; 58 return NULL;
59 port = getenv("SERVER_PORT"); 59 port = getenv("SERVER_PORT");
60 if (port && atoi(port) != 80) 60 if (port && atoi(port) != 80)
61 host = xstrdup(fmt("%s:%d", host, atoi(port))); 61 host = xstrdup(fmt("%s:%d", host, atoi(port)));
62 else 62 else
63 host = xstrdup(host); 63 host = xstrdup(host);
64 } 64 }
65 return host; 65 return host;
66} 66}
67 67
68char *cgit_rooturl() 68char *cgit_rooturl()
69{ 69{
70 if (ctx.cfg.virtual_root) 70 if (ctx.cfg.virtual_root)
71 return fmt("%s/", ctx.cfg.virtual_root); 71 return fmt("%s/", ctx.cfg.virtual_root);
72 else 72 else
73 return ctx.cfg.script_name; 73 return ctx.cfg.script_name;
74} 74}
75 75
76char *cgit_repourl(const char *reponame) 76char *cgit_repourl(const char *reponame)
77{ 77{
78 if (ctx.cfg.virtual_root) { 78 if (ctx.cfg.virtual_root) {
79 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame); 79 return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
80 } else { 80 } else {
81 return fmt("?r=%s", reponame); 81 return fmt("?r=%s", reponame);
82 } 82 }
83} 83}
84 84
85char *cgit_fileurl(const char *reponame, const char *pagename, 85char *cgit_fileurl(const char *reponame, const char *pagename,
86 const char *filename, const char *query) 86 const char *filename, const char *query)
87{ 87{
88 char *tmp; 88 char *tmp;
89 char *delim; 89 char *delim;
90 90
91 if (ctx.cfg.virtual_root) { 91 if (ctx.cfg.virtual_root) {
92 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame, 92 tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
93 pagename, (filename ? filename:"")); 93 pagename, (filename ? filename:""));
94 delim = "?"; 94 delim = "?";
95 } else { 95 } else {
96 tmp = fmt("?url=%s/%s/%s", reponame, pagename, 96 tmp = fmt("?url=%s/%s/%s", reponame, pagename,
97 (filename ? filename : "")); 97 (filename ? filename : ""));
98 delim = "&"; 98 delim = "&";
99 } 99 }
100 if (query) 100 if (query)
101 tmp = fmt("%s%s%s", tmp, delim, query); 101 tmp = fmt("%s%s%s", tmp, delim, query);
102 return tmp; 102 return tmp;
103} 103}
104 104
105char *cgit_pageurl(const char *reponame, const char *pagename, 105char *cgit_pageurl(const char *reponame, const char *pagename,
106 const char *query) 106 const char *query)
107{ 107{
108 return cgit_fileurl(reponame,pagename,0,query); 108 return cgit_fileurl(reponame,pagename,0,query);
109} 109}
110 110
111const char *cgit_repobasename(const char *reponame) 111const char *cgit_repobasename(const char *reponame)
112{ 112{
113 /* I assume we don't need to store more than one repo basename */ 113 /* I assume we don't need to store more than one repo basename */
114 static char rvbuf[1024]; 114 static char rvbuf[1024];
115 int p; 115 int p;
116 const char *rv; 116 const char *rv;
117 strncpy(rvbuf,reponame,sizeof(rvbuf)); 117 strncpy(rvbuf,reponame,sizeof(rvbuf));
118 if(rvbuf[sizeof(rvbuf)-1]) 118 if(rvbuf[sizeof(rvbuf)-1])
119 die("cgit_repobasename: truncated repository name '%s'", reponame); 119 die("cgit_repobasename: truncated repository name '%s'", reponame);
120 p = strlen(rvbuf)-1; 120 p = strlen(rvbuf)-1;
121 /* strip trailing slashes */ 121 /* strip trailing slashes */
122 while(p && rvbuf[p]=='/') rvbuf[p--]=0; 122 while(p && rvbuf[p]=='/') rvbuf[p--]=0;
123 /* strip trailing .git */ 123 /* strip trailing .git */
124 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) { 124 if(p>=3 && !strncmp(&rvbuf[p-3],".git",4)) {
125 p -= 3; rvbuf[p--] = 0; 125 p -= 3; rvbuf[p--] = 0;
126 } 126 }
127 /* strip more trailing slashes if any */ 127 /* strip more trailing slashes if any */
128 while( p && rvbuf[p]=='/') rvbuf[p--]=0; 128 while( p && rvbuf[p]=='/') rvbuf[p--]=0;
129 /* find last slash in the remaining string */ 129 /* find last slash in the remaining string */
130 rv = strrchr(rvbuf,'/'); 130 rv = strrchr(rvbuf,'/');
131 if(rv) 131 if(rv)
132 return ++rv; 132 return ++rv;
133 return rvbuf; 133 return rvbuf;
134} 134}
135 135
136char *cgit_currurl() 136char *cgit_currurl()
137{ 137{
138 if (!ctx.cfg.virtual_root) 138 if (!ctx.cfg.virtual_root)
139 return ctx.cfg.script_name; 139 return ctx.cfg.script_name;
140 else if (ctx.qry.page) 140 else if (ctx.qry.page)
141 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page); 141 return fmt("%s/%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo, ctx.qry.page);
142 else if (ctx.qry.repo) 142 else if (ctx.qry.repo)
143 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo); 143 return fmt("%s/%s/", ctx.cfg.virtual_root, ctx.qry.repo);
144 else 144 else
145 return fmt("%s/", ctx.cfg.virtual_root); 145 return fmt("%s/", ctx.cfg.virtual_root);
146} 146}
147 147
148static void site_url(char *page, char *search, int ofs) 148static void site_url(char *page, char *search, int ofs)
149{ 149{
150 char *delim = "?"; 150 char *delim = "?";
151 151
152 if (ctx.cfg.virtual_root) { 152 if (ctx.cfg.virtual_root) {
153 html_attr(ctx.cfg.virtual_root); 153 html_attr(ctx.cfg.virtual_root);
154 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 154 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
155 html("/"); 155 html("/");
156 } else 156 } else
157 html(ctx.cfg.script_name); 157 html(ctx.cfg.script_name);
158 158
159 if (page) { 159 if (page) {
160 htmlf("?p=%s", page); 160 htmlf("?p=%s", page);
161 delim = "&"; 161 delim = "&";
162 } 162 }
163 if (search) { 163 if (search) {
164 html(delim); 164 html(delim);
165 html("q="); 165 html("q=");
166 html_attr(search); 166 html_attr(search);
167 delim = "&"; 167 delim = "&";
168 } 168 }
169 if (ofs) { 169 if (ofs) {
170 html(delim); 170 html(delim);
171 htmlf("ofs=%d", ofs); 171 htmlf("ofs=%d", ofs);
172 } 172 }
173} 173}
174 174
175static void site_link(char *page, char *name, char *title, char *class, 175static void site_link(char *page, char *name, char *title, char *class,
176 char *search, int ofs) 176 char *search, int ofs)
177{ 177{
178 html("<a"); 178 html("<a");
179 if (title) { 179 if (title) {
180 html(" title='"); 180 html(" title='");
181 html_attr(title); 181 html_attr(title);
182 html("'"); 182 html("'");
183 } 183 }
184 if (class) { 184 if (class) {
185 html(" class='"); 185 html(" class='");
186 html_attr(class); 186 html_attr(class);
187 html("'"); 187 html("'");
188 } 188 }
189 html(" href='"); 189 html(" href='");
190 site_url(page, search, ofs); 190 site_url(page, search, ofs);
191 html("'>"); 191 html("'>");
192 html_txt(name); 192 html_txt(name);
193 html("</a>"); 193 html("</a>");
194} 194}
195 195
196void cgit_index_link(char *name, char *title, char *class, char *pattern, 196void cgit_index_link(char *name, char *title, char *class, char *pattern,
197 int ofs) 197 int ofs)
198{ 198{
199 site_link(NULL, name, title, class, pattern, ofs); 199 site_link(NULL, name, title, class, pattern, ofs);
200} 200}
201 201
202static char *repolink(char *title, char *class, char *page, char *head, 202static char *repolink(char *title, char *class, char *page, char *head,
203 char *path) 203 char *path)
204{ 204{
205 char *delim = "?"; 205 char *delim = "?";
206 206
207 html("<a"); 207 html("<a");
208 if (title) { 208 if (title) {
209 html(" title='"); 209 html(" title='");
210 html_attr(title); 210 html_attr(title);
211 html("'"); 211 html("'");
212 } 212 }
213 if (class) { 213 if (class) {
214 html(" class='"); 214 html(" class='");
215 html_attr(class); 215 html_attr(class);
216 html("'"); 216 html("'");
217 } 217 }
218 html(" href='"); 218 html(" href='");
219 if (ctx.cfg.virtual_root) { 219 if (ctx.cfg.virtual_root) {
220 html_url_path(ctx.cfg.virtual_root); 220 html_url_path(ctx.cfg.virtual_root);
221 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/') 221 if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
222 html("/"); 222 html("/");
223 html_url_path(ctx.repo->url); 223 html_url_path(ctx.repo->url);
224 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 224 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
225 html("/"); 225 html("/");
226 if (page) { 226 if (page) {
227 html_url_path(page); 227 html_url_path(page);
228 html("/"); 228 html("/");
229 if (path) 229 if (path)
230 html_url_path(path); 230 html_url_path(path);
231 } 231 }
232 } else { 232 } else {
233 html(ctx.cfg.script_name); 233 html(ctx.cfg.script_name);
234 html("?url="); 234 html("?url=");
235 html_url_arg(ctx.repo->url); 235 html_url_arg(ctx.repo->url);
236 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/') 236 if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
237 html("/"); 237 html("/");
238 if (page) { 238 if (page) {
239 html_url_arg(page); 239 html_url_arg(page);
240 html("/"); 240 html("/");
241 if (path) 241 if (path)
242 html_url_arg(path); 242 html_url_arg(path);
243 } 243 }
244 delim = "&amp;"; 244 delim = "&amp;";
245 } 245 }
246 if (head && strcmp(head, ctx.repo->defbranch)) { 246 if (head && strcmp(head, ctx.repo->defbranch)) {
247 html(delim); 247 html(delim);
248 html("h="); 248 html("h=");
249 html_url_arg(head); 249 html_url_arg(head);
250 delim = "&amp;"; 250 delim = "&amp;";
251 } 251 }
252 return fmt("%s", delim); 252 return fmt("%s", delim);
253} 253}
254 254
255static void reporevlink(char *page, char *name, char *title, char *class, 255static void reporevlink(char *page, char *name, char *title, char *class,
256 char *head, char *rev, char *path) 256 char *head, char *rev, char *path)
257{ 257{
258 char *delim; 258 char *delim;
259 259
260 delim = repolink(title, class, page, head, path); 260 delim = repolink(title, class, page, head, path);
261 if (rev && strcmp(rev, ctx.qry.head)) { 261 if (rev && strcmp(rev, ctx.qry.head)) {
262 html(delim); 262 html(delim);
263 html("id="); 263 html("id=");
264 html_url_arg(rev); 264 html_url_arg(rev);
265 } 265 }
266 html("'>"); 266 html("'>");
267 html_txt(name); 267 html_txt(name);
268 html("</a>"); 268 html("</a>");
269} 269}
270 270
271void cgit_summary_link(char *name, char *title, char *class, char *head) 271void cgit_summary_link(char *name, char *title, char *class, char *head)
272{ 272{
273 reporevlink(NULL, name, title, class, head, NULL, NULL); 273 reporevlink(NULL, name, title, class, head, NULL, NULL);
274} 274}
275 275
276void cgit_tag_link(char *name, char *title, char *class, char *head, 276void cgit_tag_link(char *name, char *title, char *class, char *head,
277 char *rev) 277 char *rev)
278{ 278{
279 reporevlink("tag", name, title, class, head, rev, NULL); 279 reporevlink("tag", name, title, class, head, rev, NULL);
280} 280}
281 281
282void cgit_tree_link(char *name, char *title, char *class, char *head, 282void cgit_tree_link(char *name, char *title, char *class, char *head,
283 char *rev, char *path) 283 char *rev, char *path)
284{ 284{
285 reporevlink("tree", name, title, class, head, rev, path); 285 reporevlink("tree", name, title, class, head, rev, path);
286} 286}
287 287
288void cgit_plain_link(char *name, char *title, char *class, char *head, 288void cgit_plain_link(char *name, char *title, char *class, char *head,
289 char *rev, char *path) 289 char *rev, char *path)
290{ 290{
291 reporevlink("plain", name, title, class, head, rev, path); 291 reporevlink("plain", name, title, class, head, rev, path);
292} 292}
293 293
294void cgit_log_link(char *name, char *title, char *class, char *head, 294void cgit_log_link(char *name, char *title, char *class, char *head,
295 char *rev, char *path, int ofs, char *grep, char *pattern, 295 char *rev, char *path, int ofs, char *grep, char *pattern,
296 int showmsg) 296 int showmsg)
297{ 297{
298 char *delim; 298 char *delim;
299 299
300 delim = repolink(title, class, "log", head, path); 300 delim = repolink(title, class, "log", head, path);
301 if (rev && strcmp(rev, ctx.qry.head)) { 301 if (rev && strcmp(rev, ctx.qry.head)) {
302 html(delim); 302 html(delim);
303 html("id="); 303 html("id=");
304 html_url_arg(rev); 304 html_url_arg(rev);
305 delim = "&"; 305 delim = "&";
306 } 306 }
307 if (grep && pattern) { 307 if (grep && pattern) {
308 html(delim); 308 html(delim);
309 html("qt="); 309 html("qt=");
310 html_url_arg(grep); 310 html_url_arg(grep);
311 delim = "&"; 311 delim = "&";
312 html(delim); 312 html(delim);
313 html("q="); 313 html("q=");
314 html_url_arg(pattern); 314 html_url_arg(pattern);
315 } 315 }
316 if (ofs > 0) { 316 if (ofs > 0) {
317 html(delim); 317 html(delim);
318 html("ofs="); 318 html("ofs=");
319 htmlf("%d", ofs); 319 htmlf("%d", ofs);
320 delim = "&"; 320 delim = "&";
321 } 321 }
322 if (showmsg) { 322 if (showmsg) {
323 html(delim); 323 html(delim);
324 html("showmsg=1"); 324 html("showmsg=1");
325 } 325 }
326 html("'>"); 326 html("'>");
327 html_txt(name); 327 html_txt(name);
328 html("</a>"); 328 html("</a>");
329} 329}
330 330
331void cgit_commit_link(char *name, char *title, char *class, char *head, 331void cgit_commit_link(char *name, char *title, char *class, char *head,
332 char *rev) 332 char *rev)
333{ 333{
334 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) { 334 if (strlen(name) > ctx.cfg.max_msg_len && ctx.cfg.max_msg_len >= 15) {
335 name[ctx.cfg.max_msg_len] = '\0'; 335 name[ctx.cfg.max_msg_len] = '\0';
336 name[ctx.cfg.max_msg_len - 1] = '.'; 336 name[ctx.cfg.max_msg_len - 1] = '.';
337 name[ctx.cfg.max_msg_len - 2] = '.'; 337 name[ctx.cfg.max_msg_len - 2] = '.';
338 name[ctx.cfg.max_msg_len - 3] = '.'; 338 name[ctx.cfg.max_msg_len - 3] = '.';
339 } 339 }
340 reporevlink("commit", name, title, class, head, rev, NULL); 340 reporevlink("commit", name, title, class, head, rev, NULL);
341} 341}
342 342
343void cgit_refs_link(char *name, char *title, char *class, char *head, 343void cgit_refs_link(char *name, char *title, char *class, char *head,
344 char *rev, char *path) 344 char *rev, char *path)
345{ 345{
346 reporevlink("refs", name, title, class, head, rev, path); 346 reporevlink("refs", name, title, class, head, rev, path);
347} 347}
348 348
349void cgit_snapshot_link(char *name, char *title, char *class, char *head, 349void cgit_snapshot_link(char *name, char *title, char *class, char *head,
350 char *rev, char *archivename) 350 char *rev, char *archivename)
351{ 351{
352 reporevlink("snapshot", name, title, class, head, rev, archivename); 352 reporevlink("snapshot", name, title, class, head, rev, archivename);
353} 353}
354 354
355void cgit_diff_link(char *name, char *title, char *class, char *head, 355void cgit_diff_link(char *name, char *title, char *class, char *head,
356 char *new_rev, char *old_rev, char *path) 356 char *new_rev, char *old_rev, char *path)
357{ 357{
358 char *delim; 358 char *delim;
359 359
360 delim = repolink(title, class, "diff", head, path); 360 delim = repolink(title, class, "diff", head, path);
361 if (new_rev && strcmp(new_rev, ctx.qry.head)) { 361 if (new_rev && strcmp(new_rev, ctx.qry.head)) {
362 html(delim); 362 html(delim);
363 html("id="); 363 html("id=");
364 html_url_arg(new_rev); 364 html_url_arg(new_rev);
365 delim = "&amp;"; 365 delim = "&amp;";
366 } 366 }
367 if (old_rev) { 367 if (old_rev) {
368 html(delim); 368 html(delim);
369 html("id2="); 369 html("id2=");
370 html_url_arg(old_rev); 370 html_url_arg(old_rev);
371 } 371 }
372 html("'>"); 372 html("'>");
373 html_txt(name); 373 html_txt(name);
374 html("</a>"); 374 html("</a>");
375} 375}
376 376
377void cgit_patch_link(char *name, char *title, char *class, char *head, 377void cgit_patch_link(char *name, char *title, char *class, char *head,
378 char *rev) 378 char *rev)
379{ 379{
380 reporevlink("patch", name, title, class, head, rev, NULL); 380 reporevlink("patch", name, title, class, head, rev, NULL);
381} 381}
382 382
383void cgit_stats_link(char *name, char *title, char *class, char *head, 383void cgit_stats_link(char *name, char *title, char *class, char *head,
384 char *path) 384 char *path)
385{ 385{
386 reporevlink("stats", name, title, class, head, NULL, path); 386 reporevlink("stats", name, title, class, head, NULL, path);
387} 387}
388 388
389void cgit_object_link(struct object *obj) 389void cgit_object_link(struct object *obj)
390{ 390{
391 char *page, *shortrev, *fullrev, *name; 391 char *page, *shortrev, *fullrev, *name;
392 392
393 fullrev = sha1_to_hex(obj->sha1); 393 fullrev = sha1_to_hex(obj->sha1);
394 shortrev = xstrdup(fullrev); 394 shortrev = xstrdup(fullrev);
395 shortrev[10] = '\0'; 395 shortrev[10] = '\0';
396 if (obj->type == OBJ_COMMIT) { 396 if (obj->type == OBJ_COMMIT) {
397 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL, 397 cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL,
398 ctx.qry.head, fullrev); 398 ctx.qry.head, fullrev);
399 return; 399 return;
400 } else if (obj->type == OBJ_TREE) 400 } else if (obj->type == OBJ_TREE)
401 page = "tree"; 401 page = "tree";
402 else if (obj->type == OBJ_TAG) 402 else if (obj->type == OBJ_TAG)
403 page = "tag"; 403 page = "tag";
404 else 404 else
405 page = "blob"; 405 page = "blob";
406 name = fmt("%s %s...", typename(obj->type), shortrev); 406 name = fmt("%s %s...", typename(obj->type), shortrev);
407 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL); 407 reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);
408} 408}
409 409
410void cgit_print_date(time_t secs, char *format, int local_time) 410void cgit_print_date(time_t secs, char *format, int local_time)
411{ 411{
412 char buf[64]; 412 char buf[64];
413 struct tm *time; 413 struct tm *time;
414 414
415 if (!secs) 415 if (!secs)
416 return; 416 return;
417 if(local_time) 417 if(local_time)
418 time = localtime(&secs); 418 time = localtime(&secs);
419 else 419 else
420 time = gmtime(&secs); 420 time = gmtime(&secs);
421 strftime(buf, sizeof(buf)-1, format, time); 421 strftime(buf, sizeof(buf)-1, format, time);
422 html_txt(buf); 422 html_txt(buf);
423} 423}
424 424
425void cgit_print_age(time_t t, time_t max_relative, char *format) 425void cgit_print_age(time_t t, time_t max_relative, char *format)
426{ 426{
427 time_t now, secs; 427 time_t now, secs;
428 428
429 if (!t) 429 if (!t)
430 return; 430 return;
431 time(&now); 431 time(&now);
432 secs = now - t; 432 secs = now - t;
433 433
434 if (secs > max_relative && max_relative >= 0) { 434 if (secs > max_relative && max_relative >= 0) {
435 cgit_print_date(t, format, ctx.cfg.local_time); 435 cgit_print_date(t, format, ctx.cfg.local_time);
436 return; 436 return;
437 } 437 }
438 438
439 if (secs < TM_HOUR * 2) { 439 if (secs < TM_HOUR * 2) {
440 htmlf("<span class='age-mins'>%.0f min.</span>", 440 htmlf("<span class='age-mins'>%.0f min.</span>",
441 secs * 1.0 / TM_MIN); 441 secs * 1.0 / TM_MIN);
442 return; 442 return;
443 } 443 }
444 if (secs < TM_DAY * 2) { 444 if (secs < TM_DAY * 2) {
445 htmlf("<span class='age-hours'>%.0f hours</span>", 445 htmlf("<span class='age-hours'>%.0f hours</span>",
446 secs * 1.0 / TM_HOUR); 446 secs * 1.0 / TM_HOUR);
447 return; 447 return;
448 } 448 }
449 if (secs < TM_WEEK * 2) { 449 if (secs < TM_WEEK * 2) {
450 htmlf("<span class='age-days'>%.0f days</span>", 450 htmlf("<span class='age-days'>%.0f days</span>",
451 secs * 1.0 / TM_DAY); 451 secs * 1.0 / TM_DAY);
452 return; 452 return;
453 } 453 }
454 if (secs < TM_MONTH * 2) { 454 if (secs < TM_MONTH * 2) {
455 htmlf("<span class='age-weeks'>%.0f weeks</span>", 455 htmlf("<span class='age-weeks'>%.0f weeks</span>",
456 secs * 1.0 / TM_WEEK); 456 secs * 1.0 / TM_WEEK);
457 return; 457 return;
458 } 458 }
459 if (secs < TM_YEAR * 2) { 459 if (secs < TM_YEAR * 2) {
460 htmlf("<span class='age-months'>%.0f months</span>", 460 htmlf("<span class='age-months'>%.0f months</span>",
461 secs * 1.0 / TM_MONTH); 461 secs * 1.0 / TM_MONTH);
462 return; 462 return;
463 } 463 }
464 htmlf("<span class='age-years'>%.0f years</span>", 464 htmlf("<span class='age-years'>%.0f years</span>",
465 secs * 1.0 / TM_YEAR); 465 secs * 1.0 / TM_YEAR);
466} 466}
467 467
468void cgit_print_http_headers(struct cgit_context *ctx) 468void cgit_print_http_headers(struct cgit_context *ctx)
469{ 469{
470 const char *method = getenv("REQUEST_METHOD");
471
470 if (ctx->page.status) 472 if (ctx->page.status)
471 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg); 473 htmlf("Status: %d %s\n", ctx->page.status, ctx->page.statusmsg);
472 if (ctx->page.mimetype && ctx->page.charset) 474 if (ctx->page.mimetype && ctx->page.charset)
473 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype, 475 htmlf("Content-Type: %s; charset=%s\n", ctx->page.mimetype,
474 ctx->page.charset); 476 ctx->page.charset);
475 else if (ctx->page.mimetype) 477 else if (ctx->page.mimetype)
476 htmlf("Content-Type: %s\n", ctx->page.mimetype); 478 htmlf("Content-Type: %s\n", ctx->page.mimetype);
477 if (ctx->page.size) 479 if (ctx->page.size)
478 htmlf("Content-Length: %ld\n", ctx->page.size); 480 htmlf("Content-Length: %ld\n", ctx->page.size);
479 if (ctx->page.filename) 481 if (ctx->page.filename)
480 htmlf("Content-Disposition: inline; filename=\"%s\"\n", 482 htmlf("Content-Disposition: inline; filename=\"%s\"\n",
481 ctx->page.filename); 483 ctx->page.filename);
482 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified)); 484 htmlf("Last-Modified: %s\n", http_date(ctx->page.modified));
483 htmlf("Expires: %s\n", http_date(ctx->page.expires)); 485 htmlf("Expires: %s\n", http_date(ctx->page.expires));
486 if (ctx->page.etag)
487 htmlf("ETag: \"%s\"\n", ctx->page.etag);
484 html("\n"); 488 html("\n");
489 if (method && !strcmp(method, "HEAD"))
490 exit(0);
485} 491}
486 492
487void cgit_print_docstart(struct cgit_context *ctx) 493void cgit_print_docstart(struct cgit_context *ctx)
488{ 494{
489 char *host = cgit_hosturl(); 495 char *host = cgit_hosturl();
490 html(cgit_doctype); 496 html(cgit_doctype);
491 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n"); 497 html("<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n");
492 html("<head>\n"); 498 html("<head>\n");
493 html("<title>"); 499 html("<title>");
494 html_txt(ctx->page.title); 500 html_txt(ctx->page.title);
495 html("</title>\n"); 501 html("</title>\n");
496 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version); 502 htmlf("<meta name='generator' content='cgit %s'/>\n", cgit_version);
497 if (ctx->cfg.robots && *ctx->cfg.robots) 503 if (ctx->cfg.robots && *ctx->cfg.robots)
498 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots); 504 htmlf("<meta name='robots' content='%s'/>\n", ctx->cfg.robots);
499 html("<link rel='stylesheet' type='text/css' href='"); 505 html("<link rel='stylesheet' type='text/css' href='");
500 html_attr(ctx->cfg.css); 506 html_attr(ctx->cfg.css);
501 html("'/>\n"); 507 html("'/>\n");
502 if (ctx->cfg.favicon) { 508 if (ctx->cfg.favicon) {
503 html("<link rel='shortcut icon' href='"); 509 html("<link rel='shortcut icon' href='");
504 html_attr(ctx->cfg.favicon); 510 html_attr(ctx->cfg.favicon);
505 html("'/>\n"); 511 html("'/>\n");
506 } 512 }
507 if (host && ctx->repo) { 513 if (host && ctx->repo) {
508 html("<link rel='alternate' title='Atom feed' href='"); 514 html("<link rel='alternate' title='Atom feed' href='");
509 html(cgit_httpscheme()); 515 html(cgit_httpscheme());
510 html_attr(cgit_hosturl()); 516 html_attr(cgit_hosturl());
511 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path, 517 html_attr(cgit_fileurl(ctx->repo->url, "atom", ctx->qry.path,
512 fmt("h=%s", ctx->qry.head))); 518 fmt("h=%s", ctx->qry.head)));
513 html("' type='application/atom+xml'/>"); 519 html("' type='application/atom+xml'/>");
514 } 520 }
515 html("</head>\n"); 521 html("</head>\n");
516 html("<body>\n"); 522 html("<body>\n");
517 if (ctx->cfg.header) 523 if (ctx->cfg.header)
518 html_include(ctx->cfg.header); 524 html_include(ctx->cfg.header);
519} 525}
520 526
521void cgit_print_docend() 527void cgit_print_docend()
522{ 528{
523 html("</div>"); 529 html("</div>");
524 if (ctx.cfg.footer) 530 if (ctx.cfg.footer)
525 html_include(ctx.cfg.footer); 531 html_include(ctx.cfg.footer);
526 else { 532 else {
527 htmlf("<div class='footer'>generated by cgit %s at ", 533 htmlf("<div class='footer'>generated by cgit %s at ",
528 cgit_version); 534 cgit_version);
529 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time); 535 cgit_print_date(time(NULL), FMT_LONGDATE, ctx.cfg.local_time);
530 html("</div>\n"); 536 html("</div>\n");
531 } 537 }
532 html("</body>\n</html>\n"); 538 html("</body>\n</html>\n");
533} 539}
534 540
535int print_branch_option(const char *refname, const unsigned char *sha1, 541int print_branch_option(const char *refname, const unsigned char *sha1,
536 int flags, void *cb_data) 542 int flags, void *cb_data)
537{ 543{
538 char *name = (char *)refname; 544 char *name = (char *)refname;
539 html_option(name, name, ctx.qry.head); 545 html_option(name, name, ctx.qry.head);
540 return 0; 546 return 0;
541} 547}
542 548
543int print_archive_ref(const char *refname, const unsigned char *sha1, 549int print_archive_ref(const char *refname, const unsigned char *sha1,
544 int flags, void *cb_data) 550 int flags, void *cb_data)
545{ 551{
546 struct tag *tag; 552 struct tag *tag;
547 struct taginfo *info; 553 struct taginfo *info;
548 struct object *obj; 554 struct object *obj;
549 char buf[256], *url; 555 char buf[256], *url;
550 unsigned char fileid[20]; 556 unsigned char fileid[20];
551 int *header = (int *)cb_data; 557 int *header = (int *)cb_data;
552 558
553 if (prefixcmp(refname, "refs/archives")) 559 if (prefixcmp(refname, "refs/archives"))
554 return 0; 560 return 0;
555 strncpy(buf, refname+14, sizeof(buf)); 561 strncpy(buf, refname+14, sizeof(buf));
556 obj = parse_object(sha1); 562 obj = parse_object(sha1);
557 if (!obj) 563 if (!obj)
558 return 1; 564 return 1;
559 if (obj->type == OBJ_TAG) { 565 if (obj->type == OBJ_TAG) {
560 tag = lookup_tag(sha1); 566 tag = lookup_tag(sha1);
561 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag))) 567 if (!tag || parse_tag(tag) || !(info = cgit_parse_tag(tag)))
562 return 0; 568 return 0;
563 hashcpy(fileid, tag->tagged->sha1); 569 hashcpy(fileid, tag->tagged->sha1);
564 } else if (obj->type != OBJ_BLOB) { 570 } else if (obj->type != OBJ_BLOB) {
565 return 0; 571 return 0;
566 } else { 572 } else {
567 hashcpy(fileid, sha1); 573 hashcpy(fileid, sha1);
568 } 574 }
569 if (!*header) { 575 if (!*header) {
570 html("<h1>download</h1>\n"); 576 html("<h1>download</h1>\n");
571 *header = 1; 577 *header = 1;
572 } 578 }
573 url = cgit_pageurl(ctx.qry.repo, "blob", 579 url = cgit_pageurl(ctx.qry.repo, "blob",
574 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid), 580 fmt("id=%s&amp;path=%s", sha1_to_hex(fileid),
575 buf)); 581 buf));
576 html_link_open(url, NULL, "menu"); 582 html_link_open(url, NULL, "menu");
577 html_txt(strlpart(buf, 20)); 583 html_txt(strlpart(buf, 20));
578 html_link_close(); 584 html_link_close();
579 return 0; 585 return 0;
580} 586}
581 587
582void cgit_add_hidden_formfields(int incl_head, int incl_search, char *page) 588void cgit_add_hidden_formfields(int incl_head, int incl_search, char *page)
583{ 589{
584 char *url; 590 char *url;
585 591
586 if (!ctx.cfg.virtual_root) { 592 if (!ctx.cfg.virtual_root) {
587 url = fmt("%s/%s", ctx.qry.repo, page); 593 url = fmt("%s/%s", ctx.qry.repo, page);
588 if (ctx.qry.path) 594 if (ctx.qry.path)
589 url = fmt("%s/%s", url, ctx.qry.path); 595 url = fmt("%s/%s", url, ctx.qry.path);
590 html_hidden("url", url); 596 html_hidden("url", url);
591 } 597 }
592 598
593 if (incl_head && ctx.qry.head && ctx.repo->defbranch && 599 if (incl_head && ctx.qry.head && ctx.repo->defbranch &&
594 strcmp(ctx.qry.head, ctx.repo->defbranch)) 600 strcmp(ctx.qry.head, ctx.repo->defbranch))
595 html_hidden("h", ctx.qry.head); 601 html_hidden("h", ctx.qry.head);
596 602
597 if (ctx.qry.sha1) 603 if (ctx.qry.sha1)
598 html_hidden("id", ctx.qry.sha1); 604 html_hidden("id", ctx.qry.sha1);
599 if (ctx.qry.sha2) 605 if (ctx.qry.sha2)
600 html_hidden("id2", ctx.qry.sha2); 606 html_hidden("id2", ctx.qry.sha2);
601 if (ctx.qry.showmsg) 607 if (ctx.qry.showmsg)
602 html_hidden("showmsg", "1"); 608 html_hidden("showmsg", "1");
603 609
604 if (incl_search) { 610 if (incl_search) {
605 if (ctx.qry.grep) 611 if (ctx.qry.grep)
606 html_hidden("qt", ctx.qry.grep); 612 html_hidden("qt", ctx.qry.grep);
607 if (ctx.qry.search) 613 if (ctx.qry.search)
608 html_hidden("q", ctx.qry.search); 614 html_hidden("q", ctx.qry.search);
609 } 615 }
610} 616}
611 617
612const char *fallback_cmd = "repolist"; 618const char *fallback_cmd = "repolist";
613 619
614char *hc(struct cgit_cmd *cmd, const char *page) 620char *hc(struct cgit_cmd *cmd, const char *page)
615{ 621{
616 return (strcmp(cmd ? cmd->name : fallback_cmd, page) ? NULL : "active"); 622 return (strcmp(cmd ? cmd->name : fallback_cmd, page) ? NULL : "active");
617} 623}
618 624
619void cgit_print_pageheader(struct cgit_context *ctx) 625void cgit_print_pageheader(struct cgit_context *ctx)
620{ 626{
621 struct cgit_cmd *cmd = cgit_get_cmd(ctx); 627 struct cgit_cmd *cmd = cgit_get_cmd(ctx);
622 628
623 if (!cmd && ctx->repo) 629 if (!cmd && ctx->repo)
624 fallback_cmd = "summary"; 630 fallback_cmd = "summary";
625 631
626 html("<table id='header'>\n"); 632 html("<table id='header'>\n");
627 html("<tr>\n"); 633 html("<tr>\n");
628 html("<td class='logo' rowspan='2'><a href='"); 634 html("<td class='logo' rowspan='2'><a href='");
629 if (ctx->cfg.logo_link) 635 if (ctx->cfg.logo_link)
630 html_attr(ctx->cfg.logo_link); 636 html_attr(ctx->cfg.logo_link);
631 else 637 else
632 html_attr(cgit_rooturl()); 638 html_attr(cgit_rooturl());
633 html("'><img src='"); 639 html("'><img src='");
634 html_attr(ctx->cfg.logo); 640 html_attr(ctx->cfg.logo);
635 html("' alt='cgit logo'/></a></td>\n"); 641 html("' alt='cgit logo'/></a></td>\n");
636 642
637 html("<td class='main'>"); 643 html("<td class='main'>");
638 if (ctx->repo) { 644 if (ctx->repo) {
639 cgit_index_link("index", NULL, NULL, NULL, 0); 645 cgit_index_link("index", NULL, NULL, NULL, 0);
640 html(" : "); 646 html(" : ");
641 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL); 647 cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);
642 html("</td><td class='form'>"); 648 html("</td><td class='form'>");
643 html("<form method='get' action=''>\n"); 649 html("<form method='get' action=''>\n");
644 cgit_add_hidden_formfields(0, 1, ctx->qry.page); 650 cgit_add_hidden_formfields(0, 1, ctx->qry.page);
645 html("<select name='h' onchange='this.form.submit();'>\n"); 651 html("<select name='h' onchange='this.form.submit();'>\n");
646 for_each_branch_ref(print_branch_option, ctx->qry.head); 652 for_each_branch_ref(print_branch_option, ctx->qry.head);
647 html("</select> "); 653 html("</select> ");
648 html("<input type='submit' name='' value='switch'/>"); 654 html("<input type='submit' name='' value='switch'/>");
649 html("</form>"); 655 html("</form>");
650 } else 656 } else
651 html_txt(ctx->cfg.root_title); 657 html_txt(ctx->cfg.root_title);
652 html("</td></tr>\n"); 658 html("</td></tr>\n");
653 659
654 html("<tr><td class='sub'>"); 660 html("<tr><td class='sub'>");
655 if (ctx->repo) { 661 if (ctx->repo) {
656 html_txt(ctx->repo->desc); 662 html_txt(ctx->repo->desc);
657 html("</td><td class='sub right'>"); 663 html("</td><td class='sub right'>");
658 html_txt(ctx->repo->owner); 664 html_txt(ctx->repo->owner);
659 } else { 665 } else {
660 if (ctx->cfg.root_desc) 666 if (ctx->cfg.root_desc)
661 html_txt(ctx->cfg.root_desc); 667 html_txt(ctx->cfg.root_desc);
662 else if (ctx->cfg.index_info) 668 else if (ctx->cfg.index_info)
663 html_include(ctx->cfg.index_info); 669 html_include(ctx->cfg.index_info);
664 } 670 }
665 html("</td></tr></table>\n"); 671 html("</td></tr></table>\n");
666 672
667 html("<table class='tabs'><tr><td>\n"); 673 html("<table class='tabs'><tr><td>\n");
668 if (ctx->repo) { 674 if (ctx->repo) {
669 cgit_summary_link("summary", NULL, hc(cmd, "summary"), 675 cgit_summary_link("summary", NULL, hc(cmd, "summary"),
670 ctx->qry.head); 676 ctx->qry.head);
671 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head, 677 cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,
672 ctx->qry.sha1, NULL); 678 ctx->qry.sha1, NULL);
673 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head, 679 cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head,
674 NULL, NULL, 0, NULL, NULL, ctx->qry.showmsg); 680 NULL, NULL, 0, NULL, NULL, ctx->qry.showmsg);
675 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head, 681 cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,
676 ctx->qry.sha1, NULL); 682 ctx->qry.sha1, NULL);
677 cgit_commit_link("commit", NULL, hc(cmd, "commit"), 683 cgit_commit_link("commit", NULL, hc(cmd, "commit"),
678 ctx->qry.head, ctx->qry.sha1); 684 ctx->qry.head, ctx->qry.sha1);
679 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head, 685 cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,
680 ctx->qry.sha1, ctx->qry.sha2, NULL); 686 ctx->qry.sha1, ctx->qry.sha2, NULL);
681 if (ctx->repo->max_stats) 687 if (ctx->repo->max_stats)
682 cgit_stats_link("stats", NULL, hc(cmd, "stats"), 688 cgit_stats_link("stats", NULL, hc(cmd, "stats"),
683 ctx->qry.head, NULL); 689 ctx->qry.head, NULL);
684 if (ctx->repo->readme) 690 if (ctx->repo->readme)
685 reporevlink("about", "about", NULL, 691 reporevlink("about", "about", NULL,
686 hc(cmd, "about"), ctx->qry.head, NULL, 692 hc(cmd, "about"), ctx->qry.head, NULL,
687 NULL); 693 NULL);
688 html("</td><td class='form'>"); 694 html("</td><td class='form'>");
689 html("<form class='right' method='get' action='"); 695 html("<form class='right' method='get' action='");
690 if (ctx->cfg.virtual_root) 696 if (ctx->cfg.virtual_root)
691 html_url_path(cgit_fileurl(ctx->qry.repo, "log", 697 html_url_path(cgit_fileurl(ctx->qry.repo, "log",
692 ctx->qry.path, NULL)); 698 ctx->qry.path, NULL));
693 html("'>\n"); 699 html("'>\n");
694 cgit_add_hidden_formfields(1, 0, "log"); 700 cgit_add_hidden_formfields(1, 0, "log");
695 html("<select name='qt'>\n"); 701 html("<select name='qt'>\n");
696 html_option("grep", "log msg", ctx->qry.grep); 702 html_option("grep", "log msg", ctx->qry.grep);
697 html_option("author", "author", ctx->qry.grep); 703 html_option("author", "author", ctx->qry.grep);
698 html_option("committer", "committer", ctx->qry.grep); 704 html_option("committer", "committer", ctx->qry.grep);
699 html("</select>\n"); 705 html("</select>\n");
700 html("<input class='txt' type='text' size='10' name='q' value='"); 706 html("<input class='txt' type='text' size='10' name='q' value='");
701 html_attr(ctx->qry.search); 707 html_attr(ctx->qry.search);
702 html("'/>\n"); 708 html("'/>\n");
703 html("<input type='submit' value='search'/>\n"); 709 html("<input type='submit' value='search'/>\n");
704 html("</form>\n"); 710 html("</form>\n");
705 } else { 711 } else {
706 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0); 712 site_link(NULL, "index", NULL, hc(cmd, "repolist"), NULL, 0);
707 if (ctx->cfg.root_readme) 713 if (ctx->cfg.root_readme)
708 site_link("about", "about", NULL, hc(cmd, "about"), 714 site_link("about", "about", NULL, hc(cmd, "about"),
709 NULL, 0); 715 NULL, 0);
710 html("</td><td class='form'>"); 716 html("</td><td class='form'>");
711 html("<form method='get' action='"); 717 html("<form method='get' action='");
712 html_attr(cgit_rooturl()); 718 html_attr(cgit_rooturl());
713 html("'>\n"); 719 html("'>\n");
714 html("<input type='text' name='q' size='10' value='"); 720 html("<input type='text' name='q' size='10' value='");
715 html_attr(ctx->qry.search); 721 html_attr(ctx->qry.search);
716 html("'/>\n"); 722 html("'/>\n");
717 html("<input type='submit' value='search'/>\n"); 723 html("<input type='submit' value='search'/>\n");
718 html("</form>"); 724 html("</form>");
719 } 725 }
720 html("</td></tr></table>\n"); 726 html("</td></tr></table>\n");
721 html("<div class='content'>"); 727 html("<div class='content'>");
722} 728}
723 729
724void cgit_print_filemode(unsigned short mode) 730void cgit_print_filemode(unsigned short mode)
725{ 731{
726 if (S_ISDIR(mode)) 732 if (S_ISDIR(mode))
727 html("d"); 733 html("d");
728 else if (S_ISLNK(mode)) 734 else if (S_ISLNK(mode))
729 html("l"); 735 html("l");
730 else if (S_ISGITLINK(mode)) 736 else if (S_ISGITLINK(mode))
731 html("m"); 737 html("m");
732 else 738 else
733 html("-"); 739 html("-");
734 html_fileperm(mode >> 6); 740 html_fileperm(mode >> 6);
735 html_fileperm(mode >> 3); 741 html_fileperm(mode >> 3);
736 html_fileperm(mode); 742 html_fileperm(mode);
737} 743}
738 744
739void cgit_print_snapshot_links(const char *repo, const char *head, 745void cgit_print_snapshot_links(const char *repo, const char *head,
740 const char *hex, int snapshots) 746 const char *hex, int snapshots)
741{ 747{
742 const struct cgit_snapshot_format* f; 748 const struct cgit_snapshot_format* f;
743 char *filename; 749 char *filename;
744 750
745 for (f = cgit_snapshot_formats; f->suffix; f++) { 751 for (f = cgit_snapshot_formats; f->suffix; f++) {
746 if (!(snapshots & f->bit)) 752 if (!(snapshots & f->bit))
747 continue; 753 continue;
748 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex, 754 filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,
749 f->suffix); 755 f->suffix);
750 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename); 756 cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);
751 html("<br/>"); 757 html("<br/>");
752 } 758 }
753} 759}