From 647101833ae276f3b923583e202faa3f7d78e218 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 14:25:23 +0100 Subject: Improve types of @unbare_repo and @git_working_dir decorators --- git/index/base.py | 6 +++--- git/index/util.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index f4ffba7b..8346d24a 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -410,7 +410,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # whose name contains wildcard characters. if abs_path not in resolved_paths: for f in self._iter_expand_paths(glob.glob(abs_path)): - yield f.replace(rs, '') + yield str(f).replace(rs, '') continue # END glob handling try: @@ -635,7 +635,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): @git_working_dir def _entries_for_paths(self, paths: List[str], path_rewriter: Callable, fprogress: Callable, entries: List[BaseIndexEntry]) -> List[BaseIndexEntry]: - entries_added = [] # type: List[BaseIndexEntry] + entries_added: List[BaseIndexEntry] = [] if path_rewriter: for path in paths: if osp.isabs(path): @@ -769,7 +769,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # automatically # paths can be git-added, for everything else we use git-update-index paths, entries = self._preprocess_add_items(items) - entries_added = [] + entries_added: List[BaseIndexEntry] = [] # This code needs a working tree, therefore we try not to run it unless required. # That way, we are OK on a bare repository as well. # If there are no paths, the rewriter has nothing to do either diff --git a/git/index/util.py b/git/index/util.py index 471e9262..e0daef0c 100644 --- a/git/index/util.py +++ b/git/index/util.py @@ -13,7 +13,7 @@ import os.path as osp from typing import (Any, Callable) -from git.types import PathLike +from git.types import PathLike, _T # --------------------------------------------------------------------------------- @@ -88,12 +88,12 @@ def default_index(func: Callable[..., Any]) -> Callable[..., Any]: return check_default_index -def git_working_dir(func: Callable[..., Any]) -> Callable[..., None]: +def git_working_dir(func: Callable[..., _T]) -> Callable[..., _T]: """Decorator which changes the current working dir to the one of the git repository in order to assure relative paths are handled correctly""" @wraps(func) - def set_git_working_dir(self, *args: Any, **kwargs: Any) -> None: + def set_git_working_dir(self, *args: Any, **kwargs: Any) -> _T: cur_wd = os.getcwd() os.chdir(self.repo.working_tree_dir) try: -- cgit v1.2.1 From 06eca0b84a4538c642c5e1afa2f3441a96bef444 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 16:47:39 +0100 Subject: Make subodule a forward ref in Index.base --- git/index/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 8346d24a..b37883a6 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -22,7 +22,6 @@ from git.exc import ( ) from git.objects import ( Blob, - Submodule, Tree, Object, Commit, @@ -76,6 +75,7 @@ if TYPE_CHECKING: from git.repo import Repo from git.refs.reference import Reference from git.util import Actor + from git.objects.submodule.base import Submodule StageType = int @@ -842,7 +842,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): items = [items] for item in items: - if isinstance(item, (BaseIndexEntry, (Blob, Submodule))): + if isinstance(item, (BaseIndexEntry, (Blob, 'Submodule'))): paths.append(self._to_relative_path(item.path)) elif isinstance(item, str): paths.append(self._to_relative_path(item)) @@ -853,7 +853,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): @post_clear_cache @default_index - def remove(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, Submodule]], working_tree: bool = False, + def remove(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']], working_tree: bool = False, **kwargs: Any) -> List[str]: """Remove the given items from the index and optionally from the working tree as well. @@ -905,7 +905,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): @post_clear_cache @default_index - def move(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, Submodule]], skip_errors: bool = False, + def move(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']], skip_errors: bool = False, **kwargs: Any) -> List[Tuple[str, str]]: """Rename/move the items, whereas the last item is considered the destination of the move operation. If the destination is a file, the first item ( of two ) -- cgit v1.2.1 From 33ffd0b2ed117d043fe828e5f2eabe5c8f8b0b66 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 16:51:34 +0100 Subject: Make subodule a forward ref in Index.base2 --- git/index/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index b37883a6..5a564b8c 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -75,7 +75,7 @@ if TYPE_CHECKING: from git.repo import Repo from git.refs.reference import Reference from git.util import Actor - from git.objects.submodule.base import Submodule + from git.objects import Submodule StageType = int -- cgit v1.2.1 From f372187ade056a3069e68ba0a90bf53bd7d7e464 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 17:00:44 +0100 Subject: Make subodule a forward ref in Index.base3 --- git/index/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 5a564b8c..edb79edf 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -593,7 +593,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): raise ValueError("Absolute path %r is not in git repository at %r" % (path, self.repo.working_tree_dir)) return os.path.relpath(path, self.repo.working_tree_dir) - def _preprocess_add_items(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, Submodule]] + def _preprocess_add_items(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']] ) -> Tuple[List[PathLike], List[BaseIndexEntry]]: """ Split the items into two lists of path strings and BaseEntries. """ paths = [] @@ -664,7 +664,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END path handling return entries_added - def add(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, Submodule]], force: bool = True, + def add(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']], force: bool = True, fprogress: Callable = lambda *args: None, path_rewriter: Callable = None, write: bool = True, write_extension_data: bool = False) -> List[BaseIndexEntry]: """Add files from the working tree, specific blobs or BaseIndexEntries -- cgit v1.2.1 From de36cb6f0391fcf4d756909e0cec429599585700 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 17:04:48 +0100 Subject: UnMake subodule a forward ref in Index.base --- git/index/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index edb79edf..b54a19a7 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -22,6 +22,7 @@ from git.exc import ( ) from git.objects import ( Blob, + Submodule, Tree, Object, Commit, @@ -75,7 +76,6 @@ if TYPE_CHECKING: from git.repo import Repo from git.refs.reference import Reference from git.util import Actor - from git.objects import Submodule StageType = int -- cgit v1.2.1 From 3cc0edce2a0deb159cfb3dca10b6044086900ce9 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 17:07:40 +0100 Subject: UnMake subodule a forward ref in Index.base2 --- git/index/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index b54a19a7..50bcf504 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -842,7 +842,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): items = [items] for item in items: - if isinstance(item, (BaseIndexEntry, (Blob, 'Submodule'))): + if isinstance(item, (BaseIndexEntry, (Blob, Submodule))): paths.append(self._to_relative_path(item.path)) elif isinstance(item, str): paths.append(self._to_relative_path(item)) -- cgit v1.2.1 From 28bde3978b4ca18dc97488b88b4424a2d521ac68 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 17:14:43 +0100 Subject: Type index _items_to_rela_paths() --- git/index/base.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 50bcf504..c6d92526 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -833,12 +833,13 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return entries_added - def _items_to_rela_paths(self, items): + def _items_to_rela_paths(self, items: Union[PathLike, Sequence[Union[PathLike, BaseIndexEntry, Blob, Submodule]]] + ) -> List[PathLike]: """Returns a list of repo-relative paths from the given items which may be absolute or relative paths, entries or blobs""" paths = [] # if string put in list - if isinstance(items, str): + if isinstance(items, (str, os.PathLike)): items = [items] for item in items: @@ -851,8 +852,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END for each item return paths - @post_clear_cache - @default_index + @ post_clear_cache + @ default_index def remove(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']], working_tree: bool = False, **kwargs: Any) -> List[str]: """Remove the given items from the index and optionally from @@ -903,8 +904,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # rm 'path' return [p[4:-1] for p in removed_paths] - @post_clear_cache - @default_index + @ post_clear_cache + @ default_index def move(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']], skip_errors: bool = False, **kwargs: Any) -> List[Tuple[str, str]]: """Rename/move the items, whereas the last item is considered the destination of @@ -1023,7 +1024,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): proc.wait() return stdout - @default_index + @ default_index def checkout(self, paths: Union[None, Iterable[PathLike]] = None, force: bool = False, fprogress: Callable = lambda *args: None, **kwargs: Any ) -> Union[None, Iterator[PathLike], Sequence[PathLike]]: @@ -1192,7 +1193,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END paths handling assert "Should not reach this point" - @default_index + @ default_index def reset(self, commit: Union[Commit, 'Reference', str] = 'HEAD', working_tree: bool = False, paths: Union[None, Iterable[PathLike]] = None, head: bool = False, **kwargs: Any) -> 'IndexFile': @@ -1262,7 +1263,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return self - @default_index + @ default_index def diff(self, other: Union[diff.Diffable.Index, 'IndexFile.Index', Treeish, None, object] = diff.Diffable.Index, paths: Union[str, List[PathLike], Tuple[PathLike, ...]] = None, create_patch: bool = False, **kwargs: Any ) -> diff.DiffIndex: -- cgit v1.2.1 From 873ebe61431c50bb39afd5cafff498b3e1879342 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Tue, 6 Jul 2021 17:56:24 +0100 Subject: Make diff.DiffIndex generic List['Diff'] --- git/index/util.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/index') diff --git a/git/index/util.py b/git/index/util.py index e0daef0c..3b3d6489 100644 --- a/git/index/util.py +++ b/git/index/util.py @@ -52,7 +52,7 @@ class TemporaryFileSwap(object): #{ Decorators -def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: +def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]: """Decorator for functions that alter the index using the git command. This would invalidate our possibly existing entries dictionary which is why it must be deleted to allow it to be lazily reread later. @@ -63,7 +63,7 @@ def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: """ @wraps(func) - def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> Any: + def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> _T: rval = func(self, *args, **kwargs) self._delete_entries_cache() return rval @@ -72,13 +72,13 @@ def post_clear_cache(func: Callable[..., Any]) -> Callable[..., Any]: return post_clear_cache_if_not_raised -def default_index(func: Callable[..., Any]) -> Callable[..., Any]: +def default_index(func: Callable[..., _T]) -> Callable[..., _T]: """Decorator assuring the wrapped method may only run if we are the default repository index. This is as we rely on git commands that operate on that index only. """ @wraps(func) - def check_default_index(self, *args: Any, **kwargs: Any) -> Any: + def check_default_index(self, *args: Any, **kwargs: Any) -> _T: if self._file_path != self._index_path(): raise AssertionError( "Cannot call %r on indices that do not represent the default git index" % func.__name__) -- cgit v1.2.1 From 2e2fe186d09272c3cb6c96467fff362deb90994f Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 11:30:16 +0100 Subject: Increase mypy strictness (no_implicit_optional & warn_redundant_casts) and fix errors --- git/index/base.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index c6d92526..d6670b2a 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -113,7 +113,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): _VERSION = 2 # latest version we support S_IFGITLINK = S_IFGITLINK # a submodule - def __init__(self, repo: 'Repo', file_path: PathLike = None) -> None: + def __init__(self, repo: 'Repo', file_path: Union[PathLike, None] = None) -> None: """Initialize this Index instance, optionally from the given ``file_path``. If no file_path is given, we will be created from the current index file. @@ -665,7 +665,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return entries_added def add(self, items: Sequence[Union[PathLike, Blob, BaseIndexEntry, 'Submodule']], force: bool = True, - fprogress: Callable = lambda *args: None, path_rewriter: Callable = None, + fprogress: Callable = lambda *args: None, path_rewriter: Union[Callable[..., PathLike], None] = None, write: bool = True, write_extension_data: bool = False) -> List[BaseIndexEntry]: """Add files from the working tree, specific blobs or BaseIndexEntries to the index. @@ -970,7 +970,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return out def commit(self, message: str, parent_commits=None, head: bool = True, author: Union[None, 'Actor'] = None, - committer: Union[None, 'Actor'] = None, author_date: str = None, commit_date: str = None, + committer: Union[None, 'Actor'] = None, author_date: Union[str, None] = None, + commit_date: Union[str, None] = None, skip_hooks: bool = False) -> Commit: """Commit the current default index file, creating a commit object. For more information on the arguments, see tree.commit. @@ -1265,7 +1266,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): @ default_index def diff(self, other: Union[diff.Diffable.Index, 'IndexFile.Index', Treeish, None, object] = diff.Diffable.Index, - paths: Union[str, List[PathLike], Tuple[PathLike, ...]] = None, create_patch: bool = False, **kwargs: Any + paths: Union[str, List[PathLike], Tuple[PathLike, ...], None] = None, + create_patch: bool = False, **kwargs: Any ) -> diff.DiffIndex: """Diff this index against the working copy or a Tree or Commit object -- cgit v1.2.1 From 5d3818ed3d51d400517a352b5b62e966164af8cf Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 21:42:30 +0100 Subject: Finish initial typing of index folder --- git/index/base.py | 31 +++++++++++++++++----------- git/index/fun.py | 62 +++++++++++++++++++++++++++++++------------------------ git/index/util.py | 13 +++++++----- 3 files changed, 62 insertions(+), 44 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index d6670b2a..1812faee 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -18,6 +18,7 @@ from git.compat import ( from git.exc import ( GitCommandError, CheckoutError, + GitError, InvalidGitRepositoryError ) from git.objects import ( @@ -66,10 +67,10 @@ from .util import ( # typing ----------------------------------------------------------------------------- -from typing import (Any, BinaryIO, Callable, Dict, IO, Iterable, Iterator, List, +from typing import (Any, BinaryIO, Callable, Dict, IO, Iterable, Iterator, List, NoReturn, Sequence, TYPE_CHECKING, Tuple, Union) -from git.types import PathLike, TBD +from git.types import Commit_ish, PathLike, TBD if TYPE_CHECKING: from subprocess import Popen @@ -372,13 +373,13 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # UTILITIES @unbare_repo - def _iter_expand_paths(self, paths: Sequence[PathLike]) -> Iterator[PathLike]: + def _iter_expand_paths(self: 'IndexFile', paths: Sequence[PathLike]) -> Iterator[PathLike]: """Expand the directories in list of paths to the corresponding paths accordingly, Note: git will add items multiple times even if a glob overlapped with manually specified paths or if paths where specified multiple times - we respect that and do not prune""" - def raise_exc(e): + def raise_exc(e: Exception) -> NoReturn: raise e r = str(self.repo.working_tree_dir) rs = r + os.sep @@ -426,7 +427,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # END path exception handling # END for each path - def _write_path_to_stdin(self, proc: 'Popen', filepath: PathLike, item, fmakeexc, fprogress, + def _write_path_to_stdin(self, proc: 'Popen', filepath: PathLike, item: TBD, fmakeexc: Callable[..., GitError], + fprogress: Callable[[PathLike, bool, TBD], None], read_from_stdout: bool = True) -> Union[None, str]: """Write path to proc.stdin and make sure it processes the item, including progress. @@ -498,7 +500,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): line.sort() return path_map - @classmethod + @ classmethod def entry_key(cls, *entry: Union[BaseIndexEntry, PathLike, StageType]) -> Tuple[PathLike, StageType]: return entry_key(*entry) @@ -631,8 +633,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return BaseIndexEntry((stat_mode_to_index_mode(st.st_mode), istream.binsha, 0, to_native_path_linux(filepath))) - @unbare_repo - @git_working_dir + @ unbare_repo + @ git_working_dir def _entries_for_paths(self, paths: List[str], path_rewriter: Callable, fprogress: Callable, entries: List[BaseIndexEntry]) -> List[BaseIndexEntry]: entries_added: List[BaseIndexEntry] = [] @@ -788,8 +790,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): # create objects if required, otherwise go with the existing shas null_entries_indices = [i for i, e in enumerate(entries) if e.binsha == Object.NULL_BIN_SHA] if null_entries_indices: - @git_working_dir - def handle_null_entries(self): + @ git_working_dir + def handle_null_entries(self: 'IndexFile') -> None: for ei in null_entries_indices: null_entry = entries[ei] new_entry = self._store_path(null_entry.path, fprogress) @@ -969,8 +971,13 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return out - def commit(self, message: str, parent_commits=None, head: bool = True, author: Union[None, 'Actor'] = None, - committer: Union[None, 'Actor'] = None, author_date: Union[str, None] = None, + def commit(self, + message: str, + parent_commits: Union[Commit_ish, None] = None, + head: bool = True, + author: Union[None, 'Actor'] = None, + committer: Union[None, 'Actor'] = None, + author_date: Union[str, None] = None, commit_date: Union[str, None] = None, skip_hooks: bool = False) -> Commit: """Commit the current default index file, creating a commit object. diff --git a/git/index/fun.py b/git/index/fun.py index ffd109b1..74f6efbf 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -57,6 +57,7 @@ from git.types import PathLike, TypeGuard if TYPE_CHECKING: from .base import IndexFile + from git.objects.fun import EntryTup # ------------------------------------------------------------------------------------ @@ -188,7 +189,7 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i def is_entry_tuple(entry: Tuple) -> TypeGuard[Tuple[PathLike, int]]: return isinstance(entry, tuple) and len(entry) == 2 - + if len(entry) == 1: entry_first = entry[0] assert isinstance(entry_first, BaseIndexEntry) @@ -259,8 +260,8 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 :param sl: slice indicating the range we should process on the entries list :return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of tree entries being a tuple of hexsha, mode, name""" - tree_items = [] # type: List[Tuple[Union[bytes, str], int, str]] - tree_items_append = tree_items.append + tree_items: List[Tuple[bytes, int, str]] = [] + ci = sl.start end = sl.stop while ci < end: @@ -272,7 +273,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 rbound = entry.path.find('/', si) if rbound == -1: # its not a tree - tree_items_append((entry.binsha, entry.mode, entry.path[si:])) + tree_items.append((entry.binsha, entry.mode, entry.path[si:])) else: # find common base range base = entry.path[si:rbound] @@ -289,7 +290,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 # enter recursion # ci - 1 as we want to count our current item as well sha, _tree_entry_list = write_tree_from_cache(entries, odb, slice(ci - 1, xi), rbound + 1) - tree_items_append((sha, S_IFDIR, base)) + tree_items.append((sha, S_IFDIR, base)) # skip ahead ci = xi @@ -306,7 +307,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 return (istream.binsha, tree_items_stringified) -def _tree_entry_to_baseindexentry(tree_entry: Tuple[str, int, str], stage: int) -> BaseIndexEntry: +def _tree_entry_to_baseindexentry(tree_entry: Tuple[bytes, int, str], stage: int) -> BaseIndexEntry: return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2])) @@ -319,14 +320,13 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr :param tree_shas: 1, 2 or 3 trees as identified by their binary 20 byte shas If 1 or two, the entries will effectively correspond to the last given tree If 3 are given, a 3 way merge is performed""" - out = [] # type: List[BaseIndexEntry] - out_append = out.append + out: List[BaseIndexEntry] = [] # one and two way is the same for us, as we don't have to handle an existing # index, instrea if len(tree_shas) in (1, 2): for entry in traverse_tree_recursive(odb, tree_shas[-1], ''): - out_append(_tree_entry_to_baseindexentry(entry, 0)) + out.append(_tree_entry_to_baseindexentry(entry, 0)) # END for each entry return out # END handle single tree @@ -334,8 +334,16 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr if len(tree_shas) > 3: raise ValueError("Cannot handle %i trees at once" % len(tree_shas)) + EntryTupOrNone = Union[EntryTup, None] + + def is_three_entry_list(inp) -> TypeGuard[List[EntryTupOrNone]]: + return isinstance(inp, list) and len(inp) == 3 + # three trees - for base, ours, theirs in traverse_trees_recursive(odb, tree_shas, ''): + for three_entries in traverse_trees_recursive(odb, tree_shas, ''): + + assert is_three_entry_list(three_entries) + base, ours, theirs = three_entries if base is not None: # base version exists if ours is not None: @@ -347,23 +355,23 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr if(base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or \ (base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]): # changed by both - out_append(_tree_entry_to_baseindexentry(base, 1)) - out_append(_tree_entry_to_baseindexentry(ours, 2)) - out_append(_tree_entry_to_baseindexentry(theirs, 3)) + out.append(_tree_entry_to_baseindexentry(base, 1)) + out.append(_tree_entry_to_baseindexentry(ours, 2)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) elif base[0] != ours[0] or base[1] != ours[1]: # only we changed it - out_append(_tree_entry_to_baseindexentry(ours, 0)) + out.append(_tree_entry_to_baseindexentry(ours, 0)) else: # either nobody changed it, or they did. In either # case, use theirs - out_append(_tree_entry_to_baseindexentry(theirs, 0)) + out.append(_tree_entry_to_baseindexentry(theirs, 0)) # END handle modification else: if ours[0] != base[0] or ours[1] != base[1]: # they deleted it, we changed it, conflict - out_append(_tree_entry_to_baseindexentry(base, 1)) - out_append(_tree_entry_to_baseindexentry(ours, 2)) + out.append(_tree_entry_to_baseindexentry(base, 1)) + out.append(_tree_entry_to_baseindexentry(ours, 2)) # else: # we didn't change it, ignore # pass @@ -376,8 +384,8 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr else: if theirs[0] != base[0] or theirs[1] != base[1]: # deleted in ours, changed theirs, conflict - out_append(_tree_entry_to_baseindexentry(base, 1)) - out_append(_tree_entry_to_baseindexentry(theirs, 3)) + out.append(_tree_entry_to_baseindexentry(base, 1)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) # END theirs changed # else: # theirs didn't change @@ -386,20 +394,20 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr # END handle ours else: # all three can't be None - if ours is None: + if ours is None and theirs is not None: # added in their branch - out_append(_tree_entry_to_baseindexentry(theirs, 0)) - elif theirs is None: + out.append(_tree_entry_to_baseindexentry(theirs, 0)) + elif theirs is None and ours is not None: # added in our branch - out_append(_tree_entry_to_baseindexentry(ours, 0)) - else: + out.append(_tree_entry_to_baseindexentry(ours, 0)) + elif ours is not None and theirs is not None: # both have it, except for the base, see whether it changed if ours[0] != theirs[0] or ours[1] != theirs[1]: - out_append(_tree_entry_to_baseindexentry(ours, 2)) - out_append(_tree_entry_to_baseindexentry(theirs, 3)) + out.append(_tree_entry_to_baseindexentry(ours, 2)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) else: # it was added the same in both - out_append(_tree_entry_to_baseindexentry(ours, 0)) + out.append(_tree_entry_to_baseindexentry(ours, 0)) # END handle two items # END handle heads # END handle base exists diff --git a/git/index/util.py b/git/index/util.py index 3b3d6489..4f8af553 100644 --- a/git/index/util.py +++ b/git/index/util.py @@ -11,10 +11,13 @@ import os.path as osp # typing ---------------------------------------------------------------------- -from typing import (Any, Callable) +from typing import (Any, Callable, TYPE_CHECKING) from git.types import PathLike, _T +if TYPE_CHECKING: + from git.index import IndexFile + # --------------------------------------------------------------------------------- @@ -63,7 +66,7 @@ def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]: """ @wraps(func) - def post_clear_cache_if_not_raised(self, *args: Any, **kwargs: Any) -> _T: + def post_clear_cache_if_not_raised(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T: rval = func(self, *args, **kwargs) self._delete_entries_cache() return rval @@ -78,7 +81,7 @@ def default_index(func: Callable[..., _T]) -> Callable[..., _T]: on that index only. """ @wraps(func) - def check_default_index(self, *args: Any, **kwargs: Any) -> _T: + def check_default_index(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T: if self._file_path != self._index_path(): raise AssertionError( "Cannot call %r on indices that do not represent the default git index" % func.__name__) @@ -93,9 +96,9 @@ def git_working_dir(func: Callable[..., _T]) -> Callable[..., _T]: repository in order to assure relative paths are handled correctly""" @wraps(func) - def set_git_working_dir(self, *args: Any, **kwargs: Any) -> _T: + def set_git_working_dir(self: 'IndexFile', *args: Any, **kwargs: Any) -> _T: cur_wd = os.getcwd() - os.chdir(self.repo.working_tree_dir) + os.chdir(str(self.repo.working_tree_dir)) try: return func(self, *args, **kwargs) finally: -- cgit v1.2.1 From 1533596b03ef07b07311821d90de3ef72abba5d6 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 22:20:59 +0100 Subject: Mak EntryTup a froward ref --- git/index/fun.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index 74f6efbf..382e3d44 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -57,7 +57,11 @@ from git.types import PathLike, TypeGuard if TYPE_CHECKING: from .base import IndexFile - from git.objects.fun import EntryTup + from git.objects.fun import EntryTupOrNone + + +def is_three_entry_list(inp) -> TypeGuard[List['EntryTupOrNone']]: + return isinstance(inp, list) and len(inp) == 3 # ------------------------------------------------------------------------------------ @@ -334,11 +338,6 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr if len(tree_shas) > 3: raise ValueError("Cannot handle %i trees at once" % len(tree_shas)) - EntryTupOrNone = Union[EntryTup, None] - - def is_three_entry_list(inp) -> TypeGuard[List[EntryTupOrNone]]: - return isinstance(inp, list) and len(inp) == 3 - # three trees for three_entries in traverse_trees_recursive(odb, tree_shas, ''): -- cgit v1.2.1 From 4333dcb182da3c9f9bd2c358bdf38db278cab557 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 22:49:34 +0100 Subject: Mmmmm --- git/index/fun.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index 382e3d44..f1b05f16 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -61,7 +61,7 @@ if TYPE_CHECKING: def is_three_entry_list(inp) -> TypeGuard[List['EntryTupOrNone']]: - return isinstance(inp, list) and len(inp) == 3 + return isinstance(inp, (tuple, list)) and len(inp) == 3 # ------------------------------------------------------------------------------------ @@ -339,10 +339,9 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr raise ValueError("Cannot handle %i trees at once" % len(tree_shas)) # three trees - for three_entries in traverse_trees_recursive(odb, tree_shas, ''): - - assert is_three_entry_list(three_entries) - base, ours, theirs = three_entries + for entryo in traverse_trees_recursive(odb, tree_shas, ''): + assert is_three_entry_list(entryo), f"{type(entryo)=} and {len(entryo)=}" # type:ignore + base, ours, theirs = entryo if base is not None: # base version exists if ours is not None: -- cgit v1.2.1 From fe5fef9ebd63ff79e57035cacbe647d096f110bc Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 22:57:16 +0100 Subject: Mmmmmm --- git/index/fun.py | 138 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 71 insertions(+), 67 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index f1b05f16..dbe25276 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -13,6 +13,7 @@ from stat import ( S_IFREG, S_IXUSR, ) + import subprocess from git.cmd import PROC_CREATIONFLAGS, handle_process_output @@ -339,76 +340,79 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr raise ValueError("Cannot handle %i trees at once" % len(tree_shas)) # three trees - for entryo in traverse_trees_recursive(odb, tree_shas, ''): - assert is_three_entry_list(entryo), f"{type(entryo)=} and {len(entryo)=}" # type:ignore - base, ours, theirs = entryo - if base is not None: - # base version exists - if ours is not None: - # ours exists - if theirs is not None: - # it exists in all branches, if it was changed in both - # its a conflict, otherwise we take the changed version - # This should be the most common branch, so it comes first - if(base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or \ - (base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]): - # changed by both - out.append(_tree_entry_to_baseindexentry(base, 1)) - out.append(_tree_entry_to_baseindexentry(ours, 2)) - out.append(_tree_entry_to_baseindexentry(theirs, 3)) - elif base[0] != ours[0] or base[1] != ours[1]: - # only we changed it - out.append(_tree_entry_to_baseindexentry(ours, 0)) - else: - # either nobody changed it, or they did. In either - # case, use theirs - out.append(_tree_entry_to_baseindexentry(theirs, 0)) - # END handle modification + entries = traverse_trees_recursive(odb, tree_shas, '') + base = entries[0] + ours = entries[1] + theirs = entries[2] + assert is_three_entry_list(entries), f"{type(entries)=} and {len(entries)=}" # type:ignore + + if base is not None: + # base version exists + if ours is not None: + # ours exists + if theirs is not None: + # it exists in all branches, if it was changed in both + # its a conflict, otherwise we take the changed version + # This should be the most common branch, so it comes first + if(base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or \ + (base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]): + # changed by both + out.append(_tree_entry_to_baseindexentry(base, 1)) + out.append(_tree_entry_to_baseindexentry(ours, 2)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) + elif base[0] != ours[0] or base[1] != ours[1]: + # only we changed it + out.append(_tree_entry_to_baseindexentry(ours, 0)) else: - - if ours[0] != base[0] or ours[1] != base[1]: - # they deleted it, we changed it, conflict - out.append(_tree_entry_to_baseindexentry(base, 1)) - out.append(_tree_entry_to_baseindexentry(ours, 2)) - # else: - # we didn't change it, ignore - # pass - # END handle our change - # END handle theirs + # either nobody changed it, or they did. In either + # case, use theirs + out.append(_tree_entry_to_baseindexentry(theirs, 0)) + # END handle modification else: - if theirs is None: - # deleted in both, its fine - its out - pass - else: - if theirs[0] != base[0] or theirs[1] != base[1]: - # deleted in ours, changed theirs, conflict - out.append(_tree_entry_to_baseindexentry(base, 1)) - out.append(_tree_entry_to_baseindexentry(theirs, 3)) - # END theirs changed - # else: - # theirs didn't change - # pass - # END handle theirs - # END handle ours - else: - # all three can't be None - if ours is None and theirs is not None: - # added in their branch - out.append(_tree_entry_to_baseindexentry(theirs, 0)) - elif theirs is None and ours is not None: - # added in our branch - out.append(_tree_entry_to_baseindexentry(ours, 0)) - elif ours is not None and theirs is not None: - # both have it, except for the base, see whether it changed - if ours[0] != theirs[0] or ours[1] != theirs[1]: + + if ours[0] != base[0] or ours[1] != base[1]: + # they deleted it, we changed it, conflict + out.append(_tree_entry_to_baseindexentry(base, 1)) out.append(_tree_entry_to_baseindexentry(ours, 2)) + # else: + # we didn't change it, ignore + # pass + # END handle our change + # END handle theirs + else: + if theirs is None: + # deleted in both, its fine - its out + pass + else: + if theirs[0] != base[0] or theirs[1] != base[1]: + # deleted in ours, changed theirs, conflict + out.append(_tree_entry_to_baseindexentry(base, 1)) out.append(_tree_entry_to_baseindexentry(theirs, 3)) - else: - # it was added the same in both - out.append(_tree_entry_to_baseindexentry(ours, 0)) - # END handle two items - # END handle heads - # END handle base exists - # END for each entries tuple + # END theirs changed + # else: + # theirs didn't change + # pass + # END handle theirs + # END handle ours + else: + # all three can't be None + if ours is None and theirs is not None: + # added in their branch + out.append(_tree_entry_to_baseindexentry(theirs, 0)) + elif theirs is None and ours is not None: + # added in our branch + out.append(_tree_entry_to_baseindexentry(ours, 0)) + elif ours is not None and theirs is not None: + # both have it, except for the base, see whether it changed + if ours[0] != theirs[0] or ours[1] != theirs[1]: + out.append(_tree_entry_to_baseindexentry(ours, 2)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) + else: + # it was added the same in both + out.append(_tree_entry_to_baseindexentry(ours, 0)) + # END handle two items + # END handle heads + # END handle base exists +# END for each entries tuple return out -- cgit v1.2.1 From d344abf5594bebe0147feaba7e87c0079d28374f Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 23:12:42 +0100 Subject: Fix traverse_trees_recursive() --- git/index/fun.py | 148 ++++++++++++++++++++++++++----------------------------- 1 file changed, 69 insertions(+), 79 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index dbe25276..cd71237d 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -13,7 +13,6 @@ from stat import ( S_IFREG, S_IXUSR, ) - import subprocess from git.cmd import PROC_CREATIONFLAGS, handle_process_output @@ -58,11 +57,7 @@ from git.types import PathLike, TypeGuard if TYPE_CHECKING: from .base import IndexFile - from git.objects.fun import EntryTupOrNone - - -def is_three_entry_list(inp) -> TypeGuard[List['EntryTupOrNone']]: - return isinstance(inp, (tuple, list)) and len(inp) == 3 + # from git.objects.fun import EntryTupOrNone # ------------------------------------------------------------------------------------ @@ -192,8 +187,8 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i """:return: Key suitable to be used for the index.entries dictionary :param entry: One instance of type BaseIndexEntry or the path and the stage""" - def is_entry_tuple(entry: Tuple) -> TypeGuard[Tuple[PathLike, int]]: - return isinstance(entry, tuple) and len(entry) == 2 + def is_entry_key_tup(entry_key: Tuple) -> TypeGuard[Tuple[PathLike, int]]: + return isinstance(entry_key, tuple) and len(entry_key) == 2 if len(entry) == 1: entry_first = entry[0] @@ -201,7 +196,7 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i return (entry_first.path, entry_first.stage) else: # entry = tuple(entry) - assert is_entry_tuple(entry) + assert is_entry_key_tup(entry) return entry # END handle entry @@ -340,79 +335,74 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr raise ValueError("Cannot handle %i trees at once" % len(tree_shas)) # three trees - entries = traverse_trees_recursive(odb, tree_shas, '') - base = entries[0] - ours = entries[1] - theirs = entries[2] - assert is_three_entry_list(entries), f"{type(entries)=} and {len(entries)=}" # type:ignore - - if base is not None: - # base version exists - if ours is not None: - # ours exists - if theirs is not None: - # it exists in all branches, if it was changed in both - # its a conflict, otherwise we take the changed version - # This should be the most common branch, so it comes first - if(base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or \ - (base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]): - # changed by both - out.append(_tree_entry_to_baseindexentry(base, 1)) - out.append(_tree_entry_to_baseindexentry(ours, 2)) - out.append(_tree_entry_to_baseindexentry(theirs, 3)) - elif base[0] != ours[0] or base[1] != ours[1]: - # only we changed it - out.append(_tree_entry_to_baseindexentry(ours, 0)) + for base, ours, theirs in traverse_trees_recursive(odb, tree_shas, ''): + if base is not None: + # base version exists + if ours is not None: + # ours exists + if theirs is not None: + # it exists in all branches, if it was changed in both + # its a conflict, otherwise we take the changed version + # This should be the most common branch, so it comes first + if(base[0] != ours[0] and base[0] != theirs[0] and ours[0] != theirs[0]) or \ + (base[1] != ours[1] and base[1] != theirs[1] and ours[1] != theirs[1]): + # changed by both + out.append(_tree_entry_to_baseindexentry(base, 1)) + out.append(_tree_entry_to_baseindexentry(ours, 2)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) + elif base[0] != ours[0] or base[1] != ours[1]: + # only we changed it + out.append(_tree_entry_to_baseindexentry(ours, 0)) + else: + # either nobody changed it, or they did. In either + # case, use theirs + out.append(_tree_entry_to_baseindexentry(theirs, 0)) + # END handle modification else: - # either nobody changed it, or they did. In either - # case, use theirs - out.append(_tree_entry_to_baseindexentry(theirs, 0)) - # END handle modification - else: - if ours[0] != base[0] or ours[1] != base[1]: - # they deleted it, we changed it, conflict - out.append(_tree_entry_to_baseindexentry(base, 1)) - out.append(_tree_entry_to_baseindexentry(ours, 2)) - # else: - # we didn't change it, ignore - # pass - # END handle our change - # END handle theirs - else: - if theirs is None: - # deleted in both, its fine - its out - pass + if ours[0] != base[0] or ours[1] != base[1]: + # they deleted it, we changed it, conflict + out.append(_tree_entry_to_baseindexentry(base, 1)) + out.append(_tree_entry_to_baseindexentry(ours, 2)) + # else: + # we didn't change it, ignore + # pass + # END handle our change + # END handle theirs else: - if theirs[0] != base[0] or theirs[1] != base[1]: - # deleted in ours, changed theirs, conflict - out.append(_tree_entry_to_baseindexentry(base, 1)) - out.append(_tree_entry_to_baseindexentry(theirs, 3)) - # END theirs changed - # else: - # theirs didn't change - # pass - # END handle theirs - # END handle ours - else: - # all three can't be None - if ours is None and theirs is not None: - # added in their branch - out.append(_tree_entry_to_baseindexentry(theirs, 0)) - elif theirs is None and ours is not None: - # added in our branch - out.append(_tree_entry_to_baseindexentry(ours, 0)) - elif ours is not None and theirs is not None: - # both have it, except for the base, see whether it changed - if ours[0] != theirs[0] or ours[1] != theirs[1]: - out.append(_tree_entry_to_baseindexentry(ours, 2)) - out.append(_tree_entry_to_baseindexentry(theirs, 3)) - else: - # it was added the same in both + if theirs is None: + # deleted in both, its fine - its out + pass + else: + if theirs[0] != base[0] or theirs[1] != base[1]: + # deleted in ours, changed theirs, conflict + out.append(_tree_entry_to_baseindexentry(base, 1)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) + # END theirs changed + # else: + # theirs didn't change + # pass + # END handle theirs + # END handle ours + else: + # all three can't be None + if ours is None and theirs is not None: + # added in their branch + out.append(_tree_entry_to_baseindexentry(theirs, 0)) + elif theirs is None and ours is not None: + # added in our branch out.append(_tree_entry_to_baseindexentry(ours, 0)) - # END handle two items - # END handle heads - # END handle base exists -# END for each entries tuple + elif ours is not None and theirs is not None: + # both have it, except for the base, see whether it changed + if ours[0] != theirs[0] or ours[1] != theirs[1]: + out.append(_tree_entry_to_baseindexentry(ours, 2)) + out.append(_tree_entry_to_baseindexentry(theirs, 3)) + else: + # it was added the same in both + out.append(_tree_entry_to_baseindexentry(ours, 0)) + # END handle two items + # END handle heads + # END handle base exists + # END for each entries tuple return out -- cgit v1.2.1 From 4f13b4e23526616f307370dc9a869b067e90b276 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 23:49:01 +0100 Subject: fix base,ours,theirs typing --- git/index/fun.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index cd71237d..774738db 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -386,13 +386,13 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr # END handle ours else: # all three can't be None - if ours is None and theirs is not None: + if ours is None: # added in their branch - out.append(_tree_entry_to_baseindexentry(theirs, 0)) - elif theirs is None and ours is not None: + out.append(_tree_entry_to_baseindexentry(theirs, 0)) # type: ignore + elif theirs is None: # ours is not None, theirs is None # added in our branch out.append(_tree_entry_to_baseindexentry(ours, 0)) - elif ours is not None and theirs is not None: + else: # both have it, except for the base, see whether it changed if ours[0] != theirs[0] or ours[1] != theirs[1]: out.append(_tree_entry_to_baseindexentry(ours, 2)) -- cgit v1.2.1 From 627defff96470464884ca81899fd0271e614b3e8 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 8 Jul 2021 23:55:09 +0100 Subject: Change List to MutableSequence in fun.py _find_by_name() --- git/index/fun.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index 774738db..5c09f2b9 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -388,8 +388,9 @@ def aggressive_tree_merge(odb, tree_shas: Sequence[bytes]) -> List[BaseIndexEntr # all three can't be None if ours is None: # added in their branch - out.append(_tree_entry_to_baseindexentry(theirs, 0)) # type: ignore - elif theirs is None: # ours is not None, theirs is None + assert theirs is not None + out.append(_tree_entry_to_baseindexentry(theirs, 0)) + elif theirs is None: # added in our branch out.append(_tree_entry_to_baseindexentry(ours, 0)) else: -- cgit v1.2.1 From f271c58adb36550a02607811e97cc19feae4bafb Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 00:00:19 +0100 Subject: tests TraversableIterableObj typeguard --- git/index/fun.py | 1 - 1 file changed, 1 deletion(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index 5c09f2b9..45785687 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -195,7 +195,6 @@ def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, i assert isinstance(entry_first, BaseIndexEntry) return (entry_first.path, entry_first.stage) else: - # entry = tuple(entry) assert is_entry_key_tup(entry) return entry # END handle entry -- cgit v1.2.1 From 7c6ae2b94cfd1593c12366b6abc0cd5bbb6e07b2 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 15:07:50 +0100 Subject: Try to distinguation git.diff module from diff.Diff.diff and diff.Daffable.diff() --- git/index/base.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 1812faee..bd3dde99 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -41,7 +41,7 @@ from git.util import ( from gitdb.base import IStream from gitdb.db import MemoryDB -import git.diff as diff +import git.diff as git_diff import os.path as osp from .fun import ( @@ -88,7 +88,7 @@ Treeish = Union[Tree, Commit, str, bytes] __all__ = ('IndexFile', 'CheckoutError') -class IndexFile(LazyMixin, diff.Diffable, Serializable): +class IndexFile(LazyMixin, git_diff.Diffable, Serializable): """ Implements an Index that can be manipulated using a native implementation in @@ -575,8 +575,8 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): root_tree._cache = tree_items # type: ignore return root_tree - def _process_diff_args(self, args: List[Union[str, diff.Diffable, object]] - ) -> List[Union[str, diff.Diffable, object]]: + def _process_diff_args(self, args: List[Union[str, git_diff.Diffable, object]] + ) -> List[Union[str, git_diff.Diffable, object]]: try: args.pop(args.index(self)) except IndexError: @@ -1272,10 +1272,11 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): return self @ default_index - def diff(self, other: Union[diff.Diffable.Index, 'IndexFile.Index', Treeish, None, object] = diff.Diffable.Index, + def diff(self, + other: Union[git_diff.Diffable.Index, 'IndexFile.Index', Treeish, None, object] = git_diff.Diffable.Index, paths: Union[str, List[PathLike], Tuple[PathLike, ...], None] = None, create_patch: bool = False, **kwargs: Any - ) -> diff.DiffIndex: + ) -> git_diff.DiffIndex: """Diff this index against the working copy or a Tree or Commit object For a documentation of the parameters and return values, see, @@ -1287,7 +1288,7 @@ class IndexFile(LazyMixin, diff.Diffable, Serializable): """ # index against index is always empty if other is self.Index: - return diff.DiffIndex() + return git_diff.DiffIndex() # index against anything but None is a reverse diff with the respective # item. Handle existing -R flags properly. Transform strings to the object -- cgit v1.2.1 From f916c148ea956655837a98817778abe685bf7ee7 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 15:40:14 +0100 Subject: Improve Diffable method typing --- git/index/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index bd3dde99..6738e223 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -68,7 +68,7 @@ from .util import ( # typing ----------------------------------------------------------------------------- from typing import (Any, BinaryIO, Callable, Dict, IO, Iterable, Iterator, List, NoReturn, - Sequence, TYPE_CHECKING, Tuple, Union) + Sequence, TYPE_CHECKING, Tuple, Type, Union) from git.types import Commit_ish, PathLike, TBD @@ -575,8 +575,8 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable): root_tree._cache = tree_items # type: ignore return root_tree - def _process_diff_args(self, args: List[Union[str, git_diff.Diffable, object]] - ) -> List[Union[str, git_diff.Diffable, object]]: + def _process_diff_args(self, args: List[Union[PathLike, 'git_diff.Diffable', Type['git_diff.Diffable.Index']]] + ) -> List[Union[PathLike, 'git_diff.Diffable', Type['git_diff.Diffable.Index']]]: try: args.pop(args.index(self)) except IndexError: -- cgit v1.2.1 From b03af0547f5381cf4043a43acf533687d91f0ea1 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 16:12:40 +0100 Subject: Remove defsult_index decorator from diff() and do check within function. Breaks typechecking for some reason --- git/index/base.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 6738e223..149edf5a 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -1271,10 +1271,10 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable): return self - @ default_index + # @ default_index, breaks typing for some reason, copied into function def diff(self, other: Union[git_diff.Diffable.Index, 'IndexFile.Index', Treeish, None, object] = git_diff.Diffable.Index, - paths: Union[str, List[PathLike], Tuple[PathLike, ...], None] = None, + paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None, create_patch: bool = False, **kwargs: Any ) -> git_diff.DiffIndex: """Diff this index against the working copy or a Tree or Commit object @@ -1286,6 +1286,11 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable): Will only work with indices that represent the default git index as they have not been initialized with a stream. """ + + # only run if we are the default repository index + if self._file_path != self._index_path(): + raise AssertionError( + "Cannot call %r on indices that do not represent the default git index" % self.diff()) # index against index is always empty if other is self.Index: return git_diff.DiffIndex() -- cgit v1.2.1 From 797e962fc1811ddc5a5a34308bd243953eb77135 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 16:27:34 +0100 Subject: Make IndexFile and Diffable .diff() types agree --- git/index/base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 149edf5a..75df5184 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -1273,7 +1273,8 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable): # @ default_index, breaks typing for some reason, copied into function def diff(self, - other: Union[git_diff.Diffable.Index, 'IndexFile.Index', Treeish, None, object] = git_diff.Diffable.Index, + other: Union[Type['git_diff.Diffable.Index'], 'IndexFile.Index', + 'Tree', 'Commit', str, None] = git_diff.Diffable.Index, paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None, create_patch: bool = False, **kwargs: Any ) -> git_diff.DiffIndex: -- cgit v1.2.1 From 09053c565915d114384b1c20af8eecfed98c8069 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 22:58:02 +0100 Subject: Improve IndexFile_process_diff_args() to get checks to rerun --- git/index/base.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 75df5184..6f6ea5aa 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -575,8 +575,9 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable): root_tree._cache = tree_items # type: ignore return root_tree - def _process_diff_args(self, args: List[Union[PathLike, 'git_diff.Diffable', Type['git_diff.Diffable.Index']]] - ) -> List[Union[PathLike, 'git_diff.Diffable', Type['git_diff.Diffable.Index']]]: + def _process_diff_args(self, # type: ignore[override] + args: List[Union[str, 'git_diff.Diffable', Type['git_diff.Diffable.Index']]] + ) -> List[Union[str, 'git_diff.Diffable', Type['git_diff.Diffable.Index']]]: try: args.pop(args.index(self)) except IndexError: @@ -1272,9 +1273,8 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable): return self # @ default_index, breaks typing for some reason, copied into function - def diff(self, - other: Union[Type['git_diff.Diffable.Index'], 'IndexFile.Index', - 'Tree', 'Commit', str, None] = git_diff.Diffable.Index, + def diff(self, # type: ignore[override] + other: Union[Type['git_diff.Diffable.Index'], 'Tree', 'Commit', str, None] = git_diff.Diffable.Index, paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None, create_patch: bool = False, **kwargs: Any ) -> git_diff.DiffIndex: -- cgit v1.2.1 From 2ea528e9fbcac850d99ce527ad4a5e4afb3587a8 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 23:21:16 +0100 Subject: Fix typing of index.fun.write_tree_from_cache() --- git/index/base.py | 2 +- git/index/fun.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'git/index') diff --git a/git/index/base.py b/git/index/base.py index 6f6ea5aa..3aa06e38 100644 --- a/git/index/base.py +++ b/git/index/base.py @@ -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 + root_tree._cache = tree_items # type: ignore # should this be encoded to [bytes, int, str]? return root_tree def _process_diff_args(self, # type: ignore[override] diff --git a/git/index/fun.py b/git/index/fun.py index 45785687..96833a7a 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -249,7 +249,7 @@ def read_cache(stream: IO[bytes]) -> Tuple[int, Dict[Tuple[PathLike, int], 'Inde def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 - ) -> Tuple[bytes, List[Tuple[str, int, str]]]: + ) -> Tuple[bytes, List[Tuple[bytes, int, str]]]: """Create a tree from the given sorted list of entries and put the respective trees into the given object database @@ -298,12 +298,11 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 # finally create the tree sio = BytesIO() - tree_to_stream(tree_items, sio.write) # converts bytes of each item[0] to str - tree_items_stringified = cast(List[Tuple[str, int, str]], tree_items) + tree_to_stream(tree_items, sio.write) # writes to stream as bytes, but doesnt change tree_items sio.seek(0) istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio)) - return (istream.binsha, tree_items_stringified) + return (istream.binsha, tree_items) def _tree_entry_to_baseindexentry(tree_entry: Tuple[bytes, int, str], stage: int) -> BaseIndexEntry: -- cgit v1.2.1 From e6a27adb71d21c81628acbdd65bf07037604ff90 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 23:33:53 +0100 Subject: Use TreeCacheTup type alias throughout --- git/index/fun.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index 96833a7a..df74c2c1 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -57,6 +57,7 @@ from git.types import PathLike, TypeGuard if TYPE_CHECKING: from .base import IndexFile + from objects.tree import TreeCacheTup # from git.objects.fun import EntryTupOrNone # ------------------------------------------------------------------------------------ @@ -249,7 +250,7 @@ def read_cache(stream: IO[bytes]) -> Tuple[int, Dict[Tuple[PathLike, int], 'Inde def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 - ) -> Tuple[bytes, List[Tuple[bytes, int, str]]]: + ) -> Tuple[bytes, List[TreeCacheTup]]: """Create a tree from the given sorted list of entries and put the respective trees into the given object database @@ -259,7 +260,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 :param sl: slice indicating the range we should process on the entries list :return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of tree entries being a tuple of hexsha, mode, name""" - tree_items: List[Tuple[bytes, int, str]] = [] + tree_items: List[TreeCacheTup] = [] ci = sl.start end = sl.stop @@ -305,7 +306,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 return (istream.binsha, tree_items) -def _tree_entry_to_baseindexentry(tree_entry: Tuple[bytes, int, str], stage: int) -> BaseIndexEntry: +def _tree_entry_to_baseindexentry(tree_entry: TreeCacheTup, stage: int) -> BaseIndexEntry: return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2])) -- cgit v1.2.1 From 94c66525a6e7d5c74a9aee65d14630bb674439f7 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Fri, 9 Jul 2021 23:36:35 +0100 Subject: Make TreeCacheTup forward ref --- git/index/fun.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'git/index') diff --git a/git/index/fun.py b/git/index/fun.py index df74c2c1..e5e566a0 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -57,7 +57,7 @@ from git.types import PathLike, TypeGuard if TYPE_CHECKING: from .base import IndexFile - from objects.tree import TreeCacheTup + from git.objects.tree import TreeCacheTup # from git.objects.fun import EntryTupOrNone # ------------------------------------------------------------------------------------ @@ -250,7 +250,7 @@ def read_cache(stream: IO[bytes]) -> Tuple[int, Dict[Tuple[PathLike, int], 'Inde def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 - ) -> Tuple[bytes, List[TreeCacheTup]]: + ) -> Tuple[bytes, List['TreeCacheTup']]: """Create a tree from the given sorted list of entries and put the respective trees into the given object database @@ -260,7 +260,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 :param sl: slice indicating the range we should process on the entries list :return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of tree entries being a tuple of hexsha, mode, name""" - tree_items: List[TreeCacheTup] = [] + tree_items: List['TreeCacheTup'] = [] ci = sl.start end = sl.stop @@ -306,7 +306,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0 return (istream.binsha, tree_items) -def _tree_entry_to_baseindexentry(tree_entry: TreeCacheTup, stage: int) -> BaseIndexEntry: +def _tree_entry_to_baseindexentry(tree_entry: 'TreeCacheTup', stage: int) -> BaseIndexEntry: return BaseIndexEntry((tree_entry[1], tree_entry[0], stage << CE_STAGESHIFT, tree_entry[2])) -- cgit v1.2.1