Remove Runner.cmd_runner and Store.cmd_runner

This commit is contained in:
Anthony Sottile
2017-09-05 14:49:31 -07:00
parent 95c3afacda
commit 6141c419ee
8 changed files with 65 additions and 120 deletions

View File

@@ -192,17 +192,14 @@ def get_repo_hooks(runner):
yield (repo, hook)
def _has_unmerged_paths(runner):
_, stdout, _ = runner.cmd_runner.run(['git', 'ls-files', '--unmerged'])
def _has_unmerged_paths():
_, stdout, _ = cmd_output('git', 'ls-files', '--unmerged')
return bool(stdout.strip())
def _has_unstaged_config(runner):
retcode, _, _ = runner.cmd_runner.run(
(
'git', 'diff', '--no-ext-diff', '--exit-code',
runner.config_file_path,
),
retcode, _, _ = cmd_output(
'git', 'diff', '--no-ext-diff', '--exit-code', runner.config_file_path,
retcode=None,
)
# be explicit, other git errors don't mean it has an unstaged config.
@@ -213,7 +210,7 @@ def run(runner, args, environ=os.environ):
no_stash = args.all_files or bool(args.files)
# Check if we have unresolved merge conflict files and fail fast.
if _has_unmerged_paths(runner):
if _has_unmerged_paths():
logger.error('Unmerged files. Resolve before committing.')
return 1
if bool(args.source) != bool(args.origin):
@@ -234,7 +231,7 @@ def run(runner, args, environ=os.environ):
if no_stash:
ctx = noop_context()
else:
ctx = staged_files_only(runner.cmd_runner)
ctx = staged_files_only(runner.store.directory)
with ctx:
repo_hooks = list(get_repo_hooks(runner))

View File

@@ -57,11 +57,6 @@ class Runner(object):
def pre_push_path(self):
return self.get_hook_path('pre-push')
@cached_property
def cmd_runner(self):
# TODO: remove this and inline runner.store.cmd_runner
return self.store.cmd_runner
@cached_property
def store(self):
return Store()

View File

@@ -3,44 +3,41 @@ from __future__ import unicode_literals
import contextlib
import io
import logging
import os.path
import time
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
logger = logging.getLogger('pre_commit')
def _git_apply(cmd_runner, patch):
def _git_apply(patch):
args = ('apply', '--whitespace=nowarn', patch)
try:
cmd_runner.run(('git',) + args, encoding=None)
cmd_output('git', *args, encoding=None)
except CalledProcessError:
# Retry with autocrlf=false -- see #570
cmd = ('git', '-c', 'core.autocrlf=false') + args
cmd_runner.run(cmd, encoding=None)
cmd_output('git', '-c', 'core.autocrlf=false', *args, encoding=None)
@contextlib.contextmanager
def staged_files_only(cmd_runner):
def staged_files_only(patch_dir):
"""Clear any unstaged changes from the git working directory inside this
context.
Args:
cmd_runner - PrefixedCommandRunner
"""
# Determine if there are unstaged files
tree = cmd_runner.run(('git', 'write-tree'))[1].strip()
retcode, diff_stdout_binary, _ = cmd_runner.run(
(
'git', 'diff-index', '--ignore-submodules', '--binary',
'--exit-code', '--no-color', '--no-ext-diff', tree, '--',
),
tree = cmd_output('git', 'write-tree')[1].strip()
retcode, diff_stdout_binary, _ = cmd_output(
'git', 'diff-index', '--ignore-submodules', '--binary',
'--exit-code', '--no-color', '--no-ext-diff', tree, '--',
retcode=None,
encoding=None,
)
if retcode and diff_stdout_binary.strip():
patch_filename = cmd_runner.path('patch{}'.format(int(time.time())))
patch_filename = 'patch{}'.format(int(time.time()))
patch_filename = os.path.join(patch_dir, patch_filename)
logger.warning('Unstaged files detected.')
logger.info(
'Stashing unstaged files to {}.'.format(patch_filename),
@@ -50,13 +47,13 @@ def staged_files_only(cmd_runner):
patch_file.write(diff_stdout_binary)
# Clear the working directory of unstaged changes
cmd_runner.run(('git', 'checkout', '--', '.'))
cmd_output('git', 'checkout', '--', '.')
try:
yield
finally:
# Try to apply the patch we saved
try:
_git_apply(cmd_runner, patch_filename)
_git_apply(patch_filename)
except CalledProcessError:
logger.warning(
'Stashed changes conflicted with hook auto-fixes... '
@@ -65,8 +62,8 @@ def staged_files_only(cmd_runner):
# We failed to apply the patch, presumably due to fixes made
# by hooks.
# Roll back the changes made by hooks.
cmd_runner.run(('git', 'checkout', '--', '.'))
_git_apply(cmd_runner, patch_filename)
cmd_output('git', 'checkout', '--', '.')
_git_apply(patch_filename)
logger.info('Restored changes from {}.'.format(patch_filename))
else:
# There weren't any staged files so we don't need to do anything

View File

@@ -11,7 +11,6 @@ from cached_property import cached_property
import pre_commit.constants as C
from pre_commit import file_lock
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output
from pre_commit.util import copy_tree_to_path
@@ -162,10 +161,6 @@ class Store(object):
make_local_strategy,
)
@cached_property
def cmd_runner(self):
return PrefixedCommandRunner(self.directory)
@cached_property
def db_path(self):
return os.path.join(self.directory, 'db.db')