From 65f5d7c0b4e4b256d79b1173744bec0abdd41dec Mon Sep 17 00:00:00 2001 From: Nguyen Huy Hoang <24520554@gm.uit.edu.vn> Date: Thu, 26 Mar 2026 23:05:36 +0700 Subject: [PATCH] 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> --- git/cmd.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/git/cmd.py b/git/cmd.py index b529bcc10..814621f34 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -995,6 +995,28 @@ def __getattribute__(self, name: str) -> Any: _warn_use_shell(extra_danger=False) return super().__getattribute__(name) + def latest_remote_tag(self, repository: PathLike) -> Optional[str]: + output = self.ls_remote("--tags", "--sort=-version:refname", repository) + if not output: + return None + + for line in output.splitlines(): + if not line: + continue + try: + _, ref = line.split("\t", 1) + except ValueError: + continue + if not ref.startswith("refs/tags/"): + continue + tag = ref[len("refs/tags/") :] + if tag.endswith("^{}"): + tag = tag[:-3] + if tag: + return tag + + return None + def __getattr__(self, name: str) -> Any: """A convenience method as it allows to call the command as if it was an object.