From 15b698264707d27d8c69805b7fc96bba336f71ae Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Sun, 17 Nov 2024 13:50:05 -0500 Subject: [PATCH] #92: Add major/minor/patch format placeholders --- CHANGELOG.md | 4 ++++ README.md | 3 +++ dunamai/__init__.py | 8 ++++++++ dunamai/__main__.py | 3 ++- pyproject.toml | 1 + tests/unit/test_dunamai.py | 6 +++--- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88fd341..cc2b113 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased + +* Added: `{major}`, `{minor}`, and `{patch}` format placeholders. + ## v1.22.0 (2024-08-07) * Fixed: The `--ignore-untracked` CLI flag was ignored. diff --git a/README.md b/README.md index fb3bc65..f2d26f3 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,9 @@ If you have a tag like `v9!0.1.2-beta.3+other`, then: * `{branch}` = `feature/foo` * `{branch_escaped}` = `featurefoo` * `{timestamp}` is in the format `YYYYmmddHHMMSS` as UTC +* `{major}` = `0` +* `{minor}` = `1` +* `{patch}` = `2` If you specify a substitution, its value will always be included in the output. For conditional formatting, you can do something like this (Bash): diff --git a/dunamai/__init__.py b/dunamai/__init__.py index 804f7ef..51fcd4e 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -703,6 +703,9 @@ def serialize( * {branch} * {branch_escaped} which omits any non-letter/number characters * {timestamp} which expands to YYYYmmddHHMMSS as UTC + * {major} (first part of `base` split on `.`, or 0) + * {minor} (second part of `base` split on `.`, or 0) + * {patch} (third part of `base` split on `.`, or 0) :param style: Built-in output formats. Will default to PEP 440 if not set and no custom format given. If you specify both a style and a custom format, then the format will be validated against the @@ -731,6 +734,8 @@ def serialize( out = format(new_version) else: try: + base_parts = base.split(".") + out = format.format( base=base, stage=_blank(self.stage, ""), @@ -743,6 +748,9 @@ def serialize( branch=_blank(self.branch, ""), branch_escaped=_escape_branch(_blank(self.branch, "")), timestamp=self.timestamp.strftime("%Y%m%d%H%M%S") if self.timestamp else "", + major=base_parts[0] if len(base_parts) > 0 else "0", + minor=base_parts[1] if len(base_parts) > 1 else "0", + patch=base_parts[2] if len(base_parts) > 2 else "0", ) except KeyError as e: raise KeyError("Format contains invalid placeholder: {}".format(e)) diff --git a/dunamai/__main__.py b/dunamai/__main__.py index 0efbb93..a15ee00 100644 --- a/dunamai/__main__.py +++ b/dunamai/__main__.py @@ -66,7 +66,8 @@ "help": ( "Custom output format. Available substitutions:" " {base}, {stage}, {revision}, {distance}, {commit}, {dirty}," - " {tagged_metadata}, {epoch}, {branch}, {branch_escaped}, {timestamp}" + " {tagged_metadata}, {epoch}, {branch}, {branch_escaped}, {timestamp}," + " {major}, {minor}, {patch}" ), }, { diff --git a/pyproject.toml b/pyproject.toml index dbd8b5e..7ae874e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,6 +49,7 @@ line-length = 120 [tool.ruff] line-length = 120 extend-select = ["W605", "N"] +ignore = ["E501"] [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/tests/unit/test_dunamai.py b/tests/unit/test_dunamai.py index bb0d527..99be489 100644 --- a/tests/unit/test_dunamai.py +++ b/tests/unit/test_dunamai.py @@ -379,8 +379,8 @@ def test__version__serialize__pvp_with_dirty() -> None: def test__version__serialize__format_as_str() -> None: - format = "{base},{stage},{revision},{distance},{commit},{dirty}" ",{branch},{branch_escaped},{timestamp}" - assert Version("0.1.0").serialize(format=format) == "0.1.0,,,0,,clean,,," + format = "{base},{stage},{revision},{distance},{commit},{dirty},{branch},{branch_escaped},{timestamp},{major},{minor},{patch}" + assert Version("0.1.0").serialize(format=format) == "0.1.0,,,0,,clean,,,,0,1,0" assert ( Version( "1", @@ -391,7 +391,7 @@ def test__version__serialize__format_as_str() -> None: branch="a/b", timestamp=dt.datetime(2001, 2, 3, 4, 5, 6, tzinfo=dt.timezone.utc), ).serialize(format=format) - == "1,a,2,3,abc,dirty,a/b,ab,20010203040506" + == "1,a,2,3,abc,dirty,a/b,ab,20010203040506,1,0,0" ) with pytest.raises(ValueError): Version("0.1.0").serialize(format="v{base}", style=Style.Pep440)