File tree 2 files changed +26
-0
lines changed
2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,14 @@ def __hash__(self):
47
47
def exists (self , oid : str ) -> bool :
48
48
return self .fs .exists (self .oid_to_path (oid ))
49
49
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
+
50
58
def move (self , from_info , to_info ):
51
59
self .fs .move (from_info , to_info )
52
60
Original file line number Diff line number Diff line change @@ -54,6 +54,24 @@ def test_exists(memfs):
54
54
assert odb .exists ("1234" )
55
55
56
56
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
+
57
75
def test_move (memfs ):
58
76
odb = ObjectDB (memfs , "/" )
59
77
odb .add_bytes ("1234" , b"content" )
You can’t perform that action at this time.
0 commit comments