Skip to content

feat(bump): new commit message template #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
9 changes: 8 additions & 1 deletion commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
),
},
{
Expand Down
14 changes: 12 additions & 2 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand All @@ -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
)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,3 +22,4 @@
"refactor": PATCH,
"perf": PATCH,
}
bump_message = "bump: version $current_version → $new_version"
63 changes: 52 additions & 11 deletions docs/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down
13 changes: 7 additions & 6 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configuration

**New!**: Support for `pyproject.toml`
**New!** Support for `pyproject.toml`

Add an entry to `pyproject.toml`.

Expand Down Expand Up @@ -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) |
27 changes: 27 additions & 0 deletions tests/test_bump_create_commit_message.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
"name": "cz_jira",
"version": "1.0.0",
"tag_format": None,
"bump_message": None,
"files": ["commitizen/__version__.py", "pyproject.toml"],
}

_new_config = {
"name": "cz_jira",
"version": "2.0.0",
"tag_format": None,
"bump_message": None,
"files": ["commitizen/__version__.py", "pyproject.toml"],
}

Expand Down