Skip to content

Commit 412916e

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 90191d3 commit 412916e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

sha1_file.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,19 +1780,20 @@ static enum object_type packed_to_object_type(struct packed_git *p,
17801780
}
17811781

17821782
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,
17841784
unsigned long *disk_sizep)
17851785
{
17861786
struct pack_window *w_curs = NULL;
17871787
unsigned long size;
17881788
off_t curpos = obj_offset;
17891789
enum object_type type;
17901790

1791+
/*
1792+
* We always get the representation type, but only convert it to
1793+
* a "real" type later if the caller is interested.
1794+
*/
17911795
type = unpack_object_header(p, &w_curs, &curpos, &size);
17921796

1793-
if (rtype)
1794-
*rtype = type; /* representation type */
1795-
17961797
if (sizep) {
17971798
if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
17981799
off_t tmp_pos = curpos;
@@ -1817,7 +1818,13 @@ static int packed_object_info(struct packed_git *p, off_t obj_offset,
18171818
*disk_sizep = revidx[1].offset - obj_offset;
18181819
}
18191820

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+
}
18211828

18221829
out:
18231830
unuse_pack(&w_curs);
@@ -2452,11 +2459,11 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
24522459
return -1;
24532460
}
24542461

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) {
24582465
mark_bad_packed_object(e.p, sha1);
2459-
type = sha1_object_info_extended(sha1, oi);
2466+
return sha1_object_info_extended(sha1, oi);
24602467
} else if (in_delta_base_cache(e.p, e.offset)) {
24612468
oi->whence = OI_DBCACHED;
24622469
} else {

0 commit comments

Comments
 (0)