20
20
21
21
# typing -------------------------------------------------
22
22
23
- from typing import Iterable , Iterator , Tuple , Union , cast , TYPE_CHECKING
23
+ from typing import Callable , Dict , Iterable , Iterator , List , Tuple , Type , Union , cast , TYPE_CHECKING
24
+
25
+ from git .types import PathLike
24
26
25
27
if TYPE_CHECKING :
28
+ from git .repo import Repo
26
29
from io import BytesIO
27
30
28
31
#--------------------------------------------------------
29
32
30
33
31
- cmp = lambda a , b : (a > b ) - (a < b )
34
+ cmp : Callable [[ int , int ], int ] = lambda a , b : (a > b ) - (a < b )
32
35
33
36
__all__ = ("TreeModifier" , "Tree" )
34
37
35
38
36
- def git_cmp (t1 , t2 ) :
39
+ def git_cmp (t1 : 'Tree' , t2 : 'Tree' ) -> int :
37
40
a , b = t1 [2 ], t2 [2 ]
38
41
len_a , len_b = len (a ), len (b )
39
42
min_len = min (len_a , len_b )
@@ -45,9 +48,9 @@ def git_cmp(t1, t2):
45
48
return len_a - len_b
46
49
47
50
48
- def merge_sort (a , cmp ) :
51
+ def merge_sort (a : List [ int ] , cmp : Callable [[ int , int ], int ]) -> None :
49
52
if len (a ) < 2 :
50
- return
53
+ return None
51
54
52
55
mid = len (a ) // 2
53
56
lefthalf = a [:mid ]
@@ -182,29 +185,29 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
182
185
symlink_id = 0o12
183
186
tree_id = 0o04
184
187
185
- _map_id_to_type = {
188
+ _map_id_to_type : Dict [ int , Union [ Type [ Submodule ], Type [ Blob ], Type [ 'Tree' ]]] = {
186
189
commit_id : Submodule ,
187
190
blob_id : Blob ,
188
191
symlink_id : Blob
189
192
# tree id added once Tree is defined
190
193
}
191
194
192
- def __init__ (self , repo , binsha , mode = tree_id << 12 , path = None ):
195
+ def __init__ (self , repo : 'Repo' , binsha : bytes , mode : int = tree_id << 12 , path : Union [ PathLike , None ] = None ):
193
196
super (Tree , self ).__init__ (repo , binsha , mode , path )
194
197
195
198
@classmethod
196
199
def _get_intermediate_items (cls , index_object : 'Tree' , # type: ignore
197
- ) -> Tuple ['Tree' , ...]:
200
+ ) -> Union [ Tuple ['Tree' , ...], Tuple [()] ]:
198
201
if index_object .type == "tree" :
199
202
index_object = cast ('Tree' , index_object )
200
203
return tuple (index_object ._iter_convert_to_object (index_object ._cache ))
201
204
return ()
202
205
203
- def _set_cache_ (self , attr ) :
206
+ def _set_cache_ (self , attr : str ) -> None :
204
207
if attr == "_cache" :
205
208
# Set the data when we need it
206
209
ostream = self .repo .odb .stream (self .binsha )
207
- self ._cache = tree_entries_from_data (ostream .read ())
210
+ self ._cache : List [ Tuple [ bytes , int , str ]] = tree_entries_from_data (ostream .read ())
208
211
else :
209
212
super (Tree , self )._set_cache_ (attr )
210
213
# END handle attribute
@@ -221,7 +224,7 @@ def _iter_convert_to_object(self, iterable: Iterable[Tuple[bytes, int, str]]
221
224
raise TypeError ("Unknown mode %o found in tree data for path '%s'" % (mode , path )) from e
222
225
# END for each item
223
226
224
- def join (self , file ) :
227
+ def join (self , file : str ) -> Union [ Blob , 'Tree' , Submodule ] :
225
228
"""Find the named object in this tree's contents
226
229
:return: ``git.Blob`` or ``git.Tree`` or ``git.Submodule``
227
230
@@ -254,26 +257,22 @@ def join(self, file):
254
257
raise KeyError (msg % file )
255
258
# END handle long paths
256
259
257
- def __div__ (self , file ):
258
- """For PY2 only"""
259
- return self .join (file )
260
-
261
- def __truediv__ (self , file ):
260
+ def __truediv__ (self , file : str ) -> Union ['Tree' , Blob , Submodule ]:
262
261
"""For PY3 only"""
263
262
return self .join (file )
264
263
265
264
@property
266
- def trees (self ):
265
+ def trees (self ) -> List [ 'Tree' ] :
267
266
""":return: list(Tree, ...) list of trees directly below this tree"""
268
267
return [i for i in self if i .type == "tree" ]
269
268
270
269
@property
271
- def blobs (self ):
270
+ def blobs (self ) -> List [ 'Blob' ] :
272
271
""":return: list(Blob, ...) list of blobs directly below this tree"""
273
272
return [i for i in self if i .type == "blob" ]
274
273
275
274
@property
276
- def cache (self ):
275
+ def cache (self ) -> TreeModifier :
277
276
"""
278
277
:return: An object allowing to modify the internal cache. This can be used
279
278
to change the tree's contents. When done, make sure you call ``set_done``
@@ -289,16 +288,16 @@ def traverse(self, predicate=lambda i, d: True,
289
288
return super (Tree , self ).traverse (predicate , prune , depth , branch_first , visit_once , ignore_self )
290
289
291
290
# List protocol
292
- def __getslice__ (self , i , j ) :
291
+ def __getslice__ (self , i : int , j : int ) -> List [ Union [ Blob , 'Tree' , Submodule ]] :
293
292
return list (self ._iter_convert_to_object (self ._cache [i :j ]))
294
293
295
- def __iter__ (self ):
294
+ def __iter__ (self ) -> Iterator [ Union [ Blob , 'Tree' , Submodule ]] :
296
295
return self ._iter_convert_to_object (self ._cache )
297
296
298
- def __len__ (self ):
297
+ def __len__ (self ) -> int :
299
298
return len (self ._cache )
300
299
301
- def __getitem__ (self , item ) :
300
+ def __getitem__ (self , item : Union [ str , int , slice ]) -> Union [ Blob , 'Tree' , Submodule ] :
302
301
if isinstance (item , int ):
303
302
info = self ._cache [item ]
304
303
return self ._map_id_to_type [info [1 ] >> 12 ](self .repo , info [0 ], info [1 ], join_path (self .path , info [2 ]))
@@ -310,7 +309,7 @@ def __getitem__(self, item):
310
309
311
310
raise TypeError ("Invalid index type: %r" % item )
312
311
313
- def __contains__ (self , item ) :
312
+ def __contains__ (self , item : Union [ IndexObject , PathLike ]) -> bool :
314
313
if isinstance (item , IndexObject ):
315
314
for info in self ._cache :
316
315
if item .binsha == info [0 ]:
@@ -321,10 +320,11 @@ def __contains__(self, item):
321
320
# compatibility
322
321
323
322
# treat item as repo-relative path
324
- path = self .path
325
- for info in self ._cache :
326
- if item == join_path (path , info [2 ]):
327
- return True
323
+ else :
324
+ path = self .path
325
+ for info in self ._cache :
326
+ if item == join_path (path , info [2 ]):
327
+ return True
328
328
# END for each item
329
329
return False
330
330
0 commit comments