Skip to content

Commit

Permalink
#56: Add branch/timestamp support
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Mar 9, 2022
1 parent d04ef5e commit 80496dc
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Option to bypass the version control system and set a hard-coded version
in an environment variable called `POETRY_DYNAMIC_VERSIONING_BYPASS`.
([Draft by jonringer](https://github.com/mtkennerly/poetry-dynamic-versioning/pull/69))
* `branch`, `branch_escaped`, and `timestamp` formatting variables.

## v0.13.1 (2021-08-09)

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ In your pyproject.toml file, you may configure the following options:
* `{commit}`
* `{dirty}`
* `{tagged_metadata}`
* `{branch}`
* `{branch_escaped}` which omits any non-letter/number characters
* `{timestamp}` of the current commit, which expands to YYYYmmddHHMMSS as UTC

Example: `v{base}+{distance}.{commit}`
* `format-jinja`: String. Default: unset. This defines a custom output format
Expand All @@ -127,6 +130,9 @@ In your pyproject.toml file, you may configure the following options:
* `tagged_metadata` (string or None)
* `version` (dunumai.Version)
* `env` (dictionary of environment variables)
* `branch` (string or None)
* `branch_escaped` (string or None)
* `timestamp` (string or None)

Available functions:

Expand Down
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 15 additions & 17 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import atexit
import builtins
import copy
import datetime as dt
import functools
import os
import re
Expand Down Expand Up @@ -137,23 +138,16 @@ def get_config(start: Path = None) -> Mapping:
return result


def _bump_version_per_config(version: _DUNAMAI_VERSION_ANY, bump: bool) -> _DUNAMAI_VERSION_ANY:
from dunamai import bump_version
def _escape_branch(value: Optional[str]) -> Optional[str]:
if value is None:
return None
return re.sub(r"[^a-zA-Z0-9]", "", value)

if not bump or version.distance == 0:
return version

version = copy.deepcopy(version)

if version.stage is None:
version.base = bump_version(version.base)
else:
if version.revision is None:
version.revision = 2
else:
version.revision += 1

return version
def _format_timestamp(value: Optional[dt.datetime]) -> Optional[str]:
if value is None:
return None
return value.strftime("%Y%m%d%H%M%S")


def _get_version(config: Mapping) -> str:
Expand Down Expand Up @@ -183,7 +177,8 @@ def _get_version(config: Mapping) -> str:
)

if config["format-jinja"]:
version = _bump_version_per_config(version, config["bump"])
if config["bump"] and version.distance > 0:
version = version.bump()
default_context = {
"base": version.base,
"version": version,
Expand All @@ -192,6 +187,9 @@ def _get_version(config: Mapping) -> str:
"distance": version.distance,
"commit": version.commit,
"dirty": version.dirty,
"branch": version.branch,
"branch_escaped": _escape_branch(version.branch),
"timestamp": _format_timestamp(version.timestamp),
"env": os.environ,
"bump_version": bump_version,
"tagged_metadata": version.tagged_metadata,
Expand Down Expand Up @@ -277,7 +275,7 @@ def _apply_version(version: str, config: Mapping, pyproject_path: Path) -> str:

def _revert_version() -> None:
for project, state in _state.projects.items():
if state.original_version and state.version and state.original_version != state.version[1]:
if state.original_version and state.version and state.original_version != state.version:
pyproject_path = _get_pyproject_path(state.path)
if pyproject_path is None:
return
Expand Down
2 changes: 1 addition & 1 deletion poetry_dynamic_versioning/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def main() -> None:
if pyproject_path is None:
raise RuntimeError("Unable to find pyproject.toml")

version = _get_version(config)[1]
version = _get_version(config)
print("Version: {}".format(version), file=sys.stderr)
name = _apply_version(version, config, pyproject_path)
if _state.project(name).substitutions:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include = ["zzz_poetry_dynamic_versioning.pth"]

[tool.poetry.dependencies]
python = "^3.5"
dunamai = "^1.5"
dunamai = "^1.10"
tomlkit = ">= 0.4"
jinja2 = [
{ version = "^2.11.1", python = "~3.5" },
Expand Down
30 changes: 0 additions & 30 deletions tests/test_poetry_dynamic_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,3 @@ def test__get_version__format_jinja_imports_with_module_and_item(config):
config["format-jinja"] = "{{ pow(2, 3) }}"
config["format-jinja-imports"] = [{"module": "math", "item": "pow"}]
assert plugin._get_version(config) == "8.0"


def test__bump_version_per_config__bumping_disabled():
version = Version("1.2.3")
bumped = plugin._bump_version_per_config(version, False)
assert version == bumped


def test__bump_version_per_config__distance_is_0():
version = Version("1.2.3")
bumped = plugin._bump_version_per_config(version, True)
assert version == bumped


def test__bump_version_per_config__bump_base():
version = Version("1.2.3", distance=1)
bumped = plugin._bump_version_per_config(version, True)
assert bumped == Version("1.2.4", distance=1)


def test__bump_version_per_config__bump_revision():
version = Version("1.2.3", stage=("a", 5), distance=1)
bumped = plugin._bump_version_per_config(version, True)
assert bumped == Version("1.2.3", stage=("a", 6), distance=1)


def test__bump_version_per_config__default_revision():
version = Version("1.2.3", stage=("a", None), distance=1)
bumped = plugin._bump_version_per_config(version, True)
assert bumped == Version("1.2.3", stage=("a", 2), distance=1)

0 comments on commit 80496dc

Please sign in to comment.