Skip to content

Commit 3368b66

Browse files
committed
Simplified fetching of git tags
Ticket: ENT-13778
1 parent 0db861e commit 3368b66

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/cfengine_cli/masterfiles/generate_git_tags.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,29 @@ def clone_or_update_repo(repo):
4242
def get_commit_shas_from_tags(repo_path):
4343
# Returns a mapping of git tag to commit SHA for all version tags in the repo
4444
output = (
45-
subprocess.check_output(["git", "show-ref", "--tags"], cwd=repo_path)
45+
subprocess.check_output(["git", "show-ref", "--tags", "-d"], cwd=repo_path)
4646
.decode()
4747
.strip()
4848
)
4949
tag_map = {}
5050

5151
for line in output.splitlines():
52-
ref = line.split()[1]
53-
tag = ref.split("refs/tags/")[1]
54-
if re.fullmatch(TAG_REGEX, tag):
55-
sha = (
56-
subprocess.check_output(
57-
["git", "log", "-n", "1", "--format=%H", tag], cwd=repo_path
58-
)
59-
.decode()
60-
.strip()
61-
)
62-
tag_map[tag] = sha
52+
parts = line.split()
53+
if len(parts) != 2:
54+
continue
55+
sha, ref = parts
56+
57+
if ref.startswith("refs/tags/"):
58+
tag = ref.split("refs/tags/")[1]
59+
if tag.endswith("^{}"):
60+
tag_name = tag[:-3]
61+
if re.fullmatch(TAG_REGEX, tag_name):
62+
tag_map[tag_name] = sha
63+
else:
64+
if re.fullmatch(TAG_REGEX, tag):
65+
# Only set if not already set by ^{}
66+
if tag not in tag_map:
67+
tag_map[tag] = sha
6368

6469
return tag_map
6570

0 commit comments

Comments
 (0)