Skip to content

Commit 1c563a4

Browse files
skshetryefiop
authored andcommitted
odb: add exists_prefix method
1 parent 1a03409 commit 1c563a4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/dvc_objects/db.py

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ def __hash__(self):
4747
def exists(self, oid: str) -> bool:
4848
return self.fs.exists(self.oid_to_path(oid))
4949

50+
def exists_prefix(self, short_oid: str) -> str:
51+
ret = [oid for oid in self.all() if oid.startswith(short_oid)]
52+
if not ret:
53+
raise KeyError(short_oid)
54+
if len(ret) == 1:
55+
return ret[0]
56+
raise ValueError(short_oid, ret)
57+
5058
def move(self, from_info, to_info):
5159
self.fs.move(from_info, to_info)
5260

tests/test_odb.py

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ def test_exists(memfs):
5454
assert odb.exists("1234")
5555

5656

57+
def test_exists_prefix(memfs):
58+
odb = ObjectDB(memfs, "/odb")
59+
with pytest.raises(KeyError):
60+
assert odb.exists_prefix("12")
61+
62+
odb.add_bytes("123456", b"content")
63+
assert odb.exists_prefix("123") == "123456"
64+
65+
66+
def test_exists_prefix_ambiguous(memfs):
67+
odb = ObjectDB(memfs, "/odb")
68+
odb.add_bytes("123456", b"content")
69+
odb.add_bytes("123450", b"content")
70+
with pytest.raises(ValueError) as exc:
71+
assert odb.exists_prefix("12")
72+
assert exc.value.args == ("12", ["123450", "123456"])
73+
74+
5775
def test_move(memfs):
5876
odb = ObjectDB(memfs, "/")
5977
odb.add_bytes("1234", b"content")

0 commit comments

Comments
 (0)