Skip to content

Commit 1b7b22b

Browse files
committed
Merge branch 'jc/sha1-name-object-peeler'
There was no good way to ask "I have a random string that came from outside world. I want to turn it into a 40-hex object name while making sure such an object exists". A new peeling suffix ^{object} can be used for that purpose, together with "rev-parse --verify". * jc/sha1-name-object-peeler: peel_onion(): teach $foo^{object} peeler peel_onion: disambiguate to favor tree-ish when we know we want a tree-ish
2 parents 41ae34d + a6a3f2c commit 1b7b22b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Documentation/revisions.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ some output processing may assume ref names in UTF-8.
116116
object of that type is found or the object cannot be
117117
dereferenced anymore (in which case, barf). '<rev>{caret}0'
118118
is a short-hand for '<rev>{caret}\{commit\}'.
119+
+
120+
'rev{caret}\{object\}' can be used to make sure 'rev' names an
121+
object that exists, without requiring 'rev' to be a tag, and
122+
without dereferencing 'rev'; because a tag is already an object,
123+
it does not have to be dereferenced even once to get to an object.
119124

120125
'<rev>{caret}\{\}', e.g. 'v0.99.8{caret}\{\}'::
121126
A suffix '{caret}' followed by an empty brace pair

sha1_name.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ struct object *peel_to_type(const char *name, int namelen,
594594
while (1) {
595595
if (!o || (!o->parsed && !parse_object(o->sha1)))
596596
return NULL;
597-
if (o->type == expected_type)
597+
if (expected_type == OBJ_ANY || o->type == expected_type)
598598
return o;
599599
if (o->type == OBJ_TAG)
600600
o = ((struct tag*) o)->tagged;
@@ -645,6 +645,8 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
645645
expected_type = OBJ_TREE;
646646
else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
647647
expected_type = OBJ_BLOB;
648+
else if (!prefixcmp(sp, "object}"))
649+
expected_type = OBJ_ANY;
648650
else if (sp[0] == '}')
649651
expected_type = OBJ_NONE;
650652
else if (sp[0] == '/')
@@ -654,6 +656,8 @@ static int peel_onion(const char *name, int len, unsigned char *sha1)
654656

655657
if (expected_type == OBJ_COMMIT)
656658
lookup_flags = GET_SHA1_COMMITTISH;
659+
else if (expected_type == OBJ_TREE)
660+
lookup_flags = GET_SHA1_TREEISH;
657661

658662
if (get_sha1_1(name, sp - name - 2, outer, lookup_flags))
659663
return -1;

0 commit comments

Comments
 (0)