Skip to content

Commit 837adb1

Browse files
harry-hovgitster
authored andcommitted
ref-filter: add short modifier to 'tree' atom
Sometimes while using 'tree' atom, user might want to see abbrev hash instead of full 40 character hash. Just like 'objectname', it might be convenient for users to have the `:short` and `:short=<length>` option for printing 'tree' hash. Let's introduce `short` option to 'tree' atom. Mentored-by: Christian Couder <[email protected]> Mentored-by: Heba Waly <[email protected]> Signed-off-by: Hariom Verma <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 87d3beb commit 837adb1

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ worktreepath::
222222
In addition to the above, for commit and tag objects, the header
223223
field names (`tree`, `parent`, `object`, `type`, and `tag`) can
224224
be used to specify the value in the header field.
225+
Field `tree` can also be used with modifier `:short` and
226+
`:short=<length>` just like `objectname`.
225227

226228
For commit and tag objects, the special `creatordate` and `creator`
227229
fields will correspond to the appropriate date or name-email-date tuple

ref-filter.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ static struct {
497497
{ "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
498498
{ "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
499499
{ "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
500-
{ "tree", SOURCE_OBJ },
500+
{ "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
501501
{ "parent", SOURCE_OBJ },
502502
{ "numparent", SOURCE_OBJ, FIELD_ULONG },
503503
{ "object", SOURCE_OBJ },
@@ -1005,10 +1005,9 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
10051005
continue;
10061006
if (deref)
10071007
name++;
1008-
if (!strcmp(name, "tree")) {
1009-
v->s = xstrdup(oid_to_hex(get_commit_tree_oid(commit)));
1010-
}
1011-
else if (!strcmp(name, "numparent")) {
1008+
if (grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i]))
1009+
continue;
1010+
if (!strcmp(name, "numparent")) {
10121011
v->value = commit_list_count(commit->parents);
10131012
v->s = xstrfmt("%lu", (unsigned long)v->value);
10141013
}

t/t6300-for-each-ref.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ test_atom head objectname:short $(git rev-parse --short refs/heads/master)
116116
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
117117
test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
118118
test_atom head tree $(git rev-parse refs/heads/master^{tree})
119+
test_atom head tree:short $(git rev-parse --short refs/heads/master^{tree})
120+
test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/master^{tree})
121+
test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/master^{tree})
119122
test_atom head parent ''
120123
test_atom head numparent 0
121124
test_atom head object ''
@@ -167,6 +170,9 @@ test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
167170
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
168171
test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
169172
test_atom tag tree ''
173+
test_atom tag tree:short ''
174+
test_atom tag tree:short=1 ''
175+
test_atom tag tree:short=10 ''
170176
test_atom tag parent ''
171177
test_atom tag numparent ''
172178
test_atom tag object $(git rev-parse refs/tags/testtag^0)

0 commit comments

Comments
 (0)