Skip to content

Commit 55574bd

Browse files
committed
Merge branch 'ot/ref-filter-object-info'
The "--format=<placeholder>" option of for-each-ref, branch and tag learned to show a few more traits of objects that can be learned by the object_info API. * ot/ref-filter-object-info: ref-filter: give uintmax_t to format with %PRIuMAX ref-filter: add docs for new options ref-filter: add tests for deltabase ref-filter: add deltabase option ref-filter: add tests for objectsize:disk ref-filter: add check for negative file size ref-filter: add objectsize:disk option
2 parents 3fe47ff + f2ddd9e commit 55574bd

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,18 @@ objecttype::
128128

129129
objectsize::
130130
The size of the object (the same as 'git cat-file -s' reports).
131-
131+
Append `:disk` to get the size, in bytes, that the object takes up on
132+
disk. See the note about on-disk sizes in the `CAVEATS` section below.
132133
objectname::
133134
The object name (aka SHA-1).
134135
For a non-ambiguous abbreviation of the object name append `:short`.
135136
For an abbreviation of the object name with desired length append
136137
`:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
137138
length may be exceeded to ensure unique object names.
139+
deltabase::
140+
This expands to the object name of the delta base for the
141+
given object, if it is stored as a delta. Otherwise it
142+
expands to the null object name (all zeroes).
138143

139144
upstream::
140145
The name of a local ref which can be considered ``upstream''
@@ -361,6 +366,20 @@ This prints the authorname, if present.
361366
git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"
362367
------------
363368

369+
CAVEATS
370+
-------
371+
372+
Note that the sizes of objects on disk are reported accurately, but care
373+
should be taken in drawing conclusions about which refs or objects are
374+
responsible for disk usage. The size of a packed non-delta object may be
375+
much larger than the size of objects which delta against it, but the
376+
choice of which object is the base and which is the delta is arbitrary
377+
and is subject to change during a repack.
378+
379+
Note also that multiple copies of an object may be present in the object
380+
database; in this case, it is undefined which copy's size or delta base
381+
will be reported.
382+
364383
SEE ALSO
365384
--------
366385
linkgit:git-show-ref[1]

ref-filter.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,31 @@ static int objecttype_atom_parser(const struct ref_format *format, struct used_a
230230

231231
static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom,
232232
const char *arg, struct strbuf *err)
233+
{
234+
if (!arg) {
235+
if (*atom->name == '*')
236+
oi_deref.info.sizep = &oi_deref.size;
237+
else
238+
oi.info.sizep = &oi.size;
239+
} else if (!strcmp(arg, "disk")) {
240+
if (*atom->name == '*')
241+
oi_deref.info.disk_sizep = &oi_deref.disk_size;
242+
else
243+
oi.info.disk_sizep = &oi.disk_size;
244+
} else
245+
return strbuf_addf_ret(err, -1, _("unrecognized %%(objectsize) argument: %s"), arg);
246+
return 0;
247+
}
248+
249+
static int deltabase_atom_parser(const struct ref_format *format, struct used_atom *atom,
250+
const char *arg, struct strbuf *err)
233251
{
234252
if (arg)
235-
return strbuf_addf_ret(err, -1, _("%%(objectsize) does not take arguments"));
253+
return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments"));
236254
if (*atom->name == '*')
237-
oi_deref.info.sizep = &oi_deref.size;
255+
oi_deref.info.delta_base_sha1 = oi_deref.delta_base_oid.hash;
238256
else
239-
oi.info.sizep = &oi.size;
257+
oi.info.delta_base_sha1 = oi.delta_base_oid.hash;
240258
return 0;
241259
}
242260

@@ -431,6 +449,7 @@ static struct {
431449
{ "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
432450
{ "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
433451
{ "objectname", SOURCE_OTHER, FIELD_STR, objectname_atom_parser },
452+
{ "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
434453
{ "tree", SOURCE_OBJ },
435454
{ "parent", SOURCE_OBJ },
436455
{ "numparent", SOURCE_OBJ, FIELD_ULONG },
@@ -880,10 +899,14 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
880899
name++;
881900
if (!strcmp(name, "objecttype"))
882901
v->s = xstrdup(type_name(oi->type));
883-
else if (!strcmp(name, "objectsize")) {
902+
else if (!strcmp(name, "objectsize:disk")) {
903+
v->value = oi->disk_size;
904+
v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
905+
} else if (!strcmp(name, "objectsize")) {
884906
v->value = oi->size;
885907
v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
886-
}
908+
} else if (!strcmp(name, "deltabase"))
909+
v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
887910
else if (deref)
888911
grab_objectname(name, &oi->oid, v, &used_atom[i]);
889912
}
@@ -1482,6 +1505,8 @@ static int get_object(struct ref_array_item *ref, int deref, struct object **obj
14821505
OBJECT_INFO_LOOKUP_REPLACE))
14831506
return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
14841507
oid_to_hex(&oi->oid), ref->refname);
1508+
if (oi->info.disk_sizep && oi->disk_size < 0)
1509+
BUG("Object size is less than zero.");
14851510

14861511
if (oi->info.contentp) {
14871512
*obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);

t/t6300-for-each-ref.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ test_atom head push:strip=1 remotes/myfork/master
8383
test_atom head push:strip=-1 master
8484
test_atom head objecttype commit
8585
test_atom head objectsize 171
86+
test_atom head objectsize:disk 138
87+
test_atom head deltabase 0000000000000000000000000000000000000000
8688
test_atom head objectname $(git rev-parse refs/heads/master)
8789
test_atom head objectname:short $(git rev-parse --short refs/heads/master)
8890
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
@@ -124,6 +126,10 @@ test_atom tag upstream ''
124126
test_atom tag push ''
125127
test_atom tag objecttype tag
126128
test_atom tag objectsize 154
129+
test_atom tag objectsize:disk 138
130+
test_atom tag '*objectsize:disk' 138
131+
test_atom tag deltabase 0000000000000000000000000000000000000000
132+
test_atom tag '*deltabase' 0000000000000000000000000000000000000000
127133
test_atom tag objectname $(git rev-parse refs/tags/testtag)
128134
test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
129135
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)

0 commit comments

Comments
 (0)