Skip to content

Commit 65f5d7c

Browse files
author
Nguyen Huy Hoang
committed
feat: add a first-class api to get the latest tag from a remote via git ls-remote
Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
1 parent becccc8 commit 65f5d7c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

git/cmd.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,28 @@ def __getattribute__(self, name: str) -> Any:
995995
_warn_use_shell(extra_danger=False)
996996
return super().__getattribute__(name)
997997

998+
def latest_remote_tag(self, repository: PathLike) -> Optional[str]:
999+
output = self.ls_remote("--tags", "--sort=-version:refname", repository)
1000+
if not output:
1001+
return None
1002+
1003+
for line in output.splitlines():
1004+
if not line:
1005+
continue
1006+
try:
1007+
_, ref = line.split("\t", 1)
1008+
except ValueError:
1009+
continue
1010+
if not ref.startswith("refs/tags/"):
1011+
continue
1012+
tag = ref[len("refs/tags/") :]
1013+
if tag.endswith("^{}"):
1014+
tag = tag[:-3]
1015+
if tag:
1016+
return tag
1017+
1018+
return None
1019+
9981020
def __getattr__(self, name: str) -> Any:
9991021
"""A convenience method as it allows to call the command as if it was an object.
10001022

0 commit comments

Comments
 (0)