Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support to keep values of specific parts (eg. metadata used in semver) #80 #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ commit = True
tag = True
current_version = 1.0.2-dev
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?(\+(?P<metadata>.*))?

serialize =
{major}.{minor}.{patch}-{release}
{major}.{minor}.{patch}-{release}+{metadata}
{major}.{minor}.{patch}+{metadata}
{major}.{minor}.{patch}

[bumpversion:file:setup.py]
[bumpversion:part:metadata]
keep_value = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this independent, because the metadata part can still change (if you change it explicitly).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I'm not sure if I would grok exactly what independent means at a first glance. Changing it explicitly is out-of-scope of bumpversion and in the context of bumpversion keep_value: True seems more straightforward.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to agree with @c4urself as I as well wouldn't know how to interpret independent. But I am still open for other suggestions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now I see where this independent actually comes from (ADVbumpversion). If you plan to merge both forks again I would be find with independent.
Are there any plans to do that?


[bumpversion:file:bumpversion/__init__.py]
#[bumpversion:file:setup.py]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beware, bump2version will remove these commented lines on the next bump!


[bumpversion:file:CHANGELOG.md]
search = **unreleased**
Expand Down
9 changes: 7 additions & 2 deletions bumpversion/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NumericFunction:

FIRST_NUMERIC = re.compile(r"([^\d]*)(\d+)(.*)")

def __init__(self, first_value=None):
def __init__(self, first_value=None, keep_value=False):

if first_value is not None:
try:
Expand All @@ -33,6 +33,8 @@ def __init__(self, first_value=None):

self.first_value = str(first_value)
self.optional_value = self.first_value
self.keep_value = keep_value


def bump(self, value):
part_prefix, part_numeric, part_suffix = self.FIRST_NUMERIC.search(
Expand All @@ -57,7 +59,7 @@ class ValuesFunction:
you get a ValueError exception.
"""

def __init__(self, values, optional_value=None, first_value=None):
def __init__(self, values, optional_value=None, first_value=None, keep_value=False):

if not values:
raise ValueError("Version part values cannot be empty")
Expand Down Expand Up @@ -88,6 +90,9 @@ def __init__(self, values, optional_value=None, first_value=None):

self.first_value = first_value

self.keep_value = keep_value


def bump(self, value):
try:
return self._values[self._values.index(value) + 1]
Expand Down
10 changes: 10 additions & 0 deletions bumpversion/version_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def first_value(self):
def optional_value(self):
return str(self.function.optional_value)

@property
def keep_value(self):
return bool(self.function.keep_value)

def bump(self, value=None):
return self.function.bump(value)

Expand All @@ -53,6 +57,7 @@ def __init__(self, value, config=None):

if config is None:
config = NumericVersionPartConfiguration()
#print("#+#+#+#+#+ version part: %s - %s" % (value, config.function.keep_value))

self.config = config

Expand All @@ -69,6 +74,9 @@ def bump(self):
def is_optional(self):
return self.value == self.config.optional_value

def keep_value(self):
return self.config.keep_value

def __format__(self, format_spec):
return self.value

Expand All @@ -81,6 +89,8 @@ def __eq__(self, other):
return self.value == other.value

def null(self):
if self.keep_value():
return self.copy()
return VersionPart(self.config.first_value, self.config)


Expand Down
38 changes: 38 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,44 @@ def test_bump_non_numeric_parts(tmpdir):

assert '1.6.dev' == tmpdir.join("with_pre_releases.txt").read()

@pytest.mark.parametrize("metadata", ["3.2.1", "metadata", "3.2.1-meta", "meta-3.2.1"])
def test_part_not_bumped_when_keep_value_true(tmpdir, capsys, metadata):
tmpdir.join("with_keep_value.txt").write("1.2.3-SNAPSHOT+{}".format(metadata))
tmpdir.chdir()

tmpdir.join(".bumpversion.cfg").write(dedent(r"""
[bumpversion]
current_version = 1.2.3-SNAPSHOT+%s
parse = (?P<major>\d+).(?P<minor>\d+).(?P<patch>\d+)(-(?P<release>[^\+]+))?(\+(?P<metadata>.*))?
serialize =
{major}.{minor}.{patch}-{release}+{metadata}
{major}.{minor}.{patch}+{metadata}
{major}.{minor}.{patch}

[bumpversion:part:metadata]
keep_value = True

[bumpversion:part:release]
optional_value = release
values =
SNAPSHOT
release

[bumpversion:file:with_keep_value.txt]
""" % metadata).strip())

main(['patch', '--verbose'])
assert '1.2.4-SNAPSHOT+{}'.format(metadata) == tmpdir.join("with_keep_value.txt").read()

main(['minor', '--verbose'])
assert '1.3.0-SNAPSHOT+{}'.format(metadata) == tmpdir.join("with_keep_value.txt").read()

main(['major', '--verbose'])
assert '2.0.0-SNAPSHOT+{}'.format(metadata) == tmpdir.join("with_keep_value.txt").read()

main(['release', '--verbose'])
assert '2.0.0+{}'.format(metadata) == tmpdir.join("with_keep_value.txt").read()


def test_optional_value_from_documentation(tmpdir):
tmpdir.join("optional_value_from_doc.txt").write("1.alpha")
Expand Down