Skip to content

Commit 068f85e

Browse files
bk2204gitster
authored andcommitted
sha1_file: introduce an nth_packed_object_oid function
There are places in the code where we would like to provide a struct object_id *, yet read the hash directly from the pack. Provide an nth_packed_object_oid function that is similar to the nth_packed_object_sha1 function. In order to avoid a potentially invalid cast, nth_packed_object_oid provides a variable into which to store the value, which it returns on success; on error, it returns NULL, as nth_packed_object_sha1 does. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 43bc3b6 commit 068f85e

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,12 @@ extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
16081608
* error.
16091609
*/
16101610
extern const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
1611+
/*
1612+
* Like nth_packed_object_sha1, but write the data into the object specified by
1613+
* the the first argument. Returns the first argument on success, and NULL on
1614+
* error.
1615+
*/
1616+
extern const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
16111617

16121618
/*
16131619
* Return the offset of the nth object within the specified packfile.

sha1_file.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,17 @@ const unsigned char *nth_packed_object_sha1(struct packed_git *p,
26282628
}
26292629
}
26302630

2631+
const struct object_id *nth_packed_object_oid(struct object_id *oid,
2632+
struct packed_git *p,
2633+
uint32_t n)
2634+
{
2635+
const unsigned char *hash = nth_packed_object_sha1(p, n);
2636+
if (!hash)
2637+
return NULL;
2638+
hashcpy(oid->hash, hash);
2639+
return oid;
2640+
}
2641+
26312642
void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
26322643
{
26332644
const unsigned char *ptr = vptr;
@@ -3788,13 +3799,13 @@ static int for_each_object_in_pack(struct packed_git *p, each_packed_object_fn c
37883799
int r = 0;
37893800

37903801
for (i = 0; i < p->num_objects; i++) {
3791-
const unsigned char *sha1 = nth_packed_object_sha1(p, i);
3802+
struct object_id oid;
37923803

3793-
if (!sha1)
3804+
if (!nth_packed_object_oid(&oid, p, i))
37943805
return error("unable to get sha1 of object %u in %s",
37953806
i, p->pack_name);
37963807

3797-
r = cb(sha1, p, i, data);
3808+
r = cb(oid.hash, p, i, data);
37983809
if (r)
37993810
break;
38003811
}

0 commit comments

Comments
 (0)