Skip to content

Commit

Permalink
#92: Add major/minor/patch format placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Nov 17, 2024
1 parent d6a0e3f commit 15b6982
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, ""),
Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion dunamai/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
),
},
{
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down

0 comments on commit 15b6982

Please sign in to comment.