From 3d658617fb27cbfbf383e03739dba75cd54c56da Mon Sep 17 00:00:00 2001 From: Graham Hargreaves Date: Sat, 27 Apr 2024 12:45:29 +0100 Subject: [PATCH] refactor: Use format strings Co-authored-by: Wei Lee --- commitizen/changelog_formats/asciidoc.py | 4 ++-- commitizen/changelog_formats/base.py | 2 +- commitizen/changelog_formats/markdown.py | 4 ++-- .../changelog_formats/restructuredtext.py | 8 +++++-- commitizen/changelog_formats/textile.py | 17 ++++++++------ commitizen/defaults.py | 4 +++- docs/commands/bump.md | 22 +++++++------------ docs/tutorials/monorepo_guidance.md | 21 +++++++++--------- tests/commands/test_changelog_command.py | 2 ++ 9 files changed, 45 insertions(+), 39 deletions(-) diff --git a/commitizen/changelog_formats/asciidoc.py b/commitizen/changelog_formats/asciidoc.py index bca7464b06..6007a56d16 100644 --- a/commitizen/changelog_formats/asciidoc.py +++ b/commitizen/changelog_formats/asciidoc.py @@ -27,9 +27,9 @@ def parse_version_from_title(self, line: str) -> str | None: return None if partial_matches.get("prerelease"): - partial_version += f"-{partial_matches['prerelease']}" + partial_version = f"{partial_version}-{partial_matches['prerelease']}" if partial_matches.get("devrelease"): - partial_version += f"{partial_matches['devrelease']}" + partial_version = f"{partial_version}{partial_matches['devrelease']}" return partial_version def parse_title_level(self, line: str) -> int | None: diff --git a/commitizen/changelog_formats/base.py b/commitizen/changelog_formats/base.py index 415c404c91..53527a060c 100644 --- a/commitizen/changelog_formats/base.py +++ b/commitizen/changelog_formats/base.py @@ -8,8 +8,8 @@ from commitizen.changelog import Metadata from commitizen.config.base_config import BaseConfig -from commitizen.version_schemes import get_version_scheme from commitizen.defaults import get_tag_regexes +from commitizen.version_schemes import get_version_scheme from . import ChangelogFormat diff --git a/commitizen/changelog_formats/markdown.py b/commitizen/changelog_formats/markdown.py index 2e9aa23663..29c1cce54a 100644 --- a/commitizen/changelog_formats/markdown.py +++ b/commitizen/changelog_formats/markdown.py @@ -30,9 +30,9 @@ def parse_version_from_title(self, line: str) -> str | None: return None if matches.get("prerelease"): - partial_version += f"-{matches['prerelease']}" + partial_version = f"{partial_version}-{matches['prerelease']}" if matches.get("devrelease"): - partial_version += f"{matches['devrelease']}" + partial_version = f"{partial_version}{matches['devrelease']}" return partial_version def parse_title_level(self, line: str) -> int | None: diff --git a/commitizen/changelog_formats/restructuredtext.py b/commitizen/changelog_formats/restructuredtext.py index be07322c9b..09d032400c 100644 --- a/commitizen/changelog_formats/restructuredtext.py +++ b/commitizen/changelog_formats/restructuredtext.py @@ -77,9 +77,13 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata: f"{matches['major']}.{matches['minor']}.{matches['patch']}" ) if matches.get("prerelease"): - partial_version += f"-{matches['prerelease']}" + partial_version = ( + f"{partial_version}-{matches['prerelease']}" + ) if matches.get("devrelease"): - partial_version += f"{matches['devrelease']}" + partial_version = ( + f"{partial_version}{matches['devrelease']}" + ) meta.latest_version = partial_version meta.latest_version_position = index break diff --git a/commitizen/changelog_formats/textile.py b/commitizen/changelog_formats/textile.py index 4f34b522fa..8750f0056c 100644 --- a/commitizen/changelog_formats/textile.py +++ b/commitizen/changelog_formats/textile.py @@ -19,17 +19,20 @@ def parse_version_from_title(self, line: str) -> str | None: if "version" in m.groupdict(): return m.group("version") matches = m.groupdict() - try: - partial_version = ( - f"{matches['major']}.{matches['minor']}.{matches['patch']}" - ) - except KeyError: + if not all( + [ + version_segment in matches + for version_segment in ("major", "minor", "patch") + ] + ): return None + partial_version = f"{matches['major']}.{matches['minor']}.{matches['patch']}" + if matches.get("prerelease"): - partial_version += f"-{matches['prerelease']}" + partial_version = f"{partial_version}-{matches['prerelease']}" if matches.get("devrelease"): - partial_version += f"{matches['devrelease']}" + partial_version = f"{partial_version}{matches['devrelease']}" return partial_version def parse_title_level(self, line: str) -> int | None: diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 0f857f264a..2d092d5004 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -134,7 +134,9 @@ class Settings(TypedDict, total=False): bump_message = "bump: version $current_version → $new_version" -def get_tag_regexes(version_regex: str) -> dict[str | Any, str | Any]: +def get_tag_regexes( + version_regex: str, +) -> dict[str, str]: return { "$version": version_regex, "$major": r"(?P\d+)", diff --git a/docs/commands/bump.md b/docs/commands/bump.md index ed00193241..afb43230e4 100644 --- a/docs/commands/bump.md +++ b/docs/commands/bump.md @@ -418,20 +418,14 @@ The variables must be preceded by a `$` sign and optionally can be wrapped in `{ Supported variables: -| Variable | Description | -|-----------------|---------------------------------------------| -| `$version` | full generated version | -| `$major` | MAJOR increment | -| `$minor` | MINOR increment | -| `$patch` | PATCH increment | -| `$prerelease` | Prerelease (alpha, beta, release candidate) | -| `$devrelease` | Development release | -| `${version}` | full generated version | -| `${major}` | MAJOR increment | -| `${minor}` | MINOR increment | -| `${patch}` | PATCH increment | -| `${prerelease}` | Prerelease (alpha, beta, release candidate) | -| `${devrelease}` | Development release | +| Variable | Description | +|--------------------------------|---------------------------------------------| +| `$version`, `${version}` | full generated version | +| `$major`, `${major}` | MAJOR increment | +| `$minor`, `${minor}` | MINOR increment | +| `$patch`, `${patch}` | PATCH increment | +| `$prerelease`, `${prerelease}` | Prerelease (alpha, beta, release candidate) | +| `$devrelease`, ${devrelease}` | Development release | --- diff --git a/docs/tutorials/monorepo_guidance.md b/docs/tutorials/monorepo_guidance.md index 5bb334d725..c4345d6bc2 100644 --- a/docs/tutorials/monorepo_guidance.md +++ b/docs/tutorials/monorepo_guidance.md @@ -1,21 +1,22 @@ # Configuring commitizen in a monorepo -This tutorial assumes the monorepo layout is designed with multiple components that can be released independently of each other, -some suggested layouts: +This tutorial assumes the monorepo layout is designed with multiple components that can be released independently of each +other, it also assumes that conventional commits with scopes are in use. Some suggested layouts: ``` -library-a - .cz.toml -library-b - .cz.toml +. +├── library-b +│   └── .cz.toml +└── library-z + └── .cz.toml ``` ``` src - library-b - .cz.toml - library-z - .cz.toml +├── library-b +│   └── .cz.toml +└── library-z + └── .cz.toml ``` Each component will have its own changelog, commits will need to use scopes so only relevant commits are included in the diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 5c7a8a9faa..bc0d6c6a28 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1597,9 +1597,11 @@ def test_changelog_only_tag_matching_tag_format_included_suffix( git.tag("random0.2.0") testargs = ["cz", "bump", "--changelog", "--yes"] mocker.patch.object(sys, "argv", testargs) + # bump to 0.2.0custom cli.main() wait_for_tag() create_file_and_commit("feat: another new file") + # bump to 0.3.0custom cli.main() wait_for_tag() with open(changelog_path) as f: