diff --git a/commitizen/bump.py b/commitizen/bump.py index 7d142fd61b..6f33e33326 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -4,13 +4,18 @@ from string import Template from packaging.version import Version from typing import List, Optional, Union -from commitizen.defaults import MAJOR, MINOR, PATCH, bump_pattern, bump_map +from commitizen.defaults import ( + MAJOR, + MINOR, + PATCH, + bump_pattern, + bump_map, + bump_message, +) def find_increment( - messages: List[str], - regex: str = bump_pattern, - increments_map: dict = bump_map, + messages: List[str], regex: str = bump_pattern, increments_map: dict = bump_map ) -> Optional[str]: # Most important cases are major and minor. @@ -156,3 +161,14 @@ def create_tag(version: Union[Version, str], tag_format: Optional[str] = None): return t.safe_substitute( version=version, major=major, minor=minor, patch=patch, prerelease=prerelease ) + + +def create_commit_message( + current_version: Union[Version, str], + new_version: Union[Version, str], + message_template: str = None, +) -> str: + if message_template is None: + message_template = bump_message + t = Template(message_template) + return t.safe_substitute(current_version=current_version, new_version=new_version) diff --git a/commitizen/cli.py b/commitizen/cli.py index 3c97f6065d..684d467dfa 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -68,7 +68,14 @@ "help": ( "format used to tag the commmit and read it, " "use it in existing projects, " - "wrap around simple quotes." + "wrap around simple quotes" + ), + }, + { + "name": "--bump-message", + "help": ( + "template used to create the release commmit, " + "useful when working with CI" ), }, { diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 2bae287e2a..dea00c0077 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -19,7 +19,13 @@ def __init__(self, config: dict, arguments: dict): **config, **{ key: arguments[key] - for key in ["dry_run", "tag_format", "prerelease", "increment"] + for key in [ + "dry_run", + "tag_format", + "prerelease", + "increment", + "bump_message", + ] if arguments[key] is not None }, } @@ -35,8 +41,10 @@ def __call__(self): out.error("version = 0.4.3") raise SystemExit(NO_VERSION_SPECIFIED) + # Initialize values from sources (conf) current_version: str = self.config["version"] tag_format: str = self.parameters["tag_format"] + bump_commit_message: str = self.parameters["bump_message"] current_tag_version: str = bump.create_tag( current_version, tag_format=tag_format ) @@ -87,7 +95,9 @@ def __call__(self): current_version, increment, prerelease=prerelease ) new_tag_version = bump.create_tag(new_version, tag_format=tag_format) - message = f"bump: version {current_version} → {new_version}" + message = bump.create_commit_message( + current_version, new_version, bump_commit_message + ) # Report found information out.write(message) diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 4a96cb7f42..4c0ebfbb77 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -7,6 +7,7 @@ "version": None, "files": [], "tag_format": None, # example v$version + "bump_message": None # bumped v$current_version to $new_version } MAJOR = "MAJOR" @@ -21,3 +22,4 @@ "refactor": PATCH, "perf": PATCH, } +bump_message = "bump: version $current_version → $new_version" diff --git a/docs/bump.md b/docs/bump.md index 534b9e17be..9ae347115a 100644 --- a/docs/bump.md +++ b/docs/bump.md @@ -90,10 +90,14 @@ cz bump --tag_format="v$minor.$major.$path$prerelease" In your `pyproject.toml` -```toml -[commitizen] -tag_format = "v$minor.$major.$path$prerelease" -``` + + [tool.commitizen] + tag_format = "v$minor.$major.$path$prerelease" + +Or in your `.cz` + + [commitizen] + tag_format = v$minor.$major.$path$prerelease The variables must be preceded by a `$` sign. @@ -114,13 +118,50 @@ In your `pyproject.toml` Commitizen will update it's configuration (`pyproject.toml`, `.cz`) when bumping. -```toml -[commitizen] -files = [ - "src/__version__.py", - "setup.py" -] -``` +Some examples + +`pyproject.toml` + + [tool.commitizen] + files = [ + "src/__version__.py", + "setup.py" + ] + + +`.cz` + + [commitizen] + files = [ + "src/__version__.py", + "setup.py" + ] + +`bump_message` + +Template used to specify the commit message generated when bumping + +defaults to: `bump: version $current_version → $new_version` + + + +| Variable | Description | +| --- | ----------- | +| `$current_version` | the version existing before bumping | +| `$new_version` | version generated after bumping | + +Some examples + +`pyproject.toml` + + [tool.commitizen] + bump_message = "release $current_version → $new_version [skip-ci]" + +`.cz` + + [commitizen] + bump_message = release $current_version → $new_version [skip-ci] + ## Custom bump diff --git a/docs/config.md b/docs/config.md index 3f06841948..0f3b03513b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -1,6 +1,6 @@ # Configuration -**New!**: Support for `pyproject.toml` +**New!** Support for `pyproject.toml` Add an entry to `pyproject.toml`. @@ -29,11 +29,12 @@ information: The extra tab at the end (`]`) is required. -## settings +## Settings | Variable | Type | Default | Description | | -------- | ---- | ------- | ----------- | -| name | str | `"cz_conventional_commits"` | Name of the commiting rules to use | -| version | str | `None` | Current version. Example: "0.1.2" | -| files | list | `[ ]` | Files were the version needs to be updated | -| tag_format | str | `None` | Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [See more](/bump#configuration) | +| `name` | `str` | `"cz_conventional_commits"` | Name of the commiting rules to use | +| `version` | `str` | `None` | Current version. Example: "0.1.2" | +| `files` | `list` | `[ ]` | Files were the version needs to be updated | +| `tag_format` | `str` | `None` | Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [See more](/bump#configuration) | +| `bump_message` | `str` | `None` | Create custom commit message, useful to skip ci. [See more](/bump#configuration) | diff --git a/tests/test_bump_create_commit_message.py b/tests/test_bump_create_commit_message.py new file mode 100644 index 0000000000..7a6865770e --- /dev/null +++ b/tests/test_bump_create_commit_message.py @@ -0,0 +1,27 @@ +import pytest +from packaging.version import Version +from commitizen import bump + +conversion = [ + ( + ("1.2.3", "1.3.0", "bump: $current_version -> $new_version [skip ci]"), + "bump: 1.2.3 -> 1.3.0 [skip ci]", + ), + ( + ("1.2.3", "1.3.0", None), + "bump: version 1.2.3 → 1.3.0", + ), + ( + ("1.2.3", "1.3.0", "release $new_version"), + "release 1.3.0", + ), +] + + +@pytest.mark.parametrize("test_input,expected", conversion) +def test_create_tag(test_input, expected): + current_version, new_version, message_template = test_input + new_tag = bump.create_commit_message( + Version(current_version), Version(new_version), message_template + ) + assert new_tag == expected diff --git a/tests/test_conf.py b/tests/test_conf.py index dece21f31f..db1451639a 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -31,6 +31,7 @@ "name": "cz_jira", "version": "1.0.0", "tag_format": None, + "bump_message": None, "files": ["commitizen/__version__.py", "pyproject.toml"], } @@ -38,6 +39,7 @@ "name": "cz_jira", "version": "2.0.0", "tag_format": None, + "bump_message": None, "files": ["commitizen/__version__.py", "pyproject.toml"], }