mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
Remove stateful Runner
This commit is contained in:
@@ -112,14 +112,14 @@ def _write_new_config_file(path, output):
|
||||
f.write(to_write)
|
||||
|
||||
|
||||
def autoupdate(runner, store, tags_only, repos=()):
|
||||
def autoupdate(config_file, store, tags_only, repos=()):
|
||||
"""Auto-update the pre-commit config to the latest versions of repos."""
|
||||
migrate_config(runner, quiet=True)
|
||||
migrate_config(config_file, quiet=True)
|
||||
retv = 0
|
||||
output_repos = []
|
||||
changed = False
|
||||
|
||||
input_config = load_config(runner.config_file_path)
|
||||
input_config = load_config(config_file)
|
||||
|
||||
for repo_config in input_config['repos']:
|
||||
if (
|
||||
@@ -152,6 +152,6 @@ def autoupdate(runner, store, tags_only, repos=()):
|
||||
if changed:
|
||||
output_config = input_config.copy()
|
||||
output_config['repos'] = output_repos
|
||||
_write_new_config_file(runner.config_file_path, output_config)
|
||||
_write_new_config_file(config_file, output_config)
|
||||
|
||||
return retv
|
||||
|
||||
@@ -8,6 +8,7 @@ import sys
|
||||
|
||||
from pre_commit import git
|
||||
from pre_commit import output
|
||||
from pre_commit.clientlib import load_config
|
||||
from pre_commit.languages import python
|
||||
from pre_commit.repository import repositories
|
||||
from pre_commit.util import cmd_output
|
||||
@@ -31,8 +32,8 @@ TEMPLATE_START = '# start templated\n'
|
||||
TEMPLATE_END = '# end templated\n'
|
||||
|
||||
|
||||
def _hook_paths(git_root, hook_type):
|
||||
pth = os.path.join(git.get_git_dir(git_root), 'hooks', hook_type)
|
||||
def _hook_paths(hook_type):
|
||||
pth = os.path.join(git.get_git_dir(), 'hooks', hook_type)
|
||||
return pth, '{}.legacy'.format(pth)
|
||||
|
||||
|
||||
@@ -55,7 +56,8 @@ def shebang():
|
||||
|
||||
|
||||
def install(
|
||||
runner, store, overwrite=False, hooks=False, hook_type='pre-commit',
|
||||
config_file, store,
|
||||
overwrite=False, hooks=False, hook_type='pre-commit',
|
||||
skip_on_missing_conf=False,
|
||||
):
|
||||
"""Install the pre-commit hooks."""
|
||||
@@ -66,7 +68,7 @@ def install(
|
||||
)
|
||||
return 1
|
||||
|
||||
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
|
||||
hook_path, legacy_path = _hook_paths(hook_type)
|
||||
|
||||
mkdirp(os.path.dirname(hook_path))
|
||||
|
||||
@@ -84,7 +86,7 @@ def install(
|
||||
)
|
||||
|
||||
params = {
|
||||
'CONFIG': runner.config_file,
|
||||
'CONFIG': config_file,
|
||||
'HOOK_TYPE': hook_type,
|
||||
'INSTALL_PYTHON': sys.executable,
|
||||
'SKIP_ON_MISSING_CONFIG': skip_on_missing_conf,
|
||||
@@ -108,19 +110,19 @@ def install(
|
||||
|
||||
# If they requested we install all of the hooks, do so.
|
||||
if hooks:
|
||||
install_hooks(runner, store)
|
||||
install_hooks(config_file, store)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def install_hooks(runner, store):
|
||||
for repository in repositories(runner.config, store):
|
||||
def install_hooks(config_file, store):
|
||||
for repository in repositories(load_config(config_file), store):
|
||||
repository.require_installed()
|
||||
|
||||
|
||||
def uninstall(runner, hook_type='pre-commit'):
|
||||
def uninstall(hook_type='pre-commit'):
|
||||
"""Uninstall the pre-commit hooks."""
|
||||
hook_path, legacy_path = _hook_paths(runner.git_root, hook_type)
|
||||
hook_path, legacy_path = _hook_paths(hook_type)
|
||||
|
||||
# If our file doesn't exist or it isn't ours, gtfo.
|
||||
if not os.path.exists(hook_path) or not is_our_script(hook_path):
|
||||
|
||||
@@ -45,15 +45,15 @@ def _migrate_sha_to_rev(contents):
|
||||
return reg.sub(r'\1rev:', contents)
|
||||
|
||||
|
||||
def migrate_config(runner, quiet=False):
|
||||
with io.open(runner.config_file_path) as f:
|
||||
def migrate_config(config_file, quiet=False):
|
||||
with io.open(config_file) as f:
|
||||
orig_contents = contents = f.read()
|
||||
|
||||
contents = _migrate_map(contents)
|
||||
contents = _migrate_sha_to_rev(contents)
|
||||
|
||||
if contents != orig_contents:
|
||||
with io.open(runner.config_file_path, 'w') as f:
|
||||
with io.open(config_file, 'w') as f:
|
||||
f.write(contents)
|
||||
|
||||
print('Configuration has been migrated.')
|
||||
|
||||
@@ -11,6 +11,7 @@ from identify.identify import tags_from_path
|
||||
from pre_commit import color
|
||||
from pre_commit import git
|
||||
from pre_commit import output
|
||||
from pre_commit.clientlib import load_config
|
||||
from pre_commit.output import get_hook_message
|
||||
from pre_commit.repository import repositories
|
||||
from pre_commit.staged_files_only import staged_files_only
|
||||
@@ -214,16 +215,16 @@ def _has_unmerged_paths():
|
||||
return bool(stdout.strip())
|
||||
|
||||
|
||||
def _has_unstaged_config(runner):
|
||||
def _has_unstaged_config(config_file):
|
||||
retcode, _, _ = cmd_output(
|
||||
'git', 'diff', '--no-ext-diff', '--exit-code', runner.config_file_path,
|
||||
'git', 'diff', '--no-ext-diff', '--exit-code', config_file,
|
||||
retcode=None,
|
||||
)
|
||||
# be explicit, other git errors don't mean it has an unstaged config.
|
||||
return retcode == 1
|
||||
|
||||
|
||||
def run(runner, store, args, environ=os.environ):
|
||||
def run(config_file, 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.
|
||||
@@ -233,10 +234,10 @@ def run(runner, store, args, environ=os.environ):
|
||||
if bool(args.source) != bool(args.origin):
|
||||
logger.error('Specify both --origin and --source.')
|
||||
return 1
|
||||
if _has_unstaged_config(runner) and not no_stash:
|
||||
if _has_unstaged_config(config_file) and not no_stash:
|
||||
logger.error(
|
||||
'Your pre-commit configuration is unstaged.\n'
|
||||
'`git add {}` to fix this.'.format(runner.config_file),
|
||||
'`git add {}` to fix this.'.format(config_file),
|
||||
)
|
||||
return 1
|
||||
|
||||
@@ -252,7 +253,8 @@ def run(runner, store, args, environ=os.environ):
|
||||
|
||||
with ctx:
|
||||
repo_hooks = []
|
||||
for repo in repositories(runner.config, store):
|
||||
config = load_config(config_file)
|
||||
for repo in repositories(config, store):
|
||||
for _, hook in repo.hooks:
|
||||
if (
|
||||
(not args.hook or hook['id'] == args.hook) and
|
||||
@@ -267,4 +269,4 @@ def run(runner, store, args, environ=os.environ):
|
||||
for repo in {repo for repo, _ in repo_hooks}:
|
||||
repo.require_installed()
|
||||
|
||||
return _run_hooks(runner.config, repo_hooks, args, environ)
|
||||
return _run_hooks(config, repo_hooks, args, environ)
|
||||
|
||||
@@ -11,7 +11,6 @@ from pre_commit import git
|
||||
from pre_commit import output
|
||||
from pre_commit.clientlib import load_manifest
|
||||
from pre_commit.commands.run import run
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.store import Store
|
||||
from pre_commit.util import tmpdir
|
||||
|
||||
@@ -43,4 +42,4 @@ def try_repo(args):
|
||||
output.write(config_s)
|
||||
output.write_line('=' * 79)
|
||||
|
||||
return run(Runner('.', config_filename), store, args)
|
||||
return run(config_filename, store, args)
|
||||
|
||||
@@ -30,7 +30,7 @@ def get_root():
|
||||
)
|
||||
|
||||
|
||||
def get_git_dir(git_root):
|
||||
def get_git_dir(git_root='.'):
|
||||
opts = ('--git-common-dir', '--git-dir')
|
||||
_, out, _ = cmd_output('git', 'rev-parse', *opts, cwd=git_root)
|
||||
for line, opt in zip(out.splitlines(), opts):
|
||||
|
||||
@@ -88,12 +88,12 @@ def _install_rbenv(prefix, version='default'): # pragma: windows no cover
|
||||
activate_file.write('export RBENV_VERSION="{}"\n'.format(version))
|
||||
|
||||
|
||||
def _install_ruby(runner, version): # pragma: windows no cover
|
||||
def _install_ruby(prefix, version): # pragma: windows no cover
|
||||
try:
|
||||
helpers.run_setup_cmd(runner, ('rbenv', 'download', version))
|
||||
helpers.run_setup_cmd(prefix, ('rbenv', 'download', version))
|
||||
except CalledProcessError: # pragma: no cover (usually find with download)
|
||||
# Failed to download from mirror for some reason, build it instead
|
||||
helpers.run_setup_cmd(runner, ('rbenv', 'install', version))
|
||||
helpers.run_setup_cmd(prefix, ('rbenv', 'install', version))
|
||||
|
||||
|
||||
def install_environment(
|
||||
|
||||
@@ -20,7 +20,6 @@ from pre_commit.commands.sample_config import sample_config
|
||||
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
|
||||
|
||||
|
||||
@@ -89,6 +88,20 @@ def _add_run_options(parser):
|
||||
)
|
||||
|
||||
|
||||
def _adjust_args_and_chdir(args):
|
||||
# `--config` was specified relative to the non-root working directory
|
||||
if os.path.exists(args.config):
|
||||
args.config = os.path.abspath(args.config)
|
||||
if args.command in {'run', 'try-repo'}:
|
||||
args.files = [os.path.abspath(filename) for filename in args.files]
|
||||
|
||||
os.chdir(git.get_root())
|
||||
|
||||
args.config = os.path.relpath(args.config)
|
||||
if args.command in {'run', 'try-repo'}:
|
||||
args.files = [os.path.relpath(filename) for filename in args.files]
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
argv = argv if argv is not None else sys.argv[1:]
|
||||
argv = [five.to_text(arg) for arg in argv]
|
||||
@@ -222,43 +235,40 @@ def main(argv=None):
|
||||
parser.parse_args([args.help_cmd, '--help'])
|
||||
elif args.command == 'help':
|
||||
parser.parse_args(['--help'])
|
||||
elif args.command in {'run', 'try-repo'}:
|
||||
args.files = [
|
||||
os.path.relpath(os.path.abspath(filename), git.get_root())
|
||||
for filename in args.files
|
||||
]
|
||||
|
||||
with error_handler():
|
||||
add_logging_handler(args.color)
|
||||
runner = Runner.create(args.config)
|
||||
|
||||
_adjust_args_and_chdir(args)
|
||||
|
||||
store = Store()
|
||||
git.check_for_cygwin_mismatch()
|
||||
|
||||
if args.command == 'install':
|
||||
return install(
|
||||
runner, store,
|
||||
args.config, 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, store)
|
||||
return install_hooks(args.config, store)
|
||||
elif args.command == 'uninstall':
|
||||
return uninstall(runner, hook_type=args.hook_type)
|
||||
return uninstall(hook_type=args.hook_type)
|
||||
elif args.command == 'clean':
|
||||
return clean(store)
|
||||
elif args.command == 'autoupdate':
|
||||
if args.tags_only:
|
||||
logger.warning('--tags-only is the default')
|
||||
return autoupdate(
|
||||
runner, store,
|
||||
args.config, store,
|
||||
tags_only=not args.bleeding_edge,
|
||||
repos=args.repos,
|
||||
)
|
||||
elif args.command == 'migrate-config':
|
||||
return migrate_config(runner)
|
||||
return migrate_config(args.config)
|
||||
elif args.command == 'run':
|
||||
return run(runner, store, args)
|
||||
return run(args.config, store, args)
|
||||
elif args.command == 'sample-config':
|
||||
return sample_config()
|
||||
elif args.command == 'try-repo':
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import os.path
|
||||
|
||||
from cached_property import cached_property
|
||||
|
||||
from pre_commit import git
|
||||
from pre_commit.clientlib import load_config
|
||||
|
||||
|
||||
class Runner(object):
|
||||
"""A `Runner` represents the execution context of the hooks. Notably the
|
||||
repository under test.
|
||||
"""
|
||||
|
||||
def __init__(self, git_root, config_file):
|
||||
self.git_root = git_root
|
||||
self.config_file = config_file
|
||||
|
||||
@classmethod
|
||||
def create(cls, config_file):
|
||||
"""Creates a Runner by doing the following:
|
||||
- Finds the root of the current git repository
|
||||
- chdir to that directory
|
||||
"""
|
||||
root = git.get_root()
|
||||
os.chdir(root)
|
||||
return cls(root, config_file)
|
||||
|
||||
@property
|
||||
def config_file_path(self):
|
||||
return os.path.join(self.git_root, self.config_file)
|
||||
|
||||
@cached_property
|
||||
def config(self):
|
||||
return load_config(self.config_file_path)
|
||||
Reference in New Issue
Block a user