summaryrefslogtreecommitdiff
path: root/git/index
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2021-07-20 07:04:18 +0800
committerGitHub <noreply@github.com>2021-07-20 07:04:18 +0800
commitcfd653aeeb3cb807f2cd2ae9fff5568880ac67da (patch)
tree8514389e82efdcdcdeaab5d3a85553883f28117c /git/index
parentacbd6bad9ded9a1d59e80e71d334d64b0244f5cd (diff)
parent600df043e76924d43a4f9f88f4e3241740f34c77 (diff)
downloadgitpython-cfd653aeeb3cb807f2cd2ae9fff5568880ac67da.tar.gz
Merge pull request #1295 from Yobmod/main
Add types to refs
Diffstat (limited to 'git/index')
-rw-r--r--git/index/__init__.py2
-rw-r--r--git/index/base.py31
-rw-r--r--git/index/fun.py10
3 files changed, 21 insertions, 22 deletions
diff --git a/git/index/__init__.py b/git/index/__init__.py
index 2516f01f..96b721f0 100644
--- a/git/index/__init__.py
+++ b/git/index/__init__.py
@@ -1,6 +1,4 @@
"""Initialize the index package"""
# flake8: noqa
-from __future__ import absolute_import
-
from .base import *
from .typ import *
diff --git a/git/index/base.py b/git/index/base.py
index 3aa06e38..6452419c 100644
--- a/git/index/base.py
+++ b/git/index/base.py
@@ -123,7 +123,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
self.repo = repo
self.version = self._VERSION
self._extension_data = b''
- self._file_path = file_path or self._index_path() # type: PathLike
+ self._file_path: PathLike = file_path or self._index_path()
def _set_cache_(self, attr: str) -> None:
if attr == "entries":
@@ -136,7 +136,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
ok = True
except OSError:
# in new repositories, there may be no index, which means we are empty
- self.entries = {} # type: Dict[Tuple[PathLike, StageType], IndexEntry]
+ self.entries: Dict[Tuple[PathLike, StageType], IndexEntry] = {}
return None
finally:
if not ok:
@@ -266,7 +266,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# -i : ignore working tree status
# --aggressive : handle more merge cases
# -m : do an actual merge
- args = ["--aggressive", "-i", "-m"] # type: List[Union[Treeish, str]]
+ args: List[Union[Treeish, str]] = ["--aggressive", "-i", "-m"]
if base is not None:
args.append(base)
args.append(rhs)
@@ -288,14 +288,14 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
New IndexFile instance. Its path will be undefined.
If you intend to write such a merged Index, supply an alternate file_path
to its 'write' method."""
- tree_sha_bytes = [to_bin_sha(str(t)) for t in tree_sha] # List[bytes]
+ tree_sha_bytes: List[bytes] = [to_bin_sha(str(t)) for t in tree_sha]
base_entries = aggressive_tree_merge(repo.odb, tree_sha_bytes)
inst = cls(repo)
# convert to entries dict
- entries = dict(zip(
+ entries: Dict[Tuple[PathLike, int], IndexEntry] = dict(zip(
((e.path, e.stage) for e in base_entries),
- (IndexEntry.from_base(e) for e in base_entries))) # type: Dict[Tuple[PathLike, int], IndexEntry]
+ (IndexEntry.from_base(e) for e in base_entries)))
inst.entries = entries
return inst
@@ -338,7 +338,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
if len(treeish) == 0 or len(treeish) > 3:
raise ValueError("Please specify between 1 and 3 treeish, got %i" % len(treeish))
- arg_list = [] # type: List[Union[Treeish, str]]
+ arg_list: List[Union[Treeish, str]] = []
# ignore that working tree and index possibly are out of date
if len(treeish) > 1:
# drop unmerged entries when reading our index and merging
@@ -445,7 +445,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
we will close stdin to break the pipe."""
fprogress(filepath, False, item)
- rval = None # type: Union[None, str]
+ rval: Union[None, str] = None
if proc.stdin is not None:
try:
@@ -492,7 +492,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
are at stage 3 will not have a stage 3 entry.
"""
is_unmerged_blob = lambda t: t[0] != 0
- path_map = {} # type: Dict[PathLike, List[Tuple[TBD, Blob]]]
+ path_map: Dict[PathLike, List[Tuple[TBD, Blob]]] = {}
for stage, blob in self.iter_blobs(is_unmerged_blob):
path_map.setdefault(blob.path, []).append((stage, blob))
# END for each unmerged blob
@@ -572,7 +572,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
# note: additional deserialization could be saved if write_tree_from_cache
# would return sorted tree entries
root_tree = Tree(self.repo, binsha, path='')
- root_tree._cache = tree_items # type: ignore # should this be encoded to [bytes, int, str]?
+ root_tree._cache = tree_items
return root_tree
def _process_diff_args(self, # type: ignore[override]
@@ -586,8 +586,9 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
return args
def _to_relative_path(self, path: PathLike) -> PathLike:
- """:return: Version of path relative to our git directory or raise ValueError
- if it is not within our git direcotory"""
+ """
+ :return: Version of path relative to our git directory or raise ValueError
+ if it is not within our git direcotory"""
if not osp.isabs(path):
return path
if self.repo.bare:
@@ -623,8 +624,8 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
st = os.lstat(filepath) # handles non-symlinks as well
if S_ISLNK(st.st_mode):
# in PY3, readlink is string, but we need bytes. In PY2, it's just OS encoded bytes, we assume UTF-8
- open_stream = lambda: BytesIO(force_bytes(os.readlink(filepath),
- encoding=defenc)) # type: Callable[[], BinaryIO]
+ open_stream: Callable[[], BinaryIO] = lambda: BytesIO(force_bytes(os.readlink(filepath),
+ encoding=defenc))
else:
open_stream = lambda: open(filepath, 'rb')
with open_stream() as stream:
@@ -1159,7 +1160,7 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
proc = self.repo.git.checkout_index(args, **kwargs)
# FIXME: Reading from GIL!
make_exc = lambda: GitCommandError(("git-checkout-index",) + tuple(args), 128, proc.stderr.read())
- checked_out_files = [] # type: List[PathLike]
+ checked_out_files: List[PathLike] = []
for path in paths:
co_path = to_native_path_linux(self._to_relative_path(path))
diff --git a/git/index/fun.py b/git/index/fun.py
index e5e566a0..e071e15c 100644
--- a/git/index/fun.py
+++ b/git/index/fun.py
@@ -99,8 +99,8 @@ def run_commit_hook(name: str, index: 'IndexFile', *args: str) -> None:
except Exception as ex:
raise HookExecutionError(hp, ex) from ex
else:
- stdout_list = [] # type: List[str]
- stderr_list = [] # type: List[str]
+ stdout_list: List[str] = []
+ stderr_list: List[str] = []
handle_process_output(cmd, stdout_list.append, stderr_list.append, finalize_process)
stdout = ''.join(stdout_list)
stderr = ''.join(stderr_list)
@@ -151,8 +151,8 @@ def write_cache(entries: Sequence[Union[BaseIndexEntry, 'IndexEntry']], stream:
beginoffset = tell()
write(entry[4]) # ctime
write(entry[5]) # mtime
- path_str = entry[3] # type: str
- path = force_bytes(path_str, encoding=defenc)
+ path_str: str = entry[3]
+ path: bytes = force_bytes(path_str, encoding=defenc)
plen = len(path) & CE_NAMEMASK # path length
assert plen == len(path), "Path %s too long to fit into index" % entry[3]
flags = plen | (entry[2] & CE_NAMEMASK_INV) # clear possible previous values
@@ -210,7 +210,7 @@ def read_cache(stream: IO[bytes]) -> Tuple[int, Dict[Tuple[PathLike, int], 'Inde
* content_sha is a 20 byte sha on all cache file contents"""
version, num_entries = read_header(stream)
count = 0
- entries = {} # type: Dict[Tuple[PathLike, int], 'IndexEntry']
+ entries: Dict[Tuple[PathLike, int], 'IndexEntry'] = {}
read = stream.read
tell = stream.tell