diff --git a/commitizen/commands/init.py b/commitizen/commands/init.py index 62678a2244..0773b71e42 100644 --- a/commitizen/commands/init.py +++ b/commitizen/commands/init.py @@ -11,7 +11,11 @@ from commitizen.config.factory import create_config from commitizen.cz import registry from commitizen.defaults import CONFIG_FILES, DEFAULT_SETTINGS -from commitizen.exceptions import InitFailedError, NoAnswersError +from commitizen.exceptions import ( + InitFailedError, + MissingCzCustomizeConfigError, + NoAnswersError, +) from commitizen.git import get_latest_tag_name, get_tag_names, smart_open from commitizen.version_schemes import KNOWN_SCHEMES, Version, get_version_scheme @@ -167,12 +171,28 @@ def _ask_config_path(self) -> Path: def _ask_name(self) -> str: name: str = questionary.select( "Please choose a cz (commit rule): (default: cz_conventional_commits)", - choices=list(registry.keys()), + choices=self._construct_name_choice_with_description(), default="cz_conventional_commits", style=self.cz.style, ).unsafe_ask() return name + def _construct_name_choice_with_description(self) -> list[questionary.Choice]: + choices = [] + for cz_name, cz_class in registry.items(): + try: + cz_obj = cz_class(self.config) + except MissingCzCustomizeConfigError: + choices.append(questionary.Choice(title=cz_name, value=cz_name)) + continue + first_example = cz_obj.schema().partition("\n")[0] + choices.append( + questionary.Choice( + title=cz_name, value=cz_name, description=first_example + ) + ) + return choices + def _ask_tag(self) -> str: latest_tag = get_latest_tag_name() if not latest_tag: diff --git a/tests/commands/test_init_command.py b/tests/commands/test_init_command.py index 8398e784bc..0529553cf2 100644 --- a/tests/commands/test_init_command.py +++ b/tests/commands/test_init_command.py @@ -10,6 +10,7 @@ from commitizen import cmd, commands from commitizen.__version__ import __version__ +from commitizen.cz import registry from commitizen.exceptions import InitFailedError, NoAnswersError if TYPE_CHECKING: @@ -462,3 +463,22 @@ def test_init_configuration_with_version_provider( assert ( "version = " not in config_data ) # Version should not be set when using version_provider + + +def test_construct_name_choice_with_description( + config: BaseConfig, mocker: MockFixture +): + """Test the construction of cz name choices with descriptions.""" + init = commands.Init(config) + # mock the registry to have only one cz for testing + mocker.patch.dict( + "commitizen.cz.registry", + {"cz_conventional_commits": registry["cz_conventional_commits"]}, + clear=True, + ) + choices = init._construct_name_choice_with_description() + assert len(choices) == 1 + choice = choices[0] + assert choice.title == "cz_conventional_commits" + assert choice.value == "cz_conventional_commits" + assert choice.description == "(): "