Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6141c41

Browse files
committed
Remove Runner.cmd_runner and Store.cmd_runner
1 parent 95c3afa commit 6141c41

8 files changed

Lines changed: 65 additions & 120 deletions

File tree

pre_commit/commands/run.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,14 @@ def get_repo_hooks(runner):
192192
yield (repo, hook)
193193

194194

195-
def _has_unmerged_paths(runner):
196-
_, stdout, _ = runner.cmd_runner.run(['git', 'ls-files', '--unmerged'])
195+
def _has_unmerged_paths():
196+
_, stdout, _ = cmd_output('git', 'ls-files', '--unmerged')
197197
return bool(stdout.strip())
198198

199199

200200
def _has_unstaged_config(runner):
201-
retcode, _, _ = runner.cmd_runner.run(
202-
(
203-
'git', 'diff', '--no-ext-diff', '--exit-code',
204-
runner.config_file_path,
205-
),
201+
retcode, _, _ = cmd_output(
202+
'git', 'diff', '--no-ext-diff', '--exit-code', runner.config_file_path,
206203
retcode=None,
207204
)
208205
# be explicit, other git errors don't mean it has an unstaged config.
@@ -213,7 +210,7 @@ def run(runner, args, environ=os.environ):
213210
no_stash = args.all_files or bool(args.files)
214211

215212
# Check if we have unresolved merge conflict files and fail fast.
216-
if _has_unmerged_paths(runner):
213+
if _has_unmerged_paths():
217214
logger.error('Unmerged files. Resolve before committing.')
218215
return 1
219216
if bool(args.source) != bool(args.origin):
@@ -234,7 +231,7 @@ def run(runner, args, environ=os.environ):
234231
if no_stash:
235232
ctx = noop_context()
236233
else:
237-
ctx = staged_files_only(runner.cmd_runner)
234+
ctx = staged_files_only(runner.store.directory)
238235

239236
with ctx:
240237
repo_hooks = list(get_repo_hooks(runner))

pre_commit/runner.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ def pre_commit_path(self):
5757
def pre_push_path(self):
5858
return self.get_hook_path('pre-push')
5959

60-
@cached_property
61-
def cmd_runner(self):
62-
# TODO: remove this and inline runner.store.cmd_runner
63-
return self.store.cmd_runner
64-
6560
@cached_property
6661
def store(self):
6762
return Store()

pre_commit/staged_files_only.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,41 @@
33
import contextlib
44
import io
55
import logging
6+
import os.path
67
import time
78

89
from pre_commit.util import CalledProcessError
10+
from pre_commit.util import cmd_output
911

1012

1113
logger = logging.getLogger('pre_commit')
1214

1315

14-
def _git_apply(cmd_runner, patch):
16+
def _git_apply(patch):
1517
args = ('apply', '--whitespace=nowarn', patch)
1618
try:
17-
cmd_runner.run(('git',) + args, encoding=None)
19+
cmd_output('git', *args, encoding=None)
1820
except CalledProcessError:
1921
# Retry with autocrlf=false -- see #570
20-
cmd = ('git', '-c', 'core.autocrlf=false') + args
21-
cmd_runner.run(cmd, encoding=None)
22+
cmd_output('git', '-c', 'core.autocrlf=false', *args, encoding=None)
2223

2324

2425
@contextlib.contextmanager
25-
def staged_files_only(cmd_runner):
26+
def staged_files_only(patch_dir):
2627
"""Clear any unstaged changes from the git working directory inside this
2728
context.
28-
29-
Args:
30-
cmd_runner - PrefixedCommandRunner
3129
"""
3230
# Determine if there are unstaged files
33-
tree = cmd_runner.run(('git', 'write-tree'))[1].strip()
34-
retcode, diff_stdout_binary, _ = cmd_runner.run(
35-
(
36-
'git', 'diff-index', '--ignore-submodules', '--binary',
37-
'--exit-code', '--no-color', '--no-ext-diff', tree, '--',
38-
),
31+
tree = cmd_output('git', 'write-tree')[1].strip()
32+
retcode, diff_stdout_binary, _ = cmd_output(
33+
'git', 'diff-index', '--ignore-submodules', '--binary',
34+
'--exit-code', '--no-color', '--no-ext-diff', tree, '--',
3935
retcode=None,
4036
encoding=None,
4137
)
4238
if retcode and diff_stdout_binary.strip():
43-
patch_filename = cmd_runner.path('patch{}'.format(int(time.time())))
39+
patch_filename = 'patch{}'.format(int(time.time()))
40+
patch_filename = os.path.join(patch_dir, patch_filename)
4441
logger.warning('Unstaged files detected.')
4542
logger.info(
4643
'Stashing unstaged files to {}.'.format(patch_filename),
@@ -50,13 +47,13 @@ def staged_files_only(cmd_runner):
5047
patch_file.write(diff_stdout_binary)
5148

5249
# Clear the working directory of unstaged changes
53-
cmd_runner.run(('git', 'checkout', '--', '.'))
50+
cmd_output('git', 'checkout', '--', '.')
5451
try:
5552
yield
5653
finally:
5754
# Try to apply the patch we saved
5855
try:
59-
_git_apply(cmd_runner, patch_filename)
56+
_git_apply(patch_filename)
6057
except CalledProcessError:
6158
logger.warning(
6259
'Stashed changes conflicted with hook auto-fixes... '
@@ -65,8 +62,8 @@ def staged_files_only(cmd_runner):
6562
# We failed to apply the patch, presumably due to fixes made
6663
# by hooks.
6764
# Roll back the changes made by hooks.
68-
cmd_runner.run(('git', 'checkout', '--', '.'))
69-
_git_apply(cmd_runner, patch_filename)
65+
cmd_output('git', 'checkout', '--', '.')
66+
_git_apply(patch_filename)
7067
logger.info('Restored changes from {}.'.format(patch_filename))
7168
else:
7269
# There weren't any staged files so we don't need to do anything

pre_commit/store.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import pre_commit.constants as C
1313
from pre_commit import file_lock
14-
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
1514
from pre_commit.util import clean_path_on_failure
1615
from pre_commit.util import cmd_output
1716
from pre_commit.util import copy_tree_to_path
@@ -162,10 +161,6 @@ def make_local_strategy(directory):
162161
make_local_strategy,
163162
)
164163

165-
@cached_property
166-
def cmd_runner(self):
167-
return PrefixedCommandRunner(self.directory)
168-
169164
@cached_property
170165
def db_path(self):
171166
return os.path.join(self.directory, 'db.db')

tests/commands/run_test.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import sys
88
from collections import OrderedDict
99

10-
import mock
1110
import pytest
1211

1312
import pre_commit.constants as C
@@ -327,11 +326,10 @@ def test_origin_source_error_msg(
327326
assert warning_msg not in printed
328327

329328

330-
@pytest.mark.parametrize(('output', 'expected'), (('some', True), ('', False)))
331-
def test_has_unmerged_paths(output, expected):
332-
mock_runner = mock.Mock()
333-
mock_runner.cmd_runner.run.return_value = (1, output, '')
334-
assert _has_unmerged_paths(mock_runner) is expected
329+
def test_has_unmerged_paths(in_merge_conflict):
330+
assert _has_unmerged_paths() is True
331+
cmd_output('git', 'add', '.')
332+
assert _has_unmerged_paths() is False
335333

336334

337335
def test_merge_conflict(cap_out, in_merge_conflict, mock_out_store_directory):

tests/runner_test.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,3 @@ def test_pre_push_path(in_tmpdir):
133133
runner = Runner(path, C.CONFIG_FILE)
134134
expected_path = os.path.join(path, '.git', 'hooks', 'pre-push')
135135
assert runner.pre_push_path == expected_path
136-
137-
138-
def test_cmd_runner(mock_out_store_directory):
139-
runner = Runner(os.path.join('foo', 'bar'), C.CONFIG_FILE)
140-
ret = runner.cmd_runner
141-
assert ret.prefix_dir == os.path.join(mock_out_store_directory) + os.sep

0 commit comments

Comments
 (0)