diff options
author | Junio C Hamano <gitster@pobox.com> | 2008-03-11 02:05:12 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2008-03-11 02:05:12 -0700 |
commit | 5d921e2931e5041884d8f86fdccc9004a7b071cc (patch) | |
tree | deedbb6c40af5a3cceedf01ce0f315217a39fa16 /sha1_name.c | |
parent | 1c536069780bc67c05e621f0485f787d04559f24 (diff) | |
parent | 8177631547e4d10ae071def07eff83e056092bfa (diff) | |
download | git-5d921e2931e5041884d8f86fdccc9004a7b071cc.tar.gz |
Merge branch 'jc/cherry-pick' (early part)
* 'jc/cherry-pick' (early part):
expose a helper function peel_to_type().
merge-recursive: split low-level merge functions out.
Conflicts:
Makefile
builtin-merge-recursive.c
sha1_name.c
Diffstat (limited to 'sha1_name.c')
-rw-r--r-- | sha1_name.c | 60 |
1 files changed, 38 insertions, 22 deletions
diff --git a/sha1_name.c b/sha1_name.c index 8358ba2069..8b6c76f68e 100644 --- a/sha1_name.c +++ b/sha1_name.c @@ -422,6 +422,37 @@ static int get_nth_ancestor(const char *name, int len, return 0; } +struct object *peel_to_type(const char *name, int namelen, + struct object *o, enum object_type expected_type) +{ + if (name && !namelen) + namelen = strlen(name); + if (!o) { + unsigned char sha1[20]; + if (get_sha1_1(name, namelen, sha1)) + return NULL; + o = parse_object(sha1); + } + while (1) { + if (!o || (!o->parsed && !parse_object(o->sha1))) + return NULL; + if (o->type == expected_type) + return o; + if (o->type == OBJ_TAG) + o = ((struct tag*) o)->tagged; + else if (o->type == OBJ_COMMIT) + o = &(((struct commit *) o)->tree->object); + else { + if (name) + error("%.*s: expected %s type, but the object " + "dereferences to %s type", + namelen, name, typename(expected_type), + typename(o->type)); + return NULL; + } + } +} + static int peel_onion(const char *name, int len, unsigned char *sha1) { unsigned char outer[20]; @@ -473,32 +504,17 @@ static int peel_onion(const char *name, int len, unsigned char *sha1) hashcpy(sha1, o->sha1); } else { - /* At this point, the syntax look correct, so + /* + * At this point, the syntax look correct, so * if we do not get the needed object, we should * barf. */ - - while (1) { - if (!o || (!o->parsed && !parse_object(o->sha1))) - return -1; - if (o->type == expected_type) { - hashcpy(sha1, o->sha1); - return 0; - } - if (o->type == OBJ_TAG) - o = ((struct tag*) o)->tagged; - else if (o->type == OBJ_COMMIT) - o = &(((struct commit *) o)->tree->object); - else - return error("%.*s: expected %s type, but the object dereferences to %s type", - len, name, typename(expected_type), - typename(o->type)); - if (!o) - return -1; - if (!o->parsed) - if (!parse_object(o->sha1)) - return -1; + o = peel_to_type(name, len, o, expected_type); + if (o) { + hashcpy(sha1, o->sha1); + return 0; } + return -1; } return 0; } |