Skip to content

Commit 477f980

Browse files
authored
Handle patch release versions (#2)
## Description - Updated the version regex pattern in `StaflVersion` class to support optional prerelease metadata in the format: `major.minor.patch+build-metadata` - Added `.DS_Store` to `.gitignore` to exclude macOS system files from version control - Fix lint errors ## Motivation and Context The current version regex only supports the strict format of `major.minor.patch+build` without any additional metadata. This change allows for prerelease tags and other metadata to be included after the build number with a hyphen separator, making the versioning system more flexible. ## How Has This Been Tested? Added a new test case `test_version_patch_release` that verifies the versioner can correctly parse and handle version strings with prerelease metadata (e.g., `1.2.3+4-dev/patch`). ## Affected components This change is self-contained and only affects the version parsing logic. Existing implementations that use the standard version format will continue to work as before.
1 parent 3dfeb25 commit 477f980

7 files changed

Lines changed: 22 additions & 12 deletions

File tree

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exclude=
55
env,
66
build,
77
dist
8-
max-line-length = 88
8+
max-line-length = 120
99
# E203: whitespace before ":". Sometimes violated by black.
1010
# E402: Module level import not at top of file. Violated by lazy imports.
1111
# F401: Module imported but unused.

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,7 @@ dmypy.json
130130

131131
# example output
132132
example/*.csv
133-
settings.json
133+
settings.json
134+
135+
# macOS files
136+
.DS_Store

staflversion/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""CLI interface for staflversion project.
2-
"""
1+
"""CLI interface for staflversion project."""
32

43
import argparse
54

staflversion/git.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
here you put your main classes and objects.
66
"""
77

8-
98
import subprocess
109

1110
from typing import List, Optional

staflversion/version.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@total_ordering
1010
class StaflVersion:
1111
VERSION_REGEX = re.compile(
12-
r"^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\+(?P<build>\d+)$"
12+
r"^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\+(?P<build>\d+)(-(?P<prerelease_metadata>.+))?$"
1313
)
1414

1515
def __init__(self, major: int, minor: int, patch: int, build: int):
@@ -85,14 +85,16 @@ class StaflVersioner:
8585
_INCREMENT_MAJOR = "+semver:major"
8686
_INCREMENT_MINOR = "+semver:minor"
8787
_INCREMENT_PATCH = "+semver:patch"
88-
_SET_VERSION = re.compile(r"\+semver-set:" + StaflVersion.VERSION_REGEX.pattern[1:-1])
88+
_SET_VERSION = re.compile(
89+
r"\+semver-set:" + StaflVersion.VERSION_REGEX.pattern[1:-1]
90+
)
8991

9092
def __init__(self, git: GitWrapper):
9193
self._git = git
9294

9395
def determine_version(self) -> StaflVersion:
9496
tags = self._git.get_tags()
95-
97+
9698
if tags:
9799
new_commit_messages = self._git.get_commit_messages_since_tag()
98100
else:
@@ -109,9 +111,7 @@ def determine_version(self) -> StaflVersion:
109111
increment_patch = any([self._INCREMENT_PATCH in m for m in new_commit_messages])
110112

111113
versions = [
112-
StaflVersion.parse(t)
113-
for t in tags
114-
if StaflVersion.try_parse(t) is not None
114+
StaflVersion.parse(t) for t in tags if StaflVersion.try_parse(t) is not None
115115
]
116116
versions.sort(reverse=True)
117117
last_version = versions[0] if versions else StaflVersion(0, 0, 0, 0)

tests/test_git.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def tempdir():
1515

1616
@pytest.fixture
1717
def sample_repo(tempdir: str, monkeypatch: MonkeyPatch):
18-
# tempdir above must come before monkeypatch so that the directory is changed back before the directory is cleanup up
18+
# tempdir above must come before monkeypatch so that the directory is
19+
# changed back before the directory is cleaned up
1920
monkeypatch.chdir(tempdir)
2021
system(
2122
"git init &&\

tests/test_version.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,11 @@ def test_version_determine_set_version():
132132
)
133133
versioner = StaflVersioner(git)
134134
assert versioner.determine_version() == StaflVersion(1, 2, 3, 4)
135+
136+
137+
def test_version_patch_release():
138+
git = MockGitWrapper(
139+
["Fix critical bug +semver:patch"], ["1.2.3+4-dev/patch", "1.2.2+0"]
140+
)
141+
versioner = StaflVersioner(git)
142+
assert versioner.determine_version() == StaflVersion(1, 2, 4, 0)

0 commit comments

Comments
 (0)