@@ -50,10 +50,10 @@ def __call__(self):
50
50
],
51
51
).ask ()
52
52
if hook_types :
53
- if not self . _install_pre_commit_hook ( hook_types ) :
54
- raise InitFailedError (
55
- "Installation failed. See error outputs for more information."
56
- )
53
+ try :
54
+ self . _install_pre_commit_hook ( hook_types )
55
+ except InitFailedError as e :
56
+ raise InitFailedError ( f"Failed to install pre-commit hook. \n { e } " )
57
57
58
58
out .write ("You can bump the version and create changelog running:\n " )
59
59
out .info ("cz bump --changelog" )
@@ -120,24 +120,32 @@ def _ask_tag_format(self, latest_tag) -> str:
120
120
tag_format = "$version"
121
121
return tag_format
122
122
123
- def _search_pre_commit (self ):
123
+ def _search_pre_commit (self ) -> bool :
124
+ """Check whether pre-commit is installed"""
124
125
return shutil .which ("pre-commit" ) is not None
125
126
126
127
def _exec_install_pre_commit_hook (self , hook_types : List [str ]):
128
+ cmd_str = self ._gen_pre_commit_cmd (hook_types )
129
+ c = cmd .run (cmd_str )
130
+ if c .return_code != 0 :
131
+ err_msg = (
132
+ f"Error running { cmd_str } ."
133
+ "Outputs are attached below:\n "
134
+ f"stdout: { c .out } \n "
135
+ f"stderr: { c .err } "
136
+ )
137
+ raise InitFailedError (err_msg )
138
+
139
+ def _gen_pre_commit_cmd (self , hook_types : List [str ]) -> str :
140
+ """Generate pre-commit command according to given hook types"""
127
141
if not hook_types :
128
142
raise ValueError ("At least 1 hook type should be provided." )
129
143
cmd_str = "pre-commit install " + "" .join (
130
144
f"--hook-type { ty } " for ty in hook_types
131
145
)
132
- c = cmd .run (cmd_str )
133
- if c .return_code != 0 :
134
- out .error (f"Error running { cmd_str } . Outputs are attached below:" )
135
- out .error (f"stdout: { c .out } " )
136
- out .error (f"stderr: { c .err } " )
137
- return False
138
- return True
146
+ return cmd_str
139
147
140
- def _install_pre_commit_hook (self , hook_types : Optional [List [str ]] = None ) -> bool :
148
+ def _install_pre_commit_hook (self , hook_types : Optional [List [str ]] = None ):
141
149
pre_commit_config_filename = ".pre-commit-config.yaml"
142
150
cz_hook_config = {
143
151
"repo" : "https://github.com/commitizen-tools/commitizen" ,
@@ -173,16 +181,13 @@ def _install_pre_commit_hook(self, hook_types: Optional[List[str]] = None) -> bo
173
181
yaml .safe_dump (config_data , stream = config_file )
174
182
175
183
if not self ._search_pre_commit ():
176
- out . error ( "pre-commit is not installed in current environement." )
177
- return False
178
-
184
+ raise InitFailedError (
185
+ "pre-commit is not installed in current environement."
186
+ )
179
187
if hook_types is None :
180
188
hook_types = ["commit-msg" , "pre-push" ]
181
- if not self ._exec_install_pre_commit_hook (hook_types ):
182
- return False
183
-
189
+ self ._exec_install_pre_commit_hook (hook_types )
184
190
out .write ("commitizen pre-commit hook is now installed in your '.git'\n " )
185
- return True
186
191
187
192
def _update_config_file (self , values : Dict [str , Any ]):
188
193
for key , value in values .items ():
0 commit comments