Skip to content

Commit 7463064

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 542d6ab commit 7463064

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

object.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,24 @@ struct object *lookup_unknown_object(struct repository *r, const struct object_i
185185
return obj;
186186
}
187187

188+
struct object *lookup_object_by_type(struct repository *r,
189+
const struct object_id *oid,
190+
enum object_type type)
191+
{
192+
switch (type) {
193+
case OBJ_COMMIT:
194+
return (struct object *)lookup_commit(r, oid);
195+
case OBJ_TREE:
196+
return (struct object *)lookup_tree(r, oid);
197+
case OBJ_TAG:
198+
return (struct object *)lookup_tag(r, oid);
199+
case OBJ_BLOB:
200+
return (struct object *)lookup_blob(r, oid);
201+
default:
202+
die("BUG: unknown object type %d", type);
203+
}
204+
}
205+
188206
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)
189207
{
190208
struct object *obj;

object.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ struct object *parse_object_buffer(struct repository *r, const struct object_id
158158
*/
159159
struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid);
160160

161+
/*
162+
* Dispatch to the appropriate lookup_blob(), lookup_commit(), etc, based on
163+
* "type".
164+
*/
165+
struct object *lookup_object_by_type(struct repository *r, const struct object_id *oid,
166+
enum object_type type);
167+
161168
struct object_list *object_list_insert(struct object *item,
162169
struct object_list **list_p);
163170

reachable.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,6 @@ int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
159159
FOR_EACH_OBJECT_LOCAL_ONLY);
160160
}
161161

162-
static void *lookup_object_by_type(struct repository *r,
163-
const struct object_id *oid,
164-
enum object_type type)
165-
{
166-
switch (type) {
167-
case OBJ_COMMIT:
168-
return lookup_commit(r, oid);
169-
case OBJ_TREE:
170-
return lookup_tree(r, oid);
171-
case OBJ_TAG:
172-
return lookup_tag(r, oid);
173-
case OBJ_BLOB:
174-
return lookup_blob(r, oid);
175-
default:
176-
die("BUG: unknown object type %d", type);
177-
}
178-
}
179-
180162
static int mark_object_seen(const struct object_id *oid,
181163
enum object_type type,
182164
int exclude,

0 commit comments

Comments
 (0)