1313from pre_commit import output
1414from pre_commit .clientlib import load_config
1515from pre_commit .output import get_hook_message
16- from pre_commit .repository import repositories
16+ from pre_commit .repository import all_hooks
17+ from pre_commit .repository import install_hook_envs
1718from pre_commit .staged_files_only import staged_files_only
1819from pre_commit .util import cmd_output
1920from pre_commit .util import memoize_by_cwd
@@ -32,9 +33,7 @@ def _get_skips(environ):
3233
3334
3435def _hook_msg_start (hook , verbose ):
35- return '{}{}' .format (
36- '[{}] ' .format (hook ['id' ]) if verbose else '' , hook ['name' ],
37- )
36+ return '{}{}' .format ('[{}] ' .format (hook .id ) if verbose else '' , hook .name )
3837
3938
4039def _filter_by_include_exclude (filenames , include , exclude ):
@@ -63,21 +62,21 @@ def _filter_by_types(filenames, types, exclude_types):
6362NO_FILES = '(no files to check)'
6463
6564
66- def _run_single_hook (filenames , hook , repo , args , skips , cols ):
67- include , exclude = hook [ ' files' ] , hook [ ' exclude' ]
65+ def _run_single_hook (filenames , hook , args , skips , cols ):
66+ include , exclude = hook . files , hook . exclude
6867 filenames = _filter_by_include_exclude (filenames , include , exclude )
69- types , exclude_types = hook [ ' types' ] , hook [ ' exclude_types' ]
68+ types , exclude_types = hook . types , hook . exclude_types
7069 filenames = _filter_by_types (filenames , types , exclude_types )
7170
72- if hook [ ' language' ] == 'pcre' :
71+ if hook . language == 'pcre' :
7372 logger .warning (
7473 '`{}` (from {}) uses the deprecated pcre language.\n '
7574 'The pcre language is scheduled for removal in pre-commit 2.x.\n '
7675 'The pygrep language is a more portable (and usually drop-in) '
77- 'replacement.' .format (hook [ 'id' ], repo . repo_config [ 'repo' ] ),
76+ 'replacement.' .format (hook . id , hook . src ),
7877 )
7978
80- if hook [ 'id' ] in skips or hook [ ' alias' ] in skips :
79+ if hook . id in skips or hook . alias in skips :
8180 output .write (get_hook_message (
8281 _hook_msg_start (hook , args .verbose ),
8382 end_msg = SKIPPED ,
@@ -86,7 +85,7 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
8685 cols = cols ,
8786 ))
8887 return 0
89- elif not filenames and not hook [ ' always_run' ] :
88+ elif not filenames and not hook . always_run :
9089 output .write (get_hook_message (
9190 _hook_msg_start (hook , args .verbose ),
9291 postfix = NO_FILES ,
@@ -107,8 +106,8 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
107106 diff_before = cmd_output (
108107 'git' , 'diff' , '--no-ext-diff' , retcode = None , encoding = None ,
109108 )
110- retcode , stdout , stderr = repo . run_hook (
111- hook , tuple (filenames ) if hook [ ' pass_filenames' ] else (),
109+ retcode , stdout , stderr = hook . run (
110+ tuple (filenames ) if hook . pass_filenames else (),
112111 )
113112 diff_after = cmd_output (
114113 'git' , 'diff' , '--no-ext-diff' , retcode = None , encoding = None ,
@@ -133,9 +132,9 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
133132
134133 if (
135134 (stdout or stderr or file_modifications ) and
136- (retcode or args .verbose or hook [ ' verbose' ] )
135+ (retcode or args .verbose or hook . verbose )
137136 ):
138- output .write_line ('hookid: {}\n ' .format (hook [ 'id' ] ))
137+ output .write_line ('hookid: {}\n ' .format (hook . id ))
139138
140139 # Print a message if failing due to file modifications
141140 if file_modifications :
@@ -149,7 +148,7 @@ def _run_single_hook(filenames, hook, repo, args, skips, cols):
149148 for out in (stdout , stderr ):
150149 assert type (out ) is bytes , type (out )
151150 if out .strip ():
152- output .write_line (out .strip (), logfile_name = hook [ ' log_file' ] )
151+ output .write_line (out .strip (), logfile_name = hook . log_file )
153152 output .write_line ()
154153
155154 return retcode
@@ -189,15 +188,15 @@ def _all_filenames(args):
189188 return git .get_staged_files ()
190189
191190
192- def _run_hooks (config , repo_hooks , args , environ ):
191+ def _run_hooks (config , hooks , args , environ ):
193192 """Actually run the hooks."""
194193 skips = _get_skips (environ )
195- cols = _compute_cols ([ hook for _ , hook in repo_hooks ] , args .verbose )
194+ cols = _compute_cols (hooks , args .verbose )
196195 filenames = _all_filenames (args )
197196 filenames = _filter_by_include_exclude (filenames , '' , config ['exclude' ])
198197 retval = 0
199- for repo , hook in repo_hooks :
200- retval |= _run_single_hook (filenames , hook , repo , args , skips , cols )
198+ for hook in hooks :
199+ retval |= _run_single_hook (filenames , hook , args , skips , cols )
201200 if retval and config ['fail_fast' ]:
202201 break
203202 if (
@@ -252,28 +251,18 @@ def run(config_file, store, args, environ=os.environ):
252251 ctx = staged_files_only (store .directory )
253252
254253 with ctx :
255- repo_hooks = []
256254 config = load_config (config_file )
257- for repo in repositories (config , store ):
258- for _ , hook in repo .hooks :
259- if (
260- (
261- not args .hook or
262- hook ['id' ] == args .hook or
263- hook ['alias' ] == args .hook
264- ) and
265- (
266- not hook ['stages' ] or
267- args .hook_stage in hook ['stages' ]
268- )
269- ):
270- repo_hooks .append ((repo , hook ))
271-
272- if args .hook and not repo_hooks :
255+ hooks = [
256+ hook
257+ for hook in all_hooks (config , store )
258+ if not args .hook or hook .id == args .hook or hook .alias == args .hook
259+ if not hook .stages or args .hook_stage in hook .stages
260+ ]
261+
262+ if args .hook and not hooks :
273263 output .write_line ('No hook with id `{}`' .format (args .hook ))
274264 return 1
275265
276- for repo in {repo for repo , _ in repo_hooks }:
277- repo .require_installed ()
266+ install_hook_envs (hooks , store )
278267
279- return _run_hooks (config , repo_hooks , args , environ )
268+ return _run_hooks (config , hooks , args , environ )
0 commit comments