summaryrefslogtreecommitdiff
path: root/git/remote.py
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/remote.py
parentacbd6bad9ded9a1d59e80e71d334d64b0244f5cd (diff)
parent600df043e76924d43a4f9f88f4e3241740f34c77 (diff)
downloadgitpython-cfd653aeeb3cb807f2cd2ae9fff5568880ac67da.tar.gz
Merge pull request #1295 from Yobmod/main
Add types to refs
Diffstat (limited to 'git/remote.py')
-rw-r--r--git/remote.py69
1 files changed, 41 insertions, 28 deletions
diff --git a/git/remote.py b/git/remote.py
index f59b3245..d903552f 100644
--- a/git/remote.py
+++ b/git/remote.py
@@ -36,9 +36,10 @@ from .refs import (
# typing-------------------------------------------------------
-from typing import Any, Callable, Dict, Iterator, List, Optional, Sequence, TYPE_CHECKING, Union, overload
+from typing import (Any, Callable, Dict, Iterator, List, NoReturn, Optional, Sequence, # NOQA[TC002]
+ TYPE_CHECKING, Type, Union, overload)
-from git.types import PathLike, Literal, TBD, TypeGuard, Commit_ish
+from git.types import PathLike, Literal, TBD, TypeGuard, Commit_ish # NOQA[TC002]
if TYPE_CHECKING:
from git.repo.base import Repo
@@ -83,17 +84,17 @@ def add_progress(kwargs: Any, git: Git,
#} END utilities
-@overload
+@ overload
def to_progress_instance(progress: None) -> RemoteProgress:
...
-@overload
+@ overload
def to_progress_instance(progress: Callable[..., Any]) -> CallableRemoteProgress:
...
-@overload
+@ overload
def to_progress_instance(progress: RemoteProgress) -> RemoteProgress:
...
@@ -155,11 +156,11 @@ class PushInfo(IterableObj, object):
self._old_commit_sha = old_commit
self.summary = summary
- @property
- def old_commit(self) -> Union[str, SymbolicReference, 'Commit_ish', None]:
+ @ property
+ def old_commit(self) -> Union[str, SymbolicReference, Commit_ish, None]:
return self._old_commit_sha and self._remote.repo.commit(self._old_commit_sha) or None
- @property
+ @ property
def remote_ref(self) -> Union[RemoteReference, TagReference]:
"""
:return:
@@ -175,7 +176,7 @@ class PushInfo(IterableObj, object):
raise ValueError("Could not handle remote ref: %r" % self.remote_ref_string)
# END
- @classmethod
+ @ classmethod
def _from_line(cls, remote: 'Remote', line: str) -> 'PushInfo':
"""Create a new PushInfo instance as parsed from line which is expected to be like
refs/heads/master:refs/heads/master 05d2687..1d0568e as bytes"""
@@ -192,7 +193,7 @@ class PushInfo(IterableObj, object):
# from_to handling
from_ref_string, to_ref_string = from_to.split(':')
if flags & cls.DELETED:
- from_ref = None # type: Union[SymbolicReference, None]
+ from_ref: Union[SymbolicReference, None] = None
else:
if from_ref_string == "(delete)":
from_ref = None
@@ -200,7 +201,7 @@ class PushInfo(IterableObj, object):
from_ref = Reference.from_path(remote.repo, from_ref_string)
# commit handling, could be message or commit info
- old_commit = None # type: Optional[str]
+ old_commit: Optional[str] = None
if summary.startswith('['):
if "[rejected]" in summary:
flags |= cls.REJECTED
@@ -228,6 +229,11 @@ class PushInfo(IterableObj, object):
return PushInfo(flags, from_ref, to_ref_string, remote, old_commit, summary)
+ @ classmethod
+ def iter_items(cls, repo: 'Repo', *args: Any, **kwargs: Any
+ ) -> NoReturn: # -> Iterator['PushInfo']:
+ raise NotImplementedError
+
class FetchInfo(IterableObj, object):
@@ -253,16 +259,16 @@ class FetchInfo(IterableObj, object):
_re_fetch_result = re.compile(r'^\s*(.) (\[?[\w\s\.$@]+\]?)\s+(.+) -> ([^\s]+)( \(.*\)?$)?')
- _flag_map = {
+ _flag_map: Dict[flagKeyLiteral, int] = {
'!': ERROR,
'+': FORCED_UPDATE,
'*': 0,
'=': HEAD_UPTODATE,
' ': FAST_FORWARD,
'-': TAG_UPDATE,
- } # type: Dict[flagKeyLiteral, int]
+ }
- @classmethod
+ @ classmethod
def refresh(cls) -> Literal[True]:
"""This gets called by the refresh function (see the top level
__init__).
@@ -301,25 +307,25 @@ class FetchInfo(IterableObj, object):
def __str__(self) -> str:
return self.name
- @property
+ @ property
def name(self) -> str:
""":return: Name of our remote ref"""
return self.ref.name
- @property
+ @ property
def commit(self) -> Commit_ish:
""":return: Commit of our remote ref"""
return self.ref.commit
- @classmethod
+ @ classmethod
def _from_line(cls, repo: 'Repo', line: str, fetch_line: str) -> 'FetchInfo':
"""Parse information from the given line as returned by git-fetch -v
and return a new FetchInfo object representing this information.
- We can handle a line as follows
- "%c %-*s %-*s -> %s%s"
+ We can handle a line as follows:
+ "%c %-\\*s %-\\*s -> %s%s"
- Where c is either ' ', !, +, -, *, or =
+ Where c is either ' ', !, +, -, \\*, or =
! means error
+ means success forcing update
- means a tag was updated
@@ -334,6 +340,7 @@ class FetchInfo(IterableObj, object):
raise ValueError("Failed to parse line: %r" % line)
# parse lines
+ remote_local_ref_str: str
control_character, operation, local_remote_ref, remote_local_ref_str, note = match.groups()
assert is_flagKeyLiteral(control_character), f"{control_character}"
@@ -352,7 +359,7 @@ class FetchInfo(IterableObj, object):
# END control char exception handling
# parse operation string for more info - makes no sense for symbolic refs, but we parse it anyway
- old_commit = None # type: Union[Commit_ish, None]
+ old_commit: Union[Commit_ish, None] = None
is_tag_operation = False
if 'rejected' in operation:
flags |= cls.REJECTED
@@ -375,7 +382,7 @@ class FetchInfo(IterableObj, object):
# If we do not specify a target branch like master:refs/remotes/origin/master,
# the fetch result is stored in FETCH_HEAD which destroys the rule we usually
# have. In that case we use a symbolic reference which is detached
- ref_type = None
+ ref_type: Optional[Type[SymbolicReference]] = None
if remote_local_ref_str == "FETCH_HEAD":
ref_type = SymbolicReference
elif ref_type_name == "tag" or is_tag_operation:
@@ -404,14 +411,15 @@ class FetchInfo(IterableObj, object):
# by the 'ref/' prefix. Otherwise even a tag could be in refs/remotes, which is when it will have the
# 'tags/' subdirectory in its path.
# We don't want to test for actual existence, but try to figure everything out analytically.
- ref_path = None # type: Optional[PathLike]
+ ref_path: Optional[PathLike] = None
remote_local_ref_str = remote_local_ref_str.strip()
+
if remote_local_ref_str.startswith(Reference._common_path_default + "/"):
# always use actual type if we get absolute paths
# Will always be the case if something is fetched outside of refs/remotes (if its not a tag)
ref_path = remote_local_ref_str
if ref_type is not TagReference and not \
- remote_local_ref_str.startswith(RemoteReference._common_path_default + "/"):
+ remote_local_ref_str.startswith(RemoteReference._common_path_default + "/"):
ref_type = Reference
# END downgrade remote reference
elif ref_type is TagReference and 'tags/' in remote_local_ref_str:
@@ -430,6 +438,11 @@ class FetchInfo(IterableObj, object):
return cls(remote_local_ref, flags, note, old_commit, local_remote_ref)
+ @ classmethod
+ def iter_items(cls, repo: 'Repo', *args: Any, **kwargs: Any
+ ) -> NoReturn: # -> Iterator['FetchInfo']:
+ raise NotImplementedError
+
class Remote(LazyMixin, IterableObj):
@@ -507,7 +520,7 @@ class Remote(LazyMixin, IterableObj):
return False
# end
- @classmethod
+ @ classmethod
def iter_items(cls, repo: 'Repo', *args: Any, **kwargs: Any) -> Iterator['Remote']:
""":return: Iterator yielding Remote objects of the given repository"""
for section in repo.config_reader("repository").sections():
@@ -833,7 +846,7 @@ class Remote(LazyMixin, IterableObj):
kwargs = add_progress(kwargs, self.repo.git, progress)
if isinstance(refspec, list):
- args = refspec # type: Sequence[Optional[str]] # should need this - check logic for passing None through
+ args: Sequence[Optional[str]] = refspec
else:
args = [refspec]
@@ -897,7 +910,7 @@ class Remote(LazyMixin, IterableObj):
universal_newlines=True, **kwargs)
return self._get_push_info(proc, progress)
- @property
+ @ property
def config_reader(self) -> SectionConstraint:
"""
:return:
@@ -912,7 +925,7 @@ class Remote(LazyMixin, IterableObj):
pass
# END handle exception
- @property
+ @ property
def config_writer(self) -> SectionConstraint:
"""
:return: GitConfigParser compatible object able to write options for this remote.