8000 feat: add build metadata · miguelvps/commitizen@804d20d · GitHub
[go: up one dir, main page]

Skip to content

Commit 804d20d

Browse files
committed
feat: add build metadata
1 parent e9647c7 commit 804d20d

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

commitizen/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ def __call__(
182182
"action": "store_true",
183183
"help": "bump only the local version portion",
184184
},
185+
{
186+
"name": "--build-metadata",
187+
"help": "set the build metadata portion of the version",
188+
},
185189
{
186190
"name": ["--changelog", "-ch"],
187191
"action": "store_true",

commitizen/commands/bump.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __call__(self): # noqa: C901
150150
devrelease: int | None = self.arguments["devrelease"]
151151
is_files_only: bool | None = self.arguments["files_only"]
152152
is_local_version: bool | None = self.arguments["local_version"]
153+
build_metadata: str | None = self.arguments["build_metadata"]
153154
manual_version = self.arguments["manual_version"]
154155

155156
if manual_version:
@@ -167,6 +168,11 @@ def __call__(self): # noqa: C901
167168
"--local-version cannot be combined with MANUAL_VERSION"
168169
)
169170

171+
if build_metadata:
172+
raise NotAllowed(
173+
"--build_metadata cannot be combined with MANUAL_VERSION"
174+
)
175+
170176
if major_version_zero:
171177
raise NotAllowed(
172178
"--major-version-zero cannot be combined with MANUAL_VERSION"
@@ -237,6 +243,7 @@ def __call__(self): # noqa: C901
237243
prerelease_offset=prerelease_offset,
238244
devrelease=devrelease,
239245
is_local_version=is_local_version,
246+
build_metadata=build_metadata,
240247
)
241248

242249
new_tag_version = bump.normalize_tag(

commitizen/version_schemes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def bump(
100100
prerelease_offset: int = 0,
101101
devrelease: int | None = None,
102102
is_local_version: bool = False,
103+
build_metadata: str | None = None,
103104
) -> Self:
104105
"""
105106
Based on the given increment, generate the next bumped version according to the version scheme
@@ -166,6 +167,17 @@ def generate_devrelease(self, devrelease: int | None) -> str:
166167

167168
return f"dev{devrelease}"
168169

170+
def generate_build_metadata(self, build_metadata: str | None) -> str:
171+
"""Generate build metadata
172+
173+
The build metadata should be passed directly and is not
174+
inferred based on previous versions.
175+
"""
176+
if build_metadata is None:
177+
return ""
178+
179+
return f"+{build_metadata}"
180+
169181
def increment_base(self, increment: str | None = None) -> str:
170182
prev_release = list(self.release)
171183
increments = [MAJOR, MINOR, PATCH]
@@ -195,6 +207,7 @@ def bump(
195207
prerelease_offset: int = 0,
196208
devrelease: int | None = None,
197209
is_local_version: bool = False,
210+
build_metadata: str | None = None,
198211
) -> Self:
199212
"""Based on the given increment a proper semver will be generated.
200213
@@ -215,8 +228,9 @@ def bump(
215228
base = self.increment_base(increment)
216229
dev_version = self.generate_devrelease(devrelease)
217230
pre_version = self.generate_prerelease(prerelease, offset=prerelease_offset)
231+
build_metadata = self.generate_build_metadata(build_metadata)
218232
# TODO: post version
219-
return self.scheme(f"{base}{pre_version}{dev_version}") # type: ignore
233+
return self.scheme(f"{base}{pre_version}{dev_version}{build_metadata}") # type: ignore
220234

221235

222236
class Pep440(BaseVersion):

docs/bump.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ Some examples of pep440:
5252

5353
```bash
5454
$ cz bump --help
55-
usage: cz bump [-h] [--dry-run] [--files-only] [--local-version] [--changelog]
56-
[--no-verify] [--yes] [--tag-format TAG_FORMAT]
55+
usage: cz bump [-h] [--dry-run] [--files-only] [--local-version] [--build-metadata BUILD_METADATA]
56+
[--changelog] [--no-verify] [--yes] [--tag-format TAG_FORMAT]
5757
[--bump-message BUMP_MESSAGE] [--prerelease {alpha,beta,rc}]
5858
[--devrelease DEVRELEASE] [--increment {MAJOR,MINOR,PATCH}]
5959
[--check-consistency] [--annotated-tag] [--gpg-sign]

tests/commands/test_bump_command.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,28 @@ def test_bump_local_version(mocker: MockFixture, tmp_commitizen_project):
439439
assert "4.5.1+0.2.0" in f.read()
440440

441441

442+
def test_bump_build_metadata_version(mocker: MockFixture, tmp_commitizen_project):
443+
tmp_version_file = tmp_commitizen_project.join("__version__.py")
444+
tmp_version_file.write("4.5.1")
445+
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
446+
tmp_version_file_string = str(tmp_version_file).replace("\\", "/")
447+
tmp_commitizen_cfg_file.write(
448+
f"[tool.commitizen]\n"
449+
'version="4.5.1"\n'
450+
f'version_files = ["{tmp_version_file_string}"]'
451+
)
452+
453+
create_file_and_commit("feat: new user interface")
454+
testargs = ["cz", "bump", "--yes", "--build-metadata", "metadata"]
455+
mocker.patch.object(sys, "argv", testargs)
456+
cli.main()
457+
tag_exists = git.tag_exist("4.6.0+metadata")
458+
assert tag_exists is True
459+
460+
with open(tmp_version_file, encoding="utf-8") as f:
461+
assert "4.6.0+metadata" in f.read()
462+
463+
442464
@pytest.mark.usefixtures("tmp_commitizen_project")
443465
def test_bump_dry_run(mocker: MockFixture, capsys):
444466
create_file_and_commit("feat: new file")

0 commit comments

Comments
 (0)
0