Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add types to refs/tag.py
  • Loading branch information
Yobmod committed Jul 19, 2021
commit cc63210d122ac7a113990e27b48e1bdbd07ceb4c
46 changes: 36 additions & 10 deletions git/refs/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

__all__ = ["TagReference", "Tag"]

# typing ------------------------------------------------------------------

from typing import Any, Union, TYPE_CHECKING
from git.types import Commit_ish, PathLike

if TYPE_CHECKING:
from git.repo import Repo
from git.objects import Commit
from git.objects import TagObject


# ------------------------------------------------------------------------------


class TagReference(Reference):

Expand All @@ -22,9 +35,9 @@ class TagReference(Reference):
_common_path_default = Reference._common_path_default + "/" + _common_default

@property
def commit(self):
def commit(self) -> 'Commit': # type: ignore[override] # LazyMixin has unrelated
""":return: Commit object the tag ref points to

:raise ValueError: if the tag points to a tree or blob"""
obj = self.object
while obj.type != 'commit':
Expand All @@ -37,7 +50,7 @@ def commit(self):
return obj

@property
def tag(self):
def tag(self) -> Union['TagObject', None]:
"""
:return: Tag object this tag ref points to or None in case
we are a light weight tag"""
Expand All @@ -48,10 +61,16 @@ def tag(self):

# make object read-only
# It should be reasonably hard to adjust an existing tag
object = property(Reference._get_object)

# object = property(Reference._get_object)
@property
def object(self) -> Commit_ish: # type: ignore[override]
return Reference._get_object(self)

@classmethod
def create(cls, repo, path, ref='HEAD', message=None, force=False, **kwargs):
def create(cls, repo: 'Repo', path: PathLike, reference: Union[Commit_ish, str] = 'HEAD',
logmsg: Union[str, None] = None,
force: bool = False, **kwargs: Any) -> 'TagReference':
"""Create a new tag reference.

:param path:
Expand All @@ -62,30 +81,37 @@ def create(cls, repo, path, ref='HEAD', message=None, force=False, **kwargs):
A reference to the object you want to tag. It can be a commit, tree or
blob.

:param message:
:param logmsg:
If not None, the message will be used in your tag object. This will also
create an additional tag object that allows to obtain that information, i.e.::

tagref.tag.message

:param message:
Synonym for :param logmsg:
Included for backwards compatability. :param logmsg is used in preference if both given.

:param force:
If True, to force creation of a tag even though that tag already exists.

:param kwargs:
Additional keyword arguments to be passed to git-tag

:return: A new TagReference"""
args = (path, ref)
if message:
kwargs['m'] = message
args = (path, reference)
if logmsg:
kwargs['m'] = logmsg
elif 'message' in kwargs and kwargs['message']:
kwargs['m'] = kwargs['message']

if force:
kwargs['f'] = True

repo.git.tag(*args, **kwargs)
return TagReference(repo, "%s/%s" % (cls._common_path_default, path))

@classmethod
def delete(cls, repo, *tags):
def delete(cls, repo: 'Repo', *tags: 'TagReference') -> None:
"""Delete the given existing tag or tags"""
repo.git.tag("-d", *tags)

Expand Down