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