mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Remove stateful Runner
This commit is contained in:
parent
1d40cc2104
commit
fe409f1a43
17 changed files with 209 additions and 315 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue