Skip to content

Commit 56149c3

Browse files
sacca97copernico
authored andcommitted
new logic for version_to_tag method
1 parent d2d1494 commit 56149c3

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

prospector/git/version_to_tag.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
def clean_tag(tag: str) -> str:
88
"""Clean a tag name returning only the numeric part separated by dots."""
9-
return re.sub(
10-
r"^[a-zA-Z]+|[a-zA-Z]+$",
11-
"",
12-
".".join(
13-
[n for n in re.split(r"[^0-9a-zA-Z]+", tag) if not n.isalpha()]
14-
# bool(re.search(r"\d", n))
15-
),
16-
)
9+
return re.sub(r"[^0-9a-zA-Z]+", ".", tag, flags=re.IGNORECASE)
10+
# return re.sub(
11+
# r"^[a-zA-Z]+|[a-zA-Z]+$",
12+
# "",
13+
# ".".join(
14+
# [n for n in re.split(r"[^0-9a-zA-Z]+", tag) if not n.isalpha()]
15+
# # bool(re.search(r"\d", n))
16+
# ),
17+
# )
1718

1819

1920
def get_possible_missing_tag(
@@ -37,27 +38,48 @@ def get_possible_missing_tag(
3738

3839
def get_possible_tags(tags: list[str], versions: str):
3940
"""Given a list of tags and a version interval, return the possible tag interval that matches."""
40-
tags_mapping: dict[str, list[str]] = dict()
41-
# Create a mapping between cleaned tags and the original tags (one stripped tag can match multiple tags)
42-
for tag in tags:
43-
stripped_tag = clean_tag(tag)
44-
if stripped_tag not in tags_mapping:
45-
tags_mapping[stripped_tag] = [tag]
46-
else:
47-
tags_mapping[stripped_tag].append(tag)
41+
prev_version, next_version = versions.split(":")
4842

49-
tags_mapping.pop("", None)
43+
prev_tag = [
44+
tag
45+
for tag in tags
46+
if prev_version in clean_tag(tag)
47+
and not bool(re.search(r"rc\d+|\d{9,}", tag, flags=re.IGNORECASE))
48+
]
49+
next_tag = [
50+
tag
51+
for tag in tags
52+
if next_version in clean_tag(tag)
53+
and not bool(re.search(r"rc\d+|\d{9,}", tag, flags=re.IGNORECASE))
54+
]
55+
# TODO: Remove this print
56+
print(prev_tag, next_tag)
57+
# if len(prev_tag) == 0 or len(next_tag) == 0:
58+
# tags_mapping: dict[str, list[str]] = dict()
59+
# # Create a mapping between cleaned tags and the original tags (one stripped tag can match multiple tags)
60+
# for tag in tags:
61+
# stripped_tag = clean_tag(tag)
62+
63+
# if stripped_tag not in tags_mapping:
64+
# tags_mapping[stripped_tag] = [tag]
65+
# else:
66+
# tags_mapping[stripped_tag].append(tag)
67+
68+
# tags_mapping.pop("", None)
69+
# if len(prev_tag) == 0:
70+
# prev_tag = tags_mapping.get(prev_version, [])
71+
# if len(next_tag) == 0:
72+
# next_tag = tags_mapping.get(next_version, [])
5073

51-
prev_version, next_version = versions.split(":")
52-
prev_tag = tags_mapping.get(prev_version, [])
53-
next_tag = tags_mapping.get(next_version, [])
5474
# If there are two exact matches, return them
5575
if len(prev_tag) == 1 and len(next_tag) == 1:
5676
return prev_tag[0], next_tag[0]
5777
# If there is one exact match, and multiple candidates for the other tag, return the most similar
5878
elif len(prev_tag) == 1 and len(next_tag) > 1:
79+
next_tag = [tag for tag in next_tag if tag > prev_tag[0]]
5980
return prev_tag[0], difflib.get_close_matches(prev_tag[0], next_tag, n=1)[0]
6081
elif len(prev_tag) > 1 and len(next_tag) == 1:
82+
prev_tag = [tag for tag in prev_tag if tag < next_tag[0]]
6183
return difflib.get_close_matches(next_tag[0], prev_tag, n=1)[0], next_tag[0]
6284
# If there is one exact match but no candidates for the other tag, exit and hint the user with possible candidates
6385
elif len(prev_tag) == 0 and len(next_tag) == 1:

0 commit comments

Comments
 (0)