summaryrefslogtreecommitdiffabout
path: root/shared.c
authorLars Hjemli <hjemli@gmail.com>2008-03-24 22:10:59 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-03-24 22:27:16 (UTC)
commit92908af4558d7362c7deeb05254343a5a5f11a05 (patch) (unidiff)
tree6913a6f567375446fb680bc7e94dbabb5d8e496d /shared.c
parentdf90b51bafec24336cf30339b2978ff6df22b075 (diff)
downloadcgit-92908af4558d7362c7deeb05254343a5a5f11a05.zip
cgit-92908af4558d7362c7deeb05254343a5a5f11a05.tar.gz
cgit-92908af4558d7362c7deeb05254343a5a5f11a05.tar.bz2
Move cgit_version from shared.c to cgit.c
With the matching Makefile change, this makes sure that only cgit.o and cgit proper needs to be rebuildt when VERSION has been modified. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--shared.c2
1 files changed, 0 insertions, 2 deletions
diff --git a/shared.c b/shared.c
index cd60da5..48002ac 100644
--- a/shared.c
+++ b/shared.c
@@ -1,358 +1,356 @@
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
11struct cgit_repolist cgit_repolist; 11struct cgit_repolist cgit_repolist;
12struct cgit_context ctx; 12struct cgit_context ctx;
13int cgit_cmd; 13int cgit_cmd;
14 14
15const char *cgit_version = CGIT_VERSION;
16
17int chk_zero(int result, char *msg) 15int chk_zero(int result, char *msg)
18{ 16{
19 if (result != 0) 17 if (result != 0)
20 die("%s: %s", msg, strerror(errno)); 18 die("%s: %s", msg, strerror(errno));
21 return result; 19 return result;
22} 20}
23 21
24int chk_positive(int result, char *msg) 22int chk_positive(int result, char *msg)
25{ 23{
26 if (result <= 0) 24 if (result <= 0)
27 die("%s: %s", msg, strerror(errno)); 25 die("%s: %s", msg, strerror(errno));
28 return result; 26 return result;
29} 27}
30 28
31int chk_non_negative(int result, char *msg) 29int chk_non_negative(int result, char *msg)
32{ 30{
33 if (result < 0) 31 if (result < 0)
34 die("%s: %s",msg, strerror(errno)); 32 die("%s: %s",msg, strerror(errno));
35 return result; 33 return result;
36} 34}
37 35
38struct cgit_repo *cgit_add_repo(const char *url) 36struct cgit_repo *cgit_add_repo(const char *url)
39{ 37{
40 struct cgit_repo *ret; 38 struct cgit_repo *ret;
41 39
42 if (++cgit_repolist.count > cgit_repolist.length) { 40 if (++cgit_repolist.count > cgit_repolist.length) {
43 if (cgit_repolist.length == 0) 41 if (cgit_repolist.length == 0)
44 cgit_repolist.length = 8; 42 cgit_repolist.length = 8;
45 else 43 else
46 cgit_repolist.length *= 2; 44 cgit_repolist.length *= 2;
47 cgit_repolist.repos = xrealloc(cgit_repolist.repos, 45 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
48 cgit_repolist.length * 46 cgit_repolist.length *
49 sizeof(struct cgit_repo)); 47 sizeof(struct cgit_repo));
50 } 48 }
51 49
52 ret = &cgit_repolist.repos[cgit_repolist.count-1]; 50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
53 ret->url = trim_end(url, '/'); 51 ret->url = trim_end(url, '/');
54 ret->name = ret->url; 52 ret->name = ret->url;
55 ret->path = NULL; 53 ret->path = NULL;
56 ret->desc = "[no description]"; 54 ret->desc = "[no description]";
57 ret->owner = NULL; 55 ret->owner = NULL;
58 ret->group = ctx.cfg.repo_group; 56 ret->group = ctx.cfg.repo_group;
59 ret->defbranch = "master"; 57 ret->defbranch = "master";
60 ret->snapshots = ctx.cfg.snapshots; 58 ret->snapshots = ctx.cfg.snapshots;
61 ret->enable_log_filecount = ctx.cfg.enable_log_filecount; 59 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
62 ret->enable_log_linecount = ctx.cfg.enable_log_linecount; 60 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
63 ret->module_link = ctx.cfg.module_link; 61 ret->module_link = ctx.cfg.module_link;
64 ret->readme = NULL; 62 ret->readme = NULL;
65 return ret; 63 return ret;
66} 64}
67 65
68struct cgit_repo *cgit_get_repoinfo(const char *url) 66struct cgit_repo *cgit_get_repoinfo(const char *url)
69{ 67{
70 int i; 68 int i;
71 struct cgit_repo *repo; 69 struct cgit_repo *repo;
72 70
73 for (i=0; i<cgit_repolist.count; i++) { 71 for (i=0; i<cgit_repolist.count; i++) {
74 repo = &cgit_repolist.repos[i]; 72 repo = &cgit_repolist.repos[i];
75 if (!strcmp(repo->url, url)) 73 if (!strcmp(repo->url, url))
76 return repo; 74 return repo;
77 } 75 }
78 return NULL; 76 return NULL;
79} 77}
80 78
81void *cgit_free_commitinfo(struct commitinfo *info) 79void *cgit_free_commitinfo(struct commitinfo *info)
82{ 80{
83 free(info->author); 81 free(info->author);
84 free(info->author_email); 82 free(info->author_email);
85 free(info->committer); 83 free(info->committer);
86 free(info->committer_email); 84 free(info->committer_email);
87 free(info->subject); 85 free(info->subject);
88 free(info->msg); 86 free(info->msg);
89 free(info->msg_encoding); 87 free(info->msg_encoding);
90 free(info); 88 free(info);
91 return NULL; 89 return NULL;
92} 90}
93 91
94int hextoint(char c) 92int hextoint(char c)
95{ 93{
96 if (c >= 'a' && c <= 'f') 94 if (c >= 'a' && c <= 'f')
97 return 10 + c - 'a'; 95 return 10 + c - 'a';
98 else if (c >= 'A' && c <= 'F') 96 else if (c >= 'A' && c <= 'F')
99 return 10 + c - 'A'; 97 return 10 + c - 'A';
100 else if (c >= '0' && c <= '9') 98 else if (c >= '0' && c <= '9')
101 return c - '0'; 99 return c - '0';
102 else 100 else
103 return -1; 101 return -1;
104} 102}
105 103
106char *trim_end(const char *str, char c) 104char *trim_end(const char *str, char c)
107{ 105{
108 int len; 106 int len;
109 char *s, *t; 107 char *s, *t;
110 108
111 if (str == NULL) 109 if (str == NULL)
112 return NULL; 110 return NULL;
113 t = (char *)str; 111 t = (char *)str;
114 len = strlen(t); 112 len = strlen(t);
115 while(len > 0 && t[len - 1] == c) 113 while(len > 0 && t[len - 1] == c)
116 len--; 114 len--;
117 115
118 if (len == 0) 116 if (len == 0)
119 return NULL; 117 return NULL;
120 118
121 c = t[len]; 119 c = t[len];
122 t[len] = '\0'; 120 t[len] = '\0';
123 s = xstrdup(t); 121 s = xstrdup(t);
124 t[len] = c; 122 t[len] = c;
125 return s; 123 return s;
126} 124}
127 125
128char *strlpart(char *txt, int maxlen) 126char *strlpart(char *txt, int maxlen)
129{ 127{
130 char *result; 128 char *result;
131 129
132 if (!txt) 130 if (!txt)
133 return txt; 131 return txt;
134 132
135 if (strlen(txt) <= maxlen) 133 if (strlen(txt) <= maxlen)
136 return txt; 134 return txt;
137 result = xmalloc(maxlen + 1); 135 result = xmalloc(maxlen + 1);
138 memcpy(result, txt, maxlen - 3); 136 memcpy(result, txt, maxlen - 3);
139 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.'; 137 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
140 result[maxlen] = '\0'; 138 result[maxlen] = '\0';
141 return result; 139 return result;
142} 140}
143 141
144char *strrpart(char *txt, int maxlen) 142char *strrpart(char *txt, int maxlen)
145{ 143{
146 char *result; 144 char *result;
147 145
148 if (!txt) 146 if (!txt)
149 return txt; 147 return txt;
150 148
151 if (strlen(txt) <= maxlen) 149 if (strlen(txt) <= maxlen)
152 return txt; 150 return txt;
153 result = xmalloc(maxlen + 1); 151 result = xmalloc(maxlen + 1);
154 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); 152 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
155 result[0] = result[1] = result[2] = '.'; 153 result[0] = result[1] = result[2] = '.';
156 return result; 154 return result;
157} 155}
158 156
159void cgit_add_ref(struct reflist *list, struct refinfo *ref) 157void cgit_add_ref(struct reflist *list, struct refinfo *ref)
160{ 158{
161 size_t size; 159 size_t size;
162 160
163 if (list->count >= list->alloc) { 161 if (list->count >= list->alloc) {
164 list->alloc += (list->alloc ? list->alloc : 4); 162 list->alloc += (list->alloc ? list->alloc : 4);
165 size = list->alloc * sizeof(struct refinfo *); 163 size = list->alloc * sizeof(struct refinfo *);
166 list->refs = xrealloc(list->refs, size); 164 list->refs = xrealloc(list->refs, size);
167 } 165 }
168 list->refs[list->count++] = ref; 166 list->refs[list->count++] = ref;
169} 167}
170 168
171struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) 169struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
172{ 170{
173 struct refinfo *ref; 171 struct refinfo *ref;
174 172
175 ref = xmalloc(sizeof (struct refinfo)); 173 ref = xmalloc(sizeof (struct refinfo));
176 ref->refname = xstrdup(refname); 174 ref->refname = xstrdup(refname);
177 ref->object = parse_object(sha1); 175 ref->object = parse_object(sha1);
178 switch (ref->object->type) { 176 switch (ref->object->type) {
179 case OBJ_TAG: 177 case OBJ_TAG:
180 ref->tag = cgit_parse_tag((struct tag *)ref->object); 178 ref->tag = cgit_parse_tag((struct tag *)ref->object);
181 break; 179 break;
182 case OBJ_COMMIT: 180 case OBJ_COMMIT:
183 ref->commit = cgit_parse_commit((struct commit *)ref->object); 181 ref->commit = cgit_parse_commit((struct commit *)ref->object);
184 break; 182 break;
185 } 183 }
186 return ref; 184 return ref;
187} 185}
188 186
189int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, 187int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
190 void *cb_data) 188 void *cb_data)
191{ 189{
192 struct reflist *list = (struct reflist *)cb_data; 190 struct reflist *list = (struct reflist *)cb_data;
193 struct refinfo *info = cgit_mk_refinfo(refname, sha1); 191 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
194 192
195 if (info) 193 if (info)
196 cgit_add_ref(list, info); 194 cgit_add_ref(list, info);
197 return 0; 195 return 0;
198} 196}
199 197
200void cgit_diff_tree_cb(struct diff_queue_struct *q, 198void cgit_diff_tree_cb(struct diff_queue_struct *q,
201 struct diff_options *options, void *data) 199 struct diff_options *options, void *data)
202{ 200{
203 int i; 201 int i;
204 202
205 for (i = 0; i < q->nr; i++) { 203 for (i = 0; i < q->nr; i++) {
206 if (q->queue[i]->status == 'U') 204 if (q->queue[i]->status == 'U')
207 continue; 205 continue;
208 ((filepair_fn)data)(q->queue[i]); 206 ((filepair_fn)data)(q->queue[i]);
209 } 207 }
210} 208}
211 209
212static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 210static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
213{ 211{
214 enum object_type type; 212 enum object_type type;
215 213
216 if (is_null_sha1(sha1)) { 214 if (is_null_sha1(sha1)) {
217 file->ptr = (char *)""; 215 file->ptr = (char *)"";
218 file->size = 0; 216 file->size = 0;
219 } else { 217 } else {
220 file->ptr = read_sha1_file(sha1, &type, 218 file->ptr = read_sha1_file(sha1, &type,
221 (unsigned long *)&file->size); 219 (unsigned long *)&file->size);
222 } 220 }
223 return 1; 221 return 1;
224} 222}
225 223
226/* 224/*
227 * Receive diff-buffers from xdiff and concatenate them as 225 * Receive diff-buffers from xdiff and concatenate them as
228 * needed across multiple callbacks. 226 * needed across multiple callbacks.
229 * 227 *
230 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 228 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
231 * ripped from git and modified to use globals instead of 229 * ripped from git and modified to use globals instead of
232 * a special callback-struct. 230 * a special callback-struct.
233 */ 231 */
234char *diffbuf = NULL; 232char *diffbuf = NULL;
235int buflen = 0; 233int buflen = 0;
236 234
237int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) 235int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
238{ 236{
239 int i; 237 int i;
240 238
241 for (i = 0; i < nbuf; i++) { 239 for (i = 0; i < nbuf; i++) {
242 if (mb[i].ptr[mb[i].size-1] != '\n') { 240 if (mb[i].ptr[mb[i].size-1] != '\n') {
243 /* Incomplete line */ 241 /* Incomplete line */
244 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 242 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
245 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 243 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
246 buflen += mb[i].size; 244 buflen += mb[i].size;
247 continue; 245 continue;
248 } 246 }
249 247
250 /* we have a complete line */ 248 /* we have a complete line */
251 if (!diffbuf) { 249 if (!diffbuf) {
252 ((linediff_fn)priv)(mb[i].ptr, mb[i].size); 250 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
253 continue; 251 continue;
254 } 252 }
255 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 253 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
256 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 254 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
257 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); 255 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
258 free(diffbuf); 256 free(diffbuf);
259 diffbuf = NULL; 257 diffbuf = NULL;
260 buflen = 0; 258 buflen = 0;
261 } 259 }
262 if (diffbuf) { 260 if (diffbuf) {
263 ((linediff_fn)priv)(diffbuf, buflen); 261 ((linediff_fn)priv)(diffbuf, buflen);
264 free(diffbuf); 262 free(diffbuf);
265 diffbuf = NULL; 263 diffbuf = NULL;
266 buflen = 0; 264 buflen = 0;
267 } 265 }
268 return 0; 266 return 0;
269} 267}
270 268
271int cgit_diff_files(const unsigned char *old_sha1, 269int cgit_diff_files(const unsigned char *old_sha1,
272 const unsigned char *new_sha1, 270 const unsigned char *new_sha1,
273 linediff_fn fn) 271 linediff_fn fn)
274{ 272{
275 mmfile_t file1, file2; 273 mmfile_t file1, file2;
276 xpparam_t diff_params; 274 xpparam_t diff_params;
277 xdemitconf_t emit_params; 275 xdemitconf_t emit_params;
278 xdemitcb_t emit_cb; 276 xdemitcb_t emit_cb;
279 277
280 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) 278 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
281 return 1; 279 return 1;
282 280
283 diff_params.flags = XDF_NEED_MINIMAL; 281 diff_params.flags = XDF_NEED_MINIMAL;
284 emit_params.ctxlen = 3; 282 emit_params.ctxlen = 3;
285 emit_params.flags = XDL_EMIT_FUNCNAMES; 283 emit_params.flags = XDL_EMIT_FUNCNAMES;
286 emit_params.find_func = NULL; 284 emit_params.find_func = NULL;
287 emit_cb.outf = filediff_cb; 285 emit_cb.outf = filediff_cb;
288 emit_cb.priv = fn; 286 emit_cb.priv = fn;
289 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); 287 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
290 return 0; 288 return 0;
291} 289}
292 290
293void cgit_diff_tree(const unsigned char *old_sha1, 291void cgit_diff_tree(const unsigned char *old_sha1,
294 const unsigned char *new_sha1, 292 const unsigned char *new_sha1,
295 filepair_fn fn, const char *prefix) 293 filepair_fn fn, const char *prefix)
296{ 294{
297 struct diff_options opt; 295 struct diff_options opt;
298 int ret; 296 int ret;
299 int prefixlen; 297 int prefixlen;
300 298
301 diff_setup(&opt); 299 diff_setup(&opt);
302 opt.output_format = DIFF_FORMAT_CALLBACK; 300 opt.output_format = DIFF_FORMAT_CALLBACK;
303 opt.detect_rename = 1; 301 opt.detect_rename = 1;
304 opt.rename_limit = ctx.cfg.renamelimit; 302 opt.rename_limit = ctx.cfg.renamelimit;
305 DIFF_OPT_SET(&opt, RECURSIVE); 303 DIFF_OPT_SET(&opt, RECURSIVE);
306 opt.format_callback = cgit_diff_tree_cb; 304 opt.format_callback = cgit_diff_tree_cb;
307 opt.format_callback_data = fn; 305 opt.format_callback_data = fn;
308 if (prefix) { 306 if (prefix) {
309 opt.nr_paths = 1; 307 opt.nr_paths = 1;
310 opt.paths = &prefix; 308 opt.paths = &prefix;
311 prefixlen = strlen(prefix); 309 prefixlen = strlen(prefix);
312 opt.pathlens = &prefixlen; 310 opt.pathlens = &prefixlen;
313 } 311 }
314 diff_setup_done(&opt); 312 diff_setup_done(&opt);
315 313
316 if (old_sha1 && !is_null_sha1(old_sha1)) 314 if (old_sha1 && !is_null_sha1(old_sha1))
317 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); 315 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
318 else 316 else
319 ret = diff_root_tree_sha1(new_sha1, "", &opt); 317 ret = diff_root_tree_sha1(new_sha1, "", &opt);
320 diffcore_std(&opt); 318 diffcore_std(&opt);
321 diff_flush(&opt); 319 diff_flush(&opt);
322} 320}
323 321
324void cgit_diff_commit(struct commit *commit, filepair_fn fn) 322void cgit_diff_commit(struct commit *commit, filepair_fn fn)
325{ 323{
326 unsigned char *old_sha1 = NULL; 324 unsigned char *old_sha1 = NULL;
327 325
328 if (commit->parents) 326 if (commit->parents)
329 old_sha1 = commit->parents->item->object.sha1; 327 old_sha1 = commit->parents->item->object.sha1;
330 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); 328 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
331} 329}
332 330
333int cgit_parse_snapshots_mask(const char *str) 331int cgit_parse_snapshots_mask(const char *str)
334{ 332{
335 const struct cgit_snapshot_format *f; 333 const struct cgit_snapshot_format *f;
336 static const char *delim = " \t,:/|;"; 334 static const char *delim = " \t,:/|;";
337 int tl, sl, rv = 0; 335 int tl, sl, rv = 0;
338 336
339 /* favor legacy setting */ 337 /* favor legacy setting */
340 if(atoi(str)) 338 if(atoi(str))
341 return 1; 339 return 1;
342 for(;;) { 340 for(;;) {
343 str += strspn(str,delim); 341 str += strspn(str,delim);
344 tl = strcspn(str,delim); 342 tl = strcspn(str,delim);
345 if (!tl) 343 if (!tl)
346 break; 344 break;
347 for (f = cgit_snapshot_formats; f->suffix; f++) { 345 for (f = cgit_snapshot_formats; f->suffix; f++) {
348 sl = strlen(f->suffix); 346 sl = strlen(f->suffix);
349 if((tl == sl && !strncmp(f->suffix, str, tl)) || 347 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
350 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { 348 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
351 rv |= f->bit; 349 rv |= f->bit;
352 break; 350 break;
353 } 351 }
354 } 352 }
355 str += tl; 353 str += tl;
356 } 354 }
357 return rv; 355 return rv;
358} 356}