0

I have a repo on GitLab and now I am on the need of checking if a tag exists on a specific repo. I need to check this using Python.

In GitLab API I don't see any method to check this.

If prefer doing it using the GitLab API but if it is not possible I would like to know if it can be done directly using some Git command.

Willy
  • 9,848
  • 22
  • 141
  • 284

1 Answers1

1

You can use "subprocess" library and shell command. Like this:

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")
Dmitrii Malygin
  • 144
  • 2
  • 2
  • 12