Skip to content

Commit 26bc0aa

Browse files
harry-hovgitster
authored andcommitted
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 <[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 837adb1 commit 26bc0aa

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ 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
225+
Fields `tree` and `parent` can also be used with modifier `:short` and
226226
`:short=<length>` just like `objectname`.
227227

228228
For commit and tag objects, the special `creatordate` and `creator`

ref-filter.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static struct {
498498
{ "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
499499
{ "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
500500
{ "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
501-
{ "parent", SOURCE_OBJ },
501+
{ "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
502502
{ "numparent", SOURCE_OBJ, FIELD_ULONG },
503503
{ "object", SOURCE_OBJ },
504504
{ "type", SOURCE_OBJ },
@@ -1011,14 +1011,14 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object
10111011
v->value = commit_list_count(commit->parents);
10121012
v->s = xstrfmt("%lu", (unsigned long)v->value);
10131013
}
1014-
else if (!strcmp(name, "parent")) {
1014+
else if (starts_with(name, "parent")) {
10151015
struct commit_list *parents;
10161016
struct strbuf s = STRBUF_INIT;
10171017
for (parents = commit->parents; parents; parents = parents->next) {
1018-
struct commit *parent = parents->item;
1018+
struct object_id *oid = &parents->item->object.oid;
10191019
if (parents != commit->parents)
10201020
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]));
10221022
}
10231023
v->s = strbuf_detach(&s, NULL);
10241024
}

t/t6300-for-each-ref.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ test_atom head tree:short $(git rev-parse --short refs/heads/master^{tree})
120120
test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/master^{tree})
121121
test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/master^{tree})
122122
test_atom head parent ''
123+
test_atom head parent:short ''
124+
test_atom head parent:short=1 ''
125+
test_atom head parent:short=10 ''
123126
test_atom head numparent 0
124127
test_atom head object ''
125128
test_atom head type ''
@@ -174,6 +177,9 @@ test_atom tag tree:short ''
174177
test_atom tag tree:short=1 ''
175178
test_atom tag tree:short=10 ''
176179
test_atom tag parent ''
180+
test_atom tag parent:short ''
181+
test_atom tag parent:short=1 ''
182+
test_atom tag parent:short=10 ''
177183
test_atom tag numparent ''
178184
test_atom tag object $(git rev-parse refs/tags/testtag^0)
179185
test_atom tag type 'commit'

0 commit comments

Comments
 (0)