Skip to content

Commit

Permalink
refactor: Use format strings
Browse files Browse the repository at this point in the history
Co-authored-by: Wei Lee <weilee.rx@gmail.com>
  • Loading branch information
grahamhar and Lee-W committed May 29, 2024
1 parent 9bdeba2 commit 7400023
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 39 deletions.
4 changes: 2 additions & 2 deletions commitizen/changelog_formats/asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion commitizen/changelog_formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions commitizen/changelog_formats/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions commitizen/changelog_formats/restructuredtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions commitizen/changelog_formats/textile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<major>\d+)",
Expand Down
22 changes: 8 additions & 14 deletions docs/commands/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,20 +385,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 |

---

Expand Down
21 changes: 11 additions & 10 deletions docs/tutorials/monorepo_guidance.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7400023

Please sign in to comment.