Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/git/refs/tag.py: 35%
48 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from .reference import Reference
3__all__ = ["TagReference", "Tag"]
5# typing ------------------------------------------------------------------
7from typing import Any, Type, Union, TYPE_CHECKING
8from git.types import Commit_ish, PathLike
10if TYPE_CHECKING: 10 ↛ 11line 10 didn't jump to line 11, because the condition on line 10 was never true
11 from git.repo import Repo
12 from git.objects import Commit
13 from git.objects import TagObject
14 from git.refs import SymbolicReference
17# ------------------------------------------------------------------------------
20class TagReference(Reference):
22 """Class representing a lightweight tag reference which either points to a commit
23 ,a tag object or any other object. In the latter case additional information,
24 like the signature or the tag-creator, is available.
26 This tag object will always point to a commit object, but may carry additional
27 information in a tag object::
29 tagref = TagReference.list_items(repo)[0]
30 print(tagref.commit.message)
31 if tagref.tag is not None:
32 print(tagref.tag.message)"""
34 __slots__ = ()
35 _common_default = "tags"
36 _common_path_default = Reference._common_path_default + "/" + _common_default
38 @property
39 def commit(self) -> "Commit": # type: ignore[override] # LazyMixin has unrelated commit method
40 """:return: Commit object the tag ref points to
42 :raise ValueError: if the tag points to a tree or blob"""
43 obj = self.object
44 while obj.type != "commit":
45 if obj.type == "tag":
46 # it is a tag object which carries the commit as an object - we can point to anything
47 obj = obj.object
48 else:
49 raise ValueError(
50 (
51 "Cannot resolve commit as tag %s points to a %s object - "
52 + "use the `.object` property instead to access it"
53 )
54 % (self, obj.type)
55 )
56 return obj
58 @property
59 def tag(self) -> Union["TagObject", None]:
60 """
61 :return: Tag object this tag ref points to or None in case
62 we are a light weight tag"""
63 obj = self.object
64 if obj.type == "tag":
65 return obj
66 return None
68 # make object read-only
69 # It should be reasonably hard to adjust an existing tag
71 # object = property(Reference._get_object)
72 @property
73 def object(self) -> Commit_ish: # type: ignore[override]
74 return Reference._get_object(self)
76 @classmethod
77 def create(
78 cls: Type["TagReference"],
79 repo: "Repo",
80 path: PathLike,
81 reference: Union[str, "SymbolicReference"] = "HEAD",
82 logmsg: Union[str, None] = None,
83 force: bool = False,
84 **kwargs: Any,
85 ) -> "TagReference":
86 """Create a new tag reference.
88 :param path:
89 The name of the tag, i.e. 1.0 or releases/1.0.
90 The prefix refs/tags is implied
92 :param ref:
93 A reference to the Object you want to tag. The Object can be a commit, tree or
94 blob.
96 :param logmsg:
97 If not None, the message will be used in your tag object. This will also
98 create an additional tag object that allows to obtain that information, i.e.::
100 tagref.tag.message
102 :param message:
103 Synonym for :param logmsg:
104 Included for backwards compatibility. :param logmsg is used in preference if both given.
106 :param force:
107 If True, to force creation of a tag even though that tag already exists.
109 :param kwargs:
110 Additional keyword arguments to be passed to git-tag
112 :return: A new TagReference"""
113 if "ref" in kwargs and kwargs["ref"]:
114 reference = kwargs["ref"]
116 if "message" in kwargs and kwargs["message"]:
117 kwargs["m"] = kwargs["message"]
118 del kwargs["message"]
120 if logmsg:
121 kwargs["m"] = logmsg
123 if force:
124 kwargs["f"] = True
126 args = (path, reference)
128 repo.git.tag(*args, **kwargs)
129 return TagReference(repo, "%s/%s" % (cls._common_path_default, path))
131 @classmethod
132 def delete(cls, repo: "Repo", *tags: "TagReference") -> None: # type: ignore[override]
133 """Delete the given existing tag or tags"""
134 repo.git.tag("-d", *tags)
137# provide an alias
138Tag = TagReference