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>

brian m. carlson committed Feb 21, 2017 at 23:47 UTC 068f85e31311f521adffbcbf96f6583206d77b7d
2 files changed +20 -3
cache.h
+6
@@ -1608,6 +1608,12 @@ extern void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
1608 * error.
1609 */
1610 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);
1617
1618 /*
1619 * Return the offset of the nth object within the specified packfile.
sha1_file.c
+14 -3
@@ -2628,6 +2628,17 @@ const unsigned char *nth_packed_object_sha1(struct packed_git *p,
2628 }
2629 }
2630
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 +
2642 void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
2643 {
2644 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
3799 int r = 0;
3800
3801 for (i = 0; i < p->num_objects; i++) {
3791 - const unsigned char *sha1 = nth_packed_object_sha1(p, i);
3802 + struct object_id oid;
3803
3793 - if (!sha1)
3804 + if (!nth_packed_object_oid(&oid, p, i))
3805 return error("unable to get sha1 of object %u in %s",
3806 i, p->pack_name);
3807
3797 - r = cb(sha1, p, i, data);
3808 + r = cb(oid.hash, p, i, data);
3809 if (r)
3810 break;
3811 }