Skip to content

Commit 38daf5e

Browse files
refactor: Make tag_format properly default to $version
We've been using this default already in `normalize_tag`, but setting this value in the settings dict is cleaner.
1 parent 378a428 commit 38daf5e

File tree

7 files changed

+14
-17
lines changed

7 files changed

+14
-17
lines changed

commitizen/bump.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ def _version_to_regex(version: str) -> str:
207207
return version.replace(".", r"\.").replace("+", r"\+")
208208

209209

210-
def normalize_tag(
211-
version: Union[Version, str], tag_format: Optional[str] = None
212-
) -> str:
210+
def normalize_tag(version: Union[Version, str], tag_format: str) -> str:
213211
"""The tag and the software version might be different.
214212
215213
That's why this function exists.
@@ -224,9 +222,6 @@ def normalize_tag(
224222
if isinstance(version, str):
225223
version = Version(version)
226224

227-
if not tag_format:
228-
return str(version)
229-
230225
major, minor, patch = version.release
231226
prerelease = ""
232227
# version.pre is needed for mypy check

commitizen/commands/changelog.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from commitizen import bump, changelog, defaults, factory, git, out
77
from commitizen.config import BaseConfig
8+
from commitizen.defaults import DEFAULT_SETTINGS
89
from commitizen.exceptions import (
910
DryRunExit,
1011
NoCommitsFoundError,
@@ -46,8 +47,8 @@ def __init__(self, config: BaseConfig, args):
4647
or defaults.change_type_order
4748
)
4849
self.rev_range = args.get("rev_range")
49-
self.tag_format = args.get("tag_format") or self.config.settings.get(
50-
"tag_format"
50+
self.tag_format: str = args.get("tag_format") or self.config.settings.get(
51+
"tag_format", DEFAULT_SETTINGS["tag_format"]
5152
)
5253

5354
def _find_incremental_rev(self, latest_version: str, tags: List[GitTag]) -> str:

commitizen/commands/init.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from commitizen.__version__ import __version__
1111
from commitizen.config import BaseConfig, JsonConfig, TomlConfig, YAMLConfig
1212
from commitizen.cz import registry
13-
from commitizen.defaults import config_files
13+
from commitizen.defaults import DEFAULT_SETTINGS, config_files
1414
from commitizen.exceptions import InitFailedError, NoAnswersError
1515
from commitizen.git import get_latest_tag_name, get_tag_names, smart_open
1616

@@ -110,14 +110,15 @@ def _ask_tag_format(self, latest_tag) -> str:
110110
f'Is "{tag_format}" the correct tag format?', style=self.cz.style
111111
).ask()
112112

113+
default_format = DEFAULT_SETTINGS["tag_format"]
113114
if not is_correct_format:
114115
tag_format = questionary.text(
115-
'Please enter the correct version format: (default: "$version")',
116+
f'Please enter the correct version format: (default: "{default_format}")',
116117
style=self.cz.style,
117118
).ask()
118119

119120
if not tag_format:
120-
tag_format = "$version"
121+
tag_format = default_format
121122
return tag_format
122123

123124
def _search_pre_commit(self) -> bool:

commitizen/defaults.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Settings(TypedDict, total=False):
3333
name: str
3434
version: Optional[str]
3535
version_files: List[str]
36-
tag_format: Optional[str]
36+
tag_format: str
3737
bump_message: Optional[str]
3838
allow_abort: bool
3939
changelog_file: str
@@ -63,7 +63,7 @@ class Settings(TypedDict, total=False):
6363
"name": "cz_conventional_commits",
6464
"version": None,
6565
"version_files": [],
66-
"tag_format": None, # example v$version
66+
"tag_format": "$version", # example v$version
6767
"bump_message": None, # bumped v$current_version to $new_version
6868
"allow_abort": False,
6969
"changelog_file": "CHANGELOG.md",

docs/bump.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ In your `pyproject.toml` or `.cz.toml`
300300
tag_format = "v$major.$minor.$patch$prerelease"
301301
```
302302
303-
The variables must be preceded by a `$` sign.
303+
The variables must be preceded by a `$` sign. Default is `$version`.
304304
305305
Supported variables:
306306

docs/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| `name` | `str` | `"cz_conventional_commits"` | Name of the committing rules to use |
88
| `version` | `str` | `None` | Current version. Example: "0.1.2" |
99
| `version_files` | `list` | `[ ]` | Files were the version will be updated. A pattern to match a line, can also be specified, separated by `:` [See more][version_files] |
10-
| `tag_format` | `str` | `None` | Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [See more][tag_format] |
10+
| `tag_format` | `str` | `$version` | Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [See more][tag_format] |
1111
| `update_changelog_on_bump` | `bool` | `false` | Create changelog when running `cz bump` |
1212
| `gpg_sign` | `bool` | `false` | Use gpg signed tags instead of lightweight tags. |
1313
| `annotated_tag` | `bool` | `false` | Use annotated tags instead of lightweight tags. [See difference][annotated-tags-vs-lightweight] |

tests/test_conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
_settings = {
4545
"name": "cz_jira",
4646
"version": "1.0.0",
47-
"tag_format": None,
47+
"tag_format": "$version",
4848
"bump_message": None,
4949
"allow_abort": False,
5050
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
@@ -63,7 +63,7 @@
6363
_new_settings = {
6464
"name": "cz_jira",
6565
"version": "2.0.0",
66-
"tag_format": None,
66+
"tag_format": "$version",
6767
"bump_message": None,
6868
"allow_abort": False,
6969
"version_files": ["commitizen/__version__.py", "pyproject.toml"],

0 commit comments

Comments
 (0)