Adding inital types to remote.py by Yobmod · Pull Request #1229 · gitpython-developers/GitPython
# typing-------------------------------------------------------
from typing import TYPE_CHECKING from typing import Any, Callable, Dict, Optional, TYPE_CHECKING, Union, cast, overload
from git.types import PathLike, Literal
if TYPE_CHECKING: from git.repo.base import Repo from git.objects.commit import Commit from git.objects.blob import Blob from git.objects.tree import Tree from git.objects.tag import TagObject
flagKeyLiteral = Literal[' ', '!', '+', '-', '*', '=', 't'] # -------------------------------------------------------------
log = logging.getLogger('git.remote')
def add_progress(kwargs, git, progress): def add_progress(kwargs: Any, git: Git, progress: Optional[Callable[..., Any]]) -> Any: """Add the --progress flag to the given kwargs dict if supported by the git command. If the actual progress in the given progress instance is not given, we do not request any progress
def to_progress_instance(progress): @overload def to_progress_instance(progress: None) -> RemoteProgress: ...
@overload def to_progress_instance(progress: Callable[..., Any]) -> CallableRemoteProgress: ...
@overload def to_progress_instance(progress: RemoteProgress) -> RemoteProgress: ...
def to_progress_instance(progress: Union[Callable[..., Any], RemoteProgress, None] ) -> Union[RemoteProgress, CallableRemoteProgress]: """Given the 'progress' return a suitable object derived from RemoteProgress(). """
def __init__(self, flags, local_ref, remote_ref_string, remote, old_commit=None, summary=''): """ Initialize a new instance """ def __init__(self, flags: int, local_ref: Union[SymbolicReference, None], remote_ref_string: str, remote: 'Remote', old_commit: Optional[str] = None, summary: str = '') -> None: """ Initialize a new instance local_ref: HEAD | Head | RemoteReference | TagReference | Reference | SymbolicReference | None """ self.flags = flags self.local_ref = local_ref self.remote_ref_string = remote_ref_string
@property def old_commit(self): def old_commit(self) -> Union[str, SymbolicReference, 'Commit', 'TagObject', 'Blob', 'Tree', None]: return self._old_commit_sha and self._remote.repo.commit(self._old_commit_sha) or None
@property def remote_ref(self): def remote_ref(self) -> Union[RemoteReference, TagReference]: """ :return: Remote Reference or TagReference in the local repository corresponding
@classmethod def _from_line(cls, remote, line): def _from_line(cls, 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""" control_character, from_to, summary = line.split('\t', 3)
# commit handling, could be message or commit info old_commit = None old_commit = None # type: Optional[str] if summary.startswith('['): if "[rejected]" in summary: flags |= cls.REJECTED
@classmethod def refresh(cls): def refresh(cls) -> Literal[True]: """This gets called by the refresh function (see the top level __init__). """
return True
def __init__(self, ref, flags, note='', old_commit=None, remote_ref_path=None): def __init__(self, ref: SymbolicReference, flags: int, note: str = '', old_commit: Optional['Commit'] = None, remote_ref_path: Optional[PathLike] = None) -> None: """ Initialize a new instance """
def __str__(self): def __str__(self) -> str: return self.name
@property def name(self): def name(self) -> str: """:return: Name of our remote ref""" return self.ref.name
@property def commit(self): def commit(self) -> 'Commit': """:return: Commit of our remote ref""" return self.ref.commit
@classmethod def _from_line(cls, repo, line, fetch_line): 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.
# parse lines control_character, operation, local_remote_ref, remote_local_ref, note = match.groups() control_character, operation, local_remote_ref, remote_local_ref_str, note = match.groups() control_character = cast(flagKeyLiteral, control_character) # can do this neater once 3.5 dropped
try: _new_hex_sha, _fetch_operation, fetch_note = fetch_line.split("\t") ref_type_name, fetch_note = fetch_note.split(' ', 1)
# even though the path could be within the git conventions, we make