@@ -150,9 +150,43 @@ def __call__(self) -> None:
150
150
tag_format = self ._ask_tag_format (tag ) # confirm & text
151
151
update_changelog_on_bump = self ._ask_update_changelog_on_bump () # confirm
152
152
major_version_zero = self ._ask_major_version_zero (version ) # confirm
153
+ hook_types : list [str ] | None = questionary .checkbox (
154
+ "What types of pre-commit hook you want to install? (Leave blank if you don't want to install)" ,
155
+ choices = [
156
+ questionary .Choice ("commit-msg" , checked = False ),
157
+ questionary .Choice ("pre-push" , checked = False ),
158
+ ],
159
+ ).unsafe_ask ()
153
160
except KeyboardInterrupt :
154
161
raise InitFailedError ("Stopped by user" )
155
162
163
+ if hook_types :
164
+ config_data = self ._get_config_data ()
165
+ with smart_open (
166
+ self ._PRE_COMMIT_CONFIG_PATH , "w" , encoding = self .encoding
167
+ ) as config_file :
168
+ yaml .safe_dump (config_data , stream = config_file )
169
+
170
+ if not self .project_info .is_pre_commit_installed :
171
+ raise InitFailedError (
172
+ "Failed to install pre-commit hook.\n "
173
+ "pre-commit is not installed in current environment."
174
+ )
175
+
176
+ cmd_str = "pre-commit install " + " " .join (
177
+ f"--hook-type { ty } " for ty in hook_types
178
+ )
179
+ c = cmd .run (cmd_str )
180
+ if c .return_code != 0 :
181
+ raise InitFailedError (
182
+ "Failed to install pre-commit hook.\n "
183
+ f"Error running { cmd_str } ."
184
+ "Outputs are attached below:\n "
185
+ f"stdout: { c .out } \n "
186
+ f"stderr: { c .err } "
187
+ )
188
+ out .write ("commitizen pre-commit hook is now installed in your '.git'\n " )
189
+
156
190
# Initialize configuration
157
191
if "toml" in config_path :
158
192
self .config = TomlConfig (data = "" , path = config_path )
@@ -161,20 +195,6 @@ def __call__(self) -> None:
161
195
elif "yaml" in config_path :
162
196
self .config = YAMLConfig (data = "" , path = config_path )
163
197
164
- # Collect hook data
165
- hook_types = questionary .checkbox (
166
- "What types of pre-commit hook you want to install? (Leave blank if you don't want to install)" ,
167
- choices = [
168
- questionary .Choice ("commit-msg" , checked = False ),
169
- questionary .Choice ("pre-push" , checked = False ),
170
- ],
171
- ).unsafe_ask ()
172
- if hook_types :
173
- try :
174
- self ._install_pre_commit_hook (hook_types )
175
- except InitFailedError as e :
176
- raise InitFailedError (f"Failed to install pre-commit hook.\n { e } " )
177
-
178
198
# Create and initialize config
179
199
self .config .init_empty_config_content ()
180
200
@@ -321,26 +341,6 @@ def _ask_update_changelog_on_bump(self) -> bool:
321
341
).unsafe_ask ()
322
342
return update_changelog_on_bump
323
343
324
- def _exec_install_pre_commit_hook (self , hook_types : list [str ]) -> None :
325
- cmd_str = self ._gen_pre_commit_cmd (hook_types )
326
- c = cmd .run (cmd_str )
327
- if c .return_code != 0 :
328
- err_msg = (
329
- f"Error running { cmd_str } ."
330
- "Outputs are attached below:\n "
331
- f"stdout: { c .out } \n "
332
- f"stderr: { c .err } "
333
- )
334
- raise InitFailedError (err_msg )
335
-
336
- def _gen_pre_commit_cmd (self , hook_types : list [str ]) -> str :
337
- """Generate pre-commit command according to given hook types"""
338
- if not hook_types :
339
- raise ValueError ("At least 1 hook type should be provided." )
340
- return "pre-commit install " + " " .join (
341
- f"--hook-type { ty } " for ty in hook_types
342
- )
343
-
344
344
def _get_config_data (self ) -> dict [str , Any ]:
345
345
CZ_HOOK_CONFIG = {
346
346
"repo" : "https://github.com/commitizen-tools/commitizen" ,
@@ -369,17 +369,3 @@ def _get_config_data(self) -> dict[str, Any]:
369
369
else :
370
370
repos .append (CZ_HOOK_CONFIG )
371
371
return config_data
372
-
373
- def _install_pre_commit_hook (self , hook_types : list [str ] | None = None ) -> None :
374
- config_data = self ._get_config_data ()
375
- with smart_open (
376
- self ._PRE_COMMIT_CONFIG_PATH , "w" , encoding = self .encoding
377
- ) as config_file :
378
- yaml .safe_dump (config_data , stream = config_file )
379
-
380
- if not self .project_info .is_pre_commit_installed :
381
- raise InitFailedError ("pre-commit is not installed in current environment." )
382
- if hook_types is None :
383
- hook_types = ["commit-msg" , "pre-push" ]
384
- self ._exec_install_pre_commit_hook (hook_types )
385
- out .write ("commitizen pre-commit hook is now installed in your '.git'\n " )
0 commit comments