diff options
author | Yobmod <yobmod@gmail.com> | 2021-07-06 15:56:23 +0100 |
---|---|---|
committer | Yobmod <yobmod@gmail.com> | 2021-07-06 15:56:23 +0100 |
commit | b78cca1854e24de3558b43880586dbf9632831ad (patch) | |
tree | 8024ff3af5ac1b85a01982b3531dd9e15e78057e /git/objects/submodule/base.py | |
parent | eecf1486cf445e2f23585b1bb65097dfebbc9545 (diff) | |
download | gitpython-b78cca1854e24de3558b43880586dbf9632831ad.tar.gz |
Rmv base.py types
Diffstat (limited to 'git/objects/submodule/base.py')
-rw-r--r-- | git/objects/submodule/base.py | 101 |
1 files changed, 39 insertions, 62 deletions
diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 76769cad..960ff045 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -49,14 +49,13 @@ from .util import ( # typing ---------------------------------------------------------------------- -from typing import Callable, Dict, Mapping, Sequence, TYPE_CHECKING, cast +from typing import Dict, TYPE_CHECKING from typing import Any, Iterator, Union -from git.types import Commit_ish, PathLike, TBD +from git.types import Commit_ish, PathLike if TYPE_CHECKING: from git.repo import Repo - from git.index import IndexFile # ----------------------------------------------------------------------------- @@ -132,17 +131,17 @@ class Submodule(IndexObject, TraversableIterableObj): if url is not None: self._url = url if branch_path is not None: - # assert isinstance(branch_path, str) + assert isinstance(branch_path, str) self._branch_path = branch_path if name is not None: self._name = name def _set_cache_(self, attr: str) -> None: if attr in ('path', '_url', '_branch_path'): - reader: SectionConstraint = self.config_reader() + reader = self.config_reader() # default submodule values try: - self.path: PathLike = reader.get('path') + self.path = reader.get('path') except cp.NoSectionError as e: if self.repo.working_tree_dir is not None: raise ValueError("This submodule instance does not exist anymore in '%s' file" @@ -227,7 +226,7 @@ class Submodule(IndexObject, TraversableIterableObj): return SubmoduleConfigParser(fp_module, read_only=read_only) - def _clear_cache(self) -> None: + def _clear_cache(self): # clear the possibly changed values for name in self._cache_attrs: try: @@ -247,7 +246,7 @@ class Submodule(IndexObject, TraversableIterableObj): def _config_parser_constrained(self, read_only: bool) -> SectionConstraint: """:return: Config Parser constrained to our submodule in read or write mode""" try: - pc: Union['Commit_ish', None] = self.parent_commit + pc = self.parent_commit except ValueError: pc = None # end handle empty parent repository @@ -256,12 +255,10 @@ class Submodule(IndexObject, TraversableIterableObj): return SectionConstraint(parser, sm_section(self.name)) @classmethod - def _module_abspath(cls, parent_repo: 'Repo', path: PathLike, name: str) -> PathLike: + def _module_abspath(cls, parent_repo, path, name): if cls._need_gitfile_submodules(parent_repo.git): return osp.join(parent_repo.git_dir, 'modules', name) - if parent_repo.working_tree_dir: - return osp.join(parent_repo.working_tree_dir, path) - raise NotADirectoryError() + return osp.join(parent_repo.working_tree_dir, path) # end @classmethod @@ -289,7 +286,7 @@ class Submodule(IndexObject, TraversableIterableObj): return clone @classmethod - def _to_relative_path(cls, parent_repo: 'Repo', path: PathLike) -> PathLike: + def _to_relative_path(cls, parent_repo, path): """:return: a path guaranteed to be relative to the given parent - repository :raise ValueError: if path is not contained in the parent repository's working tree""" path = to_native_path_linux(path) @@ -297,7 +294,7 @@ class Submodule(IndexObject, TraversableIterableObj): path = path[:-1] # END handle trailing slash - if osp.isabs(path) and parent_repo.working_tree_dir: + if osp.isabs(path): working_tree_linux = to_native_path_linux(parent_repo.working_tree_dir) if not path.startswith(working_tree_linux): raise ValueError("Submodule checkout path '%s' needs to be within the parents repository at '%s'" @@ -311,7 +308,7 @@ class Submodule(IndexObject, TraversableIterableObj): return path @classmethod - def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_abspath: PathLike) -> None: + def _write_git_file_and_module_config(cls, working_tree_dir, module_abspath): """Writes a .git file containing a(preferably) relative path to the actual git module repository. It is an error if the module_abspath cannot be made into a relative path, relative to the working_tree_dir :note: will overwrite existing files ! @@ -338,8 +335,7 @@ class Submodule(IndexObject, TraversableIterableObj): @classmethod def add(cls, repo: 'Repo', name: str, path: PathLike, url: Union[str, None] = None, - branch: Union[str, None] = None, no_checkout: bool = False, depth: Union[int, None] = None, - env: Mapping[str, str] = None, clone_multi_options: Union[Sequence[TBD], None] = None + branch=None, no_checkout: bool = False, depth=None, env=None ) -> 'Submodule': """Add a new submodule to the given repository. This will alter the index as well as the .gitmodules file, but will not create a new commit. @@ -373,8 +369,6 @@ class Submodule(IndexObject, TraversableIterableObj): and is defined in `os.environ`, value from `os.environ` will be used. If you want to unset some variable, consider providing empty string as its value. - :param clone_multi_options: A list of Clone options. Please see ``git.repo.base.Repo.clone`` - for details. :return: The newly created submodule instance :note: works atomically, such that no change will be done if the repository update fails for instance""" @@ -387,7 +381,7 @@ class Submodule(IndexObject, TraversableIterableObj): # assure we never put backslashes into the url, as some operating systems # like it ... if url is not None: - url = to_native_path_linux(url) + url = to_native_path_linux(url) # to_native_path_linux does nothing?? # END assure url correctness # INSTANTIATE INTERMEDIATE SM @@ -395,7 +389,7 @@ class Submodule(IndexObject, TraversableIterableObj): if sm.exists(): # reretrieve submodule from tree try: - sm = repo.head.commit.tree[str(path)] + sm = repo.head.commit.tree[path] # type: ignore sm._name = name return sm except KeyError: @@ -418,8 +412,7 @@ class Submodule(IndexObject, TraversableIterableObj): # END check url # END verify urls match - mrepo: Union[Repo, None] = None - + mrepo = None if url is None: if not has_module: raise ValueError("A URL was not given and a repository did not exist at %s" % path) @@ -432,7 +425,7 @@ class Submodule(IndexObject, TraversableIterableObj): url = urls[0] else: # clone new repo - kwargs: Dict[str, Union[bool, int, Sequence[TBD]]] = {'n': no_checkout} + kwargs: Dict[str, Union[bool, int]] = {'n': no_checkout} if not branch_is_default: kwargs['b'] = br.name # END setup checkout-branch @@ -442,8 +435,6 @@ class Submodule(IndexObject, TraversableIterableObj): kwargs['depth'] = depth else: raise ValueError("depth should be an integer") - if clone_multi_options: - kwargs['multi_options'] = clone_multi_options # _clone_repo(cls, repo, url, path, name, **kwargs): mrepo = cls._clone_repo(repo, url, path, name, env=env, **kwargs) @@ -456,8 +447,6 @@ class Submodule(IndexObject, TraversableIterableObj): # otherwise there is a '-' character in front of the submodule listing # a38efa84daef914e4de58d1905a500d8d14aaf45 mymodule (v0.9.0-1-ga38efa8) # -a38efa84daef914e4de58d1905a500d8d14aaf45 submodules/intermediate/one - writer: Union[GitConfigParser, SectionConstraint] - with sm.repo.config_writer() as writer: writer.set_value(sm_section(name), 'url', url) @@ -474,16 +463,13 @@ class Submodule(IndexObject, TraversableIterableObj): sm._branch_path = br.path # we deliberately assume that our head matches our index ! - mrepo = cast('Repo', mrepo) sm.binsha = mrepo.head.commit.binsha index.add([sm], write=True) return sm - def update(self, recursive: bool = False, init: bool = True, to_latest_revision: bool = False, - progress: Union['UpdateProgress', None] = None, dry_run: bool = False, - force: bool = False, keep_going: bool = False, env: Mapping[str, str] = None, - clone_multi_options: Union[Sequence[TBD], None] = None): + def update(self, recursive=False, init=True, to_latest_revision=False, progress=None, dry_run=False, + force=False, keep_going=False, env=None): """Update the repository of this submodule to point to the checkout we point at with the binsha of this instance. @@ -514,8 +500,6 @@ class Submodule(IndexObject, TraversableIterableObj): and is defined in `os.environ`, value from `os.environ` will be used. If you want to unset some variable, consider providing empty string as its value. - :param clone_multi_options: list of Clone options. Please see ``git.repo.base.Repo.clone`` - for details. Only take effect with `init` option. :note: does nothing in bare repositories :note: method is definitely not atomic if recurisve is True :return: self""" @@ -582,16 +566,13 @@ class Submodule(IndexObject, TraversableIterableObj): progress.update(BEGIN | CLONE, 0, 1, prefix + "Cloning url '%s' to '%s' in submodule %r" % (self.url, checkout_module_abspath, self.name)) if not dry_run: - mrepo = self._clone_repo(self.repo, self.url, self.path, self.name, n=True, env=env, - multi_options=clone_multi_options) + mrepo = self._clone_repo(self.repo, self.url, self.path, self.name, n=True, env=env) # END handle dry-run progress.update(END | CLONE, 0, 1, prefix + "Done cloning to %s" % checkout_module_abspath) if not dry_run: # see whether we have a valid branch to checkout try: - # assert isinstance(mrepo, Repo) # cant do this cos of circular import - mrepo = cast('Repo', mrepo) # Try TypeGuard wirh hasattr, or has_remotes&_head protocol? # find a remote which has our branch - we try to be flexible remote_branch = find_first_remote_branch(mrepo.remotes, self.branch_name) local_branch = mkhead(mrepo, self.branch_path) @@ -652,7 +633,7 @@ class Submodule(IndexObject, TraversableIterableObj): may_reset = True if mrepo.head.commit.binsha != self.NULL_BIN_SHA: base_commit = mrepo.merge_base(mrepo.head.commit, hexsha) - if len(base_commit) == 0 or base_commit[0].hexsha == hexsha: # type: ignore + if len(base_commit) == 0 or base_commit[0].hexsha == hexsha: if force: msg = "Will force checkout or reset on local branch that is possibly in the future of" msg += "the commit it will be checked out to, effectively 'forgetting' new commits" @@ -819,8 +800,7 @@ class Submodule(IndexObject, TraversableIterableObj): return self @unbare_repo - def remove(self, module: bool = True, force: bool = False, - configuration: bool = True, dry_run: bool = False) -> 'Submodule': + def remove(self, module=True, force=False, configuration=True, dry_run=False): """Remove this submodule from the repository. This will remove our entry from the .gitmodules file and the entry in the .git / config file. @@ -874,7 +854,7 @@ class Submodule(IndexObject, TraversableIterableObj): # TODO: If we run into permission problems, we have a highly inconsistent # state. Delete the .git folders last, start with the submodules first mp = self.abspath - method: Union[None, Callable[[PathLike], None]] = None + method = None if osp.islink(mp): method = os.remove elif osp.isdir(mp): @@ -927,7 +907,7 @@ class Submodule(IndexObject, TraversableIterableObj): import gc gc.collect() try: - rmtree(str(wtd)) + rmtree(wtd) except Exception as ex: if HIDE_WINDOWS_KNOWN_ERRORS: raise SkipTest("FIXME: fails with: PermissionError\n {}".format(ex)) from ex @@ -941,7 +921,7 @@ class Submodule(IndexObject, TraversableIterableObj): rmtree(git_dir) except Exception as ex: if HIDE_WINDOWS_KNOWN_ERRORS: - raise SkipTest(f"FIXME: fails with: PermissionError\n {ex}") from ex + raise SkipTest("FIXME: fails with: PermissionError\n %s", ex) from ex else: raise # end handle separate bare repository @@ -965,8 +945,6 @@ class Submodule(IndexObject, TraversableIterableObj): # now git config - need the config intact, otherwise we can't query # information anymore - writer: Union[GitConfigParser, SectionConstraint] - with self.repo.config_writer() as writer: writer.remove_section(sm_section(self.name)) @@ -976,7 +954,7 @@ class Submodule(IndexObject, TraversableIterableObj): return self - def set_parent_commit(self, commit: Union[Commit_ish, None], check: bool = True) -> 'Submodule': + def set_parent_commit(self, commit: Union[Commit_ish, None], check=True): """Set this instance to use the given commit whose tree is supposed to contain the .gitmodules blob. @@ -1015,7 +993,7 @@ class Submodule(IndexObject, TraversableIterableObj): # If check is False, we might see a parent-commit that doesn't even contain the submodule anymore. # in that case, mark our sha as being NULL try: - self.binsha = pctree[str(self.path)].binsha + self.binsha = pctree[self.path].binsha # type: ignore except KeyError: self.binsha = self.NULL_BIN_SHA # end @@ -1024,7 +1002,7 @@ class Submodule(IndexObject, TraversableIterableObj): return self @unbare_repo - def config_writer(self, index: Union['IndexFile', None] = None, write: bool = True) -> SectionConstraint: + def config_writer(self, index=None, write=True): """:return: a config writer instance allowing you to read and write the data belonging to this submodule into the .gitmodules file. @@ -1045,7 +1023,7 @@ class Submodule(IndexObject, TraversableIterableObj): return writer @unbare_repo - def rename(self, new_name: str) -> 'Submodule': + def rename(self, new_name): """Rename this submodule :note: This method takes care of renaming the submodule in various places, such as @@ -1080,14 +1058,13 @@ class Submodule(IndexObject, TraversableIterableObj): destination_module_abspath = self._module_abspath(self.repo, self.path, new_name) source_dir = mod.git_dir # Let's be sure the submodule name is not so obviously tied to a directory - if str(destination_module_abspath).startswith(str(mod.git_dir)): + if destination_module_abspath.startswith(mod.git_dir): tmp_dir = self._module_abspath(self.repo, self.path, str(uuid.uuid4())) os.renames(source_dir, tmp_dir) source_dir = tmp_dir # end handle self-containment os.renames(source_dir, destination_module_abspath) - if mod.working_tree_dir: - self._write_git_file_and_module_config(mod.working_tree_dir, destination_module_abspath) + self._write_git_file_and_module_config(mod.working_tree_dir, destination_module_abspath) # end move separate git repository return self @@ -1097,7 +1074,7 @@ class Submodule(IndexObject, TraversableIterableObj): #{ Query Interface @unbare_repo - def module(self) -> 'Repo': + def module(self): """:return: Repo instance initialized from the repository at our submodule path :raise InvalidGitRepositoryError: if a repository was not available. This could also mean that it was not yet initialized""" @@ -1114,7 +1091,7 @@ class Submodule(IndexObject, TraversableIterableObj): raise InvalidGitRepositoryError("Repository at %r was not yet checked out" % module_checkout_abspath) # END handle exceptions - def module_exists(self) -> bool: + def module_exists(self): """:return: True if our module exists and is a valid git repository. See module() method""" try: self.module() @@ -1123,7 +1100,7 @@ class Submodule(IndexObject, TraversableIterableObj): return False # END handle exception - def exists(self) -> bool: + def exists(self): """ :return: True if the submodule exists, False otherwise. Please note that a submodule may exist ( in the .gitmodules file) even though its module @@ -1164,26 +1141,26 @@ class Submodule(IndexObject, TraversableIterableObj): return mkhead(self.module(), self._branch_path) @property - def branch_path(self) -> PathLike: + def branch_path(self): """ :return: full(relative) path as string to the branch we would checkout from the remote and track""" return self._branch_path @property - def branch_name(self) -> str: + def branch_name(self): """:return: the name of the branch, which is the shortest possible branch name""" # use an instance method, for this we create a temporary Head instance # which uses a repository that is available at least ( it makes no difference ) return git.Head(self.repo, self._branch_path).name @property - def url(self) -> str: + def url(self): """:return: The url to the repository which our module - repository refers to""" return self._url @property - def parent_commit(self) -> 'Commit_ish': + def parent_commit(self): """:return: Commit instance with the tree containing the .gitmodules file :note: will always point to the current head's commit if it was not set explicitly""" if self._parent_commit is None: @@ -1191,7 +1168,7 @@ class Submodule(IndexObject, TraversableIterableObj): return self._parent_commit @property - def name(self) -> str: + def name(self): """:return: The name of this submodule. It is used to identify it within the .gitmodules file. :note: by default, the name is the path at which to find the submodule, but |