git 如何检查标签是否存在于repo [重复]中

4nkexdtk  于 2023-05-12  发布在  Git
关注(0)|答案(1)|浏览(123)

此问题已在此处有答案

check if pushed tag is on the git remote(4个答案)
3天前关闭。
我在GitLab上有一个repo,现在我需要检查特定repo上是否存在标记。我需要用Python来检查一下。
GitLab API中,我没有看到任何方法来检查这个。
如果喜欢使用GitLab API但如果不可能,我想知道是否可以直接使用一些Git命令来完成。

o2rvlv0m

o2rvlv0m1#

可以使用“subprocess”库和shell命令。就像这样:

import subprocess

repo_url = "https://gitlab.com/<your_namespace>/<your_project>.git"
tag_name = "v1.0.0"
branch_name = "main"

command = f"git ls-remote --tags --refs {repo_url} refs/heads/{branch_name}"

output = subprocess.check_output(command, shell=True, universal_newlines=True)

if f"refs/tags/{tag_name}" in output:
    print(f"{tag_name} exists")
else:
    print(f"{tag_name} doesn't exist")

相关问题