summaryrefslogtreecommitdiffabout
path: root/shared.c
authorLars Hjemli <hjemli@gmail.com>2007-10-25 07:30:06 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2007-10-27 07:34:15 (UTC)
commite397ff7024293223f48f235fcf072fc526cae7af (patch) (unidiff)
treecc88ee930bfc40dc1ebaad237345f79ad1085386 /shared.c
parent47bae9f58d5ecae437767b8e7835b23ad1804d0b (diff)
downloadcgit-e397ff7024293223f48f235fcf072fc526cae7af.zip
cgit-e397ff7024293223f48f235fcf072fc526cae7af.tar.gz
cgit-e397ff7024293223f48f235fcf072fc526cae7af.tar.bz2
Add functions and types for ref lists
This adds two structs, refinfo and reflist, and functions for building a list of refs. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--shared.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/shared.c b/shared.c
index 3d4feea..d815cb1 100644
--- a/shared.c
+++ b/shared.c
@@ -246,96 +246,137 @@ void cgit_querystring_cb(const char *name, const char *value)
246 } 246 }
247} 247}
248 248
249void *cgit_free_commitinfo(struct commitinfo *info) 249void *cgit_free_commitinfo(struct commitinfo *info)
250{ 250{
251 free(info->author); 251 free(info->author);
252 free(info->author_email); 252 free(info->author_email);
253 free(info->committer); 253 free(info->committer);
254 free(info->committer_email); 254 free(info->committer_email);
255 free(info->subject); 255 free(info->subject);
256 free(info); 256 free(info);
257 return NULL; 257 return NULL;
258} 258}
259 259
260int hextoint(char c) 260int hextoint(char c)
261{ 261{
262 if (c >= 'a' && c <= 'f') 262 if (c >= 'a' && c <= 'f')
263 return 10 + c - 'a'; 263 return 10 + c - 'a';
264 else if (c >= 'A' && c <= 'F') 264 else if (c >= 'A' && c <= 'F')
265 return 10 + c - 'A'; 265 return 10 + c - 'A';
266 else if (c >= '0' && c <= '9') 266 else if (c >= '0' && c <= '9')
267 return c - '0'; 267 return c - '0';
268 else 268 else
269 return -1; 269 return -1;
270} 270}
271 271
272char *trim_end(const char *str, char c) 272char *trim_end(const char *str, char c)
273{ 273{
274 int len; 274 int len;
275 char *s, *t; 275 char *s, *t;
276 276
277 if (str == NULL) 277 if (str == NULL)
278 return NULL; 278 return NULL;
279 t = (char *)str; 279 t = (char *)str;
280 len = strlen(t); 280 len = strlen(t);
281 while(len > 0 && t[len - 1] == c) 281 while(len > 0 && t[len - 1] == c)
282 len--; 282 len--;
283 283
284 if (len == 0) 284 if (len == 0)
285 return NULL; 285 return NULL;
286 286
287 c = t[len]; 287 c = t[len];
288 t[len] = '\0'; 288 t[len] = '\0';
289 s = xstrdup(t); 289 s = xstrdup(t);
290 t[len] = c; 290 t[len] = c;
291 return s; 291 return s;
292} 292}
293 293
294void cgit_add_ref(struct reflist *list, struct refinfo *ref)
295{
296 size_t size;
297
298 if (list->count >= list->alloc) {
299 list->alloc += (list->alloc ? list->alloc : 4);
300 size = list->alloc * sizeof(struct refinfo *);
301 list->refs = xrealloc(list->refs, size);
302 }
303 list->refs[list->count++] = ref;
304}
305
306struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
307{
308 struct refinfo *ref;
309
310 ref = xmalloc(sizeof (struct refinfo));
311 ref->refname = xstrdup(refname);
312 ref->object = parse_object(sha1);
313 switch (ref->object->type) {
314 case OBJ_TAG:
315 ref->tag = cgit_parse_tag((struct tag *)ref->object);
316 break;
317 case OBJ_COMMIT:
318 ref->commit = cgit_parse_commit((struct commit *)ref->object);
319 break;
320 }
321 return ref;
322}
323
324int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
325 void *cb_data)
326{
327 struct reflist *list = (struct reflist *)cb_data;
328 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
329
330 if (info)
331 cgit_add_ref(list, info);
332 return 0;
333}
334
294void cgit_diff_tree_cb(struct diff_queue_struct *q, 335void cgit_diff_tree_cb(struct diff_queue_struct *q,
295 struct diff_options *options, void *data) 336 struct diff_options *options, void *data)
296{ 337{
297 int i; 338 int i;
298 339
299 for (i = 0; i < q->nr; i++) { 340 for (i = 0; i < q->nr; i++) {
300 if (q->queue[i]->status == 'U') 341 if (q->queue[i]->status == 'U')
301 continue; 342 continue;
302 ((filepair_fn)data)(q->queue[i]); 343 ((filepair_fn)data)(q->queue[i]);
303 } 344 }
304} 345}
305 346
306static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 347static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
307{ 348{
308 enum object_type type; 349 enum object_type type;
309 350
310 if (is_null_sha1(sha1)) { 351 if (is_null_sha1(sha1)) {
311 file->ptr = (char *)""; 352 file->ptr = (char *)"";
312 file->size = 0; 353 file->size = 0;
313 } else { 354 } else {
314 file->ptr = read_sha1_file(sha1, &type, 355 file->ptr = read_sha1_file(sha1, &type,
315 (unsigned long *)&file->size); 356 (unsigned long *)&file->size);
316 } 357 }
317 return 1; 358 return 1;
318} 359}
319 360
320/* 361/*
321 * Receive diff-buffers from xdiff and concatenate them as 362 * Receive diff-buffers from xdiff and concatenate them as
322 * needed across multiple callbacks. 363 * needed across multiple callbacks.
323 * 364 *
324 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 365 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
325 * ripped from git and modified to use globals instead of 366 * ripped from git and modified to use globals instead of
326 * a special callback-struct. 367 * a special callback-struct.
327 */ 368 */
328char *diffbuf = NULL; 369char *diffbuf = NULL;
329int buflen = 0; 370int buflen = 0;
330 371
331int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) 372int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
332{ 373{
333 int i; 374 int i;
334 375
335 for (i = 0; i < nbuf; i++) { 376 for (i = 0; i < nbuf; i++) {
336 if (mb[i].ptr[mb[i].size-1] != '\n') { 377 if (mb[i].ptr[mb[i].size-1] != '\n') {
337 /* Incomplete line */ 378 /* Incomplete line */
338 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 379 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
339 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 380 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
340 buflen += mb[i].size; 381 buflen += mb[i].size;
341 continue; 382 continue;