Skip to content

Commit bd02381

Browse files
authored
feat(cli): add description when choosing a commit rule (#1825)
1 parent dd2dcca commit bd02381

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

commitizen/commands/init.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
from commitizen.config.factory import create_config
1212
from commitizen.cz import registry
1313
from commitizen.defaults import CONFIG_FILES, DEFAULT_SETTINGS
14-
from commitizen.exceptions import InitFailedError, NoAnswersError
14+
from commitizen.exceptions import (
15+
InitFailedError,
16+
MissingCzCustomizeConfigError,
17+
NoAnswersError,
18+
)
1519
from commitizen.git import get_latest_tag_name, get_tag_names, smart_open
1620
from commitizen.version_schemes import KNOWN_SCHEMES, Version, get_version_scheme
1721

@@ -167,12 +171,28 @@ def _ask_config_path(self) -> Path:
167171
def _ask_name(self) -> str:
168172
name: str = questionary.select(
169173
"Please choose a cz (commit rule): (default: cz_conventional_commits)",
170-
choices=list(registry.keys()),
174+
choices=self._construct_name_choice_with_description(),
171175
default="cz_conventional_commits",
172176
style=self.cz.style,
173177
).unsafe_ask()
174178
return name
175179

180+
def _construct_name_choice_with_description(self) -> list[questionary.Choice]:
181+
choices = []
182+
for cz_name, cz_class in registry.items():
183+
try:
184+
cz_obj = cz_class(self.config)
185+
except MissingCzCustomizeConfigError:
186+
choices.append(questionary.Choice(title=cz_name, value=cz_name))
187+
continue
188+
first_example = cz_obj.schema().partition("\n")[0]
189+
choices.append(
190+
questionary.Choice(
191+
title=cz_name, value=cz_name, description=first_example
192+
)
193+
)
194+
return choices
195+
176196
def _ask_tag(self) -> str:
177197
latest_tag = get_latest_tag_name()
178198
if not latest_tag:

tests/commands/test_init_command.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from commitizen import cmd, commands
1212
from commitizen.__version__ import __version__
13+
from commitizen.cz import registry
1314
from commitizen.exceptions import InitFailedError, NoAnswersError
1415

1516
if TYPE_CHECKING:
@@ -463,3 +464,22 @@ def test_init_configuration_with_version_provider(
463464
assert (
464465
"version = " not in config_data
465466
) # Version should not be set when using version_provider
467+
468+
469+
def test_construct_name_choice_with_description(
470+
config: BaseConfig, mocker: MockFixture
471+
):
472+
"""Test the construction of cz name choices with descriptions."""
473+
init = commands.Init(config)
474+
# mock the registry to have only one cz for testing
475+
mocker.patch.dict(
476+
"commitizen.cz.registry",
477+
{"cz_conventional_commits": registry["cz_conventional_commits"]},
478+
clear=True,
479+
)
480+
choices = init._construct_name_choice_with_description()
481+
assert len(choices) == 1
482+
choice = choices[0]
483+
assert choice.title == "cz_conventional_commits"
484+
assert choice.value == "cz_conventional_commits"
485+
assert choice.description == "<type>(<scope>): <subject>"

0 commit comments

Comments
 (0)