Skip to content

Commit a0ef51d

Browse files
fix(cz_customize): default commit_parser extracts change_type
CustomizeCommitsCz previously inherited the `commit_parser = r"(?P<message>.*)"` default from `BaseCommitizen`. That default matches everything but does not capture a `change_type` named group, so even when a `cz_customize` user configured `changelog_pattern`, `change_type_map` and `change_type_order`, the changelog generator could not group commits and emitted a single ungrouped bullet list. Set a conventional-commits-style default that captures `change_type`, `scope`, `breaking` and `message` named groups. Users with a different commit format can still override via `customize.commit_parser`. End-to-end check on the exact reproducer from the issue now produces a properly grouped changelog: ## Unreleased ### feat - **DL-4567**: new feature test ### fix - **DL-1234**: qweqwe ### chore - update deps Closes #466 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4d99415 commit a0ef51d

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

commitizen/cz/customize/customize.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ class CustomizeCommitsCz(BaseCommitizen):
2929
bump_map = defaults.BUMP_MAP
3030
bump_map_major_version_zero = defaults.BUMP_MAP_MAJOR_VERSION_ZERO
3131
change_type_order = defaults.CHANGE_TYPE_ORDER
32+
# A conventional-commits-style default so that ``cz_customize`` users who
33+
# configure ``changelog_pattern`` (and optionally ``change_type_map`` /
34+
# ``change_type_order``) but no explicit ``commit_parser`` still get a
35+
# changelog grouped by change type. It captures any word as
36+
# ``change_type``, an optional scope, an optional ``!`` breaking marker,
37+
# and the message subject. Users with a different commit format can
38+
# still override this via ``customize.commit_parser`` (#466).
39+
commit_parser = (
40+
r"^(?P<change_type>\w+)"
41+
r"(?:\((?P<scope>[^()\r\n]*)\))?"
42+
r"(?P<breaking>!)?:\s*(?P<message>.*)$"
43+
)
3244

3345
def __init__(self, config: BaseConfig) -> None:
3446
super().__init__(config)

tests/test_cz_customize.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,49 @@ def test_commit_parser_unicode(config_with_unicode):
591591
)
592592

593593

594+
def test_commit_parser_default_extracts_change_type():
595+
"""Regression test for #466: when ``customize.commit_parser`` is not set,
596+
the default must still extract a ``change_type`` named group so that the
597+
changelog can be grouped by type (e.g. ``### Feat``, ``### Fix``).
598+
Previously the default was ``r"(?P<message>.*)"`` -- inherited from
599+
``BaseCommitizen`` -- which left every commit ungrouped.
600+
"""
601+
import re
602+
603+
config = BaseConfig()
604+
config.settings.update(
605+
{
606+
"name": "cz_customize",
607+
"customize": {
608+
# No ``commit_parser`` provided -- exercises the new default.
609+
"changelog_pattern": r"^(feat|fix|chore)(\(.+\))?(!)?",
610+
},
611+
}
612+
)
613+
cz = CustomizeCommitsCz(config)
614+
615+
pattern = re.compile(cz.commit_parser, re.MULTILINE)
616+
617+
feat = pattern.match("feat(scope): a feature")
618+
assert feat is not None
619+
assert feat.group("change_type") == "feat"
620+
assert feat.group("scope") == "scope"
621+
assert feat.group("breaking") is None
622+
assert feat.group("message") == "a feature"
623+
624+
breaking = pattern.match("fix!: breaking fix")
625+
assert breaking is not None
626+
assert breaking.group("change_type") == "fix"
627+
assert breaking.group("breaking") == "!"
628+
assert breaking.group("message") == "breaking fix"
629+
630+
no_scope = pattern.match("chore: tidy up")
631+
assert no_scope is not None
632+
assert no_scope.group("change_type") == "chore"
633+
assert no_scope.group("scope") is None
634+
assert no_scope.group("message") == "tidy up"
635+
636+
594637
def test_changelog_pattern(config):
595638
cz = CustomizeCommitsCz(config)
596639
assert cz.changelog_pattern == "^(feature|bug fix)?(!)?"

0 commit comments

Comments
 (0)