Skip to content

Commit a4ac106

Browse files
peffgitster
authored andcommitted
cat-file: add %(objectsize:disk) format atom
This atom is just like %(objectsize), except that it shows the on-disk size of the object rather than the object's true size. In other words, it makes the "disk_size" query of sha1_object_info_extended available via the command-line. This can be used for rough attribution of disk usage to particular refs, though see the caveats in the documentation. This patch does not include any tests, as the exact numbers returned are volatile and subject to zlib and packing decisions. We cannot even reliably guarantee that the on-disk size is smaller than the object content (though in general this should be the case for non-trivial objects). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 93d2a60 commit a4ac106

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

Documentation/git-cat-file.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ newline. The available atoms are:
106106
The size, in bytes, of the object (the same as `cat-file -s`
107107
reports).
108108

109+
`objectsize:disk`::
110+
The size, in bytes, that the object takes up on disk. See the
111+
note about on-disk sizes in the `CAVEATS` section below.
112+
109113
If no format is specified, the default format is `%(objectname)
110114
%(objecttype) %(objectsize)`.
111115

@@ -133,6 +137,20 @@ the repository, then `cat-file` will ignore any custom format and print:
133137
<object> SP missing LF
134138
------------
135139

140+
141+
CAVEATS
142+
-------
143+
144+
Note that the sizes of objects on disk are reported accurately, but care
145+
should be taken in drawing conclusions about which refs or objects are
146+
responsible for disk usage. The size of a packed non-delta object may be
147+
much larger than the size of objects which delta against it, but the
148+
choice of which object is the base and which is the delta is arbitrary
149+
and is subject to change during a repack. Note also that multiple copies
150+
of an object may be present in the object database; in this case, it is
151+
undefined which copy's size will be reported.
152+
153+
136154
GIT
137155
---
138156
Part of the linkgit:git[1] suite

builtin/cat-file.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct expand_data {
118118
unsigned char sha1[20];
119119
enum object_type type;
120120
unsigned long size;
121+
unsigned long disk_size;
121122

122123
/*
123124
* If mark_query is true, we do not expand anything, but rather
@@ -155,6 +156,11 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
155156
data->info.sizep = &data->size;
156157
else
157158
strbuf_addf(sb, "%lu", data->size);
159+
} else if (is_atom("objectsize:disk", atom, len)) {
160+
if (data->mark_query)
161+
data->info.disk_sizep = &data->disk_size;
162+
else
163+
strbuf_addf(sb, "%lu", data->disk_size);
158164
} else
159165
die("unknown format element: %.*s", len, atom);
160166
}

0 commit comments

Comments
 (0)