Merge pull request #639 from pre-commit/lazy_install

Lazily install repositories
This commit is contained in:
Anthony Sottile 2017-10-20 13:36:41 -07:00 committed by GitHub
commit 51c476b2de
2 changed files with 14 additions and 24 deletions

View file

@ -197,12 +197,6 @@ def _run_hooks(config, repo_hooks, args, environ):
return retval return retval
def get_repo_hooks(runner):
for repo in runner.repositories:
for _, hook in repo.hooks:
yield (repo, hook)
def _has_unmerged_paths(): def _has_unmerged_paths():
_, stdout, _ = cmd_output('git', 'ls-files', '--unmerged') _, stdout, _ = cmd_output('git', 'ls-files', '--unmerged')
return bool(stdout.strip()) return bool(stdout.strip())
@ -245,21 +239,20 @@ def run(runner, args, environ=os.environ):
ctx = staged_files_only(runner.store.directory) ctx = staged_files_only(runner.store.directory)
with ctx: with ctx:
repo_hooks = list(get_repo_hooks(runner)) repo_hooks = []
for repo in runner.repositories:
for _, hook in repo.hooks:
if (
(not args.hook or hook['id'] == args.hook) and
not hook['stages'] or args.hook_stage in hook['stages']
):
repo_hooks.append((repo, hook))
if args.hook: if args.hook and not repo_hooks:
repo_hooks = [ output.write_line('No hook with id `{}`'.format(args.hook))
(repo, hook) for repo, hook in repo_hooks return 1
if hook['id'] == args.hook
]
if not repo_hooks:
output.write_line('No hook with id `{}`'.format(args.hook))
return 1
# Filter hooks for stages for repo in {repo for repo, _ in repo_hooks}:
repo_hooks = [ repo.require_installed()
(repo, hook) for repo, hook in repo_hooks
if not hook['stages'] or args.hook_stage in hook['stages']
]
return _run_hooks(runner.config, repo_hooks, args, environ) return _run_hooks(runner.config, repo_hooks, args, environ)

View file

@ -46,10 +46,7 @@ class Runner(object):
def repositories(self): def repositories(self):
"""Returns a tuple of the configured repositories.""" """Returns a tuple of the configured repositories."""
repos = self.config['repos'] repos = self.config['repos']
repos = tuple(Repository.create(x, self.store) for x in repos) return tuple(Repository.create(x, self.store) for x in repos)
for repo in repos:
repo.require_installed()
return repos
def get_hook_path(self, hook_type): def get_hook_path(self, hook_type):
return os.path.join(self.git_dir, 'hooks', hook_type) return os.path.join(self.git_dir, 'hooks', hook_type)