| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -568,7 +568,7 @@ def write_tree(self) -> Tree: | |||
| 568 | 568 | # note: additional deserialization could be saved if write_tree_from_cache | |
| 569 | 569 | # would return sorted tree entries | |
| 570 | 570 | root_tree = Tree(self.repo, binsha, path='') | |
| 571 | - root_tree._cache = tree_items | ||
| 571 | + root_tree._cache = tree_items # type: ignore | ||
| 572 | 572 | return root_tree | |
| 573 | 573 | ||
| 574 | 574 | def _process_diff_args(self, args: List[Union[str, diff.Diffable, object]] | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -293,7 +293,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 | |||
| 293 | 293 | # finally create the tree | |
| 294 | 294 | sio = BytesIO() | |
| 295 | 295 | tree_to_stream(tree_items, sio.write) # converts bytes of each item[0] to str | |
| 296 | - tree_items_stringified = cast(List[Tuple[str, int, str]], tree_items) # type: List[Tuple[str, int, str]] | ||
| 296 | + tree_items_stringified = cast(List[Tuple[str, int, str]], tree_items) | ||
| 297 | 297 | sio.seek(0) | |
| 298 | 298 | ||
| 299 | 299 | istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,19 @@ | |||
| 1 | 1 | """Module with functions which are supposed to be as fast as possible""" | |
| 2 | 2 | from stat import S_ISDIR | |
| 3 | + | ||
| 3 | 4 | from git.compat import ( | |
| 4 | 5 | safe_decode, | |
| 5 | 6 | defenc | |
| 6 | 7 | ) | |
| 7 | 8 | ||
| 9 | + # typing ---------------------------------------------- | ||
| 10 | + | ||
| 11 | + from typing import List, Tuple | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + # --------------------------------------------------- | ||
| 15 | + | ||
| 16 | + | ||
| 8 | 17 | __all__ = ('tree_to_stream', 'tree_entries_from_data', 'traverse_trees_recursive', | |
| 9 | 18 | 'traverse_tree_recursive') | |
| 10 | 19 | ||
@@ -38,7 +47,7 @@ def tree_to_stream(entries, write): | |||
| 38 | 47 | # END for each item | |
| 39 | 48 | ||
| 40 | 49 | ||
| 41 | - def tree_entries_from_data(data): | ||
| 50 | + def tree_entries_from_data(data: bytes) -> List[Tuple[bytes, int, str]]: | ||
| 42 | 51 | """Reads the binary representation of a tree and returns tuples of Tree items | |
| 43 | 52 | :param data: data block with tree data (as bytes) | |
| 44 | 53 | :return: list(tuple(binsha, mode, tree_relative_path), ...)""" | |
@@ -72,8 +81,8 @@ def tree_entries_from_data(data): | |||
| 72 | 81 | ||
| 73 | 82 | # default encoding for strings in git is utf8 | |
| 74 | 83 | # Only use the respective unicode object if the byte stream was encoded | |
| 75 | - name = data[ns:i] | ||
| 76 | - name = safe_decode(name) | ||
| 84 | + name_bytes = data[ns:i] | ||
| 85 | + name = safe_decode(name_bytes) | ||
| 77 | 86 | ||
| 78 | 87 | # byte is NULL, get next 20 | |
| 79 | 88 | i += 1 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,20 +20,23 @@ | |||
| 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | import os | |
| 8 | 8 | import re | |
| 9 | 9 | import warnings | |
| 10 | + from gitdb.db.loose import LooseObjectDB | ||
| 10 | 11 | ||
| 11 | 12 | from gitdb.exc import BadObject | |
| 12 | 13 | ||
@@ -100,7 +101,7 @@ class Repo(object): | |||
| 100 | 101 | # Subclasses may easily bring in their own custom types by placing a constructor or type here | |
| 101 | 102 | GitCommandWrapperType = Git | |
| 102 | 103 | ||
| 103 | - def __init__(self, path: Optional[PathLike] = None, odbt: Type[GitCmdObjectDB] = GitCmdObjectDB, | ||
| 104 | + def __init__(self, path: Optional[PathLike] = None, odbt: Type[LooseObjectDB] = GitCmdObjectDB, | ||
| 104 | 105 | search_parent_directories: bool = False, expand_vars: bool = True) -> None: | |
| 105 | 106 | """Create a new Repo instance | |
| 106 | 107 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments