summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2021-06-22 16:06:41 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-06-29 03:30:18 (GMT)
commit7463064b28086c0a765e247bc8336f8e32356494 (patch)
tree1d55d80344db95b9876e80537022563166ffb8a1 /object.c
parent542d6abbb4e838cf1c47612a2b4b33f2a31e6277 (diff)
downloadgit-7463064b28086c0a765e247bc8336f8e32356494.zip
git-7463064b28086c0a765e247bc8336f8e32356494.tar.gz
git-7463064b28086c0a765e247bc8336f8e32356494.tar.bz2
object.h: add lookup_object_by_type() function
In some cases it's useful for efficiency reasons to get the type of an object before deciding whether to parse it, but we still want an object struct. E.g., in reachable.c, bitmaps give us the type, but we just want to mark flags on each object. Likewise, we may loop over every object and only parse tags in order to peel them; checking the type first lets us avoid parsing the non-tags. But our lookup_blob(), etc, functions make getting an object struct annoying: we have to call the right function for every type. And we cannot just use the generic lookup_object(), because it only returns an already-seen object; it won't allocate a new object struct. Let's provide a function that dispatches to the correct lookup_* function based on a run-time type. In fact, reachable.c already has such a helper, so we'll just make that public. I did change the return type from "void *" to "struct object *". While the former is a clever way to avoid casting inside the function, it's less safe and less informative to people reading the function declaration. The next commit will add a new caller. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'object.c')
-rw-r--r--object.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/object.c b/object.c
index 1418845..07fcf23 100644
--- a/object.c
+++ b/object.c
@@ -185,6 +185,24 @@ struct object *lookup_unknown_object(struct repository *r, const struct object_i
return obj;
}
+struct object *lookup_object_by_type(struct repository *r,
+ const struct object_id *oid,
+ enum object_type type)
+{
+ switch (type) {
+ case OBJ_COMMIT:
+ return (struct object *)lookup_commit(r, oid);
+ case OBJ_TREE:
+ return (struct object *)lookup_tree(r, oid);
+ case OBJ_TAG:
+ return (struct object *)lookup_tag(r, oid);
+ case OBJ_BLOB:
+ return (struct object *)lookup_blob(r, oid);
+ default:
+ die("BUG: unknown object type %d", type);
+ }
+}
+
struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
{
struct object *obj;