Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 22 additions & 2 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions tests/commands/test_init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 == "<type>(<scope>): <subject>"