Skip to content

Commit 161f00e

Browse files
peffgitster
authored andcommitted
teach sha1_object_info_extended a "disk_size" query
Using sha1_object_info_extended, a caller can find out the type of an object, its size, and information about where it is stored. In addition to the object's "true" size, it can also be useful to know the size that the object takes on disk (e.g., to generate statistics about which refs consume space). This patch adds a "disk_sizep" field to "struct object_info", and fills it in during sha1_object_info_extended if it is non-NULL. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c07385 commit 161f00e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ extern int unpack_object_header(struct packed_git *, struct pack_window **, off_
10991099
struct object_info {
11001100
/* Request */
11011101
unsigned long *sizep;
1102+
unsigned long *disk_sizep;
11021103

11031104
/* Response */
11041105
enum {

sha1_file.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,8 @@ static int retry_bad_packed_offset(struct packed_git *p, off_t obj_offset)
16941694
#define POI_STACK_PREALLOC 64
16951695

16961696
static int packed_object_info(struct packed_git *p, off_t obj_offset,
1697-
unsigned long *sizep, int *rtype)
1697+
unsigned long *sizep, int *rtype,
1698+
unsigned long *disk_sizep)
16981699
{
16991700
struct pack_window *w_curs = NULL;
17001701
unsigned long size;
@@ -1728,6 +1729,11 @@ static int packed_object_info(struct packed_git *p, off_t obj_offset,
17281729
}
17291730
}
17301731

1732+
if (disk_sizep) {
1733+
struct revindex_entry *revidx = find_pack_revindex(p, obj_offset);
1734+
*disk_sizep = revidx[1].offset - obj_offset;
1735+
}
1736+
17311737
while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
17321738
off_t base_offset;
17331739
/* Push the object we're going to leave behind */
@@ -2338,7 +2344,8 @@ struct packed_git *find_sha1_pack(const unsigned char *sha1,
23382344

23392345
}
23402346

2341-
static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *sizep)
2347+
static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *sizep,
2348+
unsigned long *disk_sizep)
23422349
{
23432350
int status;
23442351
unsigned long mapsize, size;
@@ -2349,6 +2356,8 @@ static int sha1_loose_object_info(const unsigned char *sha1, unsigned long *size
23492356
map = map_sha1_file(sha1, &mapsize);
23502357
if (!map)
23512358
return error("unable to find %s", sha1_to_hex(sha1));
2359+
if (disk_sizep)
2360+
*disk_sizep = mapsize;
23522361
if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
23532362
status = error("unable to unpack %s header",
23542363
sha1_to_hex(sha1));
@@ -2372,13 +2381,15 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
23722381
if (co) {
23732382
if (oi->sizep)
23742383
*(oi->sizep) = co->size;
2384+
if (oi->disk_sizep)
2385+
*(oi->disk_sizep) = 0;
23752386
oi->whence = OI_CACHED;
23762387
return co->type;
23772388
}
23782389

23792390
if (!find_pack_entry(sha1, &e)) {
23802391
/* Most likely it's a loose object. */
2381-
status = sha1_loose_object_info(sha1, oi->sizep);
2392+
status = sha1_loose_object_info(sha1, oi->sizep, oi->disk_sizep);
23822393
if (status >= 0) {
23832394
oi->whence = OI_LOOSE;
23842395
return status;
@@ -2390,7 +2401,8 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
23902401
return status;
23912402
}
23922403

2393-
status = packed_object_info(e.p, e.offset, oi->sizep, &rtype);
2404+
status = packed_object_info(e.p, e.offset, oi->sizep, &rtype,
2405+
oi->disk_sizep);
23942406
if (status < 0) {
23952407
mark_bad_packed_object(e.p, sha1);
23962408
status = sha1_object_info_extended(sha1, oi);

0 commit comments

Comments
 (0)