| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ | |||
| 16 | 16 | # typing ------------------------------------------------------------------ | |
| 17 | 17 | ||
| 18 | 18 | from typing import Any, Iterator, List, Match, Optional, Tuple, Type, Union, TYPE_CHECKING | |
| 19 | - from git.types import PathLike, TBD, Final, Literal | ||
| 19 | + from git.types import PathLike, TBD, Literal | ||
| 20 | 20 | ||
| 21 | 21 | if TYPE_CHECKING: | |
| 22 | 22 | from .objects.tree import Tree | |
@@ -31,7 +31,7 @@ | |||
| 31 | 31 | __all__ = ('Diffable', 'DiffIndex', 'Diff', 'NULL_TREE') | |
| 32 | 32 | ||
| 33 | 33 | # Special object to compare against the empty tree in diffs | |
| 34 | - NULL_TREE = object() # type: Final[object] | ||
| 34 | + NULL_TREE = object() | ||
| 35 | 35 | ||
| 36 | 36 | _octal_byte_re = re.compile(b'\\\\([0-9]{3})') | |
| 37 | 37 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,7 +108,7 @@ def run_commit_hook(name: str, index: 'IndexFile', *args: str) -> None: | |||
| 108 | 108 | # end handle return code | |
| 109 | 109 | ||
| 110 | 110 | ||
| 111 | - def stat_mode_to_index_mode(mode): | ||
| 111 | + def stat_mode_to_index_mode(mode: int) -> int: | ||
| 112 | 112 | """Convert the given mode from a stat call to the corresponding index mode | |
| 113 | 113 | and return it""" | |
| 114 | 114 | if S_ISLNK(mode): # symlinks | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,16 +3,34 @@ | |||
| 3 | 3 | # | |
| 4 | 4 | # This module is part of GitPython and is released under | |
| 5 | 5 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php | |
| 6 | + | ||
| 7 | + from git.exc import WorkTreeRepositoryUnsupported | ||
| 6 | 8 | from git.util import LazyMixin, join_path_native, stream_copy, bin_to_hex | |
| 7 | 9 | ||
| 8 | 10 | import gitdb.typ as dbtyp | |
| 9 | 11 | import os.path as osp | |
| 10 | - from typing import Optional # noqa: F401 unused import | ||
| 11 | 12 | ||
| 12 | 13 | from .util import get_object_type_by_name | |
| 13 | 14 | ||
| 14 | 15 | ||
| 15 | - _assertion_msg_format = "Created object %r whose python type %r disagrees with the acutal git object type %r" | ||
| 16 | + # typing ------------------------------------------------------------------ | ||
| 17 | + | ||
| 18 | + from typing import Any, TYPE_CHECKING, Optional, Union | ||
| 19 | + | ||
| 20 | + from git.types import PathLike | ||
| 21 | + | ||
| 22 | + if TYPE_CHECKING: | ||
| 23 | + from git.repo import Repo | ||
| 24 | + from gitdb.base import OStream | ||
| 25 | + from .tree import Tree | ||
| 26 | + from .blob import Blob | ||
| 27 | + from .tag import TagObject | ||
| 28 | + from .commit import Commit | ||
| 29 | + | ||
| 30 | + # -------------------------------------------------------------------------- | ||
| 31 | + | ||
| 32 | + | ||
| 33 | + _assertion_msg_format = "Created object %r whose python type %r disagrees with the acutual git object type %r" | ||
| 16 | 34 | ||
| 17 | 35 | __all__ = ("Object", "IndexObject") | |
| 18 | 36 | ||
@@ -27,7 +45,7 @@ class Object(LazyMixin): | |||
| 27 | 45 | __slots__ = ("repo", "binsha", "size") | |
| 28 | 46 | type = None # type: Optional[str] # to be set by subclass | |
| 29 | 47 | ||
| 30 | - def __init__(self, repo, binsha): | ||
| 48 | + def __init__(self, repo: 'Repo', binsha: bytes): | ||
| 31 | 49 | """Initialize an object by identifying it by its binary sha. | |
| 32 | 50 | All keyword arguments will be set on demand if None. | |
| 33 | 51 | ||
@@ -40,7 +58,7 @@ def __init__(self, repo, binsha): | |||
| 40 | 58 | assert len(binsha) == 20, "Require 20 byte binary sha, got %r, len = %i" % (binsha, len(binsha)) | |
| 41 | 59 | ||
| 42 | 60 | @classmethod | |
| 43 | - def new(cls, repo, id): # @ReservedAssignment | ||
| 61 | + def new(cls, repo: 'Repo', id): # @ReservedAssignment | ||
| 44 | 62 | """ | |
| 45 | 63 | :return: New Object instance of a type appropriate to the object type behind | |
| 46 | 64 | id. The id of the newly created object will be a binsha even though | |
@@ -53,7 +71,7 @@ def new(cls, repo, id): # @ReservedAssignment | |||
| 53 | 71 | return repo.rev_parse(str(id)) | |
| 54 | 72 | ||
| 55 | 73 | @classmethod | |
| 56 | - def new_from_sha(cls, repo, sha1): | ||
| 74 | + def new_from_sha(cls, repo: 'Repo', sha1: bytes) -> Union['Commit', 'TagObject', 'Tree', 'Blob']: | ||
| 57 | 75 | """ | |
| 58 | 76 | :return: new object instance of a type appropriate to represent the given | |
| 59 | 77 | binary sha1 | |
@@ -67,7 +85,7 @@ def new_from_sha(cls, repo, sha1): | |||
| 67 | 85 | inst.size = oinfo.size | |
| 68 | 86 | return inst | |
| 69 | 87 | ||
| 70 | - def _set_cache_(self, attr): | ||
| 88 | + def _set_cache_(self, attr: str) -> None: | ||
| 71 | 89 | """Retrieve object information""" | |
| 72 | 90 | if attr == "size": | |
| 73 | 91 | oinfo = self.repo.odb.info(self.binsha) | |
@@ -76,43 +94,43 @@ def _set_cache_(self, attr): | |||
| 76 | 94 | else: | |
| 77 | 95 | super(Object, self)._set_cache_(attr) | |
| 78 | 96 | ||
| 79 | - def __eq__(self, other): | ||
| 97 | + def __eq__(self, other: Any) -> bool: | ||
| 80 | 98 | """:return: True if the objects have the same SHA1""" | |
| 81 | 99 | if not hasattr(other, 'binsha'): | |
| 82 | 100 | return False | |
| 83 | 101 | return self.binsha == other.binsha | |
| 84 | 102 | ||
| 85 | - def __ne__(self, other): | ||
| 103 | + def __ne__(self, other: Any) -> bool: | ||
| 86 | 104 | """:return: True if the objects do not have the same SHA1 """ | |
| 87 | 105 | if not hasattr(other, 'binsha'): | |
| 88 | 106 | return True | |
| 89 | 107 | return self.binsha != other.binsha | |
| 90 | 108 | ||
| 91 | - def __hash__(self): | ||
| 109 | + def __hash__(self) -> int: | ||
| 92 | 110 | """:return: Hash of our id allowing objects to be used in dicts and sets""" | |
| 93 | 111 | return hash(self.binsha) | |
| 94 | 112 | ||
| 95 | - def __str__(self): | ||
| 113 | + def __str__(self) -> str: | ||
| 96 | 114 | """:return: string of our SHA1 as understood by all git commands""" | |
| 97 | 115 | return self.hexsha | |
| 98 | 116 | ||
| 99 | - def __repr__(self): | ||
| 117 | + def __repr__(self) -> str: | ||
| 100 | 118 | """:return: string with pythonic representation of our object""" | |
| 101 | 119 | return '<git.%s "%s">' % (self.__class__.__name__, self.hexsha) | |
| 102 | 120 | ||
| 103 | 121 | @property | |
| 104 | - def hexsha(self): | ||
| 122 | + def hexsha(self) -> str: | ||
| 105 | 123 | """:return: 40 byte hex version of our 20 byte binary sha""" | |
| 106 | 124 | # b2a_hex produces bytes | |
| 107 | 125 | return bin_to_hex(self.binsha).decode('ascii') | |
| 108 | 126 | ||
| 109 | 127 | @property | |
| 110 | - def data_stream(self): | ||
| 128 | + def data_stream(self) -> 'OStream': | ||
| 111 | 129 | """ :return: File Object compatible stream to the uncompressed raw data of the object | |
| 112 | 130 | :note: returned streams must be read in order""" | |
| 113 | 131 | return self.repo.odb.stream(self.binsha) | |
| 114 | 132 | ||
| 115 | - def stream_data(self, ostream): | ||
| 133 | + def stream_data(self, ostream: 'OStream') -> 'Object': | ||
| 116 | 134 | """Writes our data directly to the given output stream | |
| 117 | 135 | :param ostream: File object compatible stream object. | |
| 118 | 136 | :return: self""" | |
@@ -130,7 +148,9 @@ class IndexObject(Object): | |||
| 130 | 148 | # for compatibility with iterable lists | |
| 131 | 149 | _id_attribute_ = 'path' | |
| 132 | 150 | ||
| 133 | - def __init__(self, repo, binsha, mode=None, path=None): | ||
| 151 | + def __init__(self, | ||
| 152 | + repo: 'Repo', binsha: bytes, mode: Union[None, int] = None, path: Union[None, PathLike] = None | ||
| 153 | + ) -> None: | ||
| 134 | 154 | """Initialize a newly instanced IndexObject | |
| 135 | 155 | ||
| 136 | 156 | :param repo: is the Repo we are located in | |
@@ -150,14 +170,14 @@ def __init__(self, repo, binsha, mode=None, path=None): | |||
| 150 | 170 | if path is not None: | |
| 151 | 171 | self.path = path | |
| 152 | 172 | ||
| 153 | - def __hash__(self): | ||
| 173 | + def __hash__(self) -> int: | ||
| 154 | 174 | """ | |
| 155 | 175 | :return: | |
| 156 | 176 | Hash of our path as index items are uniquely identifiable by path, not | |
| 157 | 177 | by their data !""" | |
| 158 | 178 | return hash(self.path) | |
| 159 | 179 | ||
| 160 | - def _set_cache_(self, attr): | ||
| 180 | + def _set_cache_(self, attr: str) -> None: | ||
| 161 | 181 | if attr in IndexObject.__slots__: | |
| 162 | 182 | # they cannot be retrieved lateron ( not without searching for them ) | |
| 163 | 183 | raise AttributeError( | |
@@ -168,16 +188,19 @@ def _set_cache_(self, attr): | |||
| 168 | 188 | # END handle slot attribute | |
| 169 | 189 | ||
| 170 | 190 | @property | |
| 171 | - def name(self): | ||
| 191 | + def name(self) -> str: | ||
| 172 | 192 | """:return: Name portion of the path, effectively being the basename""" | |
| 173 | 193 | return osp.basename(self.path) | |
| 174 | 194 | ||
| 175 | 195 | @property | |
| 176 | - def abspath(self): | ||
| 196 | + def abspath(self) -> PathLike: | ||
| 177 | 197 | """ | |
| 178 | 198 | :return: | |
| 179 | 199 | Absolute path to this index object in the file system ( as opposed to the | |
| 180 | 200 | .path field which is a path relative to the git repository ). | |
| 181 | 201 | ||
| 182 | 202 | The returned path will be native to the system and contains '\' on windows. """ | |
| 183 | - return join_path_native(self.repo.working_tree_dir, self.path) | ||
| 203 | + if self.repo.working_tree_dir is not None: | ||
| 204 | + return join_path_native(self.repo.working_tree_dir, self.path) | ||
| 205 | + else: | ||
| 206 | + raise WorkTreeRepositoryUnsupported | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments