author | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 16:26:08 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 16:26:08 (UTC) |
commit | 163037e79c6cde1073d555dbeae2a095298e6101 (patch) (unidiff) | |
tree | 37f36b95772eff04683a7f7efc1c5c369b7bf905 | |
parent | a4d1ca1dc6ff8171694d9e2280b6075a1beced0c (diff) | |
download | cgit-163037e79c6cde1073d555dbeae2a095298e6101.zip cgit-163037e79c6cde1073d555dbeae2a095298e6101.tar.gz cgit-163037e79c6cde1073d555dbeae2a095298e6101.tar.bz2 |
Move non-generic functions from shared.c to cgit.c
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cgit.c | 156 | ||||
-rw-r--r-- | cgit.h | 4 | ||||
-rw-r--r-- | shared.c | 152 |
3 files changed, 155 insertions, 157 deletions
@@ -1,303 +1,453 @@ | |||
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 "cmd.h" | 10 | #include "cmd.h" |
11 | #include "ui-shared.h" | 11 | #include "ui-shared.h" |
12 | 12 | ||
13 | void config_cb(const char *name, const char *value) | ||
14 | { | ||
15 | if (!strcmp(name, "root-title")) | ||
16 | ctx.cfg.root_title = xstrdup(value); | ||
17 | else if (!strcmp(name, "css")) | ||
18 | ctx.cfg.css = xstrdup(value); | ||
19 | else if (!strcmp(name, "logo")) | ||
20 | ctx.cfg.logo = xstrdup(value); | ||
21 | else if (!strcmp(name, "index-header")) | ||
22 | ctx.cfg.index_header = xstrdup(value); | ||
23 | else if (!strcmp(name, "index-info")) | ||
24 | ctx.cfg.index_info = xstrdup(value); | ||
25 | else if (!strcmp(name, "logo-link")) | ||
26 | ctx.cfg.logo_link = xstrdup(value); | ||
27 | else if (!strcmp(name, "module-link")) | ||
28 | ctx.cfg.module_link = xstrdup(value); | ||
29 | else if (!strcmp(name, "virtual-root")) { | ||
30 | ctx.cfg.virtual_root = trim_end(value, '/'); | ||
31 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | ||
32 | ctx.cfg.virtual_root = ""; | ||
33 | } else if (!strcmp(name, "nocache")) | ||
34 | ctx.cfg.nocache = atoi(value); | ||
35 | else if (!strcmp(name, "snapshots")) | ||
36 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | ||
37 | else if (!strcmp(name, "enable-index-links")) | ||
38 | ctx.cfg.enable_index_links = atoi(value); | ||
39 | else if (!strcmp(name, "enable-log-filecount")) | ||
40 | ctx.cfg.enable_log_filecount = atoi(value); | ||
41 | else if (!strcmp(name, "enable-log-linecount")) | ||
42 | ctx.cfg.enable_log_linecount = atoi(value); | ||
43 | else if (!strcmp(name, "cache-root")) | ||
44 | ctx.cfg.cache_root = xstrdup(value); | ||
45 | else if (!strcmp(name, "cache-root-ttl")) | ||
46 | ctx.cfg.cache_root_ttl = atoi(value); | ||
47 | else if (!strcmp(name, "cache-repo-ttl")) | ||
48 | ctx.cfg.cache_repo_ttl = atoi(value); | ||
49 | else if (!strcmp(name, "cache-static-ttl")) | ||
50 | ctx.cfg.cache_static_ttl = atoi(value); | ||
51 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
52 | ctx.cfg.cache_dynamic_ttl = atoi(value); | ||
53 | else if (!strcmp(name, "max-message-length")) | ||
54 | ctx.cfg.max_msg_len = atoi(value); | ||
55 | else if (!strcmp(name, "max-repodesc-length")) | ||
56 | ctx.cfg.max_repodesc_len = atoi(value); | ||
57 | else if (!strcmp(name, "max-commit-count")) | ||
58 | ctx.cfg.max_commit_count = atoi(value); | ||
59 | else if (!strcmp(name, "summary-log")) | ||
60 | ctx.cfg.summary_log = atoi(value); | ||
61 | else if (!strcmp(name, "summary-branches")) | ||
62 | ctx.cfg.summary_branches = atoi(value); | ||
63 | else if (!strcmp(name, "summary-tags")) | ||
64 | ctx.cfg.summary_tags = atoi(value); | ||
65 | else if (!strcmp(name, "agefile")) | ||
66 | ctx.cfg.agefile = xstrdup(value); | ||
67 | else if (!strcmp(name, "renamelimit")) | ||
68 | ctx.cfg.renamelimit = atoi(value); | ||
69 | else if (!strcmp(name, "robots")) | ||
70 | ctx.cfg.robots = xstrdup(value); | ||
71 | else if (!strcmp(name, "clone-prefix")) | ||
72 | ctx.cfg.clone_prefix = xstrdup(value); | ||
73 | else if (!strcmp(name, "repo.group")) | ||
74 | ctx.cfg.repo_group = xstrdup(value); | ||
75 | else if (!strcmp(name, "repo.url")) | ||
76 | ctx.repo = cgit_add_repo(value); | ||
77 | else if (!strcmp(name, "repo.name")) | ||
78 | ctx.repo->name = xstrdup(value); | ||
79 | else if (ctx.repo && !strcmp(name, "repo.path")) | ||
80 | ctx.repo->path = trim_end(value, '/'); | ||
81 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | ||
82 | ctx.repo->clone_url = xstrdup(value); | ||
83 | else if (ctx.repo && !strcmp(name, "repo.desc")) | ||
84 | ctx.repo->desc = xstrdup(value); | ||
85 | else if (ctx.repo && !strcmp(name, "repo.owner")) | ||
86 | ctx.repo->owner = xstrdup(value); | ||
87 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | ||
88 | ctx.repo->defbranch = xstrdup(value); | ||
89 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | ||
90 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
91 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | ||
92 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | ||
93 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | ||
94 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | ||
95 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | ||
96 | ctx.repo->module_link= xstrdup(value); | ||
97 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
98 | if (*value == '/') | ||
99 | ctx.repo->readme = xstrdup(value); | ||
100 | else | ||
101 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | ||
102 | } else if (!strcmp(name, "include")) | ||
103 | cgit_read_config(value, config_cb); | ||
104 | } | ||
105 | |||
106 | static void querystring_cb(const char *name, const char *value) | ||
107 | { | ||
108 | if (!strcmp(name,"r")) { | ||
109 | ctx.qry.repo = xstrdup(value); | ||
110 | ctx.repo = cgit_get_repoinfo(value); | ||
111 | } else if (!strcmp(name, "p")) { | ||
112 | ctx.qry.page = xstrdup(value); | ||
113 | } else if (!strcmp(name, "url")) { | ||
114 | cgit_parse_url(value); | ||
115 | } else if (!strcmp(name, "qt")) { | ||
116 | ctx.qry.grep = xstrdup(value); | ||
117 | } else if (!strcmp(name, "q")) { | ||
118 | ctx.qry.search = xstrdup(value); | ||
119 | } else if (!strcmp(name, "h")) { | ||
120 | ctx.qry.head = xstrdup(value); | ||
121 | ctx.qry.has_symref = 1; | ||
122 | } else if (!strcmp(name, "id")) { | ||
123 | ctx.qry.sha1 = xstrdup(value); | ||
124 | ctx.qry.has_sha1 = 1; | ||
125 | } else if (!strcmp(name, "id2")) { | ||
126 | ctx.qry.sha2 = xstrdup(value); | ||
127 | ctx.qry.has_sha1 = 1; | ||
128 | } else if (!strcmp(name, "ofs")) { | ||
129 | ctx.qry.ofs = atoi(value); | ||
130 | } else if (!strcmp(name, "path")) { | ||
131 | ctx.qry.path = trim_end(value, '/'); | ||
132 | } else if (!strcmp(name, "name")) { | ||
133 | ctx.qry.name = xstrdup(value); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | static void prepare_context(struct cgit_context *ctx) | ||
138 | { | ||
139 | memset(ctx, 0, sizeof(ctx)); | ||
140 | ctx->cfg.agefile = "info/web/last-modified"; | ||
141 | ctx->cfg.cache_dynamic_ttl = 5; | ||
142 | ctx->cfg.cache_max_create_time = 5; | ||
143 | ctx->cfg.cache_repo_ttl = 5; | ||
144 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
145 | ctx->cfg.cache_root_ttl = 5; | ||
146 | ctx->cfg.cache_static_ttl = -1; | ||
147 | ctx->cfg.css = "/cgit.css"; | ||
148 | ctx->cfg.logo = "/git-logo.png"; | ||
149 | ctx->cfg.max_commit_count = 50; | ||
150 | ctx->cfg.max_lock_attempts = 5; | ||
151 | ctx->cfg.max_msg_len = 60; | ||
152 | ctx->cfg.max_repodesc_len = 60; | ||
153 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
154 | ctx->cfg.renamelimit = -1; | ||
155 | ctx->cfg.robots = "index, nofollow"; | ||
156 | ctx->cfg.root_title = "Git repository browser"; | ||
157 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
158 | ctx->page.mimetype = "text/html"; | ||
159 | ctx->page.charset = PAGE_ENCODING; | ||
160 | ctx->page.filename = NULL; | ||
161 | } | ||
162 | |||
13 | static int cgit_prepare_cache(struct cacheitem *item) | 163 | static int cgit_prepare_cache(struct cacheitem *item) |
14 | { | 164 | { |
15 | if (!ctx.repo && ctx.qry.repo) { | 165 | if (!ctx.repo && ctx.qry.repo) { |
16 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, | 166 | ctx.page.title = fmt("%s - %s", ctx.cfg.root_title, |
17 | "Bad request"); | 167 | "Bad request"); |
18 | cgit_print_http_headers(&ctx); | 168 | cgit_print_http_headers(&ctx); |
19 | cgit_print_docstart(&ctx); | 169 | cgit_print_docstart(&ctx); |
20 | cgit_print_pageheader(&ctx); | 170 | cgit_print_pageheader(&ctx); |
21 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); | 171 | cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo)); |
22 | cgit_print_docend(); | 172 | cgit_print_docend(); |
23 | return 0; | 173 | return 0; |
24 | } | 174 | } |
25 | 175 | ||
26 | if (!ctx.repo) { | 176 | if (!ctx.repo) { |
27 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); | 177 | item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root)); |
28 | item->ttl = ctx.cfg.cache_root_ttl; | 178 | item->ttl = ctx.cfg.cache_root_ttl; |
29 | return 1; | 179 | return 1; |
30 | } | 180 | } |
31 | 181 | ||
32 | if (!cgit_cmd) { | 182 | if (!cgit_cmd) { |
33 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, | 183 | item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root, |
34 | cache_safe_filename(ctx.repo->url), | 184 | cache_safe_filename(ctx.repo->url), |
35 | cache_safe_filename(ctx.qry.raw))); | 185 | cache_safe_filename(ctx.qry.raw))); |
36 | item->ttl = ctx.cfg.cache_repo_ttl; | 186 | item->ttl = ctx.cfg.cache_repo_ttl; |
37 | } else { | 187 | } else { |
38 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, | 188 | item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root, |
39 | cache_safe_filename(ctx.repo->url), | 189 | cache_safe_filename(ctx.repo->url), |
40 | ctx.qry.page, | 190 | ctx.qry.page, |
41 | cache_safe_filename(ctx.qry.raw))); | 191 | cache_safe_filename(ctx.qry.raw))); |
42 | if (ctx.qry.has_symref) | 192 | if (ctx.qry.has_symref) |
43 | item->ttl = ctx.cfg.cache_dynamic_ttl; | 193 | item->ttl = ctx.cfg.cache_dynamic_ttl; |
44 | else if (ctx.qry.has_sha1) | 194 | else if (ctx.qry.has_sha1) |
45 | item->ttl = ctx.cfg.cache_static_ttl; | 195 | item->ttl = ctx.cfg.cache_static_ttl; |
46 | else | 196 | else |
47 | item->ttl = ctx.cfg.cache_repo_ttl; | 197 | item->ttl = ctx.cfg.cache_repo_ttl; |
48 | } | 198 | } |
49 | return 1; | 199 | return 1; |
50 | } | 200 | } |
51 | 201 | ||
52 | struct refmatch { | 202 | struct refmatch { |
53 | char *req_ref; | 203 | char *req_ref; |
54 | char *first_ref; | 204 | char *first_ref; |
55 | int match; | 205 | int match; |
56 | }; | 206 | }; |
57 | 207 | ||
58 | int find_current_ref(const char *refname, const unsigned char *sha1, | 208 | int find_current_ref(const char *refname, const unsigned char *sha1, |
59 | int flags, void *cb_data) | 209 | int flags, void *cb_data) |
60 | { | 210 | { |
61 | struct refmatch *info; | 211 | struct refmatch *info; |
62 | 212 | ||
63 | info = (struct refmatch *)cb_data; | 213 | info = (struct refmatch *)cb_data; |
64 | if (!strcmp(refname, info->req_ref)) | 214 | if (!strcmp(refname, info->req_ref)) |
65 | info->match = 1; | 215 | info->match = 1; |
66 | if (!info->first_ref) | 216 | if (!info->first_ref) |
67 | info->first_ref = xstrdup(refname); | 217 | info->first_ref = xstrdup(refname); |
68 | return info->match; | 218 | return info->match; |
69 | } | 219 | } |
70 | 220 | ||
71 | char *find_default_branch(struct cgit_repo *repo) | 221 | char *find_default_branch(struct cgit_repo *repo) |
72 | { | 222 | { |
73 | struct refmatch info; | 223 | struct refmatch info; |
74 | 224 | ||
75 | info.req_ref = repo->defbranch; | 225 | info.req_ref = repo->defbranch; |
76 | info.first_ref = NULL; | 226 | info.first_ref = NULL; |
77 | info.match = 0; | 227 | info.match = 0; |
78 | for_each_branch_ref(find_current_ref, &info); | 228 | for_each_branch_ref(find_current_ref, &info); |
79 | if (info.match) | 229 | if (info.match) |
80 | return info.req_ref; | 230 | return info.req_ref; |
81 | else | 231 | else |
82 | return info.first_ref; | 232 | return info.first_ref; |
83 | } | 233 | } |
84 | 234 | ||
85 | static int prepare_repo_cmd(struct cgit_context *ctx) | 235 | static int prepare_repo_cmd(struct cgit_context *ctx) |
86 | { | 236 | { |
87 | char *tmp; | 237 | char *tmp; |
88 | unsigned char sha1[20]; | 238 | unsigned char sha1[20]; |
89 | int nongit = 0; | 239 | int nongit = 0; |
90 | 240 | ||
91 | setenv("GIT_DIR", ctx->repo->path, 1); | 241 | setenv("GIT_DIR", ctx->repo->path, 1); |
92 | setup_git_directory_gently(&nongit); | 242 | setup_git_directory_gently(&nongit); |
93 | if (nongit) { | 243 | if (nongit) { |
94 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, | 244 | ctx->page.title = fmt("%s - %s", ctx->cfg.root_title, |
95 | "config error"); | 245 | "config error"); |
96 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); | 246 | tmp = fmt("Not a git repository: '%s'", ctx->repo->path); |
97 | ctx->repo = NULL; | 247 | ctx->repo = NULL; |
98 | cgit_print_http_headers(ctx); | 248 | cgit_print_http_headers(ctx); |
99 | cgit_print_docstart(ctx); | 249 | cgit_print_docstart(ctx); |
100 | cgit_print_pageheader(ctx); | 250 | cgit_print_pageheader(ctx); |
101 | cgit_print_error(tmp); | 251 | cgit_print_error(tmp); |
102 | cgit_print_docend(); | 252 | cgit_print_docend(); |
103 | return 1; | 253 | return 1; |
104 | } | 254 | } |
105 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); | 255 | ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc); |
106 | 256 | ||
107 | if (!ctx->qry.head) { | 257 | if (!ctx->qry.head) { |
108 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); | 258 | ctx->qry.head = xstrdup(find_default_branch(ctx->repo)); |
109 | ctx->repo->defbranch = ctx->qry.head; | 259 | ctx->repo->defbranch = ctx->qry.head; |
110 | } | 260 | } |
111 | 261 | ||
112 | if (!ctx->qry.head) { | 262 | if (!ctx->qry.head) { |
113 | cgit_print_http_headers(ctx); | 263 | cgit_print_http_headers(ctx); |
114 | cgit_print_docstart(ctx); | 264 | cgit_print_docstart(ctx); |
115 | cgit_print_pageheader(ctx); | 265 | cgit_print_pageheader(ctx); |
116 | cgit_print_error("Repository seems to be empty"); | 266 | cgit_print_error("Repository seems to be empty"); |
117 | cgit_print_docend(); | 267 | cgit_print_docend(); |
118 | return 1; | 268 | return 1; |
119 | } | 269 | } |
120 | 270 | ||
121 | if (get_sha1(ctx->qry.head, sha1)) { | 271 | if (get_sha1(ctx->qry.head, sha1)) { |
122 | tmp = xstrdup(ctx->qry.head); | 272 | tmp = xstrdup(ctx->qry.head); |
123 | ctx->qry.head = ctx->repo->defbranch; | 273 | ctx->qry.head = ctx->repo->defbranch; |
124 | cgit_print_http_headers(ctx); | 274 | cgit_print_http_headers(ctx); |
125 | cgit_print_docstart(ctx); | 275 | cgit_print_docstart(ctx); |
126 | cgit_print_pageheader(ctx); | 276 | cgit_print_pageheader(ctx); |
127 | cgit_print_error(fmt("Invalid branch: %s", tmp)); | 277 | cgit_print_error(fmt("Invalid branch: %s", tmp)); |
128 | cgit_print_docend(); | 278 | cgit_print_docend(); |
129 | return 1; | 279 | return 1; |
130 | } | 280 | } |
131 | return 0; | 281 | return 0; |
132 | } | 282 | } |
133 | 283 | ||
134 | static void process_request(struct cgit_context *ctx) | 284 | static void process_request(struct cgit_context *ctx) |
135 | { | 285 | { |
136 | struct cgit_cmd *cmd; | 286 | struct cgit_cmd *cmd; |
137 | 287 | ||
138 | cmd = cgit_get_cmd(ctx); | 288 | cmd = cgit_get_cmd(ctx); |
139 | if (!cmd) { | 289 | if (!cmd) { |
140 | ctx->page.title = "cgit error"; | 290 | ctx->page.title = "cgit error"; |
141 | ctx->repo = NULL; | 291 | ctx->repo = NULL; |
142 | cgit_print_http_headers(ctx); | 292 | cgit_print_http_headers(ctx); |
143 | cgit_print_docstart(ctx); | 293 | cgit_print_docstart(ctx); |
144 | cgit_print_pageheader(ctx); | 294 | cgit_print_pageheader(ctx); |
145 | cgit_print_error("Invalid request"); | 295 | cgit_print_error("Invalid request"); |
146 | cgit_print_docend(); | 296 | cgit_print_docend(); |
147 | return; | 297 | return; |
148 | } | 298 | } |
149 | 299 | ||
150 | if (cmd->want_repo && prepare_repo_cmd(ctx)) | 300 | if (cmd->want_repo && prepare_repo_cmd(ctx)) |
151 | return; | 301 | return; |
152 | 302 | ||
153 | if (cmd->want_layout) { | 303 | if (cmd->want_layout) { |
154 | cgit_print_http_headers(ctx); | 304 | cgit_print_http_headers(ctx); |
155 | cgit_print_docstart(ctx); | 305 | cgit_print_docstart(ctx); |
156 | cgit_print_pageheader(ctx); | 306 | cgit_print_pageheader(ctx); |
157 | } | 307 | } |
158 | 308 | ||
159 | cmd->fn(ctx); | 309 | cmd->fn(ctx); |
160 | 310 | ||
161 | if (cmd->want_layout) | 311 | if (cmd->want_layout) |
162 | cgit_print_docend(); | 312 | cgit_print_docend(); |
163 | } | 313 | } |
164 | 314 | ||
165 | static long ttl_seconds(long ttl) | 315 | static long ttl_seconds(long ttl) |
166 | { | 316 | { |
167 | if (ttl<0) | 317 | if (ttl<0) |
168 | return 60 * 60 * 24 * 365; | 318 | return 60 * 60 * 24 * 365; |
169 | else | 319 | else |
170 | return ttl * 60; | 320 | return ttl * 60; |
171 | } | 321 | } |
172 | 322 | ||
173 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) | 323 | static void cgit_fill_cache(struct cacheitem *item, int use_cache) |
174 | { | 324 | { |
175 | int stdout2; | 325 | int stdout2; |
176 | 326 | ||
177 | if (use_cache) { | 327 | if (use_cache) { |
178 | stdout2 = chk_positive(dup(STDOUT_FILENO), | 328 | stdout2 = chk_positive(dup(STDOUT_FILENO), |
179 | "Preserving STDOUT"); | 329 | "Preserving STDOUT"); |
180 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); | 330 | chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); |
181 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); | 331 | chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); |
182 | } | 332 | } |
183 | 333 | ||
184 | ctx.page.modified = time(NULL); | 334 | ctx.page.modified = time(NULL); |
185 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); | 335 | ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl); |
186 | process_request(&ctx); | 336 | process_request(&ctx); |
187 | 337 | ||
188 | if (use_cache) { | 338 | if (use_cache) { |
189 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); | 339 | chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); |
190 | chk_positive(dup2(stdout2, STDOUT_FILENO), | 340 | chk_positive(dup2(stdout2, STDOUT_FILENO), |
191 | "Restoring original STDOUT"); | 341 | "Restoring original STDOUT"); |
192 | chk_zero(close(stdout2), "Closing temporary STDOUT"); | 342 | chk_zero(close(stdout2), "Closing temporary STDOUT"); |
193 | } | 343 | } |
194 | } | 344 | } |
195 | 345 | ||
196 | static void cgit_check_cache(struct cacheitem *item) | 346 | static void cgit_check_cache(struct cacheitem *item) |
197 | { | 347 | { |
198 | int i = 0; | 348 | int i = 0; |
199 | 349 | ||
200 | top: | 350 | top: |
201 | if (++i > ctx.cfg.max_lock_attempts) { | 351 | if (++i > ctx.cfg.max_lock_attempts) { |
202 | die("cgit_refresh_cache: unable to lock %s: %s", | 352 | die("cgit_refresh_cache: unable to lock %s: %s", |
203 | item->name, strerror(errno)); | 353 | item->name, strerror(errno)); |
204 | } | 354 | } |
205 | if (!cache_exist(item)) { | 355 | if (!cache_exist(item)) { |
206 | if (!cache_lock(item)) { | 356 | if (!cache_lock(item)) { |
207 | sleep(1); | 357 | sleep(1); |
208 | goto top; | 358 | goto top; |
209 | } | 359 | } |
210 | if (!cache_exist(item)) { | 360 | if (!cache_exist(item)) { |
211 | cgit_fill_cache(item, 1); | 361 | cgit_fill_cache(item, 1); |
212 | cache_unlock(item); | 362 | cache_unlock(item); |
213 | } else { | 363 | } else { |
214 | cache_cancel_lock(item); | 364 | cache_cancel_lock(item); |
215 | } | 365 | } |
216 | } else if (cache_expired(item) && cache_lock(item)) { | 366 | } else if (cache_expired(item) && cache_lock(item)) { |
217 | if (cache_expired(item)) { | 367 | if (cache_expired(item)) { |
218 | cgit_fill_cache(item, 1); | 368 | cgit_fill_cache(item, 1); |
219 | cache_unlock(item); | 369 | cache_unlock(item); |
220 | } else { | 370 | } else { |
221 | cache_cancel_lock(item); | 371 | cache_cancel_lock(item); |
222 | } | 372 | } |
223 | } | 373 | } |
224 | } | 374 | } |
225 | 375 | ||
226 | static void cgit_print_cache(struct cacheitem *item) | 376 | static void cgit_print_cache(struct cacheitem *item) |
227 | { | 377 | { |
228 | static char buf[4096]; | 378 | static char buf[4096]; |
229 | ssize_t i; | 379 | ssize_t i; |
230 | 380 | ||
231 | int fd = open(item->name, O_RDONLY); | 381 | int fd = open(item->name, O_RDONLY); |
232 | if (fd<0) | 382 | if (fd<0) |
233 | die("Unable to open cached file %s", item->name); | 383 | die("Unable to open cached file %s", item->name); |
234 | 384 | ||
235 | while((i=read(fd, buf, sizeof(buf))) > 0) | 385 | while((i=read(fd, buf, sizeof(buf))) > 0) |
236 | write(STDOUT_FILENO, buf, i); | 386 | write(STDOUT_FILENO, buf, i); |
237 | 387 | ||
238 | close(fd); | 388 | close(fd); |
239 | } | 389 | } |
240 | 390 | ||
241 | static void cgit_parse_args(int argc, const char **argv) | 391 | static void cgit_parse_args(int argc, const char **argv) |
242 | { | 392 | { |
243 | int i; | 393 | int i; |
244 | 394 | ||
245 | for (i = 1; i < argc; i++) { | 395 | for (i = 1; i < argc; i++) { |
246 | if (!strncmp(argv[i], "--cache=", 8)) { | 396 | if (!strncmp(argv[i], "--cache=", 8)) { |
247 | ctx.cfg.cache_root = xstrdup(argv[i]+8); | 397 | ctx.cfg.cache_root = xstrdup(argv[i]+8); |
248 | } | 398 | } |
249 | if (!strcmp(argv[i], "--nocache")) { | 399 | if (!strcmp(argv[i], "--nocache")) { |
250 | ctx.cfg.nocache = 1; | 400 | ctx.cfg.nocache = 1; |
251 | } | 401 | } |
252 | if (!strncmp(argv[i], "--query=", 8)) { | 402 | if (!strncmp(argv[i], "--query=", 8)) { |
253 | ctx.qry.raw = xstrdup(argv[i]+8); | 403 | ctx.qry.raw = xstrdup(argv[i]+8); |
254 | } | 404 | } |
255 | if (!strncmp(argv[i], "--repo=", 7)) { | 405 | if (!strncmp(argv[i], "--repo=", 7)) { |
256 | ctx.qry.repo = xstrdup(argv[i]+7); | 406 | ctx.qry.repo = xstrdup(argv[i]+7); |
257 | } | 407 | } |
258 | if (!strncmp(argv[i], "--page=", 7)) { | 408 | if (!strncmp(argv[i], "--page=", 7)) { |
259 | ctx.qry.page = xstrdup(argv[i]+7); | 409 | ctx.qry.page = xstrdup(argv[i]+7); |
260 | } | 410 | } |
261 | if (!strncmp(argv[i], "--head=", 7)) { | 411 | if (!strncmp(argv[i], "--head=", 7)) { |
262 | ctx.qry.head = xstrdup(argv[i]+7); | 412 | ctx.qry.head = xstrdup(argv[i]+7); |
263 | ctx.qry.has_symref = 1; | 413 | ctx.qry.has_symref = 1; |
264 | } | 414 | } |
265 | if (!strncmp(argv[i], "--sha1=", 7)) { | 415 | if (!strncmp(argv[i], "--sha1=", 7)) { |
266 | ctx.qry.sha1 = xstrdup(argv[i]+7); | 416 | ctx.qry.sha1 = xstrdup(argv[i]+7); |
267 | ctx.qry.has_sha1 = 1; | 417 | ctx.qry.has_sha1 = 1; |
268 | } | 418 | } |
269 | if (!strncmp(argv[i], "--ofs=", 6)) { | 419 | if (!strncmp(argv[i], "--ofs=", 6)) { |
270 | ctx.qry.ofs = atoi(argv[i]+6); | 420 | ctx.qry.ofs = atoi(argv[i]+6); |
271 | } | 421 | } |
272 | } | 422 | } |
273 | } | 423 | } |
274 | 424 | ||
275 | int main(int argc, const char **argv) | 425 | int main(int argc, const char **argv) |
276 | { | 426 | { |
277 | struct cacheitem item; | 427 | struct cacheitem item; |
278 | const char *cgit_config_env = getenv("CGIT_CONFIG"); | 428 | const char *cgit_config_env = getenv("CGIT_CONFIG"); |
279 | 429 | ||
280 | cgit_prepare_context(&ctx); | 430 | prepare_context(&ctx); |
281 | item.st.st_mtime = time(NULL); | 431 | item.st.st_mtime = time(NULL); |
282 | cgit_repolist.length = 0; | 432 | cgit_repolist.length = 0; |
283 | cgit_repolist.count = 0; | 433 | cgit_repolist.count = 0; |
284 | cgit_repolist.repos = NULL; | 434 | cgit_repolist.repos = NULL; |
285 | 435 | ||
286 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, | 436 | cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, |
287 | cgit_global_config_cb); | 437 | config_cb); |
288 | if (getenv("SCRIPT_NAME")) | 438 | if (getenv("SCRIPT_NAME")) |
289 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); | 439 | ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); |
290 | if (getenv("QUERY_STRING")) | 440 | if (getenv("QUERY_STRING")) |
291 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); | 441 | ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); |
292 | cgit_parse_args(argc, argv); | 442 | cgit_parse_args(argc, argv); |
293 | cgit_parse_query(ctx.qry.raw, cgit_querystring_cb); | 443 | cgit_parse_query(ctx.qry.raw, querystring_cb); |
294 | if (!cgit_prepare_cache(&item)) | 444 | if (!cgit_prepare_cache(&item)) |
295 | return 0; | 445 | return 0; |
296 | if (ctx.cfg.nocache) { | 446 | if (ctx.cfg.nocache) { |
297 | cgit_fill_cache(&item, 0); | 447 | cgit_fill_cache(&item, 0); |
298 | } else { | 448 | } else { |
299 | cgit_check_cache(&item); | 449 | cgit_check_cache(&item); |
300 | cgit_print_cache(&item); | 450 | cgit_print_cache(&item); |
301 | } | 451 | } |
302 | return 0; | 452 | return 0; |
303 | } | 453 | } |
@@ -1,244 +1,242 @@ | |||
1 | #ifndef CGIT_H | 1 | #ifndef CGIT_H |
2 | #define CGIT_H | 2 | #define CGIT_H |
3 | 3 | ||
4 | 4 | ||
5 | #include <git-compat-util.h> | 5 | #include <git-compat-util.h> |
6 | #include <cache.h> | 6 | #include <cache.h> |
7 | #include <grep.h> | 7 | #include <grep.h> |
8 | #include <object.h> | 8 | #include <object.h> |
9 | #include <tree.h> | 9 | #include <tree.h> |
10 | #include <commit.h> | 10 | #include <commit.h> |
11 | #include <tag.h> | 11 | #include <tag.h> |
12 | #include <diff.h> | 12 | #include <diff.h> |
13 | #include <diffcore.h> | 13 | #include <diffcore.h> |
14 | #include <refs.h> | 14 | #include <refs.h> |
15 | #include <revision.h> | 15 | #include <revision.h> |
16 | #include <log-tree.h> | 16 | #include <log-tree.h> |
17 | #include <archive.h> | 17 | #include <archive.h> |
18 | #include <xdiff/xdiff.h> | 18 | #include <xdiff/xdiff.h> |
19 | #include <utf8.h> | 19 | #include <utf8.h> |
20 | 20 | ||
21 | 21 | ||
22 | /* | 22 | /* |
23 | * Dateformats used on misc. pages | 23 | * Dateformats used on misc. pages |
24 | */ | 24 | */ |
25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" | 25 | #define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" |
26 | #define FMT_SHORTDATE "%Y-%m-%d" | 26 | #define FMT_SHORTDATE "%Y-%m-%d" |
27 | 27 | ||
28 | 28 | ||
29 | /* | 29 | /* |
30 | * Limits used for relative dates | 30 | * Limits used for relative dates |
31 | */ | 31 | */ |
32 | #define TM_MIN 60 | 32 | #define TM_MIN 60 |
33 | #define TM_HOUR (TM_MIN * 60) | 33 | #define TM_HOUR (TM_MIN * 60) |
34 | #define TM_DAY (TM_HOUR * 24) | 34 | #define TM_DAY (TM_HOUR * 24) |
35 | #define TM_WEEK (TM_DAY * 7) | 35 | #define TM_WEEK (TM_DAY * 7) |
36 | #define TM_YEAR (TM_DAY * 365) | 36 | #define TM_YEAR (TM_DAY * 365) |
37 | #define TM_MONTH (TM_YEAR / 12.0) | 37 | #define TM_MONTH (TM_YEAR / 12.0) |
38 | 38 | ||
39 | 39 | ||
40 | /* | 40 | /* |
41 | * Default encoding | 41 | * Default encoding |
42 | */ | 42 | */ |
43 | #define PAGE_ENCODING "UTF-8" | 43 | #define PAGE_ENCODING "UTF-8" |
44 | 44 | ||
45 | typedef void (*configfn)(const char *name, const char *value); | 45 | typedef void (*configfn)(const char *name, const char *value); |
46 | typedef void (*filepair_fn)(struct diff_filepair *pair); | 46 | typedef void (*filepair_fn)(struct diff_filepair *pair); |
47 | typedef void (*linediff_fn)(char *line, int len); | 47 | typedef void (*linediff_fn)(char *line, int len); |
48 | 48 | ||
49 | struct cacheitem { | 49 | struct cacheitem { |
50 | char *name; | 50 | char *name; |
51 | struct stat st; | 51 | struct stat st; |
52 | int ttl; | 52 | int ttl; |
53 | int fd; | 53 | int fd; |
54 | }; | 54 | }; |
55 | 55 | ||
56 | struct cgit_repo { | 56 | struct cgit_repo { |
57 | char *url; | 57 | char *url; |
58 | char *name; | 58 | char *name; |
59 | char *path; | 59 | char *path; |
60 | char *desc; | 60 | char *desc; |
61 | char *owner; | 61 | char *owner; |
62 | char *defbranch; | 62 | char *defbranch; |
63 | char *group; | 63 | char *group; |
64 | char *module_link; | 64 | char *module_link; |
65 | char *readme; | 65 | char *readme; |
66 | char *clone_url; | 66 | char *clone_url; |
67 | int snapshots; | 67 | int snapshots; |
68 | int enable_log_filecount; | 68 | int enable_log_filecount; |
69 | int enable_log_linecount; | 69 | int enable_log_linecount; |
70 | }; | 70 | }; |
71 | 71 | ||
72 | struct cgit_repolist { | 72 | struct cgit_repolist { |
73 | int length; | 73 | int length; |
74 | int count; | 74 | int count; |
75 | struct cgit_repo *repos; | 75 | struct cgit_repo *repos; |
76 | }; | 76 | }; |
77 | 77 | ||
78 | struct commitinfo { | 78 | struct commitinfo { |
79 | struct commit *commit; | 79 | struct commit *commit; |
80 | char *author; | 80 | char *author; |
81 | char *author_email; | 81 | char *author_email; |
82 | unsigned long author_date; | 82 | unsigned long author_date; |
83 | char *committer; | 83 | char *committer; |
84 | char *committer_email; | 84 | char *committer_email; |
85 | unsigned long committer_date; | 85 | unsigned long committer_date; |
86 | char *subject; | 86 | char *subject; |
87 | char *msg; | 87 | char *msg; |
88 | char *msg_encoding; | 88 | char *msg_encoding; |
89 | }; | 89 | }; |
90 | 90 | ||
91 | struct taginfo { | 91 | struct taginfo { |
92 | char *tagger; | 92 | char *tagger; |
93 | char *tagger_email; | 93 | char *tagger_email; |
94 | int tagger_date; | 94 | int tagger_date; |
95 | char *msg; | 95 | char *msg; |
96 | }; | 96 | }; |
97 | 97 | ||
98 | struct refinfo { | 98 | struct refinfo { |
99 | const char *refname; | 99 | const char *refname; |
100 | struct object *object; | 100 | struct object *object; |
101 | union { | 101 | union { |
102 | struct taginfo *tag; | 102 | struct taginfo *tag; |
103 | struct commitinfo *commit; | 103 | struct commitinfo *commit; |
104 | }; | 104 | }; |
105 | }; | 105 | }; |
106 | 106 | ||
107 | struct reflist { | 107 | struct reflist { |
108 | struct refinfo **refs; | 108 | struct refinfo **refs; |
109 | int alloc; | 109 | int alloc; |
110 | int count; | 110 | int count; |
111 | }; | 111 | }; |
112 | 112 | ||
113 | struct cgit_query { | 113 | struct cgit_query { |
114 | int has_symref; | 114 | int has_symref; |
115 | int has_sha1; | 115 | int has_sha1; |
116 | char *raw; | 116 | char *raw; |
117 | char *repo; | 117 | char *repo; |
118 | char *page; | 118 | char *page; |
119 | char *search; | 119 | char *search; |
120 | char *grep; | 120 | char *grep; |
121 | char *head; | 121 | char *head; |
122 | char *sha1; | 122 | char *sha1; |
123 | char *sha2; | 123 | char *sha2; |
124 | char *path; | 124 | char *path; |
125 | char *name; | 125 | char *name; |
126 | int ofs; | 126 | int ofs; |
127 | }; | 127 | }; |
128 | 128 | ||
129 | struct cgit_config { | 129 | struct cgit_config { |
130 | char *agefile; | 130 | char *agefile; |
131 | char *cache_root; | 131 | char *cache_root; |
132 | char *clone_prefix; | 132 | char *clone_prefix; |
133 | char *css; | 133 | char *css; |
134 | char *index_header; | 134 | char *index_header; |
135 | char *index_info; | 135 | char *index_info; |
136 | char *logo; | 136 | char *logo; |
137 | char *logo_link; | 137 | char *logo_link; |
138 | char *module_link; | 138 | char *module_link; |
139 | char *repo_group; | 139 | char *repo_group; |
140 | char *robots; | 140 | char *robots; |
141 | char *root_title; | 141 | char *root_title; |
142 | char *script_name; | 142 | char *script_name; |
143 | char *virtual_root; | 143 | char *virtual_root; |
144 | int cache_dynamic_ttl; | 144 | int cache_dynamic_ttl; |
145 | int cache_max_create_time; | 145 | int cache_max_create_time; |
146 | int cache_repo_ttl; | 146 | int cache_repo_ttl; |
147 | int cache_root_ttl; | 147 | int cache_root_ttl; |
148 | int cache_static_ttl; | 148 | int cache_static_ttl; |
149 | int enable_index_links; | 149 | int enable_index_links; |
150 | int enable_log_filecount; | 150 | int enable_log_filecount; |
151 | int enable_log_linecount; | 151 | int enable_log_linecount; |
152 | int max_commit_count; | 152 | int max_commit_count; |
153 | int max_lock_attempts; | 153 | int max_lock_attempts; |
154 | int max_msg_len; | 154 | int max_msg_len; |
155 | int max_repodesc_len; | 155 | int max_repodesc_len; |
156 | int nocache; | 156 | int nocache; |
157 | int renamelimit; | 157 | int renamelimit; |
158 | int snapshots; | 158 | int snapshots; |
159 | int summary_branches; | 159 | int summary_branches; |
160 | int summary_log; | 160 | int summary_log; |
161 | int summary_tags; | 161 | int summary_tags; |
162 | }; | 162 | }; |
163 | 163 | ||
164 | struct cgit_page { | 164 | struct cgit_page { |
165 | time_t modified; | 165 | time_t modified; |
166 | time_t expires; | 166 | time_t expires; |
167 | char *mimetype; | 167 | char *mimetype; |
168 | char *charset; | 168 | char *charset; |
169 | char *filename; | 169 | char *filename; |
170 | char *title; | 170 | char *title; |
171 | }; | 171 | }; |
172 | 172 | ||
173 | struct cgit_context { | 173 | struct cgit_context { |
174 | struct cgit_query qry; | 174 | struct cgit_query qry; |
175 | struct cgit_config cfg; | 175 | struct cgit_config cfg; |
176 | struct cgit_repo *repo; | 176 | struct cgit_repo *repo; |
177 | struct cgit_page page; | 177 | struct cgit_page page; |
178 | }; | 178 | }; |
179 | 179 | ||
180 | struct cgit_snapshot_format { | 180 | struct cgit_snapshot_format { |
181 | const char *suffix; | 181 | const char *suffix; |
182 | const char *mimetype; | 182 | const char *mimetype; |
183 | write_archive_fn_t write_func; | 183 | write_archive_fn_t write_func; |
184 | int bit; | 184 | int bit; |
185 | }; | 185 | }; |
186 | 186 | ||
187 | extern const char *cgit_version; | 187 | extern const char *cgit_version; |
188 | 188 | ||
189 | extern struct cgit_repolist cgit_repolist; | 189 | extern struct cgit_repolist cgit_repolist; |
190 | extern struct cgit_context ctx; | 190 | extern struct cgit_context ctx; |
191 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; | 191 | extern const struct cgit_snapshot_format cgit_snapshot_formats[]; |
192 | extern int cgit_cmd; | 192 | extern int cgit_cmd; |
193 | 193 | ||
194 | extern void cgit_prepare_context(struct cgit_context *ctx); | 194 | extern struct cgit_repo *cgit_add_repo(const char *url); |
195 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); | 195 | extern struct cgit_repo *cgit_get_repoinfo(const char *url); |
196 | extern void cgit_global_config_cb(const char *name, const char *value); | ||
197 | extern void cgit_repo_config_cb(const char *name, const char *value); | 196 | extern void cgit_repo_config_cb(const char *name, const char *value); |
198 | extern void cgit_querystring_cb(const char *name, const char *value); | ||
199 | 197 | ||
200 | extern int chk_zero(int result, char *msg); | 198 | extern int chk_zero(int result, char *msg); |
201 | extern int chk_positive(int result, char *msg); | 199 | extern int chk_positive(int result, char *msg); |
202 | extern int chk_non_negative(int result, char *msg); | 200 | extern int chk_non_negative(int result, char *msg); |
203 | 201 | ||
204 | extern int hextoint(char c); | 202 | extern int hextoint(char c); |
205 | extern char *trim_end(const char *str, char c); | 203 | extern char *trim_end(const char *str, char c); |
206 | extern char *strlpart(char *txt, int maxlen); | 204 | extern char *strlpart(char *txt, int maxlen); |
207 | extern char *strrpart(char *txt, int maxlen); | 205 | extern char *strrpart(char *txt, int maxlen); |
208 | 206 | ||
209 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); | 207 | extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); |
210 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, | 208 | extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, |
211 | int flags, void *cb_data); | 209 | int flags, void *cb_data); |
212 | 210 | ||
213 | extern void *cgit_free_commitinfo(struct commitinfo *info); | 211 | extern void *cgit_free_commitinfo(struct commitinfo *info); |
214 | 212 | ||
215 | extern int cgit_diff_files(const unsigned char *old_sha1, | 213 | extern int cgit_diff_files(const unsigned char *old_sha1, |
216 | const unsigned char *new_sha1, | 214 | const unsigned char *new_sha1, |
217 | linediff_fn fn); | 215 | linediff_fn fn); |
218 | 216 | ||
219 | extern void cgit_diff_tree(const unsigned char *old_sha1, | 217 | extern void cgit_diff_tree(const unsigned char *old_sha1, |
220 | const unsigned char *new_sha1, | 218 | const unsigned char *new_sha1, |
221 | filepair_fn fn, const char *prefix); | 219 | filepair_fn fn, const char *prefix); |
222 | 220 | ||
223 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); | 221 | extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); |
224 | 222 | ||
225 | extern char *fmt(const char *format,...); | 223 | extern char *fmt(const char *format,...); |
226 | 224 | ||
227 | extern int cgit_read_config(const char *filename, configfn fn); | 225 | extern int cgit_read_config(const char *filename, configfn fn); |
228 | extern int cgit_parse_query(char *txt, configfn fn); | 226 | extern int cgit_parse_query(char *txt, configfn fn); |
229 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); | 227 | extern struct commitinfo *cgit_parse_commit(struct commit *commit); |
230 | extern struct taginfo *cgit_parse_tag(struct tag *tag); | 228 | extern struct taginfo *cgit_parse_tag(struct tag *tag); |
231 | extern void cgit_parse_url(const char *url); | 229 | extern void cgit_parse_url(const char *url); |
232 | 230 | ||
233 | extern char *cache_safe_filename(const char *unsafe); | 231 | extern char *cache_safe_filename(const char *unsafe); |
234 | extern int cache_lock(struct cacheitem *item); | 232 | extern int cache_lock(struct cacheitem *item); |
235 | extern int cache_unlock(struct cacheitem *item); | 233 | extern int cache_unlock(struct cacheitem *item); |
236 | extern int cache_cancel_lock(struct cacheitem *item); | 234 | extern int cache_cancel_lock(struct cacheitem *item); |
237 | extern int cache_exist(struct cacheitem *item); | 235 | extern int cache_exist(struct cacheitem *item); |
238 | extern int cache_expired(struct cacheitem *item); | 236 | extern int cache_expired(struct cacheitem *item); |
239 | 237 | ||
240 | extern const char *cgit_repobasename(const char *reponame); | 238 | extern const char *cgit_repobasename(const char *reponame); |
241 | 239 | ||
242 | extern int cgit_parse_snapshots_mask(const char *str); | 240 | extern int cgit_parse_snapshots_mask(const char *str); |
243 | 241 | ||
244 | #endif /* CGIT_H */ | 242 | #endif /* CGIT_H */ |
@@ -1,508 +1,358 @@ | |||
1 | /* shared.c: global vars + some callback functions | 1 | /* shared.c: global vars + some callback 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 | 10 | ||
11 | struct cgit_repolist cgit_repolist; | 11 | struct cgit_repolist cgit_repolist; |
12 | struct cgit_context ctx; | 12 | struct cgit_context ctx; |
13 | int cgit_cmd; | 13 | int cgit_cmd; |
14 | 14 | ||
15 | const char *cgit_version = CGIT_VERSION; | 15 | const char *cgit_version = CGIT_VERSION; |
16 | 16 | ||
17 | void cgit_prepare_context(struct cgit_context *ctx) | ||
18 | { | ||
19 | memset(ctx, 0, sizeof(ctx)); | ||
20 | ctx->cfg.agefile = "info/web/last-modified"; | ||
21 | ctx->cfg.cache_dynamic_ttl = 5; | ||
22 | ctx->cfg.cache_max_create_time = 5; | ||
23 | ctx->cfg.cache_repo_ttl = 5; | ||
24 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
25 | ctx->cfg.cache_root_ttl = 5; | ||
26 | ctx->cfg.cache_static_ttl = -1; | ||
27 | ctx->cfg.css = "/cgit.css"; | ||
28 | ctx->cfg.logo = "/git-logo.png"; | ||
29 | ctx->cfg.max_commit_count = 50; | ||
30 | ctx->cfg.max_lock_attempts = 5; | ||
31 | ctx->cfg.max_msg_len = 60; | ||
32 | ctx->cfg.max_repodesc_len = 60; | ||
33 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
34 | ctx->cfg.renamelimit = -1; | ||
35 | ctx->cfg.robots = "index, nofollow"; | ||
36 | ctx->cfg.root_title = "Git repository browser"; | ||
37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
38 | ctx->page.mimetype = "text/html"; | ||
39 | ctx->page.charset = PAGE_ENCODING; | ||
40 | ctx->page.filename = NULL; | ||
41 | } | ||
42 | |||
43 | int chk_zero(int result, char *msg) | 17 | int chk_zero(int result, char *msg) |
44 | { | 18 | { |
45 | if (result != 0) | 19 | if (result != 0) |
46 | die("%s: %s", msg, strerror(errno)); | 20 | die("%s: %s", msg, strerror(errno)); |
47 | return result; | 21 | return result; |
48 | } | 22 | } |
49 | 23 | ||
50 | int chk_positive(int result, char *msg) | 24 | int chk_positive(int result, char *msg) |
51 | { | 25 | { |
52 | if (result <= 0) | 26 | if (result <= 0) |
53 | die("%s: %s", msg, strerror(errno)); | 27 | die("%s: %s", msg, strerror(errno)); |
54 | return result; | 28 | return result; |
55 | } | 29 | } |
56 | 30 | ||
57 | int chk_non_negative(int result, char *msg) | 31 | int chk_non_negative(int result, char *msg) |
58 | { | 32 | { |
59 | if (result < 0) | 33 | if (result < 0) |
60 | die("%s: %s",msg, strerror(errno)); | 34 | die("%s: %s",msg, strerror(errno)); |
61 | return result; | 35 | return result; |
62 | } | 36 | } |
63 | 37 | ||
64 | struct cgit_repo *add_repo(const char *url) | 38 | struct cgit_repo *cgit_add_repo(const char *url) |
65 | { | 39 | { |
66 | struct cgit_repo *ret; | 40 | struct cgit_repo *ret; |
67 | 41 | ||
68 | if (++cgit_repolist.count > cgit_repolist.length) { | 42 | if (++cgit_repolist.count > cgit_repolist.length) { |
69 | if (cgit_repolist.length == 0) | 43 | if (cgit_repolist.length == 0) |
70 | cgit_repolist.length = 8; | 44 | cgit_repolist.length = 8; |
71 | else | 45 | else |
72 | cgit_repolist.length *= 2; | 46 | cgit_repolist.length *= 2; |
73 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 47 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
74 | cgit_repolist.length * | 48 | cgit_repolist.length * |
75 | sizeof(struct cgit_repo)); | 49 | sizeof(struct cgit_repo)); |
76 | } | 50 | } |
77 | 51 | ||
78 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 52 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
79 | ret->url = trim_end(url, '/'); | 53 | ret->url = trim_end(url, '/'); |
80 | ret->name = ret->url; | 54 | ret->name = ret->url; |
81 | ret->path = NULL; | 55 | ret->path = NULL; |
82 | ret->desc = "[no description]"; | 56 | ret->desc = "[no description]"; |
83 | ret->owner = NULL; | 57 | ret->owner = NULL; |
84 | ret->group = ctx.cfg.repo_group; | 58 | ret->group = ctx.cfg.repo_group; |
85 | ret->defbranch = "master"; | 59 | ret->defbranch = "master"; |
86 | ret->snapshots = ctx.cfg.snapshots; | 60 | ret->snapshots = ctx.cfg.snapshots; |
87 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; | 61 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
88 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; | 62 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
89 | ret->module_link = ctx.cfg.module_link; | 63 | ret->module_link = ctx.cfg.module_link; |
90 | ret->readme = NULL; | 64 | ret->readme = NULL; |
91 | return ret; | 65 | return ret; |
92 | } | 66 | } |
93 | 67 | ||
94 | struct cgit_repo *cgit_get_repoinfo(const char *url) | 68 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
95 | { | 69 | { |
96 | int i; | 70 | int i; |
97 | struct cgit_repo *repo; | 71 | struct cgit_repo *repo; |
98 | 72 | ||
99 | for (i=0; i<cgit_repolist.count; i++) { | 73 | for (i=0; i<cgit_repolist.count; i++) { |
100 | repo = &cgit_repolist.repos[i]; | 74 | repo = &cgit_repolist.repos[i]; |
101 | if (!strcmp(repo->url, url)) | 75 | if (!strcmp(repo->url, url)) |
102 | return repo; | 76 | return repo; |
103 | } | 77 | } |
104 | return NULL; | 78 | return NULL; |
105 | } | 79 | } |
106 | 80 | ||
107 | void cgit_global_config_cb(const char *name, const char *value) | ||
108 | { | ||
109 | if (!strcmp(name, "root-title")) | ||
110 | ctx.cfg.root_title = xstrdup(value); | ||
111 | else if (!strcmp(name, "css")) | ||
112 | ctx.cfg.css = xstrdup(value); | ||
113 | else if (!strcmp(name, "logo")) | ||
114 | ctx.cfg.logo = xstrdup(value); | ||
115 | else if (!strcmp(name, "index-header")) | ||
116 | ctx.cfg.index_header = xstrdup(value); | ||
117 | else if (!strcmp(name, "index-info")) | ||
118 | ctx.cfg.index_info = xstrdup(value); | ||
119 | else if (!strcmp(name, "logo-link")) | ||
120 | ctx.cfg.logo_link = xstrdup(value); | ||
121 | else if (!strcmp(name, "module-link")) | ||
122 | ctx.cfg.module_link = xstrdup(value); | ||
123 | else if (!strcmp(name, "virtual-root")) { | ||
124 | ctx.cfg.virtual_root = trim_end(value, '/'); | ||
125 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | ||
126 | ctx.cfg.virtual_root = ""; | ||
127 | } else if (!strcmp(name, "nocache")) | ||
128 | ctx.cfg.nocache = atoi(value); | ||
129 | else if (!strcmp(name, "snapshots")) | ||
130 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | ||
131 | else if (!strcmp(name, "enable-index-links")) | ||
132 | ctx.cfg.enable_index_links = atoi(value); | ||
133 | else if (!strcmp(name, "enable-log-filecount")) | ||
134 | ctx.cfg.enable_log_filecount = atoi(value); | ||
135 | else if (!strcmp(name, "enable-log-linecount")) | ||
136 | ctx.cfg.enable_log_linecount = atoi(value); | ||
137 | else if (!strcmp(name, "cache-root")) | ||
138 | ctx.cfg.cache_root = xstrdup(value); | ||
139 | else if (!strcmp(name, "cache-root-ttl")) | ||
140 | ctx.cfg.cache_root_ttl = atoi(value); | ||
141 | else if (!strcmp(name, "cache-repo-ttl")) | ||
142 | ctx.cfg.cache_repo_ttl = atoi(value); | ||
143 | else if (!strcmp(name, "cache-static-ttl")) | ||
144 | ctx.cfg.cache_static_ttl = atoi(value); | ||
145 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
146 | ctx.cfg.cache_dynamic_ttl = atoi(value); | ||
147 | else if (!strcmp(name, "max-message-length")) | ||
148 | ctx.cfg.max_msg_len = atoi(value); | ||
149 | else if (!strcmp(name, "max-repodesc-length")) | ||
150 | ctx.cfg.max_repodesc_len = atoi(value); | ||
151 | else if (!strcmp(name, "max-commit-count")) | ||
152 | ctx.cfg.max_commit_count = atoi(value); | ||
153 | else if (!strcmp(name, "summary-log")) | ||
154 | ctx.cfg.summary_log = atoi(value); | ||
155 | else if (!strcmp(name, "summary-branches")) | ||
156 | ctx.cfg.summary_branches = atoi(value); | ||
157 | else if (!strcmp(name, "summary-tags")) | ||
158 | ctx.cfg.summary_tags = atoi(value); | ||
159 | else if (!strcmp(name, "agefile")) | ||
160 | ctx.cfg.agefile = xstrdup(value); | ||
161 | else if (!strcmp(name, "renamelimit")) | ||
162 | ctx.cfg.renamelimit = atoi(value); | ||
163 | else if (!strcmp(name, "robots")) | ||
164 | ctx.cfg.robots = xstrdup(value); | ||
165 | else if (!strcmp(name, "clone-prefix")) | ||
166 | ctx.cfg.clone_prefix = xstrdup(value); | ||
167 | else if (!strcmp(name, "repo.group")) | ||
168 | ctx.cfg.repo_group = xstrdup(value); | ||
169 | else if (!strcmp(name, "repo.url")) | ||
170 | ctx.repo = add_repo(value); | ||
171 | else if (!strcmp(name, "repo.name")) | ||
172 | ctx.repo->name = xstrdup(value); | ||
173 | else if (ctx.repo && !strcmp(name, "repo.path")) | ||
174 | ctx.repo->path = trim_end(value, '/'); | ||
175 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | ||
176 | ctx.repo->clone_url = xstrdup(value); | ||
177 | else if (ctx.repo && !strcmp(name, "repo.desc")) | ||
178 | ctx.repo->desc = xstrdup(value); | ||
179 | else if (ctx.repo && !strcmp(name, "repo.owner")) | ||
180 | ctx.repo->owner = xstrdup(value); | ||
181 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | ||
182 | ctx.repo->defbranch = xstrdup(value); | ||
183 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | ||
184 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
185 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | ||
186 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | ||
187 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | ||
188 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | ||
189 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | ||
190 | ctx.repo->module_link= xstrdup(value); | ||
191 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
192 | if (*value == '/') | ||
193 | ctx.repo->readme = xstrdup(value); | ||
194 | else | ||
195 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | ||
196 | } else if (!strcmp(name, "include")) | ||
197 | cgit_read_config(value, cgit_global_config_cb); | ||
198 | } | ||
199 | |||
200 | void cgit_querystring_cb(const char *name, const char *value) | ||
201 | { | ||
202 | if (!strcmp(name,"r")) { | ||
203 | ctx.qry.repo = xstrdup(value); | ||
204 | ctx.repo = cgit_get_repoinfo(value); | ||
205 | } else if (!strcmp(name, "p")) { | ||
206 | ctx.qry.page = xstrdup(value); | ||
207 | } else if (!strcmp(name, "url")) { | ||
208 | cgit_parse_url(value); | ||
209 | } else if (!strcmp(name, "qt")) { | ||
210 | ctx.qry.grep = xstrdup(value); | ||
211 | } else if (!strcmp(name, "q")) { | ||
212 | ctx.qry.search = xstrdup(value); | ||
213 | } else if (!strcmp(name, "h")) { | ||
214 | ctx.qry.head = xstrdup(value); | ||
215 | ctx.qry.has_symref = 1; | ||
216 | } else if (!strcmp(name, "id")) { | ||
217 | ctx.qry.sha1 = xstrdup(value); | ||
218 | ctx.qry.has_sha1 = 1; | ||
219 | } else if (!strcmp(name, "id2")) { | ||
220 | ctx.qry.sha2 = xstrdup(value); | ||
221 | ctx.qry.has_sha1 = 1; | ||
222 | } else if (!strcmp(name, "ofs")) { | ||
223 | ctx.qry.ofs = atoi(value); | ||
224 | } else if (!strcmp(name, "path")) { | ||
225 | ctx.qry.path = trim_end(value, '/'); | ||
226 | } else if (!strcmp(name, "name")) { | ||
227 | ctx.qry.name = xstrdup(value); | ||
228 | } | ||
229 | } | ||
230 | |||
231 | void *cgit_free_commitinfo(struct commitinfo *info) | 81 | void *cgit_free_commitinfo(struct commitinfo *info) |
232 | { | 82 | { |
233 | free(info->author); | 83 | free(info->author); |
234 | free(info->author_email); | 84 | free(info->author_email); |
235 | free(info->committer); | 85 | free(info->committer); |
236 | free(info->committer_email); | 86 | free(info->committer_email); |
237 | free(info->subject); | 87 | free(info->subject); |
238 | free(info->msg); | 88 | free(info->msg); |
239 | free(info->msg_encoding); | 89 | free(info->msg_encoding); |
240 | free(info); | 90 | free(info); |
241 | return NULL; | 91 | return NULL; |
242 | } | 92 | } |
243 | 93 | ||
244 | int hextoint(char c) | 94 | int hextoint(char c) |
245 | { | 95 | { |
246 | if (c >= 'a' && c <= 'f') | 96 | if (c >= 'a' && c <= 'f') |
247 | return 10 + c - 'a'; | 97 | return 10 + c - 'a'; |
248 | else if (c >= 'A' && c <= 'F') | 98 | else if (c >= 'A' && c <= 'F') |
249 | return 10 + c - 'A'; | 99 | return 10 + c - 'A'; |
250 | else if (c >= '0' && c <= '9') | 100 | else if (c >= '0' && c <= '9') |
251 | return c - '0'; | 101 | return c - '0'; |
252 | else | 102 | else |
253 | return -1; | 103 | return -1; |
254 | } | 104 | } |
255 | 105 | ||
256 | char *trim_end(const char *str, char c) | 106 | char *trim_end(const char *str, char c) |
257 | { | 107 | { |
258 | int len; | 108 | int len; |
259 | char *s, *t; | 109 | char *s, *t; |
260 | 110 | ||
261 | if (str == NULL) | 111 | if (str == NULL) |
262 | return NULL; | 112 | return NULL; |
263 | t = (char *)str; | 113 | t = (char *)str; |
264 | len = strlen(t); | 114 | len = strlen(t); |
265 | while(len > 0 && t[len - 1] == c) | 115 | while(len > 0 && t[len - 1] == c) |
266 | len--; | 116 | len--; |
267 | 117 | ||
268 | if (len == 0) | 118 | if (len == 0) |
269 | return NULL; | 119 | return NULL; |
270 | 120 | ||
271 | c = t[len]; | 121 | c = t[len]; |
272 | t[len] = '\0'; | 122 | t[len] = '\0'; |
273 | s = xstrdup(t); | 123 | s = xstrdup(t); |
274 | t[len] = c; | 124 | t[len] = c; |
275 | return s; | 125 | return s; |
276 | } | 126 | } |
277 | 127 | ||
278 | char *strlpart(char *txt, int maxlen) | 128 | char *strlpart(char *txt, int maxlen) |
279 | { | 129 | { |
280 | char *result; | 130 | char *result; |
281 | 131 | ||
282 | if (!txt) | 132 | if (!txt) |
283 | return txt; | 133 | return txt; |
284 | 134 | ||
285 | if (strlen(txt) <= maxlen) | 135 | if (strlen(txt) <= maxlen) |
286 | return txt; | 136 | return txt; |
287 | result = xmalloc(maxlen + 1); | 137 | result = xmalloc(maxlen + 1); |
288 | memcpy(result, txt, maxlen - 3); | 138 | memcpy(result, txt, maxlen - 3); |
289 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; | 139 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
290 | result[maxlen] = '\0'; | 140 | result[maxlen] = '\0'; |
291 | return result; | 141 | return result; |
292 | } | 142 | } |
293 | 143 | ||
294 | char *strrpart(char *txt, int maxlen) | 144 | char *strrpart(char *txt, int maxlen) |
295 | { | 145 | { |
296 | char *result; | 146 | char *result; |
297 | 147 | ||
298 | if (!txt) | 148 | if (!txt) |
299 | return txt; | 149 | return txt; |
300 | 150 | ||
301 | if (strlen(txt) <= maxlen) | 151 | if (strlen(txt) <= maxlen) |
302 | return txt; | 152 | return txt; |
303 | result = xmalloc(maxlen + 1); | 153 | result = xmalloc(maxlen + 1); |
304 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); | 154 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
305 | result[0] = result[1] = result[2] = '.'; | 155 | result[0] = result[1] = result[2] = '.'; |
306 | return result; | 156 | return result; |
307 | } | 157 | } |
308 | 158 | ||
309 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) | 159 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
310 | { | 160 | { |
311 | size_t size; | 161 | size_t size; |
312 | 162 | ||
313 | if (list->count >= list->alloc) { | 163 | if (list->count >= list->alloc) { |
314 | list->alloc += (list->alloc ? list->alloc : 4); | 164 | list->alloc += (list->alloc ? list->alloc : 4); |
315 | size = list->alloc * sizeof(struct refinfo *); | 165 | size = list->alloc * sizeof(struct refinfo *); |
316 | list->refs = xrealloc(list->refs, size); | 166 | list->refs = xrealloc(list->refs, size); |
317 | } | 167 | } |
318 | list->refs[list->count++] = ref; | 168 | list->refs[list->count++] = ref; |
319 | } | 169 | } |
320 | 170 | ||
321 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) | 171 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
322 | { | 172 | { |
323 | struct refinfo *ref; | 173 | struct refinfo *ref; |
324 | 174 | ||
325 | ref = xmalloc(sizeof (struct refinfo)); | 175 | ref = xmalloc(sizeof (struct refinfo)); |
326 | ref->refname = xstrdup(refname); | 176 | ref->refname = xstrdup(refname); |
327 | ref->object = parse_object(sha1); | 177 | ref->object = parse_object(sha1); |
328 | switch (ref->object->type) { | 178 | switch (ref->object->type) { |
329 | case OBJ_TAG: | 179 | case OBJ_TAG: |
330 | ref->tag = cgit_parse_tag((struct tag *)ref->object); | 180 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
331 | break; | 181 | break; |
332 | case OBJ_COMMIT: | 182 | case OBJ_COMMIT: |
333 | ref->commit = cgit_parse_commit((struct commit *)ref->object); | 183 | ref->commit = cgit_parse_commit((struct commit *)ref->object); |
334 | break; | 184 | break; |
335 | } | 185 | } |
336 | return ref; | 186 | return ref; |
337 | } | 187 | } |
338 | 188 | ||
339 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, | 189 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, |
340 | void *cb_data) | 190 | void *cb_data) |
341 | { | 191 | { |
342 | struct reflist *list = (struct reflist *)cb_data; | 192 | struct reflist *list = (struct reflist *)cb_data; |
343 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); | 193 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); |
344 | 194 | ||
345 | if (info) | 195 | if (info) |
346 | cgit_add_ref(list, info); | 196 | cgit_add_ref(list, info); |
347 | return 0; | 197 | return 0; |
348 | } | 198 | } |
349 | 199 | ||
350 | void cgit_diff_tree_cb(struct diff_queue_struct *q, | 200 | void cgit_diff_tree_cb(struct diff_queue_struct *q, |
351 | struct diff_options *options, void *data) | 201 | struct diff_options *options, void *data) |
352 | { | 202 | { |
353 | int i; | 203 | int i; |
354 | 204 | ||
355 | for (i = 0; i < q->nr; i++) { | 205 | for (i = 0; i < q->nr; i++) { |
356 | if (q->queue[i]->status == 'U') | 206 | if (q->queue[i]->status == 'U') |
357 | continue; | 207 | continue; |
358 | ((filepair_fn)data)(q->queue[i]); | 208 | ((filepair_fn)data)(q->queue[i]); |
359 | } | 209 | } |
360 | } | 210 | } |
361 | 211 | ||
362 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) | 212 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) |
363 | { | 213 | { |
364 | enum object_type type; | 214 | enum object_type type; |
365 | 215 | ||
366 | if (is_null_sha1(sha1)) { | 216 | if (is_null_sha1(sha1)) { |
367 | file->ptr = (char *)""; | 217 | file->ptr = (char *)""; |
368 | file->size = 0; | 218 | file->size = 0; |
369 | } else { | 219 | } else { |
370 | file->ptr = read_sha1_file(sha1, &type, | 220 | file->ptr = read_sha1_file(sha1, &type, |
371 | (unsigned long *)&file->size); | 221 | (unsigned long *)&file->size); |
372 | } | 222 | } |
373 | return 1; | 223 | return 1; |
374 | } | 224 | } |
375 | 225 | ||
376 | /* | 226 | /* |
377 | * Receive diff-buffers from xdiff and concatenate them as | 227 | * Receive diff-buffers from xdiff and concatenate them as |
378 | * needed across multiple callbacks. | 228 | * needed across multiple callbacks. |
379 | * | 229 | * |
380 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), | 230 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), |
381 | * ripped from git and modified to use globals instead of | 231 | * ripped from git and modified to use globals instead of |
382 | * a special callback-struct. | 232 | * a special callback-struct. |
383 | */ | 233 | */ |
384 | char *diffbuf = NULL; | 234 | char *diffbuf = NULL; |
385 | int buflen = 0; | 235 | int buflen = 0; |
386 | 236 | ||
387 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) | 237 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) |
388 | { | 238 | { |
389 | int i; | 239 | int i; |
390 | 240 | ||
391 | for (i = 0; i < nbuf; i++) { | 241 | for (i = 0; i < nbuf; i++) { |
392 | if (mb[i].ptr[mb[i].size-1] != '\n') { | 242 | if (mb[i].ptr[mb[i].size-1] != '\n') { |
393 | /* Incomplete line */ | 243 | /* Incomplete line */ |
394 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 244 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
395 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 245 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
396 | buflen += mb[i].size; | 246 | buflen += mb[i].size; |
397 | continue; | 247 | continue; |
398 | } | 248 | } |
399 | 249 | ||
400 | /* we have a complete line */ | 250 | /* we have a complete line */ |
401 | if (!diffbuf) { | 251 | if (!diffbuf) { |
402 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); | 252 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); |
403 | continue; | 253 | continue; |
404 | } | 254 | } |
405 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 255 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
406 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 256 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
407 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); | 257 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); |
408 | free(diffbuf); | 258 | free(diffbuf); |
409 | diffbuf = NULL; | 259 | diffbuf = NULL; |
410 | buflen = 0; | 260 | buflen = 0; |
411 | } | 261 | } |
412 | if (diffbuf) { | 262 | if (diffbuf) { |
413 | ((linediff_fn)priv)(diffbuf, buflen); | 263 | ((linediff_fn)priv)(diffbuf, buflen); |
414 | free(diffbuf); | 264 | free(diffbuf); |
415 | diffbuf = NULL; | 265 | diffbuf = NULL; |
416 | buflen = 0; | 266 | buflen = 0; |
417 | } | 267 | } |
418 | return 0; | 268 | return 0; |
419 | } | 269 | } |
420 | 270 | ||
421 | int cgit_diff_files(const unsigned char *old_sha1, | 271 | int cgit_diff_files(const unsigned char *old_sha1, |
422 | const unsigned char *new_sha1, | 272 | const unsigned char *new_sha1, |
423 | linediff_fn fn) | 273 | linediff_fn fn) |
424 | { | 274 | { |
425 | mmfile_t file1, file2; | 275 | mmfile_t file1, file2; |
426 | xpparam_t diff_params; | 276 | xpparam_t diff_params; |
427 | xdemitconf_t emit_params; | 277 | xdemitconf_t emit_params; |
428 | xdemitcb_t emit_cb; | 278 | xdemitcb_t emit_cb; |
429 | 279 | ||
430 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) | 280 | if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) |
431 | return 1; | 281 | return 1; |
432 | 282 | ||
433 | diff_params.flags = XDF_NEED_MINIMAL; | 283 | diff_params.flags = XDF_NEED_MINIMAL; |
434 | emit_params.ctxlen = 3; | 284 | emit_params.ctxlen = 3; |
435 | emit_params.flags = XDL_EMIT_FUNCNAMES; | 285 | emit_params.flags = XDL_EMIT_FUNCNAMES; |
436 | emit_params.find_func = NULL; | 286 | emit_params.find_func = NULL; |
437 | emit_cb.outf = filediff_cb; | 287 | emit_cb.outf = filediff_cb; |
438 | emit_cb.priv = fn; | 288 | emit_cb.priv = fn; |
439 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); | 289 | xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); |
440 | return 0; | 290 | return 0; |
441 | } | 291 | } |
442 | 292 | ||
443 | void cgit_diff_tree(const unsigned char *old_sha1, | 293 | void cgit_diff_tree(const unsigned char *old_sha1, |
444 | const unsigned char *new_sha1, | 294 | const unsigned char *new_sha1, |
445 | filepair_fn fn, const char *prefix) | 295 | filepair_fn fn, const char *prefix) |
446 | { | 296 | { |
447 | struct diff_options opt; | 297 | struct diff_options opt; |
448 | int ret; | 298 | int ret; |
449 | int prefixlen; | 299 | int prefixlen; |
450 | 300 | ||
451 | diff_setup(&opt); | 301 | diff_setup(&opt); |
452 | opt.output_format = DIFF_FORMAT_CALLBACK; | 302 | opt.output_format = DIFF_FORMAT_CALLBACK; |
453 | opt.detect_rename = 1; | 303 | opt.detect_rename = 1; |
454 | opt.rename_limit = ctx.cfg.renamelimit; | 304 | opt.rename_limit = ctx.cfg.renamelimit; |
455 | DIFF_OPT_SET(&opt, RECURSIVE); | 305 | DIFF_OPT_SET(&opt, RECURSIVE); |
456 | opt.format_callback = cgit_diff_tree_cb; | 306 | opt.format_callback = cgit_diff_tree_cb; |
457 | opt.format_callback_data = fn; | 307 | opt.format_callback_data = fn; |
458 | if (prefix) { | 308 | if (prefix) { |
459 | opt.nr_paths = 1; | 309 | opt.nr_paths = 1; |
460 | opt.paths = &prefix; | 310 | opt.paths = &prefix; |
461 | prefixlen = strlen(prefix); | 311 | prefixlen = strlen(prefix); |
462 | opt.pathlens = &prefixlen; | 312 | opt.pathlens = &prefixlen; |
463 | } | 313 | } |
464 | diff_setup_done(&opt); | 314 | diff_setup_done(&opt); |
465 | 315 | ||
466 | if (old_sha1 && !is_null_sha1(old_sha1)) | 316 | if (old_sha1 && !is_null_sha1(old_sha1)) |
467 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); | 317 | ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); |
468 | else | 318 | else |
469 | ret = diff_root_tree_sha1(new_sha1, "", &opt); | 319 | ret = diff_root_tree_sha1(new_sha1, "", &opt); |
470 | diffcore_std(&opt); | 320 | diffcore_std(&opt); |
471 | diff_flush(&opt); | 321 | diff_flush(&opt); |
472 | } | 322 | } |
473 | 323 | ||
474 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) | 324 | void cgit_diff_commit(struct commit *commit, filepair_fn fn) |
475 | { | 325 | { |
476 | unsigned char *old_sha1 = NULL; | 326 | unsigned char *old_sha1 = NULL; |
477 | 327 | ||
478 | if (commit->parents) | 328 | if (commit->parents) |
479 | old_sha1 = commit->parents->item->object.sha1; | 329 | old_sha1 = commit->parents->item->object.sha1; |
480 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); | 330 | cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); |
481 | } | 331 | } |
482 | 332 | ||
483 | int cgit_parse_snapshots_mask(const char *str) | 333 | int cgit_parse_snapshots_mask(const char *str) |
484 | { | 334 | { |
485 | const struct cgit_snapshot_format *f; | 335 | const struct cgit_snapshot_format *f; |
486 | static const char *delim = " \t,:/|;"; | 336 | static const char *delim = " \t,:/|;"; |
487 | int tl, sl, rv = 0; | 337 | int tl, sl, rv = 0; |
488 | 338 | ||
489 | /* favor legacy setting */ | 339 | /* favor legacy setting */ |
490 | if(atoi(str)) | 340 | if(atoi(str)) |
491 | return 1; | 341 | return 1; |
492 | for(;;) { | 342 | for(;;) { |
493 | str += strspn(str,delim); | 343 | str += strspn(str,delim); |
494 | tl = strcspn(str,delim); | 344 | tl = strcspn(str,delim); |
495 | if (!tl) | 345 | if (!tl) |
496 | break; | 346 | break; |
497 | for (f = cgit_snapshot_formats; f->suffix; f++) { | 347 | for (f = cgit_snapshot_formats; f->suffix; f++) { |
498 | sl = strlen(f->suffix); | 348 | sl = strlen(f->suffix); |
499 | if((tl == sl && !strncmp(f->suffix, str, tl)) || | 349 | if((tl == sl && !strncmp(f->suffix, str, tl)) || |
500 | (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { | 350 | (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { |
501 | rv |= f->bit; | 351 | rv |= f->bit; |
502 | break; | 352 | break; |
503 | } | 353 | } |
504 | } | 354 | } |
505 | str += tl; | 355 | str += tl; |
506 | } | 356 | } |
507 | return rv; | 357 | return rv; |
508 | } | 358 | } |