Skip to content

Commit 67687fe

Browse files
Michael J Grubergitster
authored andcommitted
for-each-ref: Field with abbreviated objectname
Introduce a :short modifier to objectname which outputs the abbreviated object name. Signed-off-by: Michael J Gruber <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 04794fd commit 67687fe

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ objectsize::
8686

8787
objectname::
8888
The object name (aka SHA-1).
89+
For a non-ambiguous abbreviation of the object name append `:short`.
8990

9091
upstream::
9192
The name of a local ref which can be considered ``upstream''

builtin/for-each-ref.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ static void grab_common_values(struct atom_value *val, int deref, struct object
227227
strcpy(s, sha1_to_hex(obj->sha1));
228228
v->s = s;
229229
}
230+
else if (!strcmp(name, "objectname:short")) {
231+
v->s = find_unique_abbrev(obj->sha1, DEFAULT_ABBREV);
232+
}
230233
}
231234
}
232235

t/t6300-for-each-ref.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@ test_expect_success 'Check short upstream format' '
295295
test_cmp expected actual
296296
'
297297

298+
cat >expected <<EOF
299+
67a36f1
300+
EOF
301+
302+
test_expect_success 'Check short objectname format' '
303+
git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
304+
test_cmp expected actual
305+
'
306+
298307
test_expect_success 'Check for invalid refname format' '
299308
test_must_fail git for-each-ref --format="%(refname:INVALID)"
300309
'

0 commit comments

Comments
 (0)