Skip to content

Commit 479ab76

Browse files
peffttaylorr
authored andcommitted
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 <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 4d99559 commit 479ab76

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

builtin/pack-objects.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ static int want_object_in_pack_one(struct packed_git *p,
15561556
if (p == *found_pack)
15571557
offset = *found_offset;
15581558
else
1559-
offset = find_pack_entry_one(oid->hash, p);
1559+
offset = find_pack_entry_one(oid, p);
15601560

15611561
if (offset) {
15621562
if (!*found_pack) {
@@ -3984,7 +3984,7 @@ static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
39843984
while (p) {
39853985
if ((!p->pack_local || p->pack_keep ||
39863986
p->pack_keep_in_core) &&
3987-
find_pack_entry_one(oid->hash, p)) {
3987+
find_pack_entry_one(oid, p)) {
39883988
last_found = p;
39893989
return 1;
39903990
}

connected.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
7878
for (p = get_all_packs(the_repository); p; p = p->next) {
7979
if (!p->pack_promisor)
8080
continue;
81-
if (find_pack_entry_one(oid->hash, p))
81+
if (find_pack_entry_one(oid, p))
8282
goto promisor_pack_found;
8383
}
8484
/*
@@ -144,7 +144,7 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
144144
* are sure the ref is good and not sending it to
145145
* rev-list for verification.
146146
*/
147-
if (new_pack && find_pack_entry_one(oid->hash, new_pack))
147+
if (new_pack && find_pack_entry_one(oid, new_pack))
148148
continue;
149149

150150
if (fprintf(rev_list_in, "%s\n", oid_to_hex(oid)) < 0)

midx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
987987
}
988988

989989
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);
991991

992992
if (m_offset != p_offset)
993993
midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),

pack-bitmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
935935
const struct object_id *oid)
936936
{
937937
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);
939939
if (!offset)
940940
return -1;
941941

@@ -1609,7 +1609,7 @@ static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
16091609
if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
16101610
return 1;
16111611
} 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)
16131613
return 1;
16141614
}
16151615
}

packfile.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,9 @@ off_t get_delta_base(struct packed_git *p,
12391239
*curpos += used;
12401240
} else if (type == OBJ_REF_DELTA) {
12411241
/* 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);
12431245
*curpos += the_hash_algo->rawsz;
12441246
} else
12451247
die("I am totally screwed");
@@ -1971,20 +1973,18 @@ off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
19711973
}
19721974
}
19731975

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)
19761978
{
19771979
const unsigned char *index = p->index_data;
1978-
struct object_id oid;
19791980
uint32_t result;
19801981

19811982
if (!index) {
19821983
if (open_pack_index(p))
19831984
return 0;
19841985
}
19851986

1986-
hashcpy(oid.hash, sha1, the_repository->hash_algo);
1987-
if (bsearch_pack(&oid, p, &result))
1987+
if (bsearch_pack(oid, p, &result))
19881988
return nth_packed_object_offset(p, result);
19891989
return 0;
19901990
}
@@ -2016,7 +2016,7 @@ struct packed_git *find_oid_pack(const struct object_id *oid,
20162016
struct packed_git *p;
20172017

20182018
for (p = packs; p; p = p->next) {
2019-
if (find_pack_entry_one(oid->hash, p))
2019+
if (find_pack_entry_one(oid, p))
20202020
return p;
20212021
}
20222022
return NULL;
@@ -2033,7 +2033,7 @@ static int fill_pack_entry(const struct object_id *oid,
20332033
oidset_contains(&p->bad_objects, oid))
20342034
return 0;
20352035

2036-
offset = find_pack_entry_one(oid->hash, p);
2036+
offset = find_pack_entry_one(oid, p);
20372037
if (!offset)
20382038
return 0;
20392039

packfile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ int nth_packed_object_id(struct object_id *, struct packed_git *, uint32_t n);
154154
off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
155155

156156
/*
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,
158158
* return its offset within the packfile; otherwise, return 0.
159159
*/
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 *);
161161

162162
int is_pack_valid(struct packed_git *);
163163
void *unpack_entry(struct repository *r, struct packed_git *, off_t, enum object_type *, unsigned long *);

t/helper/test-find-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int cmd__find_pack(int argc, const char **argv)
4040
die("cannot parse %s as an object name", argv[0]);
4141

4242
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)) {
4444
printf("%s\n", p->pack_name);
4545
actual_count++;
4646
}

0 commit comments

Comments
 (0)