packfile: use object_id in find_pack_entry_one()

The main function we use to search a pack index for an object is find_pack_entry_one(). That function still takes a bare pointer to the hash, despite the fact that its underlying bsearch_pack() function needs an object_id struct. And so we end up making an extra copy of the hash into the struct just to do a lookup. As it turns out, all callers but one already have such an object_id. So we can just take a pointer to that struct and use it directly. This avoids the extra copy and provides a more type-safe interface. The one exception is get_delta_base() in packfile.c, when we are chasing a REF_DELTA from inside the pack (and thus we have a pointer directly to the mmap'd pack memory, not a struct). We can just bump the hashcpy() from inside find_pack_entry_one() to this one caller that needs it. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Jeff King committed Oct 25, 2024 at 03:06 UTC 479ab76c9ffbd35585a1506ac5c99fe218df70b9
7 files changed +18 -18
builtin/pack-objects.c
+2 -2
@@ -1556,7 +1556,7 @@ static int want_object_in_pack_one(struct packed_git *p,
1556 if (p == *found_pack)
1557 offset = *found_offset;
1558 else
1559 - offset = find_pack_entry_one(oid->hash, p);
1559 + offset = find_pack_entry_one(oid, p);
1560
1561 if (offset) {
1562 if (!*found_pack) {
@@ -3984,7 +3984,7 @@ static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
3984 while (p) {
3985 if ((!p->pack_local || p->pack_keep ||
3986 p->pack_keep_in_core) &&
3987 - find_pack_entry_one(oid->hash, p)) {
3987 + find_pack_entry_one(oid, p)) {
3988 last_found = p;
3989 return 1;
3990 }
connected.c
+2 -2
@@ -78,7 +78,7 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
78 for (p = get_all_packs(the_repository); p; p = p->next) {
79 if (!p->pack_promisor)
80 continue;
81 - if (find_pack_entry_one(oid->hash, p))
81 + if (find_pack_entry_one(oid, p))
82 goto promisor_pack_found;
83 }
84 /*
@@ -144,7 +144,7 @@ no_promisor_pack_found:
144 * are sure the ref is good and not sending it to
145 * rev-list for verification.
146 */
147 - if (new_pack && find_pack_entry_one(oid->hash, new_pack))
147 + if (new_pack && find_pack_entry_one(oid, new_pack))
148 continue;
149
150 if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)
midx.c
+1 -1
@@ -987,7 +987,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
987 }
988
989 m_offset = e.offset;
990 - p_offset = find_pack_entry_one(oid.hash, e.p);
990 + p_offset = find_pack_entry_one(&oid, e.p);
991
992 if (m_offset != p_offset)
993 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
pack-bitmap.c
+2 -2
@@ -935,7 +935,7 @@ static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
935 const struct object_id *oid)
936 {
937 uint32_t pos;
938 - off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
938 + off_t offset = find_pack_entry_one(oid, bitmap_git->pack);
939 if (!offset)
940 return -1;
941
@@ -1609,7 +1609,7 @@ static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1609 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1610 return 1;
1611 } else {
1612 - if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1612 + if (find_pack_entry_one(&object->oid, bitmap_git->pack) > 0)
1613 return 1;
1614 }
1615 }
packfile.c
+8 -8
@@ -1239,7 +1239,9 @@ off_t get_delta_base(struct packed_git *p,
1239 *curpos += used;
1240 } else if (type == OBJ_REF_DELTA) {
1241 /* The base entry _must_ be in the same pack */
1242 - base_offset = find_pack_entry_one(base_info, p);
1242 + struct object_id oid;
1243 + hashcpy(oid.hash, base_info, the_repository->hash_algo);
1244 + base_offset = find_pack_entry_one(&oid, p);
1245 *curpos += the_hash_algo->rawsz;
1246 } else
1247 die("I am totally screwed");
@@ -1971,11 +1973,10 @@ off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1973 }
1974 }
1975
1974 -off_t find_pack_entry_one(const unsigned char *sha1,
1975 - struct packed_git *p)
1976 +off_t find_pack_entry_one(const struct object_id *oid,
1977 + struct packed_git *p)
1978 {
1979 const unsigned char *index = p->index_data;
1978 - struct object_id oid;
1980 uint32_t result;
1981
1982 if (!index) {
@@ -1983,8 +1984,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
1984 return 0;
1985 }
1986
1986 - hashcpy(oid.hash, sha1, the_repository->hash_algo);
1987 - if (bsearch_pack(&oid, p, &result))
1987 + if (bsearch_pack(oid, p, &result))
1988 return nth_packed_object_offset(p, result);
1989 return 0;
1990 }
@@ -2016,7 +2016,7 @@ struct packed_git *find_oid_pack(const struct object_id *oid,
2016 struct packed_git *p;
2017
2018 for (p = packs; p; p = p->next) {
2019 - if (find_pack_entry_one(oid->hash, p))
2019 + if (find_pack_entry_one(oid, p))
2020 return p;
2021 }
2022 return NULL;
@@ -2033,7 +2033,7 @@ static int fill_pack_entry(const struct object_id *oid,
2033 oidset_contains(&p->bad_objects, oid))
2034 return 0;
2035
2036 - offset = find_pack_entry_one(oid->hash, p);
2036 + offset = find_pack_entry_one(oid, p);
2037 if (!offset)
2038 return 0;
2039
packfile.h
+2 -2
@@ -154,10 +154,10 @@ int nth_packed_object_id(struct object_id *, struct packed_git *, uint32_t n);
154 off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
155
156 /*
157 - * If the object named sha1 is present in the specified packfile,
157 + * If the object named by oid is present in the specified packfile,
158 * return its offset within the packfile; otherwise, return 0.
159 */
160 -off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
160 +off_t find_pack_entry_one(const struct object_id *oid, struct packed_git *);
161
162 int is_pack_valid(struct packed_git *);
163 void *unpack_entry(struct repository *r, struct packed_git *, off_t, enum object_type *, unsigned long *);
t/helper/test-find-pack.c
+1 -1
@@ -40,7 +40,7 @@ int cmd__find_pack(int argc, const char **argv)
40 die("cannot parse %s as an object name", argv[0]);
41
42 for (p = get_all_packs(the_repository); p; p = p->next)
43 - if (find_pack_entry_one(oid.hash, p)) {
43 + if (find_pack_entry_one(&oid, p)) {
44 printf("%s\n", p->pack_name);
45 actual_count++;
46 }