Skip to content

Commit 031ee08

Browse files
committed
Make version sanitization less strict
1 parent 848aa4a commit 031ee08

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

CHANGELOG.rst

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ Changelog
44
1.12
55
----
66

7+
.. changelog::
8+
:version: 1.12.1
9+
10+
.. change::
11+
:tags: core, bug
12+
13+
Make version sanitization less strict, allow to automatically convert some cases, e.g.
14+
``1.0.0+feature/abc`` to ``1.0.0+feature.abc``
15+
716
.. changelog::
817
:version: 1.12.0
918
:released: 13.10.2022

setuptools_git_versioning.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
ENV_VARS_REGEXP = re.compile(r"\{env:(?P<name>[^:}]+):?(?P<default>[^}]+\}*)?\}", re.IGNORECASE | re.UNICODE)
2828
TIMESTAMP_REGEXP = re.compile(r"\{timestamp:?(?P<fmt>[^:}]+)?\}", re.IGNORECASE | re.UNICODE)
2929

30+
# https://github.com/pypa/setuptools/blob/bc39d28bda2a1faee6680ae30e42526b9d775151/setuptools/command/dist_info.py#L108-L131
31+
UNSUPPORTED_SYMBOL_REGEXP = re.compile(r"[^\w\d]+", re.IGNORECASE | re.UNICODE)
32+
3033
LOG_FORMAT = "[%(asctime)s] %(levelname)+8s: %(message)s"
3134
# setuptools v60.2.0 changed default logging level to DEBUG: https://github.com/pypa/setuptools/pull/2974
3235
# to avoid printing information messages to the same output as version number,
@@ -447,7 +450,19 @@ def get_version_from_callback(
447450
def sanitize_version(version: str) -> str:
448451
log.log(INFO, "Before sanitization %r", version)
449452

450-
result = str(Version(version))
453+
public, sep, local = version.partition("+")
454+
455+
# replace "feature/ABC-123" with "feature.ABC.123"
456+
sanitized_public = UNSUPPORTED_SYMBOL_REGEXP.sub(".", public)
457+
sanitized_local = UNSUPPORTED_SYMBOL_REGEXP.sub(".", local)
458+
459+
sanitized_version = sanitized_public + sep + sanitized_local
460+
sanitized_version = sanitized_version.rstrip(".")
461+
462+
# replace "feature.ABC.123" with "feature.abc.123"
463+
# drop leading "v" symbol
464+
# other replacements according to PEP-440, like "-dev" -> ".dev"
465+
result = str(Version(sanitized_version))
451466
log.log(INFO, "Result %r", result)
452467
return result
453468

tests/test_integration/test_substitution.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def test_substitution_env(repo, dev_template, pipeline_id, suffix):
135135
"{tag}.post{ccount}+{}",
136136
lambda dt: (dt.strftime("%Y.%m.%dt%H.%M"),),
137137
),
138+
# unknown substitution
139+
("{tag}+git.{timestamp:%i}", "{tag}+git.i", lambda x: []),
138140
# pure string
139141
("{tag}+git.{timestamp:abc}", "{tag}+git.abc", lambda x: []),
140142
],
@@ -167,7 +169,6 @@ def test_substitution_timestamp(repo, template, fmt, callback):
167169
"{tag}+a{env:MISSING_ENV:{}}}",
168170
"{tag}+a{timestamp:A:B}",
169171
"{tag}+a{timestamp:{%Y}",
170-
"{tag}+a{timestamp:%i}",
171172
],
172173
)
173174
def test_substitution_wrong_format(repo, template):

tests/test_integration/test_tag.py

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def test_tag_missing(repo, create_config, starting_version, version):
113113
("1.2.3+local", "1.2.3+local"),
114114
("1.2.3+local-abc", "1.2.3+local.abc"),
115115
("1.2.3+local_abc", "1.2.3+local.abc"),
116+
("1.2.3+local/abc", "1.2.3+local.abc"),
117+
("1.2.3+local/abc/-", "1.2.3+local.abc"),
116118
],
117119
)
118120
def test_tag_sanitization(repo, create_config, tag, version):

tests/test_integration/test_version_callback.py

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def test_version_callback_missing(repo, create_version_py, create_config):
157157
("1.2.3+local", "1.2.3+local"),
158158
("1.2.3+local-abc", "1.2.3+local.abc"),
159159
("1.2.3+local_abc", "1.2.3+local.abc"),
160+
("1.2.3+local/abc", "1.2.3+local.abc"),
161+
("1.2.3+local/abc/-", "1.2.3+local.abc"),
160162
],
161163
)
162164
def test_version_callback_sanitization(repo, version, real_version, create_config):

tests/test_integration/test_version_file.py

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def test_version_file_dirty(repo, create_config, add, template, subst):
122122
("1.2.3+local", "1.2.3+local"),
123123
("1.2.3+local-abc", "1.2.3+local.abc"),
124124
("1.2.3+local_abc", "1.2.3+local.abc"),
125+
("1.2.3+local/abc", "1.2.3+local.abc"),
126+
("1.2.3+local/abc/-", "1.2.3+local.abc"),
125127
],
126128
)
127129
@pytest.mark.parametrize("count_commits_from_version_file", [True, False])

0 commit comments

Comments
 (0)