Skip to content

Commit fe409f1

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

File tree

17 files changed

+209
-315
lines changed

17 files changed

+209
-315
lines changed

pre_commit/commands/autoupdate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ def _write_new_config_file(path, output):
112112
f.write(to_write)
113113

114114

115-
def autoupdate(runner, store, tags_only, repos=()):
115+
def autoupdate(config_file, store, tags_only, repos=()):
116116
"""Auto-update the pre-commit config to the latest versions of repos."""
117-
migrate_config(runner, quiet=True)
117+
migrate_config(config_file, quiet=True)
118118
retv = 0
119119
output_repos = []
120120
changed = False
121121

122-
input_config = load_config(runner.config_file_path)
122+
input_config = load_config(config_file)
123123

124124
for repo_config in input_config['repos']:
125125
if (
@@ -152,6 +152,6 @@ def autoupdate(runner, store, tags_only, repos=()):
152152
if changed:
153153
output_config = input_config.copy()
154154
output_config['repos'] = output_repos
155-
_write_new_config_file(runner.config_file_path, output_config)
155+
_write_new_config_file(config_file, output_config)
156156

157157
return retv

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
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from pre_commit.commands.try_repo import try_repo
2121
from pre_commit.error_handler import error_handler
2222
from pre_commit.logging_handler import add_logging_handler
23-
from pre_commit.runner import Runner
2423
from pre_commit.store import Store
2524

2625

@@ -89,6 +88,20 @@ def _add_run_options(parser):
8988
)
9089

9190

91+
def _adjust_args_and_chdir(args):
92+
# `--config` was specified relative to the non-root working directory
93+
if os.path.exists(args.config):
94+
args.config = os.path.abspath(args.config)
95+
if args.command in {'run', 'try-repo'}:
96+
args.files = [os.path.abspath(filename) for filename in args.files]
97+
98+
os.chdir(git.get_root())
99+
100+
args.config = os.path.relpath(args.config)
101+
if args.command in {'run', 'try-repo'}:
102+
args.files = [os.path.relpath(filename) for filename in args.files]
103+
104+
92105
def main(argv=None):
93106
argv = argv if argv is not None else sys.argv[1:]
94107
argv = [five.to_text(arg) for arg in argv]
@@ -222,43 +235,40 @@ def main(argv=None):
222235
parser.parse_args([args.help_cmd, '--help'])
223236
elif args.command == 'help':
224237
parser.parse_args(['--help'])
225-
elif args.command in {'run', 'try-repo'}:
226-
args.files = [
227-
os.path.relpath(os.path.abspath(filename), git.get_root())
228-
for filename in args.files
229-
]
230238

231239
with error_handler():
232240
add_logging_handler(args.color)
233-
runner = Runner.create(args.config)
241+
242+
_adjust_args_and_chdir(args)
243+
234244
store = Store()
235245
git.check_for_cygwin_mismatch()
236246

237247
if args.command == 'install':
238248
return install(
239-
runner, store,
249+
args.config, store,
240250
overwrite=args.overwrite, hooks=args.install_hooks,
241251
hook_type=args.hook_type,
242252
skip_on_missing_conf=args.allow_missing_config,
243253
)
244254
elif args.command == 'install-hooks':
245-
return install_hooks(runner, store)
255+
return install_hooks(args.config, store)
246256
elif args.command == 'uninstall':
247-
return uninstall(runner, hook_type=args.hook_type)
257+
return uninstall(hook_type=args.hook_type)
248258
elif args.command == 'clean':
249259
return clean(store)
250260
elif args.command == 'autoupdate':
251261
if args.tags_only:
252262
logger.warning('--tags-only is the default')
253263
return autoupdate(
254-
runner, store,
264+
args.config, store,
255265
tags_only=not args.bleeding_edge,
256266
repos=args.repos,
257267
)
258268
elif args.command == 'migrate-config':
259-
return migrate_config(runner)
269+
return migrate_config(args.config)
260270
elif args.command == 'run':
261-
return run(runner, store, args)
271+
return run(args.config, store, args)
262272
elif args.command == 'sample-config':
263273
return sample_config()
264274
elif args.command == 'try-repo':

pre_commit/runner.py

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

0 commit comments

Comments
 (0)