Skip to content

Commit 2bacb27

Browse files
committed
fix(commands/init): fix init commands cannot create config file
1 parent 001d848 commit 2bacb27

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

commitizen/commands/init.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from commitizen import factory, out
44
from commitizen.cz import registry
5-
from commitizen.config import BaseConfig
5+
from commitizen.config import BaseConfig, TomlConfig, IniConfig
66
from commitizen.git import get_latest_tag, get_all_tags
7+
from commitizen.defaults import config_files
78

89

910
class Init:
@@ -16,20 +17,37 @@ def __call__(self):
1617

1718
# No config file exist
1819
if not self.config.path:
20+
config_path = self._ask_config_path()
21+
22+
if "toml" in config_path:
23+
self.config = TomlConfig(data="", path=config_path)
24+
else:
25+
self.config = IniConfig(data="", path=config_path)
26+
27+
self.config.init_empty_config_file()
28+
1929
values_to_add["name"] = self._ask_name()
2030
tag = self._ask_tag()
2131
values_to_add["version"] = tag
2232
values_to_add["tag_format"] = self._ask_tag_format(tag)
33+
self._update_config_file(values_to_add)
34+
out.write("The configuration are all set.")
2335
else:
36+
# TODO: handle the case that config file exist but no value
2437
out.line(f"Config file {self.config.path} already exists")
25-
raise SystemExit()
2638

27-
self._update_config_file(values_to_add)
28-
out.write("The configuration are all set.")
39+
def _ask_config_path(self) -> str:
40+
name = questionary.select(
41+
"Please choose a supported config file: (default: pyproject.tml)",
42+
choices=config_files,
43+
default="pyproject.toml",
44+
style=self.cz.style,
45+
).ask()
46+
return name
2947

3048
def _ask_name(self) -> str:
3149
name = questionary.select(
32-
"Please choose a cz: ",
50+
"Please choose a cz: (default: cz_conventional_commits)",
3351
choices=list(registry.keys()),
3452
default="cz_conventional_commits",
3553
style=self.cz.style,
@@ -38,14 +56,18 @@ def _ask_name(self) -> str:
3856

3957
def _ask_tag(self) -> str:
4058
latest_tag = get_latest_tag()
59+
if not latest_tag:
60+
out.error("No Existing Tag. Set tag to v0.0.1")
61+
return 'v0.0.1'
62+
4163
is_correct_tag = questionary.confirm(
4264
f"Is {latest_tag} the latest tag?", style=self.cz.style, default=False
4365
).ask()
4466
if not is_correct_tag:
4567
tags = get_all_tags()
4668
if not tags:
47-
out.line("No Existing Tag. Set tag to v0.0.1")
48-
return "v0.0.1"
69+
out.error("No Existing Tag. Set tag to v0.0.1")
70+
return 'v0.0.1'
4971

5072
latest_tag = questionary.select(
5173
"Please choose the latest tag: ",

0 commit comments

Comments
 (0)