Skip to content

Commit c01ffc8

Browse files
committed
Separate store from runner
1 parent 6d683a5 commit c01ffc8

File tree

15 files changed

+228
-347
lines changed

15 files changed

+228
-347
lines changed

pre_commit/commands/install_uninstall.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88

99
from pre_commit import output
10+
from pre_commit.repository import repositories
1011
from pre_commit.util import cmd_output
1112
from pre_commit.util import make_executable
1213
from pre_commit.util import mkdirp
@@ -36,7 +37,7 @@ def is_our_script(filename):
3637

3738

3839
def install(
39-
runner, overwrite=False, hooks=False, hook_type='pre-commit',
40+
runner, store, overwrite=False, hooks=False, hook_type='pre-commit',
4041
skip_on_missing_conf=False,
4142
):
4243
"""Install the pre-commit hooks."""
@@ -89,13 +90,13 @@ def install(
8990

9091
# If they requested we install all of the hooks, do so.
9192
if hooks:
92-
install_hooks(runner)
93+
install_hooks(runner, store)
9394

9495
return 0
9596

9697

97-
def install_hooks(runner):
98-
for repository in runner.repositories:
98+
def install_hooks(runner, store):
99+
for repository in repositories(runner.config, store):
99100
repository.require_installed()
100101

101102

pre_commit/commands/run.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pre_commit import git
1414
from pre_commit import output
1515
from pre_commit.output import get_hook_message
16+
from pre_commit.repository import repositories
1617
from pre_commit.staged_files_only import staged_files_only
1718
from pre_commit.util import cmd_output
1819
from pre_commit.util import memoize_by_cwd
@@ -223,7 +224,7 @@ def _has_unstaged_config(runner):
223224
return retcode == 1
224225

225226

226-
def run(runner, args, environ=os.environ):
227+
def run(runner, store, args, environ=os.environ):
227228
no_stash = args.all_files or bool(args.files)
228229

229230
# Check if we have unresolved merge conflict files and fail fast.
@@ -248,11 +249,11 @@ def run(runner, args, environ=os.environ):
248249
if no_stash:
249250
ctx = noop_context()
250251
else:
251-
ctx = staged_files_only(runner.store.directory)
252+
ctx = staged_files_only(store.directory)
252253

253254
with ctx:
254255
repo_hooks = []
255-
for repo in runner.repositories:
256+
for repo in repositories(runner.config, store):
256257
for _, hook in repo.hooks:
257258
if (
258259
(not args.hook or hook['id'] == args.hook) and

pre_commit/commands/try_repo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def try_repo(args):
2020
ref = args.ref or git.head_rev(args.repo)
2121

2222
with tmpdir() as tempdir:
23+
store = Store(tempdir)
2324
if args.hook:
2425
hooks = [{'id': args.hook}]
2526
else:
26-
repo_path = Store(tempdir).clone(args.repo, ref)
27+
repo_path = store.clone(args.repo, ref)
2728
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
2829
manifest = sorted(manifest, key=lambda hook: hook['id'])
2930
hooks = [{'id': hook['id']} for hook in manifest]
@@ -42,5 +43,4 @@ def try_repo(args):
4243
output.write(config_s)
4344
output.write_line('=' * 79)
4445

45-
runner = Runner('.', config_filename, store_dir=tempdir)
46-
return run(runner, args)
46+
return run(Runner('.', config_filename), store, args)

pre_commit/main.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pre_commit.error_handler import error_handler
2222
from pre_commit.logging_handler import add_logging_handler
2323
from pre_commit.runner import Runner
24+
from pre_commit.store import Store
2425

2526

2627
logger = logging.getLogger('pre_commit')
@@ -230,32 +231,34 @@ def main(argv=None):
230231
with error_handler():
231232
add_logging_handler(args.color)
232233
runner = Runner.create(args.config)
234+
store = Store()
233235
git.check_for_cygwin_mismatch()
234236

235237
if args.command == 'install':
236238
return install(
237-
runner, overwrite=args.overwrite, hooks=args.install_hooks,
239+
runner, store,
240+
overwrite=args.overwrite, hooks=args.install_hooks,
238241
hook_type=args.hook_type,
239242
skip_on_missing_conf=args.allow_missing_config,
240243
)
241244
elif args.command == 'install-hooks':
242-
return install_hooks(runner)
245+
return install_hooks(runner, store)
243246
elif args.command == 'uninstall':
244247
return uninstall(runner, hook_type=args.hook_type)
245248
elif args.command == 'clean':
246-
return clean(runner.store)
249+
return clean(store)
247250
elif args.command == 'autoupdate':
248251
if args.tags_only:
249252
logger.warning('--tags-only is the default')
250253
return autoupdate(
251-
runner, runner.store,
254+
runner, store,
252255
tags_only=not args.bleeding_edge,
253256
repos=args.repos,
254257
)
255258
elif args.command == 'migrate-config':
256259
return migrate_config(runner)
257260
elif args.command == 'run':
258-
return run(runner, args)
261+
return run(runner, store, args)
259262
elif args.command == 'sample-config':
260263
return sample_config()
261264
elif args.command == 'try-repo':

pre_commit/meta_hooks/check_hooks_apply.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
import pre_commit.constants as C
44
from pre_commit import git
5+
from pre_commit.clientlib import load_config
56
from pre_commit.commands.run import _filter_by_include_exclude
67
from pre_commit.commands.run import _filter_by_types
7-
from pre_commit.runner import Runner
8+
from pre_commit.repository import repositories
9+
from pre_commit.store import Store
810

911

1012
def check_all_hooks_match_files(config_file):
11-
runner = Runner.create(config_file)
1213
files = git.get_all_files()
1314
retv = 0
1415

15-
for repo in runner.repositories:
16+
for repo in repositories(load_config(config_file), Store()):
1617
for hook_id, hook in repo.hooks:
1718
if hook['always_run']:
1819
continue

pre_commit/repository.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,7 @@ def hooks(self):
282282
(hook['id'], _hook(self.manifest_hooks[hook['id']], hook))
283283
for hook in self.repo_config['hooks']
284284
)
285+
286+
287+
def repositories(config, store):
288+
return tuple(Repository.create(x, store) for x in config['repos'])

pre_commit/runner.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66

77
from pre_commit import git
88
from pre_commit.clientlib import load_config
9-
from pre_commit.repository import Repository
10-
from pre_commit.store import Store
119

1210

1311
class Runner(object):
1412
"""A `Runner` represents the execution context of the hooks. Notably the
1513
repository under test.
1614
"""
1715

18-
def __init__(self, git_root, config_file, store_dir=None):
16+
def __init__(self, git_root, config_file):
1917
self.git_root = git_root
2018
self.config_file = config_file
21-
self._store_dir = store_dir
2219

2320
@classmethod
2421
def create(cls, config_file):
@@ -42,12 +39,6 @@ def config_file_path(self):
4239
def config(self):
4340
return load_config(self.config_file_path)
4441

45-
@cached_property
46-
def repositories(self):
47-
"""Returns a tuple of the configured repositories."""
48-
repos = self.config['repos']
49-
return tuple(Repository.create(x, self.store) for x in repos)
50-
5142
def get_hook_path(self, hook_type):
5243
return os.path.join(self.git_dir, 'hooks', hook_type)
5344

@@ -58,7 +49,3 @@ def pre_commit_path(self):
5849
@cached_property
5950
def pre_push_path(self):
6051
return self.get_hook_path('pre-push')
61-
62-
@cached_property
63-
def store(self):
64-
return Store(self._store_dir)

pre_commit/store.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ class Store(object):
3939
__created = False
4040

4141
def __init__(self, directory=None):
42-
if directory is None:
43-
directory = self.get_default_directory()
44-
45-
self.directory = directory
42+
self.directory = directory or Store.get_default_directory()
4643

4744
@contextlib.contextmanager
4845
def exclusive_lock(self):

0 commit comments

Comments
 (0)