Skip to content

Commit 2618a07

Browse files
author
Amit Levy
committed
feat(ConventionalCommitsCz): allow to override defaults from config
Before fixing tests and adding extra ones, I would like to received partial feedback about my changes to see if this aligns with the desired code changes by maintainers. As addressed on commitizen-tools#535, when using customize commitizen, if we want to customize a small attribute, we need to redefine all commitizens options to make our custom class work. For example: ```suggestion diff --git a/cz.yaml b/cz.yaml index 2e847b0..302e961 100644 --- a/cz.yaml +++ b/cz.yaml @@ -1,17 +1,18 @@ commitizen: annotated_tag: true bump_message: 'bump: $current_version -> $new_version [skip ci]' - bump_pattern: '^(fix|feat|docs|style|refactor|test|build|ci)' - bump_map: - fix: PATCH - feat: PATCH - docs: PATCH - style: PATCH - refactor: PATCH - test: PATCH - build: PATCH - ci: PATCH - - name: cz_conventional_commits + name: cz_customize update_changelog_on_bump: true version: 0.11.0 + + customize: + bump_pattern: '^(fix|feat|docs|style|refactor|test|build|ci)' + bump_map: + fix: PATCH + feat: PATCH + docs: PATCH + style: PATCH + refactor: PATCH + test: PATCH + build: PATCH + ci: PATCH ``` making the following change on a repo would cause an unexpected behavior: ```python + bash -c cz commit Traceback (most recent call last): File "/home/amit/.local/bin/cz", line 8, in <module> sys.exit(main()) File "/home/amit/.local/lib/python3.10/site-packages/commitizen/cli.py", line 382, in main args.func(conf, vars(args))() File "/home/amit/.local/lib/python3.10/site-packages/commitizen/commands/commit.py", line 74, in __call__ m = self.prompt_commit_questions() File "/home/amit/.local/lib/python3.10/site-packages/commitizen/commands/commit.py", line 49, in prompt_commit_questions for question in filter(lambda q: q["type"] == "list", questions): File "/home/amit/.local/lib/python3.10/site-packages/commitizen/commands/commit.py", line 49, in <lambda> for question in filter(lambda q: q["type"] == "list", questions): KeyError: 'type' ``` From my best understanding, this error happens because I didn't defined question section in config, though I'm ok with using ConventionalCommitsCz default ones. This commit extends ConventionalCommitsCz to read from config and fallbacks to defaults if some are not provided. By adding this change, potentially customize commitizen can be deprecated. Closes commitizen-tools#535.
1 parent 2ff9f15 commit 2618a07

File tree

4 files changed

+49
-70
lines changed

4 files changed

+49
-70
lines changed

commitizen/cz/base.py

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
11
from abc import ABCMeta, abstractmethod
2-
from typing import Callable, Dict, List, Optional, Tuple
2+
from typing import Callable, Dict, List, Optional
33

44
from prompt_toolkit.styles import Style, merge_styles
55

6-
from commitizen import git
6+
from commitizen import defaults, git
77
from commitizen.config.base_config import BaseConfig
88
from commitizen.defaults import Questions
99

1010

1111
class BaseCommitizen(metaclass=ABCMeta):
12-
bump_pattern: Optional[str] = None
13-
bump_map: Optional[Dict[str, str]] = None
14-
default_style_config: List[Tuple[str, str]] = [
15-
("qmark", "fg:#ff9d00 bold"),
16-
("question", "bold"),
17-
("answer", "fg:#ff9d00 bold"),
18-
("pointer", "fg:#ff9d00 bold"),
19-
("highlighted", "fg:#ff9d00 bold"),
20-
("selected", "fg:#cc5454"),
21-
("separator", "fg:#cc5454"),
22-
("instruction", ""),
23-
("text", ""),
24-
("disabled", "fg:#858585 italic"),
25-
]
26-
27-
# The whole subject will be parsed as message by default
28-
# This allows supporting changelog for any rule system.
29-
# It can be modified per rule
30-
commit_parser: Optional[str] = r"(?P<message>.*)"
31-
changelog_pattern: Optional[str] = r".*"
32-
change_type_map: Optional[Dict[str, str]] = None
33-
change_type_order: Optional[List[str]] = None
34-
3512
# Executed per message parsed by the commitizen
3613
changelog_message_builder_hook: Optional[
3714
Callable[[Dict, git.GitCommit], Dict]
@@ -42,8 +19,30 @@ class BaseCommitizen(metaclass=ABCMeta):
4219

4320
def __init__(self, config: BaseConfig):
4421
self.config = config
45-
if not self.config.settings.get("style"):
46-
self.config.settings.update({"style": BaseCommitizen.default_style_config})
22+
self.default_style_config: Optional[Dict[str, str]] = self.config.settings.get(
23+
"default_style_config", defaults.default_style_config
24+
)
25+
self.bump_pattern: Optional[str] = self.config.settings.get(
26+
"bump_pattern", defaults.bump_pattern
27+
)
28+
self.bump_map: Optional[Dict[str, str]] = self.config.settings.get(
29+
"bump_map", defaults.bump_map
30+
)
31+
self.change_type_order: Optional[List[str]] = self.config.settings.get(
32+
"change_type_order", defaults.change_type_order
33+
)
34+
self.change_type_map: Optional[Dict[str, str]] = self.config.settings.get(
35+
"change_type_map", defaults.change_type_map
36+
)
37+
self.commit_parser: Optional[str] = self.config.settings.get(
38+
"commit_parser", defaults.commit_parser
39+
)
40+
self.changelog_pattern: Optional[str] = self.config.settings.get(
41+
"changelog_pattern", defaults.changelog_pattern
42+
)
43+
self.version_parser = self.config.settings.get(
44+
"version_parser", defaults.version_parser
45+
)
4746

4847
@abstractmethod
4948
def questions(self) -> Questions:
@@ -58,7 +57,7 @@ def style(self):
5857
return merge_styles(
5958
[
6059
Style(BaseCommitizen.default_style_config),
61-
Style(self.config.settings["style"]),
60+
Style(self.default_style_config),
6261
]
6362
)
6463

commitizen/cz/conventional_commits/conventional_commits.py

-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import re
33

4-
from commitizen import defaults
54
from commitizen.cz.base import BaseCommitizen
65
from commitizen.cz.utils import multiple_line_breaker, required_validator
76
from commitizen.defaults import Questions
@@ -28,18 +27,6 @@ def parse_subject(text):
2827

2928

3029
class ConventionalCommitsCz(BaseCommitizen):
31-
bump_pattern = defaults.bump_pattern
32-
bump_map = defaults.bump_map
33-
commit_parser = defaults.commit_parser
34-
version_parser = defaults.version_parser
35-
change_type_map = {
36-
"feat": "Feat",
37-
"fix": "Fix",
38-
"refactor": "Refactor",
39-
"perf": "Perf",
40-
}
41-
changelog_pattern = defaults.bump_pattern
42-
4330
def questions(self) -> Questions:
4431
questions: Questions = [
4532
{

commitizen/cz/customize/customize.py

-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from typing import Optional
77

8-
from commitizen import defaults
98
from commitizen.config import BaseConfig
109
from commitizen.cz.base import BaseCommitizen
1110
from commitizen.defaults import Questions
@@ -15,41 +14,13 @@
1514

1615

1716
class CustomizeCommitsCz(BaseCommitizen):
18-
bump_pattern = defaults.bump_pattern
19-
bump_map = defaults.bump_map
20-
change_type_order = defaults.change_type_order
21-
2217
def __init__(self, config: BaseConfig):
2318
super(CustomizeCommitsCz, self).__init__(config)
2419

2520
if "customize" not in self.config.settings:
2621
raise MissingCzCustomizeConfigError()
2722
self.custom_settings = self.config.settings["customize"]
2823

29-
custom_bump_pattern = self.custom_settings.get("bump_pattern")
30-
if custom_bump_pattern:
31-
self.bump_pattern = custom_bump_pattern
32-
33-
custom_bump_map = self.custom_settings.get("bump_map")
34-
if custom_bump_map:
35-
self.bump_map = custom_bump_map
36-
37-
custom_change_type_order = self.custom_settings.get("change_type_order")
38-
if custom_change_type_order:
39-
self.change_type_order = custom_change_type_order
40-
41-
commit_parser = self.custom_settings.get("commit_parser")
42-
if commit_parser:
43-
self.commit_parser = commit_parser
44-
45-
changelog_pattern = self.custom_settings.get("changelog_pattern")
46-
if changelog_pattern:
47-
self.changelog_pattern = changelog_pattern
48-
49-
change_type_map = self.custom_settings.get("change_type_map")
50-
if change_type_map:
51-
self.change_type_map = change_type_map
52-
5324
def questions(self) -> Questions:
5425
return self.custom_settings.get("questions", [{}])
5526

commitizen/defaults.py

+22
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,25 @@ class Settings(TypedDict, total=False):
8787

8888
commit_parser = r"^(?P<change_type>feat|fix|refactor|perf|BREAKING CHANGE)(?:\((?P<scope>[^()\r\n]*)\)|\()?(?P<breaking>!)?:\s(?P<message>.*)?" # noqa
8989
version_parser = r"(?P<version>([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?(\w+)?)"
90+
91+
change_type_map = {
92+
"feat": "Feat",
93+
"fix": "Fix",
94+
"refactor": "Refactor",
95+
"perf": "Perf",
96+
}
97+
98+
changelog_pattern = r".*"
99+
100+
default_style_config: List[Tuple[str, str]] = [
101+
("qmark", "fg:#ff9d00 bold"),
102+
("question", "bold"),
103+
("answer", "fg:#ff9d00 bold"),
104+
("pointer", "fg:#ff9d00 bold"),
105+
("highlighted", "fg:#ff9d00 bold"),
106+
("selected", "fg:#cc5454"),
107+
("separator", "fg:#cc5454"),
108+
("instruction", ""),
109+
("text", ""),
110+
("disabled", "fg:#858585 italic"),
111+
]

0 commit comments

Comments
 (0)