Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(conventional_commits): add ability to overide settings from tool…
….commitizen.customize
  • Loading branch information
AxTheB committed Sep 22, 2025
commit ed233f9960b16bc230d382174a8b3af58c6b932f
56 changes: 51 additions & 5 deletions commitizen/cz/conventional_commits/conventional_commits.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import os
from typing import TypedDict
from collections.abc import Iterable
from typing import TYPE_CHECKING, TypedDict

from commitizen import defaults
from commitizen.config import BaseConfig
from commitizen.cz.base import BaseCommitizen
from commitizen.cz.utils import multiple_line_breaker, required_validator
from commitizen.question import CzQuestion

if TYPE_CHECKING:
from jinja2 import Template
else:
try:
from jinja2 import Template
except ImportError:
from string import Template

__all__ = ["ConventionalCommitsCz"]


Expand Down Expand Up @@ -39,8 +49,31 @@ class ConventionalCommitsCz(BaseCommitizen):
}
changelog_pattern = defaults.BUMP_PATTERN

def questions(self) -> list[CzQuestion]:
return [
def __init__(self, config: BaseConfig) -> None:
super().__init__(config)

self.custom_settings: defaults.CzSettings = self.config.settings.get(
"customize", {}
)

if self.custom_settings:
for attr_name in [
"bump_pattern",
"bump_map",
"bump_map_major_version_zero",
"change_type_order",
"commit_parser",
"change_type_map",
]:
if value := self.custom_settings.get(attr_name):
setattr(self, attr_name, value)

self.changelog_pattern = (
self.custom_settings.get("changelog_pattern") or self.bump_pattern
)

def questions(self) -> Iterable[CzQuestion]:
return self.custom_settings.get("questions") or [
{
"type": "list",
"name": "prefix",
Expand Down Expand Up @@ -147,6 +180,12 @@ def questions(self) -> list[CzQuestion]:
]

def message(self, answers: ConventionalCommitsAnswers) -> str: # type: ignore[override]
if _message_template := self.custom_settings.get("message_template"):
message_template = Template(_message_template)
if getattr(Template, "substitute", None):
return message_template.substitute(**answers) # type: ignore[attr-defined,no-any-return] # pragma: no cover # TODO: check if we can fix this
return message_template.render(**answers)

prefix = answers["prefix"]
scope = answers["scope"]
subject = answers["subject"]
Expand All @@ -166,7 +205,7 @@ def message(self, answers: ConventionalCommitsAnswers) -> str: # type: ignore[o
return f"{prefix}{scope}: {subject}{body}{footer}"

def example(self) -> str:
return (
return self.custom_settings.get("example") or (
"fix: correct minor typos in code\n"
"\n"
"see the issue for details on the typos fixed\n"
Expand All @@ -175,7 +214,7 @@ def example(self) -> str:
)

def schema(self) -> str:
return (
return self.custom_settings.get("schema") or (
"<type>(<scope>): <subject>\n"
"<BLANK LINE>\n"
"<body>\n"
Expand All @@ -184,6 +223,8 @@ def schema(self) -> str:
)

def schema_pattern(self) -> str:
if schema_pattern := self.custom_settings.get("schema_pattern"):
return schema_pattern
change_types = (
"build",
"bump",
Expand All @@ -209,6 +250,11 @@ def schema_pattern(self) -> str:
)

def info(self) -> str:
if info_path := self.custom_settings.get("info_path"):
with open(info_path, encoding=self.config.settings["encoding"]) as f:
return f.read()
if info := self.custom_settings.get("info"):
return info
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "conventional_commits_info.txt")
with open(filepath, encoding=self.config.settings["encoding"]) as f:
Expand Down
10 changes: 1 addition & 9 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ We have two different ways to do so.

## 1. Customize in configuration file

The basic steps are:

1. Define your custom committing or bumping rules in the configuration file.
2. Declare `name = "cz_customize"` in your configuration file, or add `-n cz_customize` when running Commitizen.
Define your custom committing or bumping rules in the configuration file, changing behaviour of the default cz_conventional_commits commitizen.

Example:

```toml title="pyproject.toml"
[tool.commitizen]
name = "cz_customize"

[tool.commitizen.customize]
message_template = "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example = "feature: this feature enable customize through config file"
Expand Down Expand Up @@ -53,7 +47,6 @@ The equivalent example for a json config file:
```json title=".cz.json"
{
"commitizen": {
"name": "cz_customize",
"customize": {
"message_template": "{{change_type}}:{% if show_message %} {{message}}{% endif %}",
"example": "feature: this feature enable customize through config file",
Expand Down Expand Up @@ -108,7 +101,6 @@ And the correspondent example for a yaml file:

```yaml title=".cz.yaml"
commitizen:
name: cz_customize
customize:
message_template: "{{change_type}}:{% if show_message %} {{message}}{% endif %}"
example: 'feature: this feature enable customize through config file'
Expand Down
Loading