|
diff --git a/shared.c b/shared.c index 539d533..67eb67b 100644 --- a/ shared.c+++ b/ shared.c |
|
@@ -1,118 +1,106 @@ |
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) |
17 | void cgit_prepare_context(struct cgit_context *ctx) |
18 | { |
18 | { |
19 | memset(ctx, 0, sizeof(ctx)); |
19 | memset(ctx, 0, sizeof(ctx)); |
20 | ctx->cfg.agefile = "info/web/last-modified"; |
20 | ctx->cfg.agefile = "info/web/last-modified"; |
21 | ctx->cfg.cache_dynamic_ttl = 5; |
21 | ctx->cfg.cache_dynamic_ttl = 5; |
22 | ctx->cfg.cache_max_create_time = 5; |
22 | ctx->cfg.cache_max_create_time = 5; |
23 | ctx->cfg.cache_repo_ttl = 5; |
23 | ctx->cfg.cache_repo_ttl = 5; |
24 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
24 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; |
25 | ctx->cfg.cache_root_ttl = 5; |
25 | ctx->cfg.cache_root_ttl = 5; |
26 | ctx->cfg.cache_static_ttl = -1; |
26 | ctx->cfg.cache_static_ttl = -1; |
27 | ctx->cfg.css = "/cgit.css"; |
27 | ctx->cfg.css = "/cgit.css"; |
28 | ctx->cfg.logo = "/git-logo.png"; |
28 | ctx->cfg.logo = "/git-logo.png"; |
29 | ctx->cfg.max_commit_count = 50; |
29 | ctx->cfg.max_commit_count = 50; |
30 | ctx->cfg.max_lock_attempts = 5; |
30 | ctx->cfg.max_lock_attempts = 5; |
31 | ctx->cfg.max_msg_len = 60; |
31 | ctx->cfg.max_msg_len = 60; |
32 | ctx->cfg.max_repodesc_len = 60; |
32 | ctx->cfg.max_repodesc_len = 60; |
33 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
33 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; |
34 | ctx->cfg.renamelimit = -1; |
34 | ctx->cfg.renamelimit = -1; |
35 | ctx->cfg.robots = "index, nofollow"; |
35 | ctx->cfg.robots = "index, nofollow"; |
36 | ctx->cfg.root_title = "Git repository browser"; |
36 | ctx->cfg.root_title = "Git repository browser"; |
37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; |
38 | ctx->page.mimetype = "text/html"; |
38 | ctx->page.mimetype = "text/html"; |
39 | ctx->page.charset = PAGE_ENCODING; |
39 | ctx->page.charset = PAGE_ENCODING; |
40 | ctx->page.filename = NULL; |
40 | ctx->page.filename = NULL; |
41 | } |
41 | } |
42 | |
42 | |
43 | int cgit_get_cmd_index(const char *cmd) |
| |
44 | { |
| |
45 | static char *cmds[] = {"log", "commit", "diff", "tree", "blob", |
| |
46 | "snapshot", "tag", "refs", "patch", NULL}; |
| |
47 | int i; |
| |
48 | |
| |
49 | for(i = 0; cmds[i]; i++) |
| |
50 | if (!strcmp(cmd, cmds[i])) |
| |
51 | return i + 1; |
| |
52 | return 0; |
| |
53 | } |
| |
54 | |
| |
55 | int chk_zero(int result, char *msg) |
43 | int chk_zero(int result, char *msg) |
56 | { |
44 | { |
57 | if (result != 0) |
45 | if (result != 0) |
58 | die("%s: %s", msg, strerror(errno)); |
46 | die("%s: %s", msg, strerror(errno)); |
59 | return result; |
47 | return result; |
60 | } |
48 | } |
61 | |
49 | |
62 | int chk_positive(int result, char *msg) |
50 | int chk_positive(int result, char *msg) |
63 | { |
51 | { |
64 | if (result <= 0) |
52 | if (result <= 0) |
65 | die("%s: %s", msg, strerror(errno)); |
53 | die("%s: %s", msg, strerror(errno)); |
66 | return result; |
54 | return result; |
67 | } |
55 | } |
68 | |
56 | |
69 | int chk_non_negative(int result, char *msg) |
57 | int chk_non_negative(int result, char *msg) |
70 | { |
58 | { |
71 | if (result < 0) |
59 | if (result < 0) |
72 | die("%s: %s",msg, strerror(errno)); |
60 | die("%s: %s",msg, strerror(errno)); |
73 | return result; |
61 | return result; |
74 | } |
62 | } |
75 | |
63 | |
76 | struct cgit_repo *add_repo(const char *url) |
64 | struct cgit_repo *add_repo(const char *url) |
77 | { |
65 | { |
78 | struct cgit_repo *ret; |
66 | struct cgit_repo *ret; |
79 | |
67 | |
80 | if (++cgit_repolist.count > cgit_repolist.length) { |
68 | if (++cgit_repolist.count > cgit_repolist.length) { |
81 | if (cgit_repolist.length == 0) |
69 | if (cgit_repolist.length == 0) |
82 | cgit_repolist.length = 8; |
70 | cgit_repolist.length = 8; |
83 | else |
71 | else |
84 | cgit_repolist.length *= 2; |
72 | cgit_repolist.length *= 2; |
85 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
73 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
86 | cgit_repolist.length * |
74 | cgit_repolist.length * |
87 | sizeof(struct cgit_repo)); |
75 | sizeof(struct cgit_repo)); |
88 | } |
76 | } |
89 | |
77 | |
90 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
78 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
91 | ret->url = trim_end(url, '/'); |
79 | ret->url = trim_end(url, '/'); |
92 | ret->name = ret->url; |
80 | ret->name = ret->url; |
93 | ret->path = NULL; |
81 | ret->path = NULL; |
94 | ret->desc = "[no description]"; |
82 | ret->desc = "[no description]"; |
95 | ret->owner = NULL; |
83 | ret->owner = NULL; |
96 | ret->group = ctx.cfg.repo_group; |
84 | ret->group = ctx.cfg.repo_group; |
97 | ret->defbranch = "master"; |
85 | ret->defbranch = "master"; |
98 | ret->snapshots = ctx.cfg.snapshots; |
86 | ret->snapshots = ctx.cfg.snapshots; |
99 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
87 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
100 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
88 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
101 | ret->module_link = ctx.cfg.module_link; |
89 | ret->module_link = ctx.cfg.module_link; |
102 | ret->readme = NULL; |
90 | ret->readme = NULL; |
103 | return ret; |
91 | return ret; |
104 | } |
92 | } |
105 | |
93 | |
106 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
94 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
107 | { |
95 | { |
108 | int i; |
96 | int i; |
109 | struct cgit_repo *repo; |
97 | struct cgit_repo *repo; |
110 | |
98 | |
111 | for (i=0; i<cgit_repolist.count; i++) { |
99 | for (i=0; i<cgit_repolist.count; i++) { |
112 | repo = &cgit_repolist.repos[i]; |
100 | repo = &cgit_repolist.repos[i]; |
113 | if (!strcmp(repo->url, url)) |
101 | if (!strcmp(repo->url, url)) |
114 | return repo; |
102 | return repo; |
115 | } |
103 | } |
116 | return NULL; |
104 | return NULL; |
117 | } |
105 | } |
118 | |
106 | |
@@ -155,129 +143,128 @@ void cgit_global_config_cb(const char *name, const char *value) |
155 | else if (!strcmp(name, "cache-static-ttl")) |
143 | else if (!strcmp(name, "cache-static-ttl")) |
156 | ctx.cfg.cache_static_ttl = atoi(value); |
144 | ctx.cfg.cache_static_ttl = atoi(value); |
157 | else if (!strcmp(name, "cache-dynamic-ttl")) |
145 | else if (!strcmp(name, "cache-dynamic-ttl")) |
158 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
146 | ctx.cfg.cache_dynamic_ttl = atoi(value); |
159 | else if (!strcmp(name, "max-message-length")) |
147 | else if (!strcmp(name, "max-message-length")) |
160 | ctx.cfg.max_msg_len = atoi(value); |
148 | ctx.cfg.max_msg_len = atoi(value); |
161 | else if (!strcmp(name, "max-repodesc-length")) |
149 | else if (!strcmp(name, "max-repodesc-length")) |
162 | ctx.cfg.max_repodesc_len = atoi(value); |
150 | ctx.cfg.max_repodesc_len = atoi(value); |
163 | else if (!strcmp(name, "max-commit-count")) |
151 | else if (!strcmp(name, "max-commit-count")) |
164 | ctx.cfg.max_commit_count = atoi(value); |
152 | ctx.cfg.max_commit_count = atoi(value); |
165 | else if (!strcmp(name, "summary-log")) |
153 | else if (!strcmp(name, "summary-log")) |
166 | ctx.cfg.summary_log = atoi(value); |
154 | ctx.cfg.summary_log = atoi(value); |
167 | else if (!strcmp(name, "summary-branches")) |
155 | else if (!strcmp(name, "summary-branches")) |
168 | ctx.cfg.summary_branches = atoi(value); |
156 | ctx.cfg.summary_branches = atoi(value); |
169 | else if (!strcmp(name, "summary-tags")) |
157 | else if (!strcmp(name, "summary-tags")) |
170 | ctx.cfg.summary_tags = atoi(value); |
158 | ctx.cfg.summary_tags = atoi(value); |
171 | else if (!strcmp(name, "agefile")) |
159 | else if (!strcmp(name, "agefile")) |
172 | ctx.cfg.agefile = xstrdup(value); |
160 | ctx.cfg.agefile = xstrdup(value); |
173 | else if (!strcmp(name, "renamelimit")) |
161 | else if (!strcmp(name, "renamelimit")) |
174 | ctx.cfg.renamelimit = atoi(value); |
162 | ctx.cfg.renamelimit = atoi(value); |
175 | else if (!strcmp(name, "robots")) |
163 | else if (!strcmp(name, "robots")) |
176 | ctx.cfg.robots = xstrdup(value); |
164 | ctx.cfg.robots = xstrdup(value); |
177 | else if (!strcmp(name, "clone-prefix")) |
165 | else if (!strcmp(name, "clone-prefix")) |
178 | ctx.cfg.clone_prefix = xstrdup(value); |
166 | ctx.cfg.clone_prefix = xstrdup(value); |
179 | else if (!strcmp(name, "repo.group")) |
167 | else if (!strcmp(name, "repo.group")) |
180 | ctx.cfg.repo_group = xstrdup(value); |
168 | ctx.cfg.repo_group = xstrdup(value); |
181 | else if (!strcmp(name, "repo.url")) |
169 | else if (!strcmp(name, "repo.url")) |
182 | ctx.repo = add_repo(value); |
170 | ctx.repo = add_repo(value); |
183 | else if (!strcmp(name, "repo.name")) |
171 | else if (!strcmp(name, "repo.name")) |
184 | ctx.repo->name = xstrdup(value); |
172 | ctx.repo->name = xstrdup(value); |
185 | else if (ctx.repo && !strcmp(name, "repo.path")) |
173 | else if (ctx.repo && !strcmp(name, "repo.path")) |
186 | ctx.repo->path = trim_end(value, '/'); |
174 | ctx.repo->path = trim_end(value, '/'); |
187 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
175 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) |
188 | ctx.repo->clone_url = xstrdup(value); |
176 | ctx.repo->clone_url = xstrdup(value); |
189 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
177 | else if (ctx.repo && !strcmp(name, "repo.desc")) |
190 | ctx.repo->desc = xstrdup(value); |
178 | ctx.repo->desc = xstrdup(value); |
191 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
179 | else if (ctx.repo && !strcmp(name, "repo.owner")) |
192 | ctx.repo->owner = xstrdup(value); |
180 | ctx.repo->owner = xstrdup(value); |
193 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
181 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) |
194 | ctx.repo->defbranch = xstrdup(value); |
182 | ctx.repo->defbranch = xstrdup(value); |
195 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
183 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) |
196 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
184 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ |
197 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
185 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) |
198 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
186 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); |
199 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
187 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) |
200 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
188 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); |
201 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
189 | else if (ctx.repo && !strcmp(name, "repo.module-link")) |
202 | ctx.repo->module_link= xstrdup(value); |
190 | ctx.repo->module_link= xstrdup(value); |
203 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
191 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { |
204 | if (*value == '/') |
192 | if (*value == '/') |
205 | ctx.repo->readme = xstrdup(value); |
193 | ctx.repo->readme = xstrdup(value); |
206 | else |
194 | else |
207 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
195 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); |
208 | } else if (!strcmp(name, "include")) |
196 | } else if (!strcmp(name, "include")) |
209 | cgit_read_config(value, cgit_global_config_cb); |
197 | cgit_read_config(value, cgit_global_config_cb); |
210 | } |
198 | } |
211 | |
199 | |
212 | void cgit_querystring_cb(const char *name, const char *value) |
200 | void cgit_querystring_cb(const char *name, const char *value) |
213 | { |
201 | { |
214 | if (!strcmp(name,"r")) { |
202 | if (!strcmp(name,"r")) { |
215 | ctx.qry.repo = xstrdup(value); |
203 | ctx.qry.repo = xstrdup(value); |
216 | ctx.repo = cgit_get_repoinfo(value); |
204 | ctx.repo = cgit_get_repoinfo(value); |
217 | } else if (!strcmp(name, "p")) { |
205 | } else if (!strcmp(name, "p")) { |
218 | ctx.qry.page = xstrdup(value); |
206 | ctx.qry.page = xstrdup(value); |
219 | cgit_cmd = cgit_get_cmd_index(value); |
| |
220 | } else if (!strcmp(name, "url")) { |
207 | } else if (!strcmp(name, "url")) { |
221 | cgit_parse_url(value); |
208 | cgit_parse_url(value); |
222 | } else if (!strcmp(name, "qt")) { |
209 | } else if (!strcmp(name, "qt")) { |
223 | ctx.qry.grep = xstrdup(value); |
210 | ctx.qry.grep = xstrdup(value); |
224 | } else if (!strcmp(name, "q")) { |
211 | } else if (!strcmp(name, "q")) { |
225 | ctx.qry.search = xstrdup(value); |
212 | ctx.qry.search = xstrdup(value); |
226 | } else if (!strcmp(name, "h")) { |
213 | } else if (!strcmp(name, "h")) { |
227 | ctx.qry.head = xstrdup(value); |
214 | ctx.qry.head = xstrdup(value); |
228 | ctx.qry.has_symref = 1; |
215 | ctx.qry.has_symref = 1; |
229 | } else if (!strcmp(name, "id")) { |
216 | } else if (!strcmp(name, "id")) { |
230 | ctx.qry.sha1 = xstrdup(value); |
217 | ctx.qry.sha1 = xstrdup(value); |
231 | ctx.qry.has_sha1 = 1; |
218 | ctx.qry.has_sha1 = 1; |
232 | } else if (!strcmp(name, "id2")) { |
219 | } else if (!strcmp(name, "id2")) { |
233 | ctx.qry.sha2 = xstrdup(value); |
220 | ctx.qry.sha2 = xstrdup(value); |
234 | ctx.qry.has_sha1 = 1; |
221 | ctx.qry.has_sha1 = 1; |
235 | } else if (!strcmp(name, "ofs")) { |
222 | } else if (!strcmp(name, "ofs")) { |
236 | ctx.qry.ofs = atoi(value); |
223 | ctx.qry.ofs = atoi(value); |
237 | } else if (!strcmp(name, "path")) { |
224 | } else if (!strcmp(name, "path")) { |
238 | ctx.qry.path = trim_end(value, '/'); |
225 | ctx.qry.path = trim_end(value, '/'); |
239 | } else if (!strcmp(name, "name")) { |
226 | } else if (!strcmp(name, "name")) { |
240 | ctx.qry.name = xstrdup(value); |
227 | ctx.qry.name = xstrdup(value); |
241 | } |
228 | } |
242 | } |
229 | } |
243 | |
230 | |
244 | void *cgit_free_commitinfo(struct commitinfo *info) |
231 | void *cgit_free_commitinfo(struct commitinfo *info) |
245 | { |
232 | { |
246 | free(info->author); |
233 | free(info->author); |
247 | free(info->author_email); |
234 | free(info->author_email); |
248 | free(info->committer); |
235 | free(info->committer); |
249 | free(info->committer_email); |
236 | free(info->committer_email); |
250 | free(info->subject); |
237 | free(info->subject); |
251 | free(info->msg); |
238 | free(info->msg); |
252 | free(info->msg_encoding); |
239 | free(info->msg_encoding); |
253 | free(info); |
240 | free(info); |
254 | return NULL; |
241 | return NULL; |
255 | } |
242 | } |
256 | |
243 | |
257 | int hextoint(char c) |
244 | int hextoint(char c) |
258 | { |
245 | { |
259 | if (c >= 'a' && c <= 'f') |
246 | if (c >= 'a' && c <= 'f') |
260 | return 10 + c - 'a'; |
247 | return 10 + c - 'a'; |
261 | else if (c >= 'A' && c <= 'F') |
248 | else if (c >= 'A' && c <= 'F') |
262 | return 10 + c - 'A'; |
249 | return 10 + c - 'A'; |
263 | else if (c >= '0' && c <= '9') |
250 | else if (c >= '0' && c <= '9') |
264 | return c - '0'; |
251 | return c - '0'; |
265 | else |
252 | else |
266 | return -1; |
253 | return -1; |
267 | } |
254 | } |
268 | |
255 | |
269 | char *trim_end(const char *str, char c) |
256 | char *trim_end(const char *str, char c) |
270 | { |
257 | { |
271 | int len; |
258 | int len; |
272 | char *s, *t; |
259 | char *s, *t; |
273 | |
260 | |
274 | if (str == NULL) |
261 | if (str == NULL) |
275 | return NULL; |
262 | return NULL; |
276 | t = (char *)str; |
263 | t = (char *)str; |
277 | len = strlen(t); |
264 | len = strlen(t); |
278 | while(len > 0 && t[len - 1] == c) |
265 | while(len > 0 && t[len - 1] == c) |
279 | len--; |
266 | len--; |
280 | |
267 | |
281 | if (len == 0) |
268 | if (len == 0) |
282 | return NULL; |
269 | return NULL; |
283 | |
270 | |
|