author | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 16:26:08 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-03-24 16:26:08 (UTC) |
commit | 163037e79c6cde1073d555dbeae2a095298e6101 (patch) (unidiff) | |
tree | 37f36b95772eff04683a7f7efc1c5c369b7bf905 /shared.c | |
parent | a4d1ca1dc6ff8171694d9e2280b6075a1beced0c (diff) | |
download | cgit-163037e79c6cde1073d555dbeae2a095298e6101.zip cgit-163037e79c6cde1073d555dbeae2a095298e6101.tar.gz cgit-163037e79c6cde1073d555dbeae2a095298e6101.tar.bz2 |
Move non-generic functions from shared.c to cgit.c
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | shared.c | 152 |
1 files changed, 1 insertions, 151 deletions
@@ -1,422 +1,272 @@ | |||
1 | /* shared.c: global vars + some callback functions | 1 | /* shared.c: global vars + some callback functions |
2 | * | 2 | * |
3 | * Copyright (C) 2006 Lars Hjemli | 3 | * Copyright (C) 2006 Lars Hjemli |
4 | * | 4 | * |
5 | * Licensed under GNU General Public License v2 | 5 | * Licensed under GNU General Public License v2 |
6 | * (see COPYING for full license text) | 6 | * (see COPYING for full license text) |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include "cgit.h" | 9 | #include "cgit.h" |
10 | 10 | ||
11 | struct cgit_repolist cgit_repolist; | 11 | struct cgit_repolist cgit_repolist; |
12 | struct cgit_context ctx; | 12 | struct cgit_context ctx; |
13 | int cgit_cmd; | 13 | int cgit_cmd; |
14 | 14 | ||
15 | const char *cgit_version = CGIT_VERSION; | 15 | const char *cgit_version = CGIT_VERSION; |
16 | 16 | ||
17 | void cgit_prepare_context(struct cgit_context *ctx) | ||
18 | { | ||
19 | memset(ctx, 0, sizeof(ctx)); | ||
20 | ctx->cfg.agefile = "info/web/last-modified"; | ||
21 | ctx->cfg.cache_dynamic_ttl = 5; | ||
22 | ctx->cfg.cache_max_create_time = 5; | ||
23 | ctx->cfg.cache_repo_ttl = 5; | ||
24 | ctx->cfg.cache_root = CGIT_CACHE_ROOT; | ||
25 | ctx->cfg.cache_root_ttl = 5; | ||
26 | ctx->cfg.cache_static_ttl = -1; | ||
27 | ctx->cfg.css = "/cgit.css"; | ||
28 | ctx->cfg.logo = "/git-logo.png"; | ||
29 | ctx->cfg.max_commit_count = 50; | ||
30 | ctx->cfg.max_lock_attempts = 5; | ||
31 | ctx->cfg.max_msg_len = 60; | ||
32 | ctx->cfg.max_repodesc_len = 60; | ||
33 | ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; | ||
34 | ctx->cfg.renamelimit = -1; | ||
35 | ctx->cfg.robots = "index, nofollow"; | ||
36 | ctx->cfg.root_title = "Git repository browser"; | ||
37 | ctx->cfg.script_name = CGIT_SCRIPT_NAME; | ||
38 | ctx->page.mimetype = "text/html"; | ||
39 | ctx->page.charset = PAGE_ENCODING; | ||
40 | ctx->page.filename = NULL; | ||
41 | } | ||
42 | |||
43 | int chk_zero(int result, char *msg) | 17 | int chk_zero(int result, char *msg) |
44 | { | 18 | { |
45 | if (result != 0) | 19 | if (result != 0) |
46 | die("%s: %s", msg, strerror(errno)); | 20 | die("%s: %s", msg, strerror(errno)); |
47 | return result; | 21 | return result; |
48 | } | 22 | } |
49 | 23 | ||
50 | int chk_positive(int result, char *msg) | 24 | int chk_positive(int result, char *msg) |
51 | { | 25 | { |
52 | if (result <= 0) | 26 | if (result <= 0) |
53 | die("%s: %s", msg, strerror(errno)); | 27 | die("%s: %s", msg, strerror(errno)); |
54 | return result; | 28 | return result; |
55 | } | 29 | } |
56 | 30 | ||
57 | int chk_non_negative(int result, char *msg) | 31 | int chk_non_negative(int result, char *msg) |
58 | { | 32 | { |
59 | if (result < 0) | 33 | if (result < 0) |
60 | die("%s: %s",msg, strerror(errno)); | 34 | die("%s: %s",msg, strerror(errno)); |
61 | return result; | 35 | return result; |
62 | } | 36 | } |
63 | 37 | ||
64 | struct cgit_repo *add_repo(const char *url) | 38 | struct cgit_repo *cgit_add_repo(const char *url) |
65 | { | 39 | { |
66 | struct cgit_repo *ret; | 40 | struct cgit_repo *ret; |
67 | 41 | ||
68 | if (++cgit_repolist.count > cgit_repolist.length) { | 42 | if (++cgit_repolist.count > cgit_repolist.length) { |
69 | if (cgit_repolist.length == 0) | 43 | if (cgit_repolist.length == 0) |
70 | cgit_repolist.length = 8; | 44 | cgit_repolist.length = 8; |
71 | else | 45 | else |
72 | cgit_repolist.length *= 2; | 46 | cgit_repolist.length *= 2; |
73 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, | 47 | cgit_repolist.repos = xrealloc(cgit_repolist.repos, |
74 | cgit_repolist.length * | 48 | cgit_repolist.length * |
75 | sizeof(struct cgit_repo)); | 49 | sizeof(struct cgit_repo)); |
76 | } | 50 | } |
77 | 51 | ||
78 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; | 52 | ret = &cgit_repolist.repos[cgit_repolist.count-1]; |
79 | ret->url = trim_end(url, '/'); | 53 | ret->url = trim_end(url, '/'); |
80 | ret->name = ret->url; | 54 | ret->name = ret->url; |
81 | ret->path = NULL; | 55 | ret->path = NULL; |
82 | ret->desc = "[no description]"; | 56 | ret->desc = "[no description]"; |
83 | ret->owner = NULL; | 57 | ret->owner = NULL; |
84 | ret->group = ctx.cfg.repo_group; | 58 | ret->group = ctx.cfg.repo_group; |
85 | ret->defbranch = "master"; | 59 | ret->defbranch = "master"; |
86 | ret->snapshots = ctx.cfg.snapshots; | 60 | ret->snapshots = ctx.cfg.snapshots; |
87 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; | 61 | ret->enable_log_filecount = ctx.cfg.enable_log_filecount; |
88 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; | 62 | ret->enable_log_linecount = ctx.cfg.enable_log_linecount; |
89 | ret->module_link = ctx.cfg.module_link; | 63 | ret->module_link = ctx.cfg.module_link; |
90 | ret->readme = NULL; | 64 | ret->readme = NULL; |
91 | return ret; | 65 | return ret; |
92 | } | 66 | } |
93 | 67 | ||
94 | struct cgit_repo *cgit_get_repoinfo(const char *url) | 68 | struct cgit_repo *cgit_get_repoinfo(const char *url) |
95 | { | 69 | { |
96 | int i; | 70 | int i; |
97 | struct cgit_repo *repo; | 71 | struct cgit_repo *repo; |
98 | 72 | ||
99 | for (i=0; i<cgit_repolist.count; i++) { | 73 | for (i=0; i<cgit_repolist.count; i++) { |
100 | repo = &cgit_repolist.repos[i]; | 74 | repo = &cgit_repolist.repos[i]; |
101 | if (!strcmp(repo->url, url)) | 75 | if (!strcmp(repo->url, url)) |
102 | return repo; | 76 | return repo; |
103 | } | 77 | } |
104 | return NULL; | 78 | return NULL; |
105 | } | 79 | } |
106 | 80 | ||
107 | void cgit_global_config_cb(const char *name, const char *value) | ||
108 | { | ||
109 | if (!strcmp(name, "root-title")) | ||
110 | ctx.cfg.root_title = xstrdup(value); | ||
111 | else if (!strcmp(name, "css")) | ||
112 | ctx.cfg.css = xstrdup(value); | ||
113 | else if (!strcmp(name, "logo")) | ||
114 | ctx.cfg.logo = xstrdup(value); | ||
115 | else if (!strcmp(name, "index-header")) | ||
116 | ctx.cfg.index_header = xstrdup(value); | ||
117 | else if (!strcmp(name, "index-info")) | ||
118 | ctx.cfg.index_info = xstrdup(value); | ||
119 | else if (!strcmp(name, "logo-link")) | ||
120 | ctx.cfg.logo_link = xstrdup(value); | ||
121 | else if (!strcmp(name, "module-link")) | ||
122 | ctx.cfg.module_link = xstrdup(value); | ||
123 | else if (!strcmp(name, "virtual-root")) { | ||
124 | ctx.cfg.virtual_root = trim_end(value, '/'); | ||
125 | if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) | ||
126 | ctx.cfg.virtual_root = ""; | ||
127 | } else if (!strcmp(name, "nocache")) | ||
128 | ctx.cfg.nocache = atoi(value); | ||
129 | else if (!strcmp(name, "snapshots")) | ||
130 | ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); | ||
131 | else if (!strcmp(name, "enable-index-links")) | ||
132 | ctx.cfg.enable_index_links = atoi(value); | ||
133 | else if (!strcmp(name, "enable-log-filecount")) | ||
134 | ctx.cfg.enable_log_filecount = atoi(value); | ||
135 | else if (!strcmp(name, "enable-log-linecount")) | ||
136 | ctx.cfg.enable_log_linecount = atoi(value); | ||
137 | else if (!strcmp(name, "cache-root")) | ||
138 | ctx.cfg.cache_root = xstrdup(value); | ||
139 | else if (!strcmp(name, "cache-root-ttl")) | ||
140 | ctx.cfg.cache_root_ttl = atoi(value); | ||
141 | else if (!strcmp(name, "cache-repo-ttl")) | ||
142 | ctx.cfg.cache_repo_ttl = atoi(value); | ||
143 | else if (!strcmp(name, "cache-static-ttl")) | ||
144 | ctx.cfg.cache_static_ttl = atoi(value); | ||
145 | else if (!strcmp(name, "cache-dynamic-ttl")) | ||
146 | ctx.cfg.cache_dynamic_ttl = atoi(value); | ||
147 | else if (!strcmp(name, "max-message-length")) | ||
148 | ctx.cfg.max_msg_len = atoi(value); | ||
149 | else if (!strcmp(name, "max-repodesc-length")) | ||
150 | ctx.cfg.max_repodesc_len = atoi(value); | ||
151 | else if (!strcmp(name, "max-commit-count")) | ||
152 | ctx.cfg.max_commit_count = atoi(value); | ||
153 | else if (!strcmp(name, "summary-log")) | ||
154 | ctx.cfg.summary_log = atoi(value); | ||
155 | else if (!strcmp(name, "summary-branches")) | ||
156 | ctx.cfg.summary_branches = atoi(value); | ||
157 | else if (!strcmp(name, "summary-tags")) | ||
158 | ctx.cfg.summary_tags = atoi(value); | ||
159 | else if (!strcmp(name, "agefile")) | ||
160 | ctx.cfg.agefile = xstrdup(value); | ||
161 | else if (!strcmp(name, "renamelimit")) | ||
162 | ctx.cfg.renamelimit = atoi(value); | ||
163 | else if (!strcmp(name, "robots")) | ||
164 | ctx.cfg.robots = xstrdup(value); | ||
165 | else if (!strcmp(name, "clone-prefix")) | ||
166 | ctx.cfg.clone_prefix = xstrdup(value); | ||
167 | else if (!strcmp(name, "repo.group")) | ||
168 | ctx.cfg.repo_group = xstrdup(value); | ||
169 | else if (!strcmp(name, "repo.url")) | ||
170 | ctx.repo = add_repo(value); | ||
171 | else if (!strcmp(name, "repo.name")) | ||
172 | ctx.repo->name = xstrdup(value); | ||
173 | else if (ctx.repo && !strcmp(name, "repo.path")) | ||
174 | ctx.repo->path = trim_end(value, '/'); | ||
175 | else if (ctx.repo && !strcmp(name, "repo.clone-url")) | ||
176 | ctx.repo->clone_url = xstrdup(value); | ||
177 | else if (ctx.repo && !strcmp(name, "repo.desc")) | ||
178 | ctx.repo->desc = xstrdup(value); | ||
179 | else if (ctx.repo && !strcmp(name, "repo.owner")) | ||
180 | ctx.repo->owner = xstrdup(value); | ||
181 | else if (ctx.repo && !strcmp(name, "repo.defbranch")) | ||
182 | ctx.repo->defbranch = xstrdup(value); | ||
183 | else if (ctx.repo && !strcmp(name, "repo.snapshots")) | ||
184 | ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ | ||
185 | else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) | ||
186 | ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); | ||
187 | else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) | ||
188 | ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); | ||
189 | else if (ctx.repo && !strcmp(name, "repo.module-link")) | ||
190 | ctx.repo->module_link= xstrdup(value); | ||
191 | else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { | ||
192 | if (*value == '/') | ||
193 | ctx.repo->readme = xstrdup(value); | ||
194 | else | ||
195 | ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); | ||
196 | } else if (!strcmp(name, "include")) | ||
197 | cgit_read_config(value, cgit_global_config_cb); | ||
198 | } | ||
199 | |||
200 | void cgit_querystring_cb(const char *name, const char *value) | ||
201 | { | ||
202 | if (!strcmp(name,"r")) { | ||
203 | ctx.qry.repo = xstrdup(value); | ||
204 | ctx.repo = cgit_get_repoinfo(value); | ||
205 | } else if (!strcmp(name, "p")) { | ||
206 | ctx.qry.page = xstrdup(value); | ||
207 | } else if (!strcmp(name, "url")) { | ||
208 | cgit_parse_url(value); | ||
209 | } else if (!strcmp(name, "qt")) { | ||
210 | ctx.qry.grep = xstrdup(value); | ||
211 | } else if (!strcmp(name, "q")) { | ||
212 | ctx.qry.search = xstrdup(value); | ||
213 | } else if (!strcmp(name, "h")) { | ||
214 | ctx.qry.head = xstrdup(value); | ||
215 | ctx.qry.has_symref = 1; | ||
216 | } else if (!strcmp(name, "id")) { | ||
217 | ctx.qry.sha1 = xstrdup(value); | ||
218 | ctx.qry.has_sha1 = 1; | ||
219 | } else if (!strcmp(name, "id2")) { | ||
220 | ctx.qry.sha2 = xstrdup(value); | ||
221 | ctx.qry.has_sha1 = 1; | ||
222 | } else if (!strcmp(name, "ofs")) { | ||
223 | ctx.qry.ofs = atoi(value); | ||
224 | } else if (!strcmp(name, "path")) { | ||
225 | ctx.qry.path = trim_end(value, '/'); | ||
226 | } else if (!strcmp(name, "name")) { | ||
227 | ctx.qry.name = xstrdup(value); | ||
228 | } | ||
229 | } | ||
230 | |||
231 | void *cgit_free_commitinfo(struct commitinfo *info) | 81 | void *cgit_free_commitinfo(struct commitinfo *info) |
232 | { | 82 | { |
233 | free(info->author); | 83 | free(info->author); |
234 | free(info->author_email); | 84 | free(info->author_email); |
235 | free(info->committer); | 85 | free(info->committer); |
236 | free(info->committer_email); | 86 | free(info->committer_email); |
237 | free(info->subject); | 87 | free(info->subject); |
238 | free(info->msg); | 88 | free(info->msg); |
239 | free(info->msg_encoding); | 89 | free(info->msg_encoding); |
240 | free(info); | 90 | free(info); |
241 | return NULL; | 91 | return NULL; |
242 | } | 92 | } |
243 | 93 | ||
244 | int hextoint(char c) | 94 | int hextoint(char c) |
245 | { | 95 | { |
246 | if (c >= 'a' && c <= 'f') | 96 | if (c >= 'a' && c <= 'f') |
247 | return 10 + c - 'a'; | 97 | return 10 + c - 'a'; |
248 | else if (c >= 'A' && c <= 'F') | 98 | else if (c >= 'A' && c <= 'F') |
249 | return 10 + c - 'A'; | 99 | return 10 + c - 'A'; |
250 | else if (c >= '0' && c <= '9') | 100 | else if (c >= '0' && c <= '9') |
251 | return c - '0'; | 101 | return c - '0'; |
252 | else | 102 | else |
253 | return -1; | 103 | return -1; |
254 | } | 104 | } |
255 | 105 | ||
256 | char *trim_end(const char *str, char c) | 106 | char *trim_end(const char *str, char c) |
257 | { | 107 | { |
258 | int len; | 108 | int len; |
259 | char *s, *t; | 109 | char *s, *t; |
260 | 110 | ||
261 | if (str == NULL) | 111 | if (str == NULL) |
262 | return NULL; | 112 | return NULL; |
263 | t = (char *)str; | 113 | t = (char *)str; |
264 | len = strlen(t); | 114 | len = strlen(t); |
265 | while(len > 0 && t[len - 1] == c) | 115 | while(len > 0 && t[len - 1] == c) |
266 | len--; | 116 | len--; |
267 | 117 | ||
268 | if (len == 0) | 118 | if (len == 0) |
269 | return NULL; | 119 | return NULL; |
270 | 120 | ||
271 | c = t[len]; | 121 | c = t[len]; |
272 | t[len] = '\0'; | 122 | t[len] = '\0'; |
273 | s = xstrdup(t); | 123 | s = xstrdup(t); |
274 | t[len] = c; | 124 | t[len] = c; |
275 | return s; | 125 | return s; |
276 | } | 126 | } |
277 | 127 | ||
278 | char *strlpart(char *txt, int maxlen) | 128 | char *strlpart(char *txt, int maxlen) |
279 | { | 129 | { |
280 | char *result; | 130 | char *result; |
281 | 131 | ||
282 | if (!txt) | 132 | if (!txt) |
283 | return txt; | 133 | return txt; |
284 | 134 | ||
285 | if (strlen(txt) <= maxlen) | 135 | if (strlen(txt) <= maxlen) |
286 | return txt; | 136 | return txt; |
287 | result = xmalloc(maxlen + 1); | 137 | result = xmalloc(maxlen + 1); |
288 | memcpy(result, txt, maxlen - 3); | 138 | memcpy(result, txt, maxlen - 3); |
289 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; | 139 | result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; |
290 | result[maxlen] = '\0'; | 140 | result[maxlen] = '\0'; |
291 | return result; | 141 | return result; |
292 | } | 142 | } |
293 | 143 | ||
294 | char *strrpart(char *txt, int maxlen) | 144 | char *strrpart(char *txt, int maxlen) |
295 | { | 145 | { |
296 | char *result; | 146 | char *result; |
297 | 147 | ||
298 | if (!txt) | 148 | if (!txt) |
299 | return txt; | 149 | return txt; |
300 | 150 | ||
301 | if (strlen(txt) <= maxlen) | 151 | if (strlen(txt) <= maxlen) |
302 | return txt; | 152 | return txt; |
303 | result = xmalloc(maxlen + 1); | 153 | result = xmalloc(maxlen + 1); |
304 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); | 154 | memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); |
305 | result[0] = result[1] = result[2] = '.'; | 155 | result[0] = result[1] = result[2] = '.'; |
306 | return result; | 156 | return result; |
307 | } | 157 | } |
308 | 158 | ||
309 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) | 159 | void cgit_add_ref(struct reflist *list, struct refinfo *ref) |
310 | { | 160 | { |
311 | size_t size; | 161 | size_t size; |
312 | 162 | ||
313 | if (list->count >= list->alloc) { | 163 | if (list->count >= list->alloc) { |
314 | list->alloc += (list->alloc ? list->alloc : 4); | 164 | list->alloc += (list->alloc ? list->alloc : 4); |
315 | size = list->alloc * sizeof(struct refinfo *); | 165 | size = list->alloc * sizeof(struct refinfo *); |
316 | list->refs = xrealloc(list->refs, size); | 166 | list->refs = xrealloc(list->refs, size); |
317 | } | 167 | } |
318 | list->refs[list->count++] = ref; | 168 | list->refs[list->count++] = ref; |
319 | } | 169 | } |
320 | 170 | ||
321 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) | 171 | struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) |
322 | { | 172 | { |
323 | struct refinfo *ref; | 173 | struct refinfo *ref; |
324 | 174 | ||
325 | ref = xmalloc(sizeof (struct refinfo)); | 175 | ref = xmalloc(sizeof (struct refinfo)); |
326 | ref->refname = xstrdup(refname); | 176 | ref->refname = xstrdup(refname); |
327 | ref->object = parse_object(sha1); | 177 | ref->object = parse_object(sha1); |
328 | switch (ref->object->type) { | 178 | switch (ref->object->type) { |
329 | case OBJ_TAG: | 179 | case OBJ_TAG: |
330 | ref->tag = cgit_parse_tag((struct tag *)ref->object); | 180 | ref->tag = cgit_parse_tag((struct tag *)ref->object); |
331 | break; | 181 | break; |
332 | case OBJ_COMMIT: | 182 | case OBJ_COMMIT: |
333 | ref->commit = cgit_parse_commit((struct commit *)ref->object); | 183 | ref->commit = cgit_parse_commit((struct commit *)ref->object); |
334 | break; | 184 | break; |
335 | } | 185 | } |
336 | return ref; | 186 | return ref; |
337 | } | 187 | } |
338 | 188 | ||
339 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, | 189 | int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, |
340 | void *cb_data) | 190 | void *cb_data) |
341 | { | 191 | { |
342 | struct reflist *list = (struct reflist *)cb_data; | 192 | struct reflist *list = (struct reflist *)cb_data; |
343 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); | 193 | struct refinfo *info = cgit_mk_refinfo(refname, sha1); |
344 | 194 | ||
345 | if (info) | 195 | if (info) |
346 | cgit_add_ref(list, info); | 196 | cgit_add_ref(list, info); |
347 | return 0; | 197 | return 0; |
348 | } | 198 | } |
349 | 199 | ||
350 | void cgit_diff_tree_cb(struct diff_queue_struct *q, | 200 | void cgit_diff_tree_cb(struct diff_queue_struct *q, |
351 | struct diff_options *options, void *data) | 201 | struct diff_options *options, void *data) |
352 | { | 202 | { |
353 | int i; | 203 | int i; |
354 | 204 | ||
355 | for (i = 0; i < q->nr; i++) { | 205 | for (i = 0; i < q->nr; i++) { |
356 | if (q->queue[i]->status == 'U') | 206 | if (q->queue[i]->status == 'U') |
357 | continue; | 207 | continue; |
358 | ((filepair_fn)data)(q->queue[i]); | 208 | ((filepair_fn)data)(q->queue[i]); |
359 | } | 209 | } |
360 | } | 210 | } |
361 | 211 | ||
362 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) | 212 | static int load_mmfile(mmfile_t *file, const unsigned char *sha1) |
363 | { | 213 | { |
364 | enum object_type type; | 214 | enum object_type type; |
365 | 215 | ||
366 | if (is_null_sha1(sha1)) { | 216 | if (is_null_sha1(sha1)) { |
367 | file->ptr = (char *)""; | 217 | file->ptr = (char *)""; |
368 | file->size = 0; | 218 | file->size = 0; |
369 | } else { | 219 | } else { |
370 | file->ptr = read_sha1_file(sha1, &type, | 220 | file->ptr = read_sha1_file(sha1, &type, |
371 | (unsigned long *)&file->size); | 221 | (unsigned long *)&file->size); |
372 | } | 222 | } |
373 | return 1; | 223 | return 1; |
374 | } | 224 | } |
375 | 225 | ||
376 | /* | 226 | /* |
377 | * Receive diff-buffers from xdiff and concatenate them as | 227 | * Receive diff-buffers from xdiff and concatenate them as |
378 | * needed across multiple callbacks. | 228 | * needed across multiple callbacks. |
379 | * | 229 | * |
380 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), | 230 | * This is basically a copy of xdiff-interface.c/xdiff_outf(), |
381 | * ripped from git and modified to use globals instead of | 231 | * ripped from git and modified to use globals instead of |
382 | * a special callback-struct. | 232 | * a special callback-struct. |
383 | */ | 233 | */ |
384 | char *diffbuf = NULL; | 234 | char *diffbuf = NULL; |
385 | int buflen = 0; | 235 | int buflen = 0; |
386 | 236 | ||
387 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) | 237 | int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) |
388 | { | 238 | { |
389 | int i; | 239 | int i; |
390 | 240 | ||
391 | for (i = 0; i < nbuf; i++) { | 241 | for (i = 0; i < nbuf; i++) { |
392 | if (mb[i].ptr[mb[i].size-1] != '\n') { | 242 | if (mb[i].ptr[mb[i].size-1] != '\n') { |
393 | /* Incomplete line */ | 243 | /* Incomplete line */ |
394 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 244 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
395 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 245 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
396 | buflen += mb[i].size; | 246 | buflen += mb[i].size; |
397 | continue; | 247 | continue; |
398 | } | 248 | } |
399 | 249 | ||
400 | /* we have a complete line */ | 250 | /* we have a complete line */ |
401 | if (!diffbuf) { | 251 | if (!diffbuf) { |
402 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); | 252 | ((linediff_fn)priv)(mb[i].ptr, mb[i].size); |
403 | continue; | 253 | continue; |
404 | } | 254 | } |
405 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); | 255 | diffbuf = xrealloc(diffbuf, buflen + mb[i].size); |
406 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); | 256 | memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); |
407 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); | 257 | ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); |
408 | free(diffbuf); | 258 | free(diffbuf); |
409 | diffbuf = NULL; | 259 | diffbuf = NULL; |
410 | buflen = 0; | 260 | buflen = 0; |
411 | } | 261 | } |
412 | if (diffbuf) { | 262 | if (diffbuf) { |
413 | ((linediff_fn)priv)(diffbuf, buflen); | 263 | ((linediff_fn)priv)(diffbuf, buflen); |
414 | free(diffbuf); | 264 | free(diffbuf); |
415 | diffbuf = NULL; | 265 | diffbuf = NULL; |
416 | buflen = 0; | 266 | buflen = 0; |
417 | } | 267 | } |
418 | return 0; | 268 | return 0; |
419 | } | 269 | } |
420 | 270 | ||
421 | int cgit_diff_files(const unsigned char *old_sha1, | 271 | int cgit_diff_files(const unsigned char *old_sha1, |
422 | const unsigned char *new_sha1, | 272 | const unsigned char *new_sha1, |