Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51c476b

Browse files
authored
Merge pull request #639 from pre-commit/lazy_install
Lazily install repositories
2 parents 2a984c3 + 10912fa commit 51c476b

2 files changed

Lines changed: 16 additions & 26 deletions

File tree

pre_commit/commands/run.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,6 @@ def _run_hooks(config, repo_hooks, args, environ):
197197
return retval
198198

199199

200-
def get_repo_hooks(runner):
201-
for repo in runner.repositories:
202-
for _, hook in repo.hooks:
203-
yield (repo, hook)
204-
205-
206200
def _has_unmerged_paths():
207201
_, stdout, _ = cmd_output('git', 'ls-files', '--unmerged')
208202
return bool(stdout.strip())
@@ -245,21 +239,20 @@ def run(runner, args, environ=os.environ):
245239
ctx = staged_files_only(runner.store.directory)
246240

247241
with ctx:
248-
repo_hooks = list(get_repo_hooks(runner))
249-
250-
if args.hook:
251-
repo_hooks = [
252-
(repo, hook) for repo, hook in repo_hooks
253-
if hook['id'] == args.hook
254-
]
255-
if not repo_hooks:
256-
output.write_line('No hook with id `{}`'.format(args.hook))
257-
return 1
258-
259-
# Filter hooks for stages
260-
repo_hooks = [
261-
(repo, hook) for repo, hook in repo_hooks
262-
if not hook['stages'] or args.hook_stage in hook['stages']
263-
]
242+
repo_hooks = []
243+
for repo in runner.repositories:
244+
for _, hook in repo.hooks:
245+
if (
246+
(not args.hook or hook['id'] == args.hook) and
247+
not hook['stages'] or args.hook_stage in hook['stages']
248+
):
249+
repo_hooks.append((repo, hook))
250+
251+
if args.hook and not repo_hooks:
252+
output.write_line('No hook with id `{}`'.format(args.hook))
253+
return 1
254+
255+
for repo in {repo for repo, _ in repo_hooks}:
256+
repo.require_installed()
264257

265258
return _run_hooks(runner.config, repo_hooks, args, environ)

pre_commit/runner.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ def config(self):
4646
def repositories(self):
4747
"""Returns a tuple of the configured repositories."""
4848
repos = self.config['repos']
49-
repos = tuple(Repository.create(x, self.store) for x in repos)
50-
for repo in repos:
51-
repo.require_installed()
52-
return repos
49+
return tuple(Repository.create(x, self.store) for x in repos)
5350

5451
def get_hook_path(self, hook_type):
5552
return os.path.join(self.git_dir, 'hooks', hook_type)

0 commit comments

Comments
 (0)