Remove stateful Runner · AppliedIntuition/pre-commit@fe409f1 · GitHub
Skip to content

Commit fe409f1

Browse files
committed
Remove stateful Runner
1 parent 1d40cc2 commit fe409f1

17 files changed

Lines changed: 209 additions & 315 deletions

pre_commit/commands/autoupdate.py

Lines changed: 4 additions & 4 deletions

pre_commit/commands/install_uninstall.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from pre_commit import git
1010
from pre_commit import output
11+
from pre_commit.clientlib import load_config
1112
from pre_commit.languages import python
1213
from pre_commit.repository import repositories
1314
from pre_commit.util import cmd_output
@@ -31,8 +32,8 @@
3132
TEMPLATE_END = '# end templated\n'
3233

3334

34-
def _hook_paths(git_root, hook_type):
35-
pth = os.path.join(git.get_git_dir(git_root), 'hooks', hook_type)
35+
def _hook_paths(hook_type):
36+
pth = os.path.join(git.get_git_dir(), 'hooks', hook_type)
3637
return pth, '{}.legacy'.format(pth)
3738

3839

@@ -55,7 +56,8 @@ def shebang():
5556

5657

5758
def install(
58-
runner, store, overwrite=False, hooks=False, hook_type='pre-commit',
59+
config_file, store,
60+
overwrite=False, hooks=False, hook_type='pre-commit',
5961
skip_on_missing_conf=False,
6062
):
6163
"""Install the pre-commit hooks."""
@@ -66,7 +68,7 @@ def install(
6668
)
6769
return 1
6870

69-
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
71+
hook_path, legacy_path = _hook_paths(hook_type)
7072

7173
mkdirp(os.path.dirname(hook_path))
7274

@@ -84,7 +86,7 @@ def install(
8486
)
8587

8688
params = {
87-
'CONFIG': runner.config_file,
89+
'CONFIG': config_file,
8890
'HOOK_TYPE': hook_type,
8991
'INSTALL_PYTHON': sys.executable,
9092
'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,
@@ -108,19 +110,19 @@ def install(
108110

109111
# If they requested we install all of the hooks, do so.
110112
if hooks:
111-
install_hooks(runner, store)
113+
install_hooks(config_file, store)
112114

113115
return 0
114116

115117

116-
def install_hooks(runner, store):
117-
for repository in repositories(runner.config, store):
118+
def install_hooks(config_file, store):
119+
for repository in repositories(load_config(config_file), store):
118120
repository.require_installed()
119121

120122

121-
def uninstall(runner, hook_type='pre-commit'):
123+
def uninstall(hook_type='pre-commit'):
122124
"""Uninstall the pre-commit hooks."""
123-
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
125+
hook_path, legacy_path = _hook_paths(hook_type)
124126

125127
# If our file doesn't exist or it isn't ours, gtfo.
126128
if not os.path.exists(hook_path) or not is_our_script(hook_path):

pre_commit/commands/migrate_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ def _migrate_sha_to_rev(contents):
4545
return reg.sub(r'\1rev:', contents)
4646

4747

48-
def migrate_config(runner, quiet=False):
49-
with io.open(runner.config_file_path) as f:
48+
def migrate_config(config_file, quiet=False):
49+
with io.open(config_file) as f:
5050
orig_contents = contents = f.read()
5151

5252
contents = _migrate_map(contents)
5353
contents = _migrate_sha_to_rev(contents)
5454

5555
if contents != orig_contents:
56-
with io.open(runner.config_file_path, 'w') as f:
56+
with io.open(config_file, 'w') as f:
5757
f.write(contents)
5858

5959
print('Configuration has been migrated.')

pre_commit/commands/run.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pre_commit import color
1212
from pre_commit import git
1313
from pre_commit import output
14+
from pre_commit.clientlib import load_config
1415
from pre_commit.output import get_hook_message
1516
from pre_commit.repository import repositories
1617
from pre_commit.staged_files_only import staged_files_only
@@ -214,16 +215,16 @@ def _has_unmerged_paths():
214215
return bool(stdout.strip())
215216

216217

217-
def _has_unstaged_config(runner):
218+
def _has_unstaged_config(config_file):
218219
retcode, _, _ = cmd_output(
219-
'git', 'diff', '--no-ext-diff', '--exit-code', runner.config_file_path,
220+
'git', 'diff', '--no-ext-diff', '--exit-code', config_file,
220221
retcode=None,
221222
)
222223
# be explicit, other git errors don't mean it has an unstaged config.
223224
return retcode == 1
224225

225226

226-
def run(runner, store, args, environ=os.environ):
227+
def run(config_file, 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.
@@ -233,10 +234,10 @@ def run(runner, store, args, environ=os.environ):
233234
if bool(args.source) != bool(args.origin):
234235
logger.error('Specify both --origin and --source.')
235236
return 1
236-
if _has_unstaged_config(runner) and not no_stash:
237+
if _has_unstaged_config(config_file) and not no_stash:
237238
logger.error(
238239
'Your pre-commit configuration is unstaged.\n'
239-
'`git add {}` to fix this.'.format(runner.config_file),
240+
'`git add {}` to fix this.'.format(config_file),
240241
)
241242
return 1
242243

@@ -252,7 +253,8 @@ def run(runner, store, args, environ=os.environ):
252253

253254
with ctx:
254255
repo_hooks = []
255-
for repo in repositories(runner.config, store):
256+
config = load_config(config_file)
257+
for repo in repositories(config, store):
256258
for _, hook in repo.hooks:
257259
if (
258260
(not args.hook or hook['id'] == args.hook) and
@@ -267,4 +269,4 @@ def run(runner, store, args, environ=os.environ):
267269
for repo in {repo for repo, _ in repo_hooks}:
268270
repo.require_installed()
269271

270-
return _run_hooks(runner.config, repo_hooks, args, environ)
272+
return _run_hooks(config, repo_hooks, args, environ)

pre_commit/commands/try_repo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pre_commit import output
1212
from pre_commit.clientlib import load_manifest
1313
from pre_commit.commands.run import run
14-
from pre_commit.runner import Runner
1514
from pre_commit.store import Store
1615
from pre_commit.util import tmpdir
1716

@@ -43,4 +42,4 @@ def try_repo(args):
4342
output.write(config_s)
4443
output.write_line('=' * 79)
4544

46-
return run(Runner('.', config_filename), store, args)
45+
return run(config_filename, store, args)

pre_commit/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_root():
3030
)
3131

3232

33-
def get_git_dir(git_root):
33+
def get_git_dir(git_root='.'):
3434
opts = ('--git-common-dir', '--git-dir')
3535
_, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root)
3636
for line, opt in zip(out.splitlines(), opts):

pre_commit/languages/ruby.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ def _install_rbenv(prefix, version='default'): # pragma: windows no cover
8888
activate_file.write('export RBENV_VERSION="{}"\n'.format(version))
8989

9090

91-
def _install_ruby(runner, version): # pragma: windows no cover
91+
def _install_ruby(prefix, version): # pragma: windows no cover
9292
try:
93-
helpers.run_setup_cmd(runner, ('rbenv', 'download', version))
93+
helpers.run_setup_cmd(prefix, ('rbenv', 'download', version))
9494
except CalledProcessError: # pragma: no cover (usually find with download)
9595
# Failed to download from mirror for some reason, build it instead
96-
helpers.run_setup_cmd(runner, ('rbenv', 'install', version))
96+
helpers.run_setup_cmd(prefix, ('rbenv', 'install', version))
9797

9898

9999
def install_environment(

pre_commit/main.py

Lines changed: 23 additions & 13 deletions

pre_commit/runner.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)