@matheusbsbs / matheus-bagliano / commits / 8177631547
Fork 0

expose a helper function peel_to_type().

This helper function is the core of "$object^{type}" parser. Now it is made available to callers outside sha1_name.c

Junio C Hamano committed Dec 24, 2007 at 00:51 UTC 8177631547e4d10ae071def07eff83e056092bfa
2 files changed +41 -19
cache.h
+3
@@ -499,6 +499,9 @@ extern void *read_object_with_reference(const unsigned char *sha1,
499 unsigned long *size,
500 unsigned char *sha1_ret);
501
502 +extern struct object *peel_to_type(const char *name, int namelen,
503 + struct object *o, enum object_type);
504 +
505 enum date_mode {
506 DATE_NORMAL = 0,
507 DATE_RELATIVE,
sha1_name.c
+38 -19
@@ -423,6 +423,37 @@ static int get_nth_ancestor(const char *name, int len,
423 return 0;
424 }
425
426 +struct object *peel_to_type(const char *name, int namelen,
427 + struct object *o, enum object_type expected_type)
428 +{
429 + if (name && !namelen)
430 + namelen = strlen(name);
431 + if (!o) {
432 + unsigned char sha1[20];
433 + if (get_sha1_1(name, namelen, sha1))
434 + return NULL;
435 + o = parse_object(sha1);
436 + }
437 + while (1) {
438 + if (!o || (!o->parsed && !parse_object(o->sha1)))
439 + return NULL;
440 + if (o->type == expected_type)
441 + return o;
442 + if (o->type == OBJ_TAG)
443 + o = ((struct tag*) o)->tagged;
444 + else if (o->type == OBJ_COMMIT)
445 + o = &(((struct commit *) o)->tree->object);
446 + else {
447 + if (name)
448 + error("%.*s: expected %s type, but the object "
449 + "dereferences to %s type",
450 + namelen, name, typename(expected_type),
451 + typename(o->type));
452 + return NULL;
453 + }
454 + }
455 +}
456 +
457 static int peel_onion(const char *name, int len, unsigned char *sha1)
458 {
459 unsigned char outer[20];
@@ -474,29 +505,17 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
505 hashcpy(sha1, o->sha1);
506 }
507 else {
477 - /* At this point, the syntax look correct, so
508 + /*
509 + * At this point, the syntax look correct, so
510 * if we do not get the needed object, we should
511 * barf.
512 */
481 -
482 - while (1) {
483 - if (!o || (!o->parsed && !parse_object(o->sha1)))
484 - return -1;
485 - if (o->type == expected_type) {
486 - hashcpy(sha1, o->sha1);
487 - return 0;
488 - }
489 - if (o->type == OBJ_TAG)
490 - o = ((struct tag*) o)->tagged;
491 - else if (o->type == OBJ_COMMIT)
492 - o = &(((struct commit *) o)->tree->object);
493 - else
494 - return error("%.*s: expected %s type, but the object dereferences to %s type",
495 - len, name, typename(expected_type),
496 - typename(o->type));
497 - if (!o->parsed)
498 - parse_object(o->sha1);
513 + o = peel_to_type(name, len, o, expected_type);
514 + if (o) {
515 + hashcpy(sha1, o->sha1);
516 + return 0;
517 }
518 + return -1;
519 }
520 return 0;
521 }