summaryrefslogtreecommitdiffabout
path: root/cgit.c
Unidiff
Diffstat (limited to 'cgit.c') (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/cgit.c b/cgit.c
index aa1107a..dbec196 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,511 +1,534 @@
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 add_mimetype(const char *name, const char *value) 20void add_mimetype(const char *name, const char *value)
21{ 21{
22 struct string_list_item *item; 22 struct string_list_item *item;
23 23
24 item = string_list_insert(xstrdup(name), &ctx.cfg.mimetypes); 24 item = string_list_insert(xstrdup(name), &ctx.cfg.mimetypes);
25 item->util = xstrdup(value); 25 item->util = xstrdup(value);
26} 26}
27 27
28struct cgit_filter *new_filter(const char *cmd, int extra_args)
29{
30 struct cgit_filter *f;
31
32 if (!cmd || !cmd[0])
33 return NULL;
34
35 f = xmalloc(sizeof(struct cgit_filter));
36 f->cmd = xstrdup(cmd);
37 f->argv = xmalloc((2 + extra_args) * sizeof(char *));
38 f->argv[0] = f->cmd;
39 f->argv[1] = NULL;
40 return f;
41}
42
28void config_cb(const char *name, const char *value) 43void config_cb(const char *name, const char *value)
29{ 44{
30 if (!strcmp(name, "root-title")) 45 if (!strcmp(name, "root-title"))
31 ctx.cfg.root_title = xstrdup(value); 46 ctx.cfg.root_title = xstrdup(value);
32 else if (!strcmp(name, "root-desc")) 47 else if (!strcmp(name, "root-desc"))
33 ctx.cfg.root_desc = xstrdup(value); 48 ctx.cfg.root_desc = xstrdup(value);
34 else if (!strcmp(name, "root-readme")) 49 else if (!strcmp(name, "root-readme"))
35 ctx.cfg.root_readme = xstrdup(value); 50 ctx.cfg.root_readme = xstrdup(value);
36 else if (!strcmp(name, "css")) 51 else if (!strcmp(name, "css"))
37 ctx.cfg.css = xstrdup(value); 52 ctx.cfg.css = xstrdup(value);
38 else if (!strcmp(name, "favicon")) 53 else if (!strcmp(name, "favicon"))
39 ctx.cfg.favicon = xstrdup(value); 54 ctx.cfg.favicon = xstrdup(value);
40 else if (!strcmp(name, "footer")) 55 else if (!strcmp(name, "footer"))
41 ctx.cfg.footer = xstrdup(value); 56 ctx.cfg.footer = xstrdup(value);
42 else if (!strcmp(name, "head-include")) 57 else if (!strcmp(name, "head-include"))
43 ctx.cfg.head_include = xstrdup(value); 58 ctx.cfg.head_include = xstrdup(value);
44 else if (!strcmp(name, "header")) 59 else if (!strcmp(name, "header"))
45 ctx.cfg.header = xstrdup(value); 60 ctx.cfg.header = xstrdup(value);
46 else if (!strcmp(name, "logo")) 61 else if (!strcmp(name, "logo"))
47 ctx.cfg.logo = xstrdup(value); 62 ctx.cfg.logo = xstrdup(value);
48 else if (!strcmp(name, "index-header")) 63 else if (!strcmp(name, "index-header"))
49 ctx.cfg.index_header = xstrdup(value); 64 ctx.cfg.index_header = xstrdup(value);
50 else if (!strcmp(name, "index-info")) 65 else if (!strcmp(name, "index-info"))
51 ctx.cfg.index_info = xstrdup(value); 66 ctx.cfg.index_info = xstrdup(value);
52 else if (!strcmp(name, "logo-link")) 67 else if (!strcmp(name, "logo-link"))
53 ctx.cfg.logo_link = xstrdup(value); 68 ctx.cfg.logo_link = xstrdup(value);
54 else if (!strcmp(name, "module-link")) 69 else if (!strcmp(name, "module-link"))
55 ctx.cfg.module_link = xstrdup(value); 70 ctx.cfg.module_link = xstrdup(value);
56 else if (!strcmp(name, "virtual-root")) { 71 else if (!strcmp(name, "virtual-root")) {
57 ctx.cfg.virtual_root = trim_end(value, '/'); 72 ctx.cfg.virtual_root = trim_end(value, '/');
58 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 73 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
59 ctx.cfg.virtual_root = ""; 74 ctx.cfg.virtual_root = "";
60 } else if (!strcmp(name, "nocache")) 75 } else if (!strcmp(name, "nocache"))
61 ctx.cfg.nocache = atoi(value); 76 ctx.cfg.nocache = atoi(value);
62 else if (!strcmp(name, "noplainemail")) 77 else if (!strcmp(name, "noplainemail"))
63 ctx.cfg.noplainemail = atoi(value); 78 ctx.cfg.noplainemail = atoi(value);
64 else if (!strcmp(name, "noheader")) 79 else if (!strcmp(name, "noheader"))
65 ctx.cfg.noheader = atoi(value); 80 ctx.cfg.noheader = atoi(value);
66 else if (!strcmp(name, "snapshots")) 81 else if (!strcmp(name, "snapshots"))
67 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 82 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
68 else if (!strcmp(name, "enable-index-links")) 83 else if (!strcmp(name, "enable-index-links"))
69 ctx.cfg.enable_index_links = atoi(value); 84 ctx.cfg.enable_index_links = atoi(value);
70 else if (!strcmp(name, "enable-log-filecount")) 85 else if (!strcmp(name, "enable-log-filecount"))
71 ctx.cfg.enable_log_filecount = atoi(value); 86 ctx.cfg.enable_log_filecount = atoi(value);
72 else if (!strcmp(name, "enable-log-linecount")) 87 else if (!strcmp(name, "enable-log-linecount"))
73 ctx.cfg.enable_log_linecount = atoi(value); 88 ctx.cfg.enable_log_linecount = atoi(value);
74 else if (!strcmp(name, "max-stats")) 89 else if (!strcmp(name, "max-stats"))
75 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL); 90 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
76 else if (!strcmp(name, "cache-size")) 91 else if (!strcmp(name, "cache-size"))
77 ctx.cfg.cache_size = atoi(value); 92 ctx.cfg.cache_size = atoi(value);
78 else if (!strcmp(name, "cache-root")) 93 else if (!strcmp(name, "cache-root"))
79 ctx.cfg.cache_root = xstrdup(value); 94 ctx.cfg.cache_root = xstrdup(value);
80 else if (!strcmp(name, "cache-root-ttl")) 95 else if (!strcmp(name, "cache-root-ttl"))
81 ctx.cfg.cache_root_ttl = atoi(value); 96 ctx.cfg.cache_root_ttl = atoi(value);
82 else if (!strcmp(name, "cache-repo-ttl")) 97 else if (!strcmp(name, "cache-repo-ttl"))
83 ctx.cfg.cache_repo_ttl = atoi(value); 98 ctx.cfg.cache_repo_ttl = atoi(value);
84 else if (!strcmp(name, "cache-static-ttl")) 99 else if (!strcmp(name, "cache-static-ttl"))
85 ctx.cfg.cache_static_ttl = atoi(value); 100 ctx.cfg.cache_static_ttl = atoi(value);
86 else if (!strcmp(name, "cache-dynamic-ttl")) 101 else if (!strcmp(name, "cache-dynamic-ttl"))
87 ctx.cfg.cache_dynamic_ttl = atoi(value); 102 ctx.cfg.cache_dynamic_ttl = atoi(value);
103 else if (!strcmp(name, "commit-filter"))
104 ctx.cfg.commit_filter = new_filter(value, 0);
88 else if (!strcmp(name, "embedded")) 105 else if (!strcmp(name, "embedded"))
89 ctx.cfg.embedded = atoi(value); 106 ctx.cfg.embedded = atoi(value);
90 else if (!strcmp(name, "max-message-length")) 107 else if (!strcmp(name, "max-message-length"))
91 ctx.cfg.max_msg_len = atoi(value); 108 ctx.cfg.max_msg_len = atoi(value);
92 else if (!strcmp(name, "max-repodesc-length")) 109 else if (!strcmp(name, "max-repodesc-length"))
93 ctx.cfg.max_repodesc_len = atoi(value); 110 ctx.cfg.max_repodesc_len = atoi(value);
94 else if (!strcmp(name, "max-repo-count")) 111 else if (!strcmp(name, "max-repo-count"))
95 ctx.cfg.max_repo_count = atoi(value); 112 ctx.cfg.max_repo_count = atoi(value);
96 else if (!strcmp(name, "max-commit-count")) 113 else if (!strcmp(name, "max-commit-count"))
97 ctx.cfg.max_commit_count = atoi(value); 114 ctx.cfg.max_commit_count = atoi(value);
115 else if (!strcmp(name, "source-filter"))
116 ctx.cfg.source_filter = new_filter(value, 1);
98 else if (!strcmp(name, "summary-log")) 117 else if (!strcmp(name, "summary-log"))
99 ctx.cfg.summary_log = atoi(value); 118 ctx.cfg.summary_log = atoi(value);
100 else if (!strcmp(name, "summary-branches")) 119 else if (!strcmp(name, "summary-branches"))
101 ctx.cfg.summary_branches = atoi(value); 120 ctx.cfg.summary_branches = atoi(value);
102 else if (!strcmp(name, "summary-tags")) 121 else if (!strcmp(name, "summary-tags"))
103 ctx.cfg.summary_tags = atoi(value); 122 ctx.cfg.summary_tags = atoi(value);
104 else if (!strcmp(name, "agefile")) 123 else if (!strcmp(name, "agefile"))
105 ctx.cfg.agefile = xstrdup(value); 124 ctx.cfg.agefile = xstrdup(value);
106 else if (!strcmp(name, "renamelimit")) 125 else if (!strcmp(name, "renamelimit"))
107 ctx.cfg.renamelimit = atoi(value); 126 ctx.cfg.renamelimit = atoi(value);
108 else if (!strcmp(name, "robots")) 127 else if (!strcmp(name, "robots"))
109 ctx.cfg.robots = xstrdup(value); 128 ctx.cfg.robots = xstrdup(value);
110 else if (!strcmp(name, "clone-prefix")) 129 else if (!strcmp(name, "clone-prefix"))
111 ctx.cfg.clone_prefix = xstrdup(value); 130 ctx.cfg.clone_prefix = xstrdup(value);
112 else if (!strcmp(name, "local-time")) 131 else if (!strcmp(name, "local-time"))
113 ctx.cfg.local_time = atoi(value); 132 ctx.cfg.local_time = atoi(value);
114 else if (!prefixcmp(name, "mimetype.")) 133 else if (!prefixcmp(name, "mimetype."))
115 add_mimetype(name + 9, value); 134 add_mimetype(name + 9, value);
116 else if (!strcmp(name, "repo.group")) 135 else if (!strcmp(name, "repo.group"))
117 ctx.cfg.repo_group = xstrdup(value); 136 ctx.cfg.repo_group = xstrdup(value);
118 else if (!strcmp(name, "repo.url")) 137 else if (!strcmp(name, "repo.url"))
119 ctx.repo = cgit_add_repo(value); 138 ctx.repo = cgit_add_repo(value);
120 else if (!strcmp(name, "repo.name")) 139 else if (!strcmp(name, "repo.name"))
121 ctx.repo->name = xstrdup(value); 140 ctx.repo->name = xstrdup(value);
122 else if (ctx.repo && !strcmp(name, "repo.path")) 141 else if (ctx.repo && !strcmp(name, "repo.path"))
123 ctx.repo->path = trim_end(value, '/'); 142 ctx.repo->path = trim_end(value, '/');
124 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 143 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
125 ctx.repo->clone_url = xstrdup(value); 144 ctx.repo->clone_url = xstrdup(value);
126 else if (ctx.repo && !strcmp(name, "repo.desc")) 145 else if (ctx.repo && !strcmp(name, "repo.desc"))
127 ctx.repo->desc = xstrdup(value); 146 ctx.repo->desc = xstrdup(value);
128 else if (ctx.repo && !strcmp(name, "repo.owner")) 147 else if (ctx.repo && !strcmp(name, "repo.owner"))
129 ctx.repo->owner = xstrdup(value); 148 ctx.repo->owner = xstrdup(value);
130 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 149 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
131 ctx.repo->defbranch = xstrdup(value); 150 ctx.repo->defbranch = xstrdup(value);
132 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 151 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
133 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 152 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
134 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 153 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
135 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 154 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
136 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 155 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
137 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 156 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
138 else if (ctx.repo && !strcmp(name, "repo.max-stats")) 157 else if (ctx.repo && !strcmp(name, "repo.max-stats"))
139 ctx.repo->max_stats = cgit_find_stats_period(value, NULL); 158 ctx.repo->max_stats = cgit_find_stats_period(value, NULL);
140 else if (ctx.repo && !strcmp(name, "repo.module-link")) 159 else if (ctx.repo && !strcmp(name, "repo.module-link"))
141 ctx.repo->module_link= xstrdup(value); 160 ctx.repo->module_link= xstrdup(value);
161 else if (ctx.repo && !strcmp(name, "repo.commit-filter"))
162 ctx.repo->commit_filter = new_filter(value, 0);
163 else if (ctx.repo && !strcmp(name, "repo.source-filter"))
164 ctx.repo->source_filter = new_filter(value, 1);
142 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 165 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
143 if (*value == '/') 166 if (*value == '/')
144 ctx.repo->readme = xstrdup(value); 167 ctx.repo->readme = xstrdup(value);
145 else 168 else
146 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 169 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
147 } else if (!strcmp(name, "include")) 170 } else if (!strcmp(name, "include"))
148 parse_configfile(value, config_cb); 171 parse_configfile(value, config_cb);
149} 172}
150 173
151static void querystring_cb(const char *name, const char *value) 174static void querystring_cb(const char *name, const char *value)
152{ 175{
153 if (!strcmp(name,"r")) { 176 if (!strcmp(name,"r")) {
154 ctx.qry.repo = xstrdup(value); 177 ctx.qry.repo = xstrdup(value);
155 ctx.repo = cgit_get_repoinfo(value); 178 ctx.repo = cgit_get_repoinfo(value);
156 } else if (!strcmp(name, "p")) { 179 } else if (!strcmp(name, "p")) {
157 ctx.qry.page = xstrdup(value); 180 ctx.qry.page = xstrdup(value);
158 } else if (!strcmp(name, "url")) { 181 } else if (!strcmp(name, "url")) {
159 ctx.qry.url = xstrdup(value); 182 ctx.qry.url = xstrdup(value);
160 cgit_parse_url(value); 183 cgit_parse_url(value);
161 } else if (!strcmp(name, "qt")) { 184 } else if (!strcmp(name, "qt")) {
162 ctx.qry.grep = xstrdup(value); 185 ctx.qry.grep = xstrdup(value);
163 } else if (!strcmp(name, "q")) { 186 } else if (!strcmp(name, "q")) {
164 ctx.qry.search = xstrdup(value); 187 ctx.qry.search = xstrdup(value);
165 } else if (!strcmp(name, "h")) { 188 } else if (!strcmp(name, "h")) {
166 ctx.qry.head = xstrdup(value); 189 ctx.qry.head = xstrdup(value);
167 ctx.qry.has_symref = 1; 190 ctx.qry.has_symref = 1;
168 } else if (!strcmp(name, "id")) { 191 } else if (!strcmp(name, "id")) {
169 ctx.qry.sha1 = xstrdup(value); 192 ctx.qry.sha1 = xstrdup(value);
170 ctx.qry.has_sha1 = 1; 193 ctx.qry.has_sha1 = 1;
171 } else if (!strcmp(name, "id2")) { 194 } else if (!strcmp(name, "id2")) {
172 ctx.qry.sha2 = xstrdup(value); 195 ctx.qry.sha2 = xstrdup(value);
173 ctx.qry.has_sha1 = 1; 196 ctx.qry.has_sha1 = 1;
174 } else if (!strcmp(name, "ofs")) { 197 } else if (!strcmp(name, "ofs")) {
175 ctx.qry.ofs = atoi(value); 198 ctx.qry.ofs = atoi(value);
176 } else if (!strcmp(name, "path")) { 199 } else if (!strcmp(name, "path")) {
177 ctx.qry.path = trim_end(value, '/'); 200 ctx.qry.path = trim_end(value, '/');
178 } else if (!strcmp(name, "name")) { 201 } else if (!strcmp(name, "name")) {
179 ctx.qry.name = xstrdup(value); 202 ctx.qry.name = xstrdup(value);
180 } else if (!strcmp(name, "mimetype")) { 203 } else if (!strcmp(name, "mimetype")) {
181 ctx.qry.mimetype = xstrdup(value); 204 ctx.qry.mimetype = xstrdup(value);
182 } else if (!strcmp(name, "s")){ 205 } else if (!strcmp(name, "s")){
183 ctx.qry.sort = xstrdup(value); 206 ctx.qry.sort = xstrdup(value);
184 } else if (!strcmp(name, "showmsg")) { 207 } else if (!strcmp(name, "showmsg")) {
185 ctx.qry.showmsg = atoi(value); 208 ctx.qry.showmsg = atoi(value);
186 } else if (!strcmp(name, "period")) { 209 } else if (!strcmp(name, "period")) {
187 ctx.qry.period = xstrdup(value); 210 ctx.qry.period = xstrdup(value);
188 } 211 }
189} 212}
190 213
191static void prepare_context(struct cgit_context *ctx) 214static void prepare_context(struct cgit_context *ctx)
192{ 215{
193 memset(ctx, 0, sizeof(ctx)); 216 memset(ctx, 0, sizeof(ctx));
194 ctx->cfg.agefile = "info/web/last-modified"; 217 ctx->cfg.agefile = "info/web/last-modified";
195 ctx->cfg.nocache = 0; 218 ctx->cfg.nocache = 0;
196 ctx->cfg.cache_size = 0; 219 ctx->cfg.cache_size = 0;
197 ctx->cfg.cache_dynamic_ttl = 5; 220 ctx->cfg.cache_dynamic_ttl = 5;
198 ctx->cfg.cache_max_create_time = 5; 221 ctx->cfg.cache_max_create_time = 5;
199 ctx->cfg.cache_repo_ttl = 5; 222 ctx->cfg.cache_repo_ttl = 5;
200 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 223 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
201 ctx->cfg.cache_root_ttl = 5; 224 ctx->cfg.cache_root_ttl = 5;
202 ctx->cfg.cache_static_ttl = -1; 225 ctx->cfg.cache_static_ttl = -1;
203 ctx->cfg.css = "/cgit.css"; 226 ctx->cfg.css = "/cgit.css";
204 ctx->cfg.logo = "/git-logo.png"; 227 ctx->cfg.logo = "/git-logo.png";
205 ctx->cfg.local_time = 0; 228 ctx->cfg.local_time = 0;
206 ctx->cfg.max_repo_count = 50; 229 ctx->cfg.max_repo_count = 50;
207 ctx->cfg.max_commit_count = 50; 230 ctx->cfg.max_commit_count = 50;
208 ctx->cfg.max_lock_attempts = 5; 231 ctx->cfg.max_lock_attempts = 5;
209 ctx->cfg.max_msg_len = 80; 232 ctx->cfg.max_msg_len = 80;
210 ctx->cfg.max_repodesc_len = 80; 233 ctx->cfg.max_repodesc_len = 80;
211 ctx->cfg.max_stats = 0; 234 ctx->cfg.max_stats = 0;
212 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 235 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
213 ctx->cfg.renamelimit = -1; 236 ctx->cfg.renamelimit = -1;
214 ctx->cfg.robots = "index, nofollow"; 237 ctx->cfg.robots = "index, nofollow";
215 ctx->cfg.root_title = "Git repository browser"; 238 ctx->cfg.root_title = "Git repository browser";
216 ctx->cfg.root_desc = "a fast webinterface for the git dscm"; 239 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
217 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 240 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
218 ctx->cfg.summary_branches = 10; 241 ctx->cfg.summary_branches = 10;
219 ctx->cfg.summary_log = 10; 242 ctx->cfg.summary_log = 10;
220 ctx->cfg.summary_tags = 10; 243 ctx->cfg.summary_tags = 10;
221 ctx->page.mimetype = "text/html"; 244 ctx->page.mimetype = "text/html";
222 ctx->page.charset = PAGE_ENCODING; 245 ctx->page.charset = PAGE_ENCODING;
223 ctx->page.filename = NULL; 246 ctx->page.filename = NULL;
224 ctx->page.size = 0; 247 ctx->page.size = 0;
225 ctx->page.modified = time(NULL); 248 ctx->page.modified = time(NULL);
226 ctx->page.expires = ctx->page.modified; 249 ctx->page.expires = ctx->page.modified;
227 ctx->page.etag = NULL; 250 ctx->page.etag = NULL;
228 memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list)); 251 memset(&ctx->cfg.mimetypes, 0, sizeof(struct string_list));
229} 252}
230 253
231struct refmatch { 254struct refmatch {
232 char *req_ref; 255 char *req_ref;
233 char *first_ref; 256 char *first_ref;
234 int match; 257 int match;
235}; 258};
236 259
237int find_current_ref(const char *refname, const unsigned char *sha1, 260int find_current_ref(const char *refname, const unsigned char *sha1,
238 int flags, void *cb_data) 261 int flags, void *cb_data)
239{ 262{
240 struct refmatch *info; 263 struct refmatch *info;
241 264
242 info = (struct refmatch *)cb_data; 265 info = (struct refmatch *)cb_data;
243 if (!strcmp(refname, info->req_ref)) 266 if (!strcmp(refname, info->req_ref))
244 info->match = 1; 267 info->match = 1;
245 if (!info->first_ref) 268 if (!info->first_ref)
246 info->first_ref = xstrdup(refname); 269 info->first_ref = xstrdup(refname);
247 return info->match; 270 return info->match;
248} 271}
249 272
250char *find_default_branch(struct cgit_repo *repo) 273char *find_default_branch(struct cgit_repo *repo)
251{ 274{
252 struct refmatch info; 275 struct refmatch info;
253 char *ref; 276 char *ref;
254 277
255 info.req_ref = repo->defbranch; 278 info.req_ref = repo->defbranch;
256 info.first_ref = NULL; 279 info.first_ref = NULL;
257 info.match = 0; 280 info.match = 0;
258 for_each_branch_ref(find_current_ref, &info); 281 for_each_branch_ref(find_current_ref, &info);
259 if (info.match) 282 if (info.match)
260 ref = info.req_ref; 283 ref = info.req_ref;
261 else 284 else
262 ref = info.first_ref; 285 ref = info.first_ref;
263 if (ref) 286 if (ref)
264 ref = xstrdup(ref); 287 ref = xstrdup(ref);
265 return ref; 288 return ref;
266} 289}
267 290
268static int prepare_repo_cmd(struct cgit_context *ctx) 291static int prepare_repo_cmd(struct cgit_context *ctx)
269{ 292{
270 char *tmp; 293 char *tmp;
271 unsigned char sha1[20]; 294 unsigned char sha1[20];
272 int nongit = 0; 295 int nongit = 0;
273 296
274 setenv("GIT_DIR", ctx->repo->path, 1); 297 setenv("GIT_DIR", ctx->repo->path, 1);
275 setup_git_directory_gently(&nongit); 298 setup_git_directory_gently(&nongit);
276 if (nongit) { 299 if (nongit) {
277 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, 300 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
278 "config error"); 301 "config error");
279 tmp = fmt("Not a git repository: '%s'", ctx->repo->path); 302 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
280 ctx->repo = NULL; 303 ctx->repo = NULL;
281 cgit_print_http_headers(ctx); 304 cgit_print_http_headers(ctx);
282 cgit_print_docstart(ctx); 305 cgit_print_docstart(ctx);
283 cgit_print_pageheader(ctx); 306 cgit_print_pageheader(ctx);
284 cgit_print_error(tmp); 307 cgit_print_error(tmp);
285 cgit_print_docend(); 308 cgit_print_docend();
286 return 1; 309 return 1;
287 } 310 }
288 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); 311 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
289 312
290 if (!ctx->qry.head) { 313 if (!ctx->qry.head) {
291 ctx->qry.nohead = 1; 314 ctx->qry.nohead = 1;
292 ctx->qry.head = find_default_branch(ctx->repo); 315 ctx->qry.head = find_default_branch(ctx->repo);
293 ctx->repo->defbranch = ctx->qry.head; 316 ctx->repo->defbranch = ctx->qry.head;
294 } 317 }
295 318
296 if (!ctx->qry.head) { 319 if (!ctx->qry.head) {
297 cgit_print_http_headers(ctx); 320 cgit_print_http_headers(ctx);
298 cgit_print_docstart(ctx); 321 cgit_print_docstart(ctx);
299 cgit_print_pageheader(ctx); 322 cgit_print_pageheader(ctx);
300 cgit_print_error("Repository seems to be empty"); 323 cgit_print_error("Repository seems to be empty");
301 cgit_print_docend(); 324 cgit_print_docend();
302 return 1; 325 return 1;
303 } 326 }
304 327
305 if (get_sha1(ctx->qry.head, sha1)) { 328 if (get_sha1(ctx->qry.head, sha1)) {
306 tmp = xstrdup(ctx->qry.head); 329 tmp = xstrdup(ctx->qry.head);
307 ctx->qry.head = ctx->repo->defbranch; 330 ctx->qry.head = ctx->repo->defbranch;
308 ctx->page.status = 404; 331 ctx->page.status = 404;
309 ctx->page.statusmsg = "not found"; 332 ctx->page.statusmsg = "not found";
310 cgit_print_http_headers(ctx); 333 cgit_print_http_headers(ctx);
311 cgit_print_docstart(ctx); 334 cgit_print_docstart(ctx);
312 cgit_print_pageheader(ctx); 335 cgit_print_pageheader(ctx);
313 cgit_print_error(fmt("Invalid branch: %s", tmp)); 336 cgit_print_error(fmt("Invalid branch: %s", tmp));
314 cgit_print_docend(); 337 cgit_print_docend();
315 return 1; 338 return 1;
316 } 339 }
317 return 0; 340 return 0;
318} 341}
319 342
320static void process_request(void *cbdata) 343static void process_request(void *cbdata)
321{ 344{
322 struct cgit_context *ctx = cbdata; 345 struct cgit_context *ctx = cbdata;
323 struct cgit_cmd *cmd; 346 struct cgit_cmd *cmd;
324 347
325 cmd = cgit_get_cmd(ctx); 348 cmd = cgit_get_cmd(ctx);
326 if (!cmd) { 349 if (!cmd) {
327 ctx->page.title = "cgit error"; 350 ctx->page.title = "cgit error";
328 cgit_print_http_headers(ctx); 351 cgit_print_http_headers(ctx);
329 cgit_print_docstart(ctx); 352 cgit_print_docstart(ctx);
330 cgit_print_pageheader(ctx); 353 cgit_print_pageheader(ctx);
331 cgit_print_error("Invalid request"); 354 cgit_print_error("Invalid request");
332 cgit_print_docend(); 355 cgit_print_docend();
333 return; 356 return;
334 } 357 }
335 358
336 if (cmd->want_repo && !ctx->repo) { 359 if (cmd->want_repo && !ctx->repo) {
337 cgit_print_http_headers(ctx); 360 cgit_print_http_headers(ctx);
338 cgit_print_docstart(ctx); 361 cgit_print_docstart(ctx);
339 cgit_print_pageheader(ctx); 362 cgit_print_pageheader(ctx);
340 cgit_print_error(fmt("No repository selected")); 363 cgit_print_error(fmt("No repository selected"));
341 cgit_print_docend(); 364 cgit_print_docend();
342 return; 365 return;
343 } 366 }
344 367
345 if (ctx->repo && prepare_repo_cmd(ctx)) 368 if (ctx->repo && prepare_repo_cmd(ctx))
346 return; 369 return;
347 370
348 if (cmd->want_layout) { 371 if (cmd->want_layout) {
349 cgit_print_http_headers(ctx); 372 cgit_print_http_headers(ctx);
350 cgit_print_docstart(ctx); 373 cgit_print_docstart(ctx);
351 cgit_print_pageheader(ctx); 374 cgit_print_pageheader(ctx);
352 } 375 }
353 376
354 cmd->fn(ctx); 377 cmd->fn(ctx);
355 378
356 if (cmd->want_layout) 379 if (cmd->want_layout)
357 cgit_print_docend(); 380 cgit_print_docend();
358} 381}
359 382
360int cmp_repos(const void *a, const void *b) 383int cmp_repos(const void *a, const void *b)
361{ 384{
362 const struct cgit_repo *ra = a, *rb = b; 385 const struct cgit_repo *ra = a, *rb = b;
363 return strcmp(ra->url, rb->url); 386 return strcmp(ra->url, rb->url);
364} 387}
365 388
366void print_repo(struct cgit_repo *repo) 389void print_repo(struct cgit_repo *repo)
367{ 390{
368 printf("repo.url=%s\n", repo->url); 391 printf("repo.url=%s\n", repo->url);
369 printf("repo.name=%s\n", repo->name); 392 printf("repo.name=%s\n", repo->name);
370 printf("repo.path=%s\n", repo->path); 393 printf("repo.path=%s\n", repo->path);
371 if (repo->owner) 394 if (repo->owner)
372 printf("repo.owner=%s\n", repo->owner); 395 printf("repo.owner=%s\n", repo->owner);
373 if (repo->desc) 396 if (repo->desc)
374 printf("repo.desc=%s\n", repo->desc); 397 printf("repo.desc=%s\n", repo->desc);
375 if (repo->readme) 398 if (repo->readme)
376 printf("repo.readme=%s\n", repo->readme); 399 printf("repo.readme=%s\n", repo->readme);
377 printf("\n"); 400 printf("\n");
378} 401}
379 402
380void print_repolist(struct cgit_repolist *list) 403void print_repolist(struct cgit_repolist *list)
381{ 404{
382 int i; 405 int i;
383 406
384 for(i = 0; i < list->count; i++) 407 for(i = 0; i < list->count; i++)
385 print_repo(&list->repos[i]); 408 print_repo(&list->repos[i]);
386} 409}
387 410
388 411
389static void cgit_parse_args(int argc, const char **argv) 412static void cgit_parse_args(int argc, const char **argv)
390{ 413{
391 int i; 414 int i;
392 int scan = 0; 415 int scan = 0;
393 416
394 for (i = 1; i < argc; i++) { 417 for (i = 1; i < argc; i++) {
395 if (!strncmp(argv[i], "--cache=", 8)) { 418 if (!strncmp(argv[i], "--cache=", 8)) {
396 ctx.cfg.cache_root = xstrdup(argv[i]+8); 419 ctx.cfg.cache_root = xstrdup(argv[i]+8);
397 } 420 }
398 if (!strcmp(argv[i], "--nocache")) { 421 if (!strcmp(argv[i], "--nocache")) {
399 ctx.cfg.nocache = 1; 422 ctx.cfg.nocache = 1;
400 } 423 }
401 if (!strncmp(argv[i], "--query=", 8)) { 424 if (!strncmp(argv[i], "--query=", 8)) {
402 ctx.qry.raw = xstrdup(argv[i]+8); 425 ctx.qry.raw = xstrdup(argv[i]+8);
403 } 426 }
404 if (!strncmp(argv[i], "--repo=", 7)) { 427 if (!strncmp(argv[i], "--repo=", 7)) {
405 ctx.qry.repo = xstrdup(argv[i]+7); 428 ctx.qry.repo = xstrdup(argv[i]+7);
406 } 429 }
407 if (!strncmp(argv[i], "--page=", 7)) { 430 if (!strncmp(argv[i], "--page=", 7)) {
408 ctx.qry.page = xstrdup(argv[i]+7); 431 ctx.qry.page = xstrdup(argv[i]+7);
409 } 432 }
410 if (!strncmp(argv[i], "--head=", 7)) { 433 if (!strncmp(argv[i], "--head=", 7)) {
411 ctx.qry.head = xstrdup(argv[i]+7); 434 ctx.qry.head = xstrdup(argv[i]+7);
412 ctx.qry.has_symref = 1; 435 ctx.qry.has_symref = 1;
413 } 436 }
414 if (!strncmp(argv[i], "--sha1=", 7)) { 437 if (!strncmp(argv[i], "--sha1=", 7)) {
415 ctx.qry.sha1 = xstrdup(argv[i]+7); 438 ctx.qry.sha1 = xstrdup(argv[i]+7);
416 ctx.qry.has_sha1 = 1; 439 ctx.qry.has_sha1 = 1;
417 } 440 }
418 if (!strncmp(argv[i], "--ofs=", 6)) { 441 if (!strncmp(argv[i], "--ofs=", 6)) {
419 ctx.qry.ofs = atoi(argv[i]+6); 442 ctx.qry.ofs = atoi(argv[i]+6);
420 } 443 }
421 if (!strncmp(argv[i], "--scan-tree=", 12)) { 444 if (!strncmp(argv[i], "--scan-tree=", 12)) {
422 scan++; 445 scan++;
423 scan_tree(argv[i] + 12); 446 scan_tree(argv[i] + 12);
424 } 447 }
425 } 448 }
426 if (scan) { 449 if (scan) {
427 qsort(cgit_repolist.repos, cgit_repolist.count, 450 qsort(cgit_repolist.repos, cgit_repolist.count,
428 sizeof(struct cgit_repo), cmp_repos); 451 sizeof(struct cgit_repo), cmp_repos);
429 print_repolist(&cgit_repolist); 452 print_repolist(&cgit_repolist);
430 exit(0); 453 exit(0);
431 } 454 }
432} 455}
433 456
434static int calc_ttl() 457static int calc_ttl()
435{ 458{
436 if (!ctx.repo) 459 if (!ctx.repo)
437 return ctx.cfg.cache_root_ttl; 460 return ctx.cfg.cache_root_ttl;
438 461
439 if (!ctx.qry.page) 462 if (!ctx.qry.page)
440 return ctx.cfg.cache_repo_ttl; 463 return ctx.cfg.cache_repo_ttl;
441 464
442 if (ctx.qry.has_symref) 465 if (ctx.qry.has_symref)
443 return ctx.cfg.cache_dynamic_ttl; 466 return ctx.cfg.cache_dynamic_ttl;
444 467
445 if (ctx.qry.has_sha1) 468 if (ctx.qry.has_sha1)
446 return ctx.cfg.cache_static_ttl; 469 return ctx.cfg.cache_static_ttl;
447 470
448 return ctx.cfg.cache_repo_ttl; 471 return ctx.cfg.cache_repo_ttl;
449} 472}
450 473
451int main(int argc, const char **argv) 474int main(int argc, const char **argv)
452{ 475{
453 const char *cgit_config_env = getenv("CGIT_CONFIG"); 476 const char *cgit_config_env = getenv("CGIT_CONFIG");
454 const char *method = getenv("REQUEST_METHOD"); 477 const char *method = getenv("REQUEST_METHOD");
455 const char *path; 478 const char *path;
456 char *qry; 479 char *qry;
457 int err, ttl; 480 int err, ttl;
458 481
459 prepare_context(&ctx); 482 prepare_context(&ctx);
460 cgit_repolist.length = 0; 483 cgit_repolist.length = 0;
461 cgit_repolist.count = 0; 484 cgit_repolist.count = 0;
462 cgit_repolist.repos = NULL; 485 cgit_repolist.repos = NULL;
463 486
464 if (getenv("SCRIPT_NAME")) 487 if (getenv("SCRIPT_NAME"))
465 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 488 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
466 if (getenv("QUERY_STRING")) 489 if (getenv("QUERY_STRING"))
467 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 490 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
468 cgit_parse_args(argc, argv); 491 cgit_parse_args(argc, argv);
469 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 492 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
470 config_cb); 493 config_cb);
471 ctx.repo = NULL; 494 ctx.repo = NULL;
472 http_parse_querystring(ctx.qry.raw, querystring_cb); 495 http_parse_querystring(ctx.qry.raw, querystring_cb);
473 496
474 /* If virtual-root isn't specified in cgitrc, lets pretend 497 /* If virtual-root isn't specified in cgitrc, lets pretend
475 * that virtual-root equals SCRIPT_NAME. 498 * that virtual-root equals SCRIPT_NAME.
476 */ 499 */
477 if (!ctx.cfg.virtual_root) 500 if (!ctx.cfg.virtual_root)
478 ctx.cfg.virtual_root = ctx.cfg.script_name; 501 ctx.cfg.virtual_root = ctx.cfg.script_name;
479 502
480 /* If no url parameter is specified on the querystring, lets 503 /* If no url parameter is specified on the querystring, lets
481 * use PATH_INFO as url. This allows cgit to work with virtual 504 * use PATH_INFO as url. This allows cgit to work with virtual
482 * urls without the need for rewriterules in the webserver (as 505 * urls without the need for rewriterules in the webserver (as
483 * long as PATH_INFO is included in the cache lookup key). 506 * long as PATH_INFO is included in the cache lookup key).
484 */ 507 */
485 path = getenv("PATH_INFO"); 508 path = getenv("PATH_INFO");
486 if (!ctx.qry.url && path) { 509 if (!ctx.qry.url && path) {
487 if (path[0] == '/') 510 if (path[0] == '/')
488 path++; 511 path++;
489 ctx.qry.url = xstrdup(path); 512 ctx.qry.url = xstrdup(path);
490 if (ctx.qry.raw) { 513 if (ctx.qry.raw) {
491 qry = ctx.qry.raw; 514 qry = ctx.qry.raw;
492 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); 515 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
493 free(qry); 516 free(qry);
494 } else 517 } else
495 ctx.qry.raw = ctx.qry.url; 518 ctx.qry.raw = ctx.qry.url;
496 cgit_parse_url(ctx.qry.url); 519 cgit_parse_url(ctx.qry.url);
497 } 520 }
498 521
499 ttl = calc_ttl(); 522 ttl = calc_ttl();
500 ctx.page.expires += ttl*60; 523 ctx.page.expires += ttl*60;
501 if (method && !strcmp(method, "HEAD")) 524 if (method && !strcmp(method, "HEAD"))
502 ctx.cfg.nocache = 1; 525 ctx.cfg.nocache = 1;
503 if (ctx.cfg.nocache) 526 if (ctx.cfg.nocache)
504 ctx.cfg.cache_size = 0; 527 ctx.cfg.cache_size = 0;
505 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, 528 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
506 ctx.qry.raw, ttl, process_request, &ctx); 529 ctx.qry.raw, ttl, process_request, &ctx);
507 if (err) 530 if (err)
508 cgit_print_error(fmt("Error processing page: %s (%d)", 531 cgit_print_error(fmt("Error processing page: %s (%d)",
509 strerror(err), err)); 532 strerror(err), err));
510 return err; 533 return err;
511} 534}