packed_object_info: make type lookup optional
Currently, packed_object_info can save some work by not calculating the size or disk_size of the object if the caller is not interested. However, it always calculates the true object type, whether the caller cares or not, and only optionally returns the easy-to-get "representation type". Let's swap these types. The function will now return the representation type (or OBJ_BAD on failure), and will only optionally fill in the true type. There should be no behavior change yet, as the only caller, sha1_object_info_extended, will always feed it a type pointer. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jul 12, 2013 at 02:32 UTC
412916ee131cf8b0aaac96313ce8686aebe3a47a
1 file changed
+16
-9
sha1_file.c
+16
-9
@@ -1780,7 +1780,7 @@ unwind:
1780
}
1781
1782
static int packed_object_info(struct packed_git *p, off_t obj_offset,
1783
- unsigned long *sizep, int *rtype,
1783
+ enum object_type *typep, unsigned long *sizep,
1784
unsigned long *disk_sizep)
1785
{
1786
struct pack_window *w_curs = NULL;
@@ -1788,11 +1788,12 @@ static int packed_object_info(struct packed_git *p, off_t obj_offset,
1788
off_t curpos = obj_offset;
1789
enum object_type type;
1790
1791
+ /*
1792
+ * We always get the representation type, but only convert it to
1793
+ * a "real" type later if the caller is interested.
1794
+ */
1795
type = unpack_object_header(p, &w_curs, &curpos, &size);
1796
1793
- if (rtype)
1794
- *rtype = type; /* representation type */
1795
-
1797
if (sizep) {
1798
if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1799
off_t tmp_pos = curpos;
@@ -1817,7 +1818,13 @@ static int packed_object_info(struct packed_git *p, off_t obj_offset,
1818
*disk_sizep = revidx[1].offset - obj_offset;
1819
}
1820
1820
- type = packed_to_object_type(p, obj_offset, type, &w_curs, curpos);
1821
+ if (typep) {
1822
+ *typep = packed_to_object_type(p, obj_offset, type, &w_curs, curpos);
1823
+ if (*typep < 0) {
1824
+ type = OBJ_BAD;
1825
+ goto out;
1826
+ }
1827
+ }
1828
1829
out:
1830
unuse_pack(&w_curs);
@@ -2452,11 +2459,11 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
2459
return -1;
2460
}
2461
2455
- type = packed_object_info(e.p, e.offset, oi->sizep, &rtype,
2456
- oi->disk_sizep);
2457
- if (type < 0) {
2462
+ rtype = packed_object_info(e.p, e.offset, &type, oi->sizep,
2463
+ oi->disk_sizep);
2464
+ if (rtype < 0) {
2465
mark_bad_packed_object(e.p, sha1);
2459
- type = sha1_object_info_extended(sha1, oi);
2466
+ return sha1_object_info_extended(sha1, oi);
2467
} else if (in_delta_base_cache(e.p, e.offset)) {
2468
oi->whence = OI_DBCACHED;
2469
} else {