]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object.h: add lookup_object_by_type() function
authorJeff King <peff@peff.net>
Tue, 22 Jun 2021 16:06:41 +0000 (12:06 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 29 Jun 2021 03:30:18 +0000 (20:30 -0700)
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>
object.c
object.h
reachable.c

index 14188453c56706e0c364c995946bdc16d3d97fea..07fcf23d7b322f88b063b2c6789c98a9602ed04b 100644 (file)
--- 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;
index eb7e481c39ad0637b2c4fd338e773da977857b80..3b38c9cc988b8fcc9bd2600ce53ed74614a24b19 100644 (file)
--- a/object.h
+++ b/object.h
@@ -158,6 +158,13 @@ struct object *parse_object_buffer(struct repository *r, const struct object_id
  */
 struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid);
 
+/*
+ * Dispatch to the appropriate lookup_blob(), lookup_commit(), etc, based on
+ * "type".
+ */
+struct object *lookup_object_by_type(struct repository *r, const struct object_id *oid,
+                                    enum object_type type);
+
 struct object_list *object_list_insert(struct object *item,
                                       struct object_list **list_p);
 
index c59847257a5209d0d011e80b45898aac1a08f66a..84e3d0d75ed05fbae051bb031a5e54d600def1e0 100644 (file)
@@ -159,24 +159,6 @@ int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
                                      FOR_EACH_OBJECT_LOCAL_ONLY);
 }
 
-static void *lookup_object_by_type(struct repository *r,
-                                  const struct object_id *oid,
-                                  enum object_type type)
-{
-       switch (type) {
-       case OBJ_COMMIT:
-               return lookup_commit(r, oid);
-       case OBJ_TREE:
-               return lookup_tree(r, oid);
-       case OBJ_TAG:
-               return lookup_tag(r, oid);
-       case OBJ_BLOB:
-               return lookup_blob(r, oid);
-       default:
-               die("BUG: unknown object type %d", type);
-       }
-}
-
 static int mark_object_seen(const struct object_id *oid,
                             enum object_type type,
                             int exclude,