Skip to content

Commit

Permalink
Merge pull request #234 from andrivet/feature-independent
Browse files Browse the repository at this point in the history
Implementation of the independent feature from ADVbumpversion
  • Loading branch information
florisla authored Mar 2, 2022
2 parents f8e6d14 + 4adeeef commit bc95374
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,32 @@ first_value = 1
`bump2version release` would bump `1.alpha1` to `1.beta0`, starting
the build at `0`.


#### `independent =`
**default**: `False`

When this value is set to `True`, the part is not reset when other parts are incremented. Its incrementation is
independent of the other parts. It is in particular useful when you have a build number in your version that is
incremented independently of the actual version.

Example:

```ini
[bumpversion]
current_version: 2.1.6-5123
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\-(?P<build>\d+)
serialize = {major}.{minor}.{patch}-{build}

[bumpversion:file:VERSION.txt]

[bumpversion:part:build]
independent = True
```

Here, `bump2version build` would bump `2.1.6-5123` to `2.1.6-5124`. Executing`bump2version major`
would bump `2.1.6-5124` to `3.0.0-5124` without resetting the build number.


### Configuration file -- File specific configuration

This configuration is in the section: `[bumpversion:file:…]` or `[bumpversion:glob:…]`
Expand Down
5 changes: 3 additions & 2 deletions RELATED.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* [tbump](https://github.com/tankerhq/tbump) is a complete rewrite, with a nicer UX and additional features, like running commands (aka hooks) before or after the bump. It only works for Git repos right now.

* [ADVbumpversion](https://github.com/andrivet/advbumpversion) is another fork.
It offers some features which are still work in progress here; it's
definitely our desire to merge back (issue [#121](https://github.com/c4urself/bump2version/issues/121)).
It offered some features that are now incorporated by its author into `bump2version`.
This fork is thus now deprecated, and it recommends to use `bump2version`
(issue [#121](https://github.com/c4urself/bump2version/issues/121)).

* [zest.releaser](https://pypi.org/project/zest.releaser/) manages
your Python package releases and keeps the version number in one location.
Expand Down
3 changes: 3 additions & 0 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ def _load_configuration(config_file, explicit_config, defaults):
)
ThisVersionPartConfiguration = ConfiguredVersionPartConfiguration

if config.has_option(section_name, 'independent'):
section_config['independent'] = config.getboolean(section_name, 'independent')

part_configs[section_value] = ThisVersionPartConfiguration(
**section_config
)
Expand Down
6 changes: 4 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, independent=False):

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

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

def bump(self, value):
part_prefix, part_numeric, part_suffix = self.FIRST_NUMERIC.search(
Expand All @@ -57,7 +58,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, independent=False):

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

self.first_value = first_value
self.independent = independent

def bump(self, value):
try:
Expand Down
9 changes: 8 additions & 1 deletion bumpversion/version_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def first_value(self):
def optional_value(self):
return str(self.function.optional_value)

@property
def independent(self):
return self.function.independent

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

Expand Down Expand Up @@ -73,6 +77,9 @@ def bump(self):
def is_optional(self):
return self.value == self.config.optional_value

def is_independent(self):
return self.config.independent

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

Expand Down Expand Up @@ -117,7 +124,7 @@ def bump(self, part_name, order):
if label == part_name:
new_values[label] = self._values[label].bump()
bumped = True
elif bumped:
elif bumped and not self._values[label].is_independent():
new_values[label] = self._values[label].null()
else:
new_values[label] = self._values[label].copy()
Expand Down
47 changes: 47 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2345,3 +2345,50 @@ def test_2optional_mixed_2positional(self):

assert positional == ['minor', 'setup.py']
assert optional == ['--allow-dirty', '-m', '"Commit"']


def test_build_number_configuration(tmpdir):
tmpdir.join("VERSION.txt").write("2.1.6-5123")
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write(dedent(r"""
[bumpversion]
current_version: 2.1.6-5123
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\-(?P<build>\d+)
serialize = {major}.{minor}.{patch}-{build}
[bumpversion:file:VERSION.txt]
[bumpversion:part:build]
independent = True
"""))

main(['build'])
assert '2.1.6-5124' == tmpdir.join("VERSION.txt").read()

main(['major'])
assert '3.0.0-5124' == tmpdir.join("VERSION.txt").read()

main(['build'])
assert '3.0.0-5125' == tmpdir.join("VERSION.txt").read()


def test_independent_falsy_value_in_config_does_not_bump_independently(tmpdir):
tmpdir.join("VERSION").write("2.1.0-5123")
tmpdir.chdir()
tmpdir.join(".bumpversion.cfg").write(dedent(r"""
[bumpversion]
current_version: 2.1.0-5123
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\-(?P<build>\d+)
serialize = {major}.{minor}.{patch}-{build}
[bumpversion:file:VERSION]
[bumpversion:part:build]
independent = 0
"""))

main(['build'])
assert '2.1.0-5124' == tmpdir.join("VERSION").read()

main(['major'])
assert '3.0.0-0' == tmpdir.join("VERSION").read()

0 comments on commit bc95374

Please sign in to comment.