@matheusbsbs / matheus-bagliano / commits / 26bc0aaf99
Fork 0

ref-filter: add `short` modifier to 'parent' atom

Sometimes while using 'parent' 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 'parent' hash. Let's introduce `short` option to 'parent' 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 26bc0aaf99147cdf2d29cb6d831dfa72ec94b61b
3 files changed +11 -5
Documentation/git-for-each-ref.txt
+1 -1
@@ -222,7 +222,7 @@ 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
225 +Fields `tree` and `parent` 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`
ref-filter.c
+4 -4
@@ -498,7 +498,7 @@ static struct {
498 { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
499 { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
500 { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
501 - { "parent", SOURCE_OBJ },
501 + { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
502 { "numparent", SOURCE_OBJ, FIELD_ULONG },
503 { "object", SOURCE_OBJ },
504 { "type", SOURCE_OBJ },
@@ -1011,14 +1011,14 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
1011 v->value = commit_list_count(commit->parents);
1012 v->s = xstrfmt("%lu", (unsigned long)v->value);
1013 }
1014 - else if (!strcmp(name, "parent")) {
1014 + else if (starts_with(name, "parent")) {
1015 struct commit_list *parents;
1016 struct strbuf s = STRBUF_INIT;
1017 for (parents = commit->parents; parents; parents = parents->next) {
1018 - struct commit *parent = parents->item;
1018 + struct object_id *oid = &parents->item->object.oid;
1019 if (parents != commit->parents)
1020 strbuf_addch(&s, ' ');
1021 - strbuf_addstr(&s, oid_to_hex(&parent->object.oid));
1021 + strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i]));
1022 }
1023 v->s = strbuf_detach(&s, NULL);
1024 }
t/t6300-for-each-ref.sh
+6
@@ -120,6 +120,9 @@ 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 parent:short ''
124 +test_atom head parent:short=1 ''
125 +test_atom head parent:short=10 ''
126 test_atom head numparent 0
127 test_atom head object ''
128 test_atom head type ''
@@ -174,6 +177,9 @@ test_atom tag tree:short ''
177 test_atom tag tree:short=1 ''
178 test_atom tag tree:short=10 ''
179 test_atom tag parent ''
180 +test_atom tag parent:short ''
181 +test_atom tag parent:short=1 ''
182 +test_atom tag parent:short=10 ''
183 test_atom tag numparent ''
184 test_atom tag object $(git rev-parse refs/tags/testtag^0)
185 test_atom tag type 'commit'