Skip to content

Commit 8d870e4

Browse files
committed
fix: improve bad tag handling
1 parent 3191997 commit 8d870e4

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

test/test_git.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ def test_nonversion_tags(self):
8080
version.local = (f'git{self.repo_head_hexsha}',)
8181
self.assertEqual(version, upcoming_version)
8282

83+
def test_too_short_version_tag(self):
84+
self.git_commit_new_file()
85+
self.repo.create_tag('v1.0')
86+
self.git_commit_new_file()
87+
self.repo.create_tag('v')
88+
self.git_commit_new_file()
89+
self.repo.create_tag('ver')
90+
current_version = query_git_repo(self.repo_path)
91+
_LOG.debug('current version is %s', current_version)
92+
self.assertEqual(current_version.to_str(), '1.0')
93+
8394
@unittest.skipUnless(
8495
os.environ.get('TEST_LONG') or os.environ.get('CI'), 'skipping long test')
8596
def test_too_long_no_tag(self):

version_query/git_query.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414

1515
def preprocess_git_version_tag(tag: str):
1616
"""Remove a prefix from a version tag."""
17-
if tag.startswith('ver') and len(tag) > 3:
17+
if tag.startswith('ver'):
18+
if len(tag) == 3:
19+
raise ValueError(f'the tag "{tag}" does not contain any version information')
1820
return tag[3:]
19-
if tag.startswith('v') and len(tag) > 1:
21+
if tag.startswith('v'):
22+
if len(tag) == 1:
23+
raise ValueError(f'the tag "{tag}" does not contain any version information')
2024
return tag[1:]
2125
if tag and tag[0] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
2226
return tag
@@ -26,8 +30,6 @@ def preprocess_git_version_tag(tag: str):
2630
def _git_version_tags(repo: git.Repo) -> t.Mapping[git.Tag, Version]:
2731
versions = {}
2832
for tag in repo.tags:
29-
if tag.name is None or not tag.name:
30-
continue
3133
try:
3234
tag_str = preprocess_git_version_tag(tag.name)
3335
except ValueError:

0 commit comments

Comments
 (0)