mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-07 00:39:09 -06:00
Separate store from runner
This commit is contained in:
@@ -7,6 +7,7 @@ import os.path
|
||||
import sys
|
||||
|
||||
from pre_commit import output
|
||||
from pre_commit.repository import repositories
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import make_executable
|
||||
from pre_commit.util import mkdirp
|
||||
@@ -36,7 +37,7 @@ def is_our_script(filename):
|
||||
|
||||
|
||||
def install(
|
||||
runner, overwrite=False, hooks=False, hook_type='pre-commit',
|
||||
runner, store, overwrite=False, hooks=False, hook_type='pre-commit',
|
||||
skip_on_missing_conf=False,
|
||||
):
|
||||
"""Install the pre-commit hooks."""
|
||||
@@ -89,13 +90,13 @@ def install(
|
||||
|
||||
# If they requested we install all of the hooks, do so.
|
||||
if hooks:
|
||||
install_hooks(runner)
|
||||
install_hooks(runner, store)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def install_hooks(runner):
|
||||
for repository in runner.repositories:
|
||||
def install_hooks(runner, store):
|
||||
for repository in repositories(runner.config, store):
|
||||
repository.require_installed()
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ from pre_commit import color
|
||||
from pre_commit import git
|
||||
from pre_commit import output
|
||||
from pre_commit.output import get_hook_message
|
||||
from pre_commit.repository import repositories
|
||||
from pre_commit.staged_files_only import staged_files_only
|
||||
from pre_commit.util import cmd_output
|
||||
from pre_commit.util import memoize_by_cwd
|
||||
@@ -223,7 +224,7 @@ def _has_unstaged_config(runner):
|
||||
return retcode == 1
|
||||
|
||||
|
||||
def run(runner, args, environ=os.environ):
|
||||
def run(runner, store, args, environ=os.environ):
|
||||
no_stash = args.all_files or bool(args.files)
|
||||
|
||||
# Check if we have unresolved merge conflict files and fail fast.
|
||||
@@ -248,11 +249,11 @@ def run(runner, args, environ=os.environ):
|
||||
if no_stash:
|
||||
ctx = noop_context()
|
||||
else:
|
||||
ctx = staged_files_only(runner.store.directory)
|
||||
ctx = staged_files_only(store.directory)
|
||||
|
||||
with ctx:
|
||||
repo_hooks = []
|
||||
for repo in runner.repositories:
|
||||
for repo in repositories(runner.config, store):
|
||||
for _, hook in repo.hooks:
|
||||
if (
|
||||
(not args.hook or hook['id'] == args.hook) and
|
||||
|
||||
@@ -20,10 +20,11 @@ def try_repo(args):
|
||||
ref = args.ref or git.head_rev(args.repo)
|
||||
|
||||
with tmpdir() as tempdir:
|
||||
store = Store(tempdir)
|
||||
if args.hook:
|
||||
hooks = [{'id': args.hook}]
|
||||
else:
|
||||
repo_path = Store(tempdir).clone(args.repo, ref)
|
||||
repo_path = store.clone(args.repo, ref)
|
||||
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
|
||||
manifest = sorted(manifest, key=lambda hook: hook['id'])
|
||||
hooks = [{'id': hook['id']} for hook in manifest]
|
||||
@@ -42,5 +43,4 @@ def try_repo(args):
|
||||
output.write(config_s)
|
||||
output.write_line('=' * 79)
|
||||
|
||||
runner = Runner('.', config_filename, store_dir=tempdir)
|
||||
return run(runner, args)
|
||||
return run(Runner('.', config_filename), store, args)
|
||||
|
||||
@@ -21,6 +21,7 @@ from pre_commit.commands.try_repo import try_repo
|
||||
from pre_commit.error_handler import error_handler
|
||||
from pre_commit.logging_handler import add_logging_handler
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.store import Store
|
||||
|
||||
|
||||
logger = logging.getLogger('pre_commit')
|
||||
@@ -230,32 +231,34 @@ def main(argv=None):
|
||||
with error_handler():
|
||||
add_logging_handler(args.color)
|
||||
runner = Runner.create(args.config)
|
||||
store = Store()
|
||||
git.check_for_cygwin_mismatch()
|
||||
|
||||
if args.command == 'install':
|
||||
return install(
|
||||
runner, overwrite=args.overwrite, hooks=args.install_hooks,
|
||||
runner, store,
|
||||
overwrite=args.overwrite, hooks=args.install_hooks,
|
||||
hook_type=args.hook_type,
|
||||
skip_on_missing_conf=args.allow_missing_config,
|
||||
)
|
||||
elif args.command == 'install-hooks':
|
||||
return install_hooks(runner)
|
||||
return install_hooks(runner, store)
|
||||
elif args.command == 'uninstall':
|
||||
return uninstall(runner, hook_type=args.hook_type)
|
||||
elif args.command == 'clean':
|
||||
return clean(runner.store)
|
||||
return clean(store)
|
||||
elif args.command == 'autoupdate':
|
||||
if args.tags_only:
|
||||
logger.warning('--tags-only is the default')
|
||||
return autoupdate(
|
||||
runner, runner.store,
|
||||
runner, store,
|
||||
tags_only=not args.bleeding_edge,
|
||||
repos=args.repos,
|
||||
)
|
||||
elif args.command == 'migrate-config':
|
||||
return migrate_config(runner)
|
||||
elif args.command == 'run':
|
||||
return run(runner, args)
|
||||
return run(runner, store, args)
|
||||
elif args.command == 'sample-config':
|
||||
return sample_config()
|
||||
elif args.command == 'try-repo':
|
||||
|
||||
@@ -2,17 +2,18 @@ import argparse
|
||||
|
||||
import pre_commit.constants as C
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib import load_config
|
||||
from pre_commit.commands.run import _filter_by_include_exclude
|
||||
from pre_commit.commands.run import _filter_by_types
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.repository import repositories
|
||||
from pre_commit.store import Store
|
||||
|
||||
|
||||
def check_all_hooks_match_files(config_file):
|
||||
runner = Runner.create(config_file)
|
||||
files = git.get_all_files()
|
||||
retv = 0
|
||||
|
||||
for repo in runner.repositories:
|
||||
for repo in repositories(load_config(config_file), Store()):
|
||||
for hook_id, hook in repo.hooks:
|
||||
if hook['always_run']:
|
||||
continue
|
||||
|
||||
@@ -282,3 +282,7 @@ class MetaRepository(LocalRepository):
|
||||
(hook['id'], _hook(self.manifest_hooks[hook['id']], hook))
|
||||
for hook in self.repo_config['hooks']
|
||||
)
|
||||
|
||||
|
||||
def repositories(config, store):
|
||||
return tuple(Repository.create(x, store) for x in config['repos'])
|
||||
|
||||
@@ -6,8 +6,6 @@ from cached_property import cached_property
|
||||
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib import load_config
|
||||
from pre_commit.repository import Repository
|
||||
from pre_commit.store import Store
|
||||
|
||||
|
||||
class Runner(object):
|
||||
@@ -15,10 +13,9 @@ class Runner(object):
|
||||
repository under test.
|
||||
"""
|
||||
|
||||
def __init__(self, git_root, config_file, store_dir=None):
|
||||
def __init__(self, git_root, config_file):
|
||||
self.git_root = git_root
|
||||
self.config_file = config_file
|
||||
self._store_dir = store_dir
|
||||
|
||||
@classmethod
|
||||
def create(cls, config_file):
|
||||
@@ -42,12 +39,6 @@ class Runner(object):
|
||||
def config(self):
|
||||
return load_config(self.config_file_path)
|
||||
|
||||
@cached_property
|
||||
def repositories(self):
|
||||
"""Returns a tuple of the configured repositories."""
|
||||
repos = self.config['repos']
|
||||
return tuple(Repository.create(x, self.store) for x in repos)
|
||||
|
||||
def get_hook_path(self, hook_type):
|
||||
return os.path.join(self.git_dir, 'hooks', hook_type)
|
||||
|
||||
@@ -58,7 +49,3 @@ class Runner(object):
|
||||
@cached_property
|
||||
def pre_push_path(self):
|
||||
return self.get_hook_path('pre-push')
|
||||
|
||||
@cached_property
|
||||
def store(self):
|
||||
return Store(self._store_dir)
|
||||
|
||||
@@ -39,10 +39,7 @@ class Store(object):
|
||||
__created = False
|
||||
|
||||
def __init__(self, directory=None):
|
||||
if directory is None:
|
||||
directory = self.get_default_directory()
|
||||
|
||||
self.directory = directory
|
||||
self.directory = directory or Store.get_default_directory()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def exclusive_lock(self):
|
||||
|
||||
Reference in New Issue
Block a user