summaryrefslogtreecommitdiffabout
path: root/shared.c
authorLars Hjemli <hjemli@gmail.com>2008-03-24 15:00:27 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-03-24 15:00:27 (UTC)
commitf34478cbe0214a201e7ecef3e79ed6c957b7beee (patch) (unidiff)
tree1ee05da742488cab51a06e083d26b7b58d829b43 /shared.c
parente0e4478e7b4812f822d60a13a33525f8e529e1e8 (diff)
downloadcgit-f34478cbe0214a201e7ecef3e79ed6c957b7beee.zip
cgit-f34478cbe0214a201e7ecef3e79ed6c957b7beee.tar.gz
cgit-f34478cbe0214a201e7ecef3e79ed6c957b7beee.tar.bz2
Refactor snapshot support
The snapshot support needs to be split between output- and config-related functions to get the layering between shared.c and ui-*.c right. There is also some codestyle-issues which needs fixing to make the snapshot functions more similar to the rest of the cgit code. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'shared.c') (more/less context) (ignore whitespace changes)
-rw-r--r--shared.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/shared.c b/shared.c
index 67eb67b..800c06a 100644
--- a/shared.c
+++ b/shared.c
@@ -290,192 +290,219 @@ char *strlpart(char *txt, int maxlen)
290 result[maxlen] = '\0'; 290 result[maxlen] = '\0';
291 return result; 291 return result;
292} 292}
293 293
294char *strrpart(char *txt, int maxlen) 294char *strrpart(char *txt, int maxlen)
295{ 295{
296 char *result; 296 char *result;
297 297
298 if (!txt) 298 if (!txt)
299 return txt; 299 return txt;
300 300
301 if (strlen(txt) <= maxlen) 301 if (strlen(txt) <= maxlen)
302 return txt; 302 return txt;
303 result = xmalloc(maxlen + 1); 303 result = xmalloc(maxlen + 1);
304 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3); 304 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
305 result[0] = result[1] = result[2] = '.'; 305 result[0] = result[1] = result[2] = '.';
306 return result; 306 return result;
307} 307}
308 308
309void cgit_add_ref(struct reflist *list, struct refinfo *ref) 309void cgit_add_ref(struct reflist *list, struct refinfo *ref)
310{ 310{
311 size_t size; 311 size_t size;
312 312
313 if (list->count >= list->alloc) { 313 if (list->count >= list->alloc) {
314 list->alloc += (list->alloc ? list->alloc : 4); 314 list->alloc += (list->alloc ? list->alloc : 4);
315 size = list->alloc * sizeof(struct refinfo *); 315 size = list->alloc * sizeof(struct refinfo *);
316 list->refs = xrealloc(list->refs, size); 316 list->refs = xrealloc(list->refs, size);
317 } 317 }
318 list->refs[list->count++] = ref; 318 list->refs[list->count++] = ref;
319} 319}
320 320
321struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1) 321struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
322{ 322{
323 struct refinfo *ref; 323 struct refinfo *ref;
324 324
325 ref = xmalloc(sizeof (struct refinfo)); 325 ref = xmalloc(sizeof (struct refinfo));
326 ref->refname = xstrdup(refname); 326 ref->refname = xstrdup(refname);
327 ref->object = parse_object(sha1); 327 ref->object = parse_object(sha1);
328 switch (ref->object->type) { 328 switch (ref->object->type) {
329 case OBJ_TAG: 329 case OBJ_TAG:
330 ref->tag = cgit_parse_tag((struct tag *)ref->object); 330 ref->tag = cgit_parse_tag((struct tag *)ref->object);
331 break; 331 break;
332 case OBJ_COMMIT: 332 case OBJ_COMMIT:
333 ref->commit = cgit_parse_commit((struct commit *)ref->object); 333 ref->commit = cgit_parse_commit((struct commit *)ref->object);
334 break; 334 break;
335 } 335 }
336 return ref; 336 return ref;
337} 337}
338 338
339int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags, 339int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
340 void *cb_data) 340 void *cb_data)
341{ 341{
342 struct reflist *list = (struct reflist *)cb_data; 342 struct reflist *list = (struct reflist *)cb_data;
343 struct refinfo *info = cgit_mk_refinfo(refname, sha1); 343 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
344 344
345 if (info) 345 if (info)
346 cgit_add_ref(list, info); 346 cgit_add_ref(list, info);
347 return 0; 347 return 0;
348} 348}
349 349
350void cgit_diff_tree_cb(struct diff_queue_struct *q, 350void cgit_diff_tree_cb(struct diff_queue_struct *q,
351 struct diff_options *options, void *data) 351 struct diff_options *options, void *data)
352{ 352{
353 int i; 353 int i;
354 354
355 for (i = 0; i < q->nr; i++) { 355 for (i = 0; i < q->nr; i++) {
356 if (q->queue[i]->status == 'U') 356 if (q->queue[i]->status == 'U')
357 continue; 357 continue;
358 ((filepair_fn)data)(q->queue[i]); 358 ((filepair_fn)data)(q->queue[i]);
359 } 359 }
360} 360}
361 361
362static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 362static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
363{ 363{
364 enum object_type type; 364 enum object_type type;
365 365
366 if (is_null_sha1(sha1)) { 366 if (is_null_sha1(sha1)) {
367 file->ptr = (char *)""; 367 file->ptr = (char *)"";
368 file->size = 0; 368 file->size = 0;
369 } else { 369 } else {
370 file->ptr = read_sha1_file(sha1, &type, 370 file->ptr = read_sha1_file(sha1, &type,
371 (unsigned long *)&file->size); 371 (unsigned long *)&file->size);
372 } 372 }
373 return 1; 373 return 1;
374} 374}
375 375
376/* 376/*
377 * Receive diff-buffers from xdiff and concatenate them as 377 * Receive diff-buffers from xdiff and concatenate them as
378 * needed across multiple callbacks. 378 * needed across multiple callbacks.
379 * 379 *
380 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 380 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
381 * ripped from git and modified to use globals instead of 381 * ripped from git and modified to use globals instead of
382 * a special callback-struct. 382 * a special callback-struct.
383 */ 383 */
384char *diffbuf = NULL; 384char *diffbuf = NULL;
385int buflen = 0; 385int buflen = 0;
386 386
387int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf) 387int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
388{ 388{
389 int i; 389 int i;
390 390
391 for (i = 0; i < nbuf; i++) { 391 for (i = 0; i < nbuf; i++) {
392 if (mb[i].ptr[mb[i].size-1] != '\n') { 392 if (mb[i].ptr[mb[i].size-1] != '\n') {
393 /* Incomplete line */ 393 /* Incomplete line */
394 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 394 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
395 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 395 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
396 buflen += mb[i].size; 396 buflen += mb[i].size;
397 continue; 397 continue;
398 } 398 }
399 399
400 /* we have a complete line */ 400 /* we have a complete line */
401 if (!diffbuf) { 401 if (!diffbuf) {
402 ((linediff_fn)priv)(mb[i].ptr, mb[i].size); 402 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
403 continue; 403 continue;
404 } 404 }
405 diffbuf = xrealloc(diffbuf, buflen + mb[i].size); 405 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
406 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size); 406 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
407 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size); 407 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
408 free(diffbuf); 408 free(diffbuf);
409 diffbuf = NULL; 409 diffbuf = NULL;
410 buflen = 0; 410 buflen = 0;
411 } 411 }
412 if (diffbuf) { 412 if (diffbuf) {
413 ((linediff_fn)priv)(diffbuf, buflen); 413 ((linediff_fn)priv)(diffbuf, buflen);
414 free(diffbuf); 414 free(diffbuf);
415 diffbuf = NULL; 415 diffbuf = NULL;
416 buflen = 0; 416 buflen = 0;
417 } 417 }
418 return 0; 418 return 0;
419} 419}
420 420
421int cgit_diff_files(const unsigned char *old_sha1, 421int cgit_diff_files(const unsigned char *old_sha1,
422 const unsigned char *new_sha1, 422 const unsigned char *new_sha1,
423 linediff_fn fn) 423 linediff_fn fn)
424{ 424{
425 mmfile_t file1, file2; 425 mmfile_t file1, file2;
426 xpparam_t diff_params; 426 xpparam_t diff_params;
427 xdemitconf_t emit_params; 427 xdemitconf_t emit_params;
428 xdemitcb_t emit_cb; 428 xdemitcb_t emit_cb;
429 429
430 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1)) 430 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
431 return 1; 431 return 1;
432 432
433 diff_params.flags = XDF_NEED_MINIMAL; 433 diff_params.flags = XDF_NEED_MINIMAL;
434 emit_params.ctxlen = 3; 434 emit_params.ctxlen = 3;
435 emit_params.flags = XDL_EMIT_FUNCNAMES; 435 emit_params.flags = XDL_EMIT_FUNCNAMES;
436 emit_params.find_func = NULL; 436 emit_params.find_func = NULL;
437 emit_cb.outf = filediff_cb; 437 emit_cb.outf = filediff_cb;
438 emit_cb.priv = fn; 438 emit_cb.priv = fn;
439 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); 439 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
440 return 0; 440 return 0;
441} 441}
442 442
443void cgit_diff_tree(const unsigned char *old_sha1, 443void cgit_diff_tree(const unsigned char *old_sha1,
444 const unsigned char *new_sha1, 444 const unsigned char *new_sha1,
445 filepair_fn fn, const char *prefix) 445 filepair_fn fn, const char *prefix)
446{ 446{
447 struct diff_options opt; 447 struct diff_options opt;
448 int ret; 448 int ret;
449 int prefixlen; 449 int prefixlen;
450 450
451 diff_setup(&opt); 451 diff_setup(&opt);
452 opt.output_format = DIFF_FORMAT_CALLBACK; 452 opt.output_format = DIFF_FORMAT_CALLBACK;
453 opt.detect_rename = 1; 453 opt.detect_rename = 1;
454 opt.rename_limit = ctx.cfg.renamelimit; 454 opt.rename_limit = ctx.cfg.renamelimit;
455 DIFF_OPT_SET(&opt, RECURSIVE); 455 DIFF_OPT_SET(&opt, RECURSIVE);
456 opt.format_callback = cgit_diff_tree_cb; 456 opt.format_callback = cgit_diff_tree_cb;
457 opt.format_callback_data = fn; 457 opt.format_callback_data = fn;
458 if (prefix) { 458 if (prefix) {
459 opt.nr_paths = 1; 459 opt.nr_paths = 1;
460 opt.paths = &prefix; 460 opt.paths = &prefix;
461 prefixlen = strlen(prefix); 461 prefixlen = strlen(prefix);
462 opt.pathlens = &prefixlen; 462 opt.pathlens = &prefixlen;
463 } 463 }
464 diff_setup_done(&opt); 464 diff_setup_done(&opt);
465 465
466 if (old_sha1 && !is_null_sha1(old_sha1)) 466 if (old_sha1 && !is_null_sha1(old_sha1))
467 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt); 467 ret = diff_tree_sha1(old_sha1, new_sha1, "", &opt);
468 else 468 else
469 ret = diff_root_tree_sha1(new_sha1, "", &opt); 469 ret = diff_root_tree_sha1(new_sha1, "", &opt);
470 diffcore_std(&opt); 470 diffcore_std(&opt);
471 diff_flush(&opt); 471 diff_flush(&opt);
472} 472}
473 473
474void cgit_diff_commit(struct commit *commit, filepair_fn fn) 474void cgit_diff_commit(struct commit *commit, filepair_fn fn)
475{ 475{
476 unsigned char *old_sha1 = NULL; 476 unsigned char *old_sha1 = NULL;
477 477
478 if (commit->parents) 478 if (commit->parents)
479 old_sha1 = commit->parents->item->object.sha1; 479 old_sha1 = commit->parents->item->object.sha1;
480 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); 480 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
481} 481}
482
483int cgit_parse_snapshots_mask(const char *str)
484{
485 const struct cgit_snapshot_format *f;
486 static const char *delim = " \t,:/|;";
487 int tl, sl, rv = 0;
488
489 /* favor legacy setting */
490 if(atoi(str))
491 return 1;
492 for(;;) {
493 str += strspn(str,delim);
494 tl = strcspn(str,delim);
495 if (!tl)
496 break;
497 for (f = cgit_snapshot_formats; f->suffix; f++) {
498 sl = strlen(f->suffix);
499 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
500 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
501 rv |= f->bit;
502 break;
503 }
504 }
505 str += tl;
506 }
507 return rv;
508}