1+ import os
2+
13import questionary
4+ import yaml
25from packaging .version import Version
36
4- from commitizen import factory , out
5- from commitizen .config import BaseConfig , IniConfig , TomlConfig
7+ from commitizen import cmd , factory , out
8+ from commitizen .__version__ import __version__
9+ from commitizen .config import BaseConfig , TomlConfig
610from commitizen .cz import registry
7- from commitizen .defaults import long_term_support_config_files
11+ from commitizen .defaults import config_files
812from commitizen .exceptions import NoAnswersError
913from commitizen .git import get_latest_tag_name , get_tag_names
1014
@@ -20,11 +24,8 @@ def __call__(self):
2024 # No config for commitizen exist
2125 if not self .config .path :
2226 config_path = self ._ask_config_path ()
23-
2427 if "toml" in config_path :
2528 self .config = TomlConfig (data = "" , path = config_path )
26- else :
27- self .config = IniConfig (data = "" , path = config_path )
2829
2930 self .config .init_empty_config_content ()
3031
@@ -33,17 +34,20 @@ def __call__(self):
3334 values_to_add ["version" ] = Version (tag ).public
3435 values_to_add ["tag_format" ] = self ._ask_tag_format (tag )
3536 self ._update_config_file (values_to_add )
37+
38+ if questionary .confirm ("Do you want to install pre-commit hook?" ).ask ():
39+ self ._install_pre_commit_hook ()
40+
3641 out .write ("You can bump the version and create changelog running:\n " )
3742 out .info ("cz bump --changelog" )
3843 out .success ("The configuration are all set." )
3944 else :
40- # TODO: handle the case that config file exist but no value
4145 out .line (f"Config file { self .config .path } already exists" )
4246
4347 def _ask_config_path (self ) -> str :
4448 name = questionary .select (
4549 "Please choose a supported config file: (default: pyproject.toml)" ,
46- choices = long_term_support_config_files ,
50+ choices = config_files ,
4751 default = "pyproject.toml" ,
4852 style = self .cz .style ,
4953 ).ask ()
@@ -101,6 +105,50 @@ def _ask_tag_format(self, latest_tag) -> str:
101105 tag_format = "$version"
102106 return tag_format
103107
108+ def _install_pre_commit_hook (self ):
109+ pre_commit_config_filename = ".pre-commit-config.yaml"
110+ cz_hook_config = {
111+ "repo" : "https://github.com/commitizen-tools/commitizen" ,
112+ "rev" : f"v{ __version__ } " ,
113+ "hooks" : [{"id" : "commitizen" , "stages" : ["commit-msg" ]}],
114+ }
115+
116+ config_data = {}
117+ if not os .path .isfile (pre_commit_config_filename ):
118+ # .pre-commit-config does not exist
119+ config_data ["repos" ] = [cz_hook_config ]
120+ else :
121+ # breakpoint()
122+ with open (pre_commit_config_filename ) as config_file :
123+ yaml_data = yaml .safe_load (config_file )
124+ if yaml_data :
125+ config_data = yaml_data
126+
127+ if "repos" in config_data :
128+ for pre_commit_hook in config_data ["repos" ]:
129+ if "commitizen" in pre_commit_hook ["repo" ]:
130+ out .write ("commitizen already in pre-commit config" )
131+ break
132+ else :
133+ config_data ["repos" ].append (cz_hook_config )
134+ else :
135+ # .pre-commit-config exists but there's no "repos" key
136+ config_data ["repos" ] = [cz_hook_config ]
137+
138+ with open (pre_commit_config_filename , "w" ) as config_file :
139+ yaml .safe_dump (config_data , stream = config_file )
140+
141+ c = cmd .run ("pre-commit install --hook-type commit-msg" )
142+ if c .return_code == 127 :
143+ out .error (
144+ "pre-commit is not installed in current environement.\n "
145+ "Run 'pre-commit install --hook-type commit-msg' again after it's installed"
146+ )
147+ elif c .return_code != 0 :
148+ out .error (c .err )
149+ else :
150+ out .write ("commitizen pre-commit hook is now installed in your '.git'\n " )
151+
104152 def _update_config_file (self , values ):
105153 for key , value in values .items ():
106154 self .config .set_key (key , value )
0 commit comments