Skip to content

Commit bcca51f

Browse files
committed
#220: Allow customizing the bump index
1 parent 8635cd5 commit bcca51f

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Unreleased
22

3+
* Added:
4+
* The `bump` config may now be set to a table,
5+
which supports an `index` field.
36
* Fixed:
47
* Compatibility with other plugins (such as `poetry-monoranger-plugin`)
58
that replace the `ConsoleCommandEvent.command.poetry` instance.

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,24 @@ In your pyproject.toml file, you may configure the following options:
224224
* `latest-tag` (boolean, default: false):
225225
If true, then only check the latest tag for a version,
226226
rather than looking through all the tags until a suitable one is found to match the `pattern`.
227-
* `bump` (boolean, default: false):
228-
If true, then increment the last part of the version `base` by 1,
227+
* `bump` (boolean or table, default: false):
228+
If enabled, then increment the last part of the version `base` by 1,
229229
unless the `stage` is set,
230230
in which case increment the `revision` by 1 or set it to a default of 2 if there was no `revision`.
231231
Does nothing when on a commit with a version tag.
232232
233+
One of:
234+
235+
* When set to a boolean, true means enable bumping, with other settings as default.
236+
* When set to a table, these fields are allowed:
237+
* `enable` (boolean, default: false):
238+
If true, enable bumping.
239+
* `index` (integer, default: -1):
240+
Numerical position to increment in the base.
241+
This follows Python indexing rules, so positive numbers start from
242+
the left side and count up from 0, while negative numbers start from
243+
the right side and count down from -1.
244+
233245
Example, if there have been 3 commits since the `v1.3.1` tag:
234246
* PEP 440 with `bump = false`: `1.3.1.post3.dev0+28c1684`
235247
* PEP 440 with `bump = true`: `1.3.2.dev3+28c1684`

poetry_dynamic_versioning/__init__.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@
8787
},
8888
)
8989

90+
_Bump = TypedDict(
91+
"_Bump",
92+
{
93+
"enable": bool,
94+
"index": int,
95+
},
96+
)
97+
9098
_Config = TypedDict(
9199
"_Config",
92100
{
@@ -103,7 +111,7 @@
103111
"format": Optional[str],
104112
"format-jinja": Optional[str],
105113
"format-jinja-imports": Sequence[_JinjaImport],
106-
"bump": bool,
114+
"bump": Union[bool, _Bump],
107115
"tagged-metadata": bool,
108116
"full-commit": bool,
109117
"tag-branch": Optional[str],
@@ -195,6 +203,19 @@ def from_config(config: _Config, root: Path) -> Sequence["_FolderConfig"]:
195203
return [main, *extra]
196204

197205

206+
class _BumpConfig:
207+
def __init__(self, enable: bool, index: int):
208+
self.enable = enable
209+
self.index = index
210+
211+
@staticmethod
212+
def from_config(config: Union[bool, _Bump]) -> "_BumpConfig":
213+
if isinstance(config, bool):
214+
return _BumpConfig(config, -1)
215+
else:
216+
return _BumpConfig(config["enable"], config["index"])
217+
218+
198219
def _default_config() -> Mapping:
199220
return {
200221
"tool": {
@@ -369,8 +390,10 @@ def _render_jinja(version: Version, template: str, config: _Config, extra: Optio
369390
if extra is None:
370391
extra = {}
371392

372-
if config["bump"] and version.distance > 0:
373-
version = version.bump()
393+
bump_config = _BumpConfig.from_config(config["bump"])
394+
395+
if bump_config.enable and version.distance > 0:
396+
version = version.bump(index=bump_config.index)
374397
default_context = {
375398
"base": version.base,
376399
"version": version,
@@ -509,12 +532,17 @@ def _get_version(config: _Config, name: Optional[str] = None) -> Tuple[str, Vers
509532
if style is not None:
510533
check_version(serialized, style)
511534
else:
512-
serialized = version.serialize(
535+
bump_config = _BumpConfig.from_config(config["bump"])
536+
if bump_config.enable:
537+
updated = version.bump(index=bump_config.index, smart=True)
538+
else:
539+
updated = version
540+
541+
serialized = updated.serialize(
513542
metadata=config["metadata"],
514543
dirty=config["dirty"],
515544
format=config["format"],
516545
style=style,
517-
bump=config["bump"],
518546
tagged_metadata=config["tagged-metadata"],
519547
)
520548

tests/test_unit.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ def test__get_config_from_path__with_plugin_customizations():
4747
assert config["tag-dir"] == "alt/tags"
4848

4949

50+
def test__get_config__bump():
51+
config = plugin._get_config({"tool": {"poetry-dynamic-versioning": {"bump": True}}})
52+
bump = plugin._BumpConfig.from_config(config["bump"])
53+
assert bump.enable is True
54+
assert bump.index == -1
55+
assert not plugin._validate_config(config)
56+
57+
config = plugin._get_config({"tool": {"poetry-dynamic-versioning": {"bump": {"enable": True, "index": -2}}}})
58+
bump = plugin._BumpConfig.from_config(config["bump"])
59+
assert bump.enable is True
60+
assert bump.index == -2
61+
assert not plugin._validate_config(config)
62+
63+
5064
def test__get_version__defaults(config):
5165
assert plugin._get_version(config)[0] == Version.from_git().serialize()
5266

0 commit comments

Comments
 (0)