Skip to content

Commit e62d4aa

Browse files
Add release-branch-semver scheme
This is a scheme that assumes that the upcoming release for most branches is a minor release, and that only for release branches (those whose branch names parse as a version number according to current configuration) is the upcoming version a patch release. Better document the differences between the schemes in README.rst
1 parent 6ad91d0 commit e62d4aa

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

README.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,20 @@ Version number construction
477477

478478
Available implementations:
479479

480-
:guess-next-dev: automatically guesses the next development version (default)
481-
:post-release: generates post release versions (adds :code:`postN`)
482-
:python-simplified-semver: basic semantic versioning similar to ``guess-next-dev``
480+
:guess-next-dev: Automatically guesses the next development version (default).
481+
Guesses the upcoming release by incrementing the pre-release segment if present,
482+
otherwise by incrementing the micro segment. Then appends :code:`.devN`.
483+
:post-release: generates post release versions (adds :code:`.postN`)
484+
:python-simplified-semver: Basic semantic versioning. Guesses the upcoming release
485+
by incrementing the minor segment and setting the micro segment to zero if the
486+
current branch contains the string ``'feature'``, otherwise by incrementing the
487+
micro version. Then appends :code:`.devN`. Not compatible with pre-releases.
488+
:release-branch-semver: Semantic versioning for projects with release branches. The
489+
same as ``guess-next-dev`` (incrementing the pre-release or micro segment) if on
490+
a release branch: a branch whose name (ignoring namespace) parses as a version
491+
that matches the most recent tag up to the minor segment. Otherwise if on a
492+
non-release branch, increments the minor segment and sets the micro segment to
493+
zero, then appends :code:`.devN`.
483494

484495
``setuptools_scm.local_scheme``
485496
Configures how the local part of a version is rendered given a

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def parse(root):
8989
guess-next-dev = setuptools_scm.version:guess_next_dev_version
9090
post-release = setuptools_scm.version:postrelease_version
9191
python-simplified-semver = setuptools_scm.version:simplified_semver_version
92+
release-branch-semver = setuptools_scm.version:release_branch_semver
9293
9394
[setuptools_scm.local_scheme]
9495
node-and-date = setuptools_scm.version:get_local_node_and_date

src/setuptools_scm/version.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def __init__(
132132
dirty=False,
133133
preformatted=False,
134134
branch=None,
135+
config=None,
135136
**kw
136137
):
137138
if kw:
@@ -146,6 +147,7 @@ def __init__(
146147
self.dirty = dirty
147148
self.preformatted = preformatted
148149
self.branch = branch
150+
self.config = config
149151

150152
@property
151153
def extra(self):
@@ -193,7 +195,14 @@ def _parse_tag(tag, preformatted, config):
193195

194196

195197
def meta(
196-
tag, distance=None, dirty=False, node=None, preformatted=False, config=None, **kw
198+
tag,
199+
distance=None,
200+
dirty=False,
201+
node=None,
202+
preformatted=False,
203+
branch=None,
204+
config=None,
205+
**kw
197206
):
198207
if not config:
199208
warnings.warn(
@@ -203,7 +212,9 @@ def meta(
203212
parsed_version = _parse_tag(tag, preformatted, config)
204213
trace("version", tag, "->", parsed_version)
205214
assert parsed_version is not None, "cant parse version %s" % tag
206-
return ScmVersion(parsed_version, distance, node, dirty, preformatted, **kw)
215+
return ScmVersion(
216+
parsed_version, distance, node, dirty, preformatted, branch, config, **kw
217+
)
207218

208219

209220
def guess_next_version(tag_version):
@@ -260,6 +271,25 @@ def simplified_semver_version(version):
260271
)
261272

262273

274+
def release_branch_semver(version):
275+
if version.exact:
276+
return version.format_with("{tag}")
277+
if version.branch is not None:
278+
# Does the branch name (stripped of namespace) parse as a version?
279+
branch_ver = _parse_version_tag(version.branch.split("/")[-1], version.config)
280+
if branch_ver is not None:
281+
# Does the branch version up to the minor part match the tag? If not it
282+
# might be like, an issue number or something and not a version number, so
283+
# we only want to use it if it matches.
284+
tag_ver_up_to_minor = str(version.tag).split(".")[:SEMVER_MINOR]
285+
branch_ver_up_to_minor = branch_ver["version"].split(".")[:SEMVER_MINOR]
286+
if branch_ver_up_to_minor == tag_ver_up_to_minor:
287+
# We're in a release/maintenance branch, next is a patch/rc/beta bump:
288+
return version.format_next_version(guess_next_version)
289+
# We're in a development branch, next is a minor bump:
290+
return version.format_next_version(guess_next_simple_semver, retain=SEMVER_MINOR)
291+
292+
263293
def _format_local_with_time(version, time_format):
264294

265295
if version.exact or version.node is None:

testing/test_version.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import pytest
22
from setuptools_scm.config import Configuration
3-
from setuptools_scm.version import meta, simplified_semver_version, tags_to_versions
3+
from setuptools_scm.version import (
4+
meta,
5+
simplified_semver_version,
6+
release_branch_semver,
7+
tags_to_versions,
8+
)
49

510

611
c = Configuration()
@@ -43,6 +48,42 @@ def test_next_semver(version, expected_next):
4348
assert computed == expected_next
4449

4550

51+
@pytest.mark.parametrize(
52+
"version, expected_next",
53+
[
54+
pytest.param(meta("1.0.0", config=c), "1.0.0", id="exact"),
55+
pytest.param(
56+
meta("1.0.0", distance=2, branch="master", config=c),
57+
"1.1.0.dev2",
58+
id="development_branch",
59+
),
60+
pytest.param(
61+
meta("1.0.0rc1", distance=2, branch="master", config=c),
62+
"1.1.0.dev2",
63+
id="development_branch_release_candidate",
64+
),
65+
pytest.param(
66+
meta("1.0.0", distance=2, branch="maintenance/1.0.x", config=c),
67+
"1.0.1.dev2",
68+
id="release_branch_legacy_version",
69+
),
70+
pytest.param(
71+
meta("1.0.0", distance=2, branch="release-1.0", config=c),
72+
"1.0.1.dev2",
73+
id="release_branch_with_prefix",
74+
),
75+
pytest.param(
76+
meta("1.0.0", distance=2, branch="bugfix/3434", config=c),
77+
"1.1.0.dev2",
78+
id="false_positive_release_branch",
79+
),
80+
],
81+
)
82+
def test_next_release_branch_semver(version, expected_next):
83+
computed = release_branch_semver(version)
84+
assert computed == expected_next
85+
86+
4687
@pytest.mark.parametrize(
4788
"tag, expected",
4889
[

0 commit comments

Comments
 (0)