Skip to content

Commit 46ad4c4

Browse files
committed
feat(bump): new commit message template
closes #20
1 parent ef3ce95 commit 46ad4c4

File tree

8 files changed

+130
-24
lines changed

8 files changed

+130
-24
lines changed

commitizen/bump.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
from string import Template
55
from packaging.version import Version
66
from typing import List, Optional, Union
7-
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_pattern, bump_map
7+
from commitizen.defaults import (
8+
MAJOR,
9+
MINOR,
10+
PATCH,
11+
bump_pattern,
12+
bump_map,
13+
bump_message,
14+
)
815

916

1017
def find_increment(
11-
messages: List[str],
12-
regex: str = bump_pattern,
13-
increments_map: dict = bump_map,
18+
messages: List[str], regex: str = bump_pattern, increments_map: dict = bump_map
1419
) -> Optional[str]:
1520

1621
# Most important cases are major and minor.
@@ -156,3 +161,14 @@ def create_tag(version: Union[Version, str], tag_format: Optional[str] = None):
156161
return t.safe_substitute(
157162
version=version, major=major, minor=minor, patch=patch, prerelease=prerelease
158163
)
164+
165+
166+
def create_commit_message(
167+
current_version: Union[Version, str],
168+
new_version: Union[Version, str],
169+
message_template: str = None,
170+
) -> str:
171+
if message_template is None:
172+
message_template = bump_message
173+
t = Template(message_template)
174+
return t.safe_substitute(current_version=current_version, new_version=new_version)

commitizen/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@
6868
"help": (
6969
"format used to tag the commmit and read it, "
7070
"use it in existing projects, "
71-
"wrap around simple quotes."
71+
"wrap around simple quotes"
72+
),
73+
},
74+
{
75+
"name": "--bump-message",
76+
"help": (
77+
"template used to create the release commmit, "
78+
"useful when working with CI"
7279
),
7380
},
7481
{

commitizen/commands/bump.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ def __init__(self, config: dict, arguments: dict):
1919
**config,
2020
**{
2121
key: arguments[key]
22-
for key in ["dry_run", "tag_format", "prerelease", "increment"]
22+
for key in [
23+
"dry_run",
24+
"tag_format",
25+
"prerelease",
26+
"increment",
27+
"bump_message",
28+
]
2329
if arguments[key] is not None
2430
},
2531
}
@@ -35,8 +41,10 @@ def __call__(self):
3541
out.error("version = 0.4.3")
3642
raise SystemExit(NO_VERSION_SPECIFIED)
3743

44+
# Initialize values from sources (conf)
3845
current_version: str = self.config["version"]
3946
tag_format: str = self.parameters["tag_format"]
47+
bump_commit_message: str = self.parameters["bump_message"]
4048
current_tag_version: str = bump.create_tag(
4149
current_version, tag_format=tag_format
4250
)
@@ -87,7 +95,9 @@ def __call__(self):
8795
current_version, increment, prerelease=prerelease
8896
)
8997
new_tag_version = bump.create_tag(new_version, tag_format=tag_format)
90-
message = f"bump: version {current_version}{new_version}"
98+
message = bump.create_commit_message(
99+
current_version, new_version, bump_commit_message
100+
)
91101

92102
# Report found information
93103
out.write(message)

commitizen/defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"version": None,
88
"files": [],
99
"tag_format": None, # example v$version
10+
"bump_message": None # bumped v$current_version to $new_version
1011
}
1112

1213
MAJOR = "MAJOR"
@@ -21,3 +22,4 @@
2122
"refactor": PATCH,
2223
"perf": PATCH,
2324
}
25+
bump_message = "bump: version $current_version → $new_version"

docs/bump.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ cz bump --tag_format="v$minor.$major.$path$prerelease"
9090

9191
In your `pyproject.toml`
9292

93-
```toml
94-
[commitizen]
95-
tag_format = "v$minor.$major.$path$prerelease"
96-
```
93+
94+
[tool.commitizen]
95+
tag_format = "v$minor.$major.$path$prerelease"
96+
97+
Or in your `.cz`
98+
99+
[commitizen]
100+
tag_format = v$minor.$major.$path$prerelease
97101

98102
The variables must be preceded by a `$` sign.
99103

@@ -114,13 +118,50 @@ In your `pyproject.toml`
114118

115119
Commitizen will update it's configuration (`pyproject.toml`, `.cz`) when bumping.
116120

117-
```toml
118-
[commitizen]
119-
files = [
120-
"src/__version__.py",
121-
"setup.py"
122-
]
123-
```
121+
Some examples
122+
123+
`pyproject.toml`
124+
125+
[tool.commitizen]
126+
files = [
127+
"src/__version__.py",
128+
"setup.py"
129+
]
130+
131+
132+
`.cz`
133+
134+
[commitizen]
135+
files = [
136+
"src/__version__.py",
137+
"setup.py"
138+
]
139+
140+
`bump_message`
141+
142+
Template used to specify the commit message generated when bumping
143+
144+
defaults to: `bump: version $current_version → $new_version`
145+
146+
147+
148+
| Variable | Description |
149+
| --- | ----------- |
150+
| `$current_version` | the version existing before bumping |
151+
| `$new_version` | version generated after bumping |
152+
153+
Some examples
154+
155+
`pyproject.toml`
156+
157+
[tool.commitizen]
158+
bump_message = "release $current_version → $new_version [skip-ci]"
159+
160+
`.cz`
161+
162+
[commitizen]
163+
bump_message = release $current_version → $new_version [skip-ci]
164+
124165

125166
## Custom bump
126167

docs/config.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration
22

3-
**New!**: Support for `pyproject.toml`
3+
**New!** Support for `pyproject.toml`
44

55
Add an entry to `pyproject.toml`.
66

@@ -29,11 +29,12 @@ information:
2929

3030
The extra tab at the end (`]`) is required.
3131

32-
## settings
32+
## Settings
3333

3434
| Variable | Type | Default | Description |
3535
| -------- | ---- | ------- | ----------- |
36-
| name | str | `"cz_conventional_commits"` | Name of the commiting rules to use |
37-
| version | str | `None` | Current version. Example: "0.1.2" |
38-
| files | list | `[ ]` | Files were the version needs to be updated |
39-
| 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) |
36+
| `name` | `str` | `"cz_conventional_commits"` | Name of the commiting rules to use |
37+
| `version` | `str` | `None` | Current version. Example: "0.1.2" |
38+
| `files` | `list` | `[ ]` | Files were the version needs to be updated |
39+
| `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) |
40+
| `bump_message` | `str` | `None` | Create custom commit message, useful to skip ci. [See more](/bump#configuration) |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from packaging.version import Version
3+
from commitizen import bump
4+
5+
conversion = [
6+
(
7+
("1.2.3", "1.3.0", "bump: $current_version -> $new_version [skip ci]"),
8+
"bump: 1.2.3 -> 1.3.0 [skip ci]",
9+
),
10+
(
11+
("1.2.3", "1.3.0", None),
12+
"bump: version 1.2.3 → 1.3.0",
13+
),
14+
(
15+
("1.2.3", "1.3.0", "release $new_version"),
16+
"release 1.3.0",
17+
),
18+
]
19+
20+
21+
@pytest.mark.parametrize("test_input,expected", conversion)
22+
def test_create_tag(test_input, expected):
23+
current_version, new_version, message_template = test_input
24+
new_tag = bump.create_commit_message(
25+
Version(current_version), Version(new_version), message_template
26+
)
27+
assert new_tag == expected

tests/test_conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
"name": "cz_jira",
3232
"version": "1.0.0",
3333
"tag_format": None,
34+
"bump_message": None,
3435
"files": ["commitizen/__version__.py", "pyproject.toml"],
3536
}
3637

3738
_new_config = {
3839
"name": "cz_jira",
3940
"version": "2.0.0",
4041
"tag_format": None,
42+
"bump_message": None,
4143
"files": ["commitizen/__version__.py", "pyproject.toml"],
4244
}
4345

0 commit comments

Comments
 (0)