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 <chriscool@tuxfamily.org> Mentored-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Hariom Verma committed
Aug 21, 2020 at 21:41 UTC
837adb102f6bfd6e05c75afc630f20c1c720cc74
3 files changed
+12
-5
Documentation/git-for-each-ref.txt
+2
@@ -222,6 +222,8 @@ worktreepath::
222
In addition to the above, for commit and tag objects, the header
223
field names (`tree`, `parent`, `object`, `type`, and `tag`) can
224
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`.
227
228
For commit and tag objects, the special `creatordate` and `creator`
229
fields will correspond to the appropriate date or name-email-date tuple
ref-filter.c
+4
-5
@@ -497,7 +497,7 @@ static struct {
497
{ "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
498
{ "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
499
{ "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
500
- { "tree", SOURCE_OBJ },
500
+ { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
501
{ "parent", SOURCE_OBJ },
502
{ "numparent", SOURCE_OBJ, FIELD_ULONG },
503
{ "object", SOURCE_OBJ },
@@ -1005,10 +1005,9 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
1005
continue;
1006
if (deref)
1007
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")) {
1011
v->value = commit_list_count(commit->parents);
1012
v->s = xstrfmt("%lu", (unsigned long)v->value);
1013
}
t/t6300-for-each-ref.sh
+6
@@ -116,6 +116,9 @@ test_atom head objectname:short $(git rev-parse --short refs/heads/master)
116
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
117
test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
118
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})
122
test_atom head parent ''
123
test_atom head numparent 0
124
test_atom head object ''
@@ -167,6 +170,9 @@ test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
170
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
171
test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
172
test_atom tag tree ''
173
+test_atom tag tree:short ''
174
+test_atom tag tree:short=1 ''
175
+test_atom tag tree:short=10 ''
176
test_atom tag parent ''
177
test_atom tag numparent ''
178
test_atom tag object $(git rev-parse refs/tags/testtag^0)