diff options
Diffstat (limited to 'git')
-rw-r--r-- | git/objects/commit.py | 1 | ||||
-rw-r--r-- | git/objects/util.py | 5 | ||||
-rw-r--r-- | git/refs/head.py | 13 | ||||
-rw-r--r-- | git/refs/reference.py | 4 | ||||
-rw-r--r-- | git/refs/symbolic.py | 47 | ||||
-rw-r--r-- | git/refs/tag.py | 4 |
6 files changed, 47 insertions, 27 deletions
diff --git a/git/objects/commit.py b/git/objects/commit.py index 884f6522..9d709656 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -128,6 +128,7 @@ class Commit(base.Object, TraversableIterableObj, Diffable, Serializable): as what time.altzone returns. The sign is inverted compared to git's UTC timezone.""" super(Commit, self).__init__(repo, binsha) + self.binsha = binsha if tree is not None: assert isinstance(tree, Tree), "Tree needs to be a Tree instance, was %s" % type(tree) if tree is not None: diff --git a/git/objects/util.py b/git/objects/util.py index ef1ae77b..db7807c2 100644 --- a/git/objects/util.py +++ b/git/objects/util.py @@ -493,6 +493,11 @@ class TraversableIterableObj(IterableObj, Traversable): return super(TraversableIterableObj, self)._list_traverse(* args, **kwargs) @ overload # type: ignore + def traverse(self: T_TIobj + ) -> Iterator[T_TIobj]: + ... + + @ overload def traverse(self: T_TIobj, predicate: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool], prune: Callable[[Union[T_TIobj, Tuple[Union[T_TIobj, None], T_TIobj]], int], bool], diff --git a/git/refs/head.py b/git/refs/head.py index 4b9bf33c..260bf5e7 100644 --- a/git/refs/head.py +++ b/git/refs/head.py @@ -1,4 +1,4 @@ -from git.config import SectionConstraint +from git.config import GitConfigParser, SectionConstraint from git.util import join_path from git.exc import GitCommandError @@ -203,7 +203,7 @@ class Head(Reference): self.path = "%s/%s" % (self._common_path_default, new_path) return self - def checkout(self, force: bool = False, **kwargs: Any): + def checkout(self, force: bool = False, **kwargs: Any) -> Union['HEAD', 'Head']: """Checkout this head by setting the HEAD to this reference, by updating the index to reflect the tree we point to and by updating the working tree to reflect the latest index. @@ -235,10 +235,11 @@ class Head(Reference): self.repo.git.checkout(self, **kwargs) if self.repo.head.is_detached: return self.repo.head - return self.repo.active_branch + else: + return self.repo.active_branch #{ Configuration - def _config_parser(self, read_only: bool) -> SectionConstraint: + def _config_parser(self, read_only: bool) -> SectionConstraint[GitConfigParser]: if read_only: parser = self.repo.config_reader() else: @@ -247,13 +248,13 @@ class Head(Reference): return SectionConstraint(parser, 'branch "%s"' % self.name) - def config_reader(self) -> SectionConstraint: + def config_reader(self) -> SectionConstraint[GitConfigParser]: """ :return: A configuration parser instance constrained to only read this instance's values""" return self._config_parser(read_only=True) - def config_writer(self) -> SectionConstraint: + def config_writer(self) -> SectionConstraint[GitConfigParser]: """ :return: A configuration writer instance with read-and write access to options of this head""" diff --git a/git/refs/reference.py b/git/refs/reference.py index 64662281..bc2c6e80 100644 --- a/git/refs/reference.py +++ b/git/refs/reference.py @@ -62,7 +62,9 @@ class Reference(SymbolicReference, LazyMixin, IterableObj): #{ Interface - def set_object(self, object: Commit_ish, logmsg: Union[str, None] = None) -> 'Reference': # @ReservedAssignment + # @ReservedAssignment + def set_object(self, object: Union[Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None + ) -> 'SymbolicReference': """Special version which checks if the head-log needs an update as well :return: self""" oldbinsha = None diff --git a/git/refs/symbolic.py b/git/refs/symbolic.py index 4ae7a9d1..4171fe23 100644 --- a/git/refs/symbolic.py +++ b/git/refs/symbolic.py @@ -16,7 +16,7 @@ from gitdb.exc import ( BadName ) -from .log import RefLog +from .log import RefLog, RefLogEntry # typing ------------------------------------------------------------------ @@ -26,6 +26,8 @@ from git.types import Commit_ish, PathLike, TBD, Literal if TYPE_CHECKING: from git.repo import Repo from git.refs import Reference, Head, TagReference, RemoteReference + from git.config import GitConfigParser + from git.objects.commit import Actor T_References = TypeVar('T_References', bound='SymbolicReference') @@ -229,11 +231,13 @@ class SymbolicReference(object): invalid_type = False if isinstance(commit, Object): invalid_type = commit.type != Commit.type + commit = cast('Commit', commit) elif isinstance(commit, SymbolicReference): invalid_type = commit.object.type != Commit.type else: try: - invalid_type = self.repo.rev_parse(commit).type != Commit.type + commit = self.repo.rev_parse(commit) + invalid_type = commit.type != Commit.type except (BadObject, BadName) as e: raise ValueError("Invalid object: %s" % commit) from e # END handle exception @@ -249,7 +253,9 @@ class SymbolicReference(object): # return self return None - def set_object(self, object, logmsg=None): # @ReservedAssignment + def set_object(self, object: Union[Commit_ish, 'SymbolicReference'], + logmsg: Union[str, None] = None + ) -> 'SymbolicReference': # @ReservedAssignment """Set the object we point to, possibly dereference our symbolic reference first. If the reference does not exist, it will be created @@ -276,8 +282,8 @@ class SymbolicReference(object): # set the commit on our reference return self._get_reference().set_object(object, logmsg) - commit = property(_get_commit, set_commit, doc="Query or set commits directly") - object = property(_get_object, set_object, doc="Return the object our ref currently refers to") + commit = cast('Commit', property(_get_commit, set_commit, doc="Query or set commits directly")) + object = property(_get_object, set_object, doc="Return the object our ref currently refers to") # type: ignore def _get_reference(self ) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']: @@ -290,7 +296,7 @@ class SymbolicReference(object): return self.from_path(self.repo, target_ref_path) def set_reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None - ) -> None: + ) -> 'SymbolicReference': """Set ourselves to the given ref. It will stay a symbol if the ref is a Reference. Otherwise an Object, given as Object instance or refspec, is assumed and if valid, will be set which effectively detaches the refererence if it was a purely @@ -331,7 +337,7 @@ class SymbolicReference(object): raise TypeError("Require commit, got %r" % obj) # END verify type - oldbinsha = None + oldbinsha: bytes = b'' if logmsg is not None: try: oldbinsha = self.commit.binsha @@ -357,7 +363,7 @@ class SymbolicReference(object): if logmsg is not None: self.log_append(oldbinsha, logmsg) - return None + return self @ property def reference(self) -> Union['Head', 'RemoteReference', 'TagReference', 'Reference']: @@ -365,7 +371,7 @@ class SymbolicReference(object): @ reference.setter def reference(self, ref: Union[str, Commit_ish, 'SymbolicReference'], logmsg: Union[str, None] = None - ) -> None: + ) -> 'SymbolicReference': return self.set_reference(ref=ref, logmsg=logmsg) def is_valid(self) -> bool: @@ -392,7 +398,7 @@ class SymbolicReference(object): except TypeError: return True - def log(self): + def log(self) -> 'RefLog': """ :return: RefLog for this reference. Its last entry reflects the latest change applied to this reference @@ -401,7 +407,8 @@ class SymbolicReference(object): instead of calling this method repeatedly. It should be considered read-only.""" return RefLog.from_file(RefLog.path(self)) - def log_append(self, oldbinsha, message, newbinsha=None): + def log_append(self, oldbinsha: bytes, message: Union[str, None], + newbinsha: Union[bytes, None] = None) -> 'RefLogEntry': """Append a logentry to the logfile of this ref :param oldbinsha: binary sha this ref used to point to @@ -413,15 +420,19 @@ class SymbolicReference(object): # correct to allow overriding the committer on a per-commit level. # See https://github.com/gitpython-developers/GitPython/pull/146 try: - committer_or_reader = self.commit.committer + committer_or_reader: Union['Actor', 'GitConfigParser'] = self.commit.committer except ValueError: committer_or_reader = self.repo.config_reader() # end handle newly cloned repositories - return RefLog.append_entry(committer_or_reader, RefLog.path(self), oldbinsha, - (newbinsha is None and self.commit.binsha) or newbinsha, - message) + if newbinsha is None: + newbinsha = self.commit.binsha + + if message is None: + message = '' + + return RefLog.append_entry(committer_or_reader, RefLog.path(self), oldbinsha, newbinsha, message) - def log_entry(self, index): + def log_entry(self, index: int) -> RefLogEntry: """:return: RefLogEntry at the given index :param index: python list compatible positive or negative index @@ -540,7 +551,7 @@ class SymbolicReference(object): @classmethod def create(cls: Type[T_References], repo: 'Repo', path: PathLike, - reference: Union[Commit_ish, str, 'SymbolicReference'] = 'SymbolicReference', + reference: Union[str, 'SymbolicReference'] = 'SymbolicReference', logmsg: Union[str, None] = None, force: bool = False, **kwargs: Any) -> T_References: """Create a new symbolic reference, hence a reference pointing , to another reference. @@ -553,7 +564,7 @@ class SymbolicReference(object): :param reference: The reference to which the new symbolic reference should point to. - If it is a commit'ish, the symbolic ref will be detached. + If it is a ref to a commit'ish, the symbolic ref will be detached. :param force: if True, force creation even if a symbolic reference with that name already exists. diff --git a/git/refs/tag.py b/git/refs/tag.py index ce2a58a0..edfab33d 100644 --- a/git/refs/tag.py +++ b/git/refs/tag.py @@ -70,7 +70,7 @@ class TagReference(Reference): @classmethod def create(cls: Type['TagReference'], repo: 'Repo', path: PathLike, - reference: Union[Commit_ish, str, 'SymbolicReference'] = 'HEAD', + reference: Union[str, 'SymbolicReference'] = 'HEAD', logmsg: Union[str, None] = None, force: bool = False, **kwargs: Any) -> 'TagReference': """Create a new tag reference. @@ -80,7 +80,7 @@ class TagReference(Reference): The prefix refs/tags is implied :param ref: - A reference to the object you want to tag. It can be a commit, tree or + A reference to the Object you want to tag. The Object can be a commit, tree or blob. :param logmsg: |