Skip to content

Commit 519d676

Browse files
committed
Remove all non-numeric symbols from version prefix
1 parent eabf9d5 commit 519d676

File tree

8 files changed

+40
-20
lines changed

8 files changed

+40
-20
lines changed

CHANGELOG.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ Changelog
1010
.. change::
1111
:tags: core, breaking
1212

13-
``version_callback`` option is used even if there are some tags in the current branch
13+
:ref:`version-callback-option` option is used even if there are some tags in the current branch
14+
15+
.. change::
16+
:tags: config, feature
17+
18+
Raise exception if both :ref:`version-callback-option` and :ref:`version-file-option` options are set
1419

1520
.. change::
1621
:tags: core, feature
1722

18-
Raise exception if both ``version_file`` and ``version_callback`` options are set
23+
Remove all non-numeric symbols from version prefix, not just ``v``
1924

2025
1.9
2126
----

docs/options/dev_template.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Version number template for :ref:`tag-release` or :ref:`version-file` versioning schemas.
77

8-
Used if no untracked files and current commit is not tagged.
8+
Used if there are no untracked files, and current commit is not tagged.
99

1010
.. note::
1111

docs/schemas/callback/version_callback.rst

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ If a value of this option is not a function but just str, it also could be used:
9898
is set**.
9999
You should explicitly call ``setuptools_git_versioning.version_from_git`` function in the callback.
100100

101+
.. note::
102+
103+
Callback result is returned *as is*, so it should be a :pep:`440` compatible version number
101104

102105
See also
103106
""""""""

setuptools_git_versioning.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
DEFAULT_SORT_BY = "creatordate"
2222
ENV_VARS_REGEXP = re.compile(r"\{env:(?P<name>[^:}]+):?(?P<default>[^}]+\}*)?\}", re.IGNORECASE | re.UNICODE)
2323
TIMESTAMP_REGEXP = re.compile(r"\{timestamp:?(?P<fmt>[^:}]+)?\}", re.IGNORECASE | re.UNICODE)
24+
LOCAL_REGEXP = re.compile(r"[^a-z\d.]+", re.IGNORECASE)
25+
VERSION_PREFIX_REGEXP = re.compile(r"^[^\d]+", re.IGNORECASE | re.UNICODE)
2426

2527
DEFAULT_CONFIG = {
2628
"template": DEFAULT_TEMPLATE,
@@ -343,16 +345,16 @@ def get_version_from_callback(
343345
log.warning(f"Parsing version_callback {version_callback} with type {type(version_callback)}")
344346

345347
if callable(version_callback):
346-
return version_callback()
347-
348-
result = version_callback
348+
result = version_callback()
349+
else:
350+
result = version_callback
349351

350-
try:
351-
return load_callable(version_callback, package_name)()
352-
except ValueError:
353-
result = import_reference(version_callback, package_name)
354-
except (ImportError, NameError) as e:
355-
log.warning(f"version_callback is not a valid reference:\n\t{e}")
352+
try:
353+
result = load_callable(version_callback, package_name)()
354+
except ValueError:
355+
result = import_reference(version_callback, package_name)
356+
except (ImportError, NameError) as e:
357+
log.warning(f"version_callback is not a valid reference:\n\t{e}")
356358

357359
from packaging.version import Version
358360

@@ -402,8 +404,7 @@ def version_from_git(
402404
return starting_version
403405

404406
if not count_commits_from_version_file:
405-
# TODO: drop all leading non-numeric symbols
406-
return tag.lstrip("v") # for tag "v1.0.0" drop leading "v" symbol
407+
return VERSION_PREFIX_REGEXP.sub("", tag) # for tag "v1.0.0" drop leading "v" symbol
407408

408409
tag_sha = get_latest_file_commit(version_file)
409410
else:
@@ -435,7 +436,6 @@ def version_from_git(
435436

436437
# Ensure local version label only contains permitted characters
437438
public, sep, local = version.partition("+")
438-
local_sanitized = re.sub(r"[^a-zA-Z0-9.]", ".", local)
439-
# TODO: drop all leading non-numeric symbols
440-
public_sanitized = public.lstrip("v") # for version "v1.0.0" drop leading "v" symbol
439+
local_sanitized = LOCAL_REGEXP.sub(".", local)
440+
public_sanitized = VERSION_PREFIX_REGEXP.sub("", public) # for version "v1.0.0" drop leading "v" symbol
441441
return public_sanitized + sep + local_sanitized

tests/lib/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def create_file(
100100
if content is None:
101101
content = rand_str()
102102

103-
log.warning(content)
103+
log.info(content)
104104
with open(os.path.join(cwd, name), "w") as f:
105105
f.write(content)
106106

tests/test_integration/test_tag.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ def test_tag_no_tag(repo, create_config, starting_version, version):
103103
[
104104
("1.0.0", "1.0.0"),
105105
("v1.2.3", "1.2.3"),
106+
("beta1.2.3", "1.2.3"),
107+
("alpha1.2.3", "1.2.3"),
106108
],
107109
)
108-
def test_tag_drop_leading_v(repo, create_config, tag, version):
110+
def test_tag_drop_leading_non_numbers(repo, create_config, tag, version):
109111
create_config(repo)
110112

111113
create_tag(repo, tag)

tests/test_integration/test_version_callback.py

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ def test_version_callback_drop_leading_v(repo, version, real_version, create_con
137137
assert get_version(repo) == real_version
138138

139139

140+
def test_version_callback_wrong_version_number(repo, create_config):
141+
create_file(repo, "version.py", VERSION_PY.format(version="alpha1.2.3"), commit=False)
142+
create_config(repo, {"version_callback": "version:get_version"})
143+
144+
with pytest.raises(subprocess.CalledProcessError):
145+
get_version(repo)
146+
147+
140148
def test_version_callback_not_a_repo(repo_dir, create_config):
141149
version = "1.0.0"
142150
create_file(repo_dir, "version.py", VERSION_PY.format(version=version), add=False, commit=False)

tests/test_integration/test_version_file.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,11 @@ def test_version_file_dirty(repo, create_config, add, template, subst):
113113
[
114114
("1.0.0", "1.0.0"),
115115
("v1.2.3", "1.2.3"),
116+
("beta1.2.3", "1.2.3"),
117+
("alpha1.2.3", "1.2.3"),
116118
],
117119
)
118-
def test_version_file_drop_leading_v(repo, create_config, version, real_version):
120+
def test_version_file_drop_leading_non_numbers(repo, create_config, version, real_version):
119121
create_config(
120122
repo,
121123
{

0 commit comments

Comments
 (0)