Skip to content

Commit bb108bf

Browse files
authored
Merge pull request pre-commit#1160 from pre-commit/cmd_output_b
Split out cmd_output_b
2 parents ab06397 + f612aeb commit bb108bf

File tree

20 files changed

+79
-70
lines changed

20 files changed

+79
-70
lines changed

pre_commit/commands/autoupdate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pre_commit.commands.migrate_config import migrate_config
2222
from pre_commit.util import CalledProcessError
2323
from pre_commit.util import cmd_output
24+
from pre_commit.util import cmd_output_b
2425
from pre_commit.util import tmpdir
2526

2627

@@ -38,7 +39,7 @@ def _update_repo(repo_config, store, tags_only):
3839
"""
3940
with tmpdir() as repo_path:
4041
git.init_repo(repo_path, repo_config['repo'])
41-
cmd_output('git', 'fetch', 'origin', 'HEAD', '--tags', cwd=repo_path)
42+
cmd_output_b('git', 'fetch', 'origin', 'HEAD', '--tags', cwd=repo_path)
4243

4344
tag_cmd = ('git', 'describe', 'FETCH_HEAD', '--tags')
4445
if tags_only:

pre_commit/commands/install_uninstall.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from pre_commit.clientlib import load_config
1414
from pre_commit.repository import all_hooks
1515
from pre_commit.repository import install_hook_envs
16-
from pre_commit.util import cmd_output
1716
from pre_commit.util import make_executable
1817
from pre_commit.util import mkdirp
1918
from pre_commit.util import resource_text
@@ -117,7 +116,7 @@ def install(
117116
overwrite=False, hooks=False,
118117
skip_on_missing_config=False, git_dir=None,
119118
):
120-
if cmd_output('git', 'config', 'core.hooksPath', retcode=None)[1].strip():
119+
if git.has_core_hookpaths_set():
121120
logger.error(
122121
'Cowardly refusing to install hooks with `core.hooksPath` set.\n'
123122
'hint: `git config --unset-all core.hooksPath`',

pre_commit/commands/run.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pre_commit.repository import all_hooks
1717
from pre_commit.repository import install_hook_envs
1818
from pre_commit.staged_files_only import staged_files_only
19-
from pre_commit.util import cmd_output
19+
from pre_commit.util import cmd_output_b
2020
from pre_commit.util import noop_context
2121

2222

@@ -117,15 +117,11 @@ def _run_single_hook(classifier, hook, args, skips, cols):
117117
)
118118
sys.stdout.flush()
119119

120-
diff_before = cmd_output(
121-
'git', 'diff', '--no-ext-diff', retcode=None, encoding=None,
122-
)
120+
diff_before = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
123121
retcode, stdout, stderr = hook.run(
124122
tuple(filenames) if hook.pass_filenames else (),
125123
)
126-
diff_after = cmd_output(
127-
'git', 'diff', '--no-ext-diff', retcode=None, encoding=None,
128-
)
124+
diff_after = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
129125

130126
file_modifications = diff_before != diff_after
131127

@@ -235,12 +231,12 @@ def _run_hooks(config, hooks, args, environ):
235231

236232

237233
def _has_unmerged_paths():
238-
_, stdout, _ = cmd_output('git', 'ls-files', '--unmerged')
234+
_, stdout, _ = cmd_output_b('git', 'ls-files', '--unmerged')
239235
return bool(stdout.strip())
240236

241237

242238
def _has_unstaged_config(config_file):
243-
retcode, _, _ = cmd_output(
239+
retcode, _, _ = cmd_output_b(
244240
'git', 'diff', '--no-ext-diff', '--exit-code', config_file,
245241
retcode=None,
246242
)

pre_commit/commands/try_repo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pre_commit.clientlib import load_manifest
1414
from pre_commit.commands.run import run
1515
from pre_commit.store import Store
16-
from pre_commit.util import cmd_output
16+
from pre_commit.util import cmd_output_b
1717
from pre_commit.util import tmpdir
1818
from pre_commit.xargs import xargs
1919

@@ -31,8 +31,8 @@ def _repo_ref(tmpdir, repo, ref):
3131
logger.warning('Creating temporary repo with uncommitted changes...')
3232

3333
shadow = os.path.join(tmpdir, 'shadow-repo')
34-
cmd_output('git', 'clone', repo, shadow)
35-
cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)
34+
cmd_output_b('git', 'clone', repo, shadow)
35+
cmd_output_b('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)
3636

3737
idx = git.git_path('index', repo=shadow)
3838
objs = git.git_path('objects', repo=shadow)
@@ -42,7 +42,7 @@ def _repo_ref(tmpdir, repo, ref):
4242
if staged_files:
4343
xargs(('git', 'add', '--'), staged_files, cwd=repo, env=env)
4444

45-
cmd_output('git', 'add', '-u', cwd=repo, env=env)
45+
cmd_output_b('git', 'add', '-u', cwd=repo, env=env)
4646
git.commit(repo=shadow)
4747

4848
return shadow, git.head_rev(shadow)

pre_commit/git.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66

77
from pre_commit.util import cmd_output
8+
from pre_commit.util import cmd_output_b
89

910

1011
logger = logging.getLogger(__name__)
@@ -50,8 +51,8 @@ def get_git_dir(git_root='.'):
5051

5152

5253
def get_remote_url(git_root):
53-
ret = cmd_output('git', 'config', 'remote.origin.url', cwd=git_root)[1]
54-
return ret.strip()
54+
_, out, _ = cmd_output('git', 'config', 'remote.origin.url', cwd=git_root)
55+
return out.strip()
5556

5657

5758
def is_in_merge_conflict():
@@ -105,8 +106,8 @@ def get_staged_files(cwd=None):
105106

106107

107108
def intent_to_add_files():
108-
_, stdout_binary, _ = cmd_output('git', 'status', '--porcelain', '-z')
109-
parts = list(reversed(zsplit(stdout_binary)))
109+
_, stdout, _ = cmd_output('git', 'status', '--porcelain', '-z')
110+
parts = list(reversed(zsplit(stdout)))
110111
intent_to_add = []
111112
while parts:
112113
line = parts.pop()
@@ -140,16 +141,21 @@ def has_diff(*args, **kwargs):
140141
repo = kwargs.pop('repo', '.')
141142
assert not kwargs, kwargs
142143
cmd = ('git', 'diff', '--quiet', '--no-ext-diff') + args
143-
return cmd_output(*cmd, cwd=repo, retcode=None)[0]
144+
return cmd_output_b(*cmd, cwd=repo, retcode=None)[0]
145+
146+
147+
def has_core_hookpaths_set():
148+
_, out, _ = cmd_output_b('git', 'config', 'core.hooksPath', retcode=None)
149+
return bool(out.strip())
144150

145151

146152
def init_repo(path, remote):
147153
if os.path.isdir(remote):
148154
remote = os.path.abspath(remote)
149155

150156
env = no_git_env()
151-
cmd_output('git', 'init', path, env=env)
152-
cmd_output('git', 'remote', 'add', 'origin', remote, cwd=path, env=env)
157+
cmd_output_b('git', 'init', path, env=env)
158+
cmd_output_b('git', 'remote', 'add', 'origin', remote, cwd=path, env=env)
153159

154160

155161
def commit(repo='.'):
@@ -158,7 +164,7 @@ def commit(repo='.'):
158164
env['GIT_AUTHOR_NAME'] = env['GIT_COMMITTER_NAME'] = name
159165
env['GIT_AUTHOR_EMAIL'] = env['GIT_COMMITTER_EMAIL'] = email
160166
cmd = ('git', 'commit', '--no-edit', '--no-gpg-sign', '-n', '-minit')
161-
cmd_output(*cmd, cwd=repo, env=env)
167+
cmd_output_b(*cmd, cwd=repo, env=env)
162168

163169

164170
def git_path(name, repo='.'):

pre_commit/languages/docker.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pre_commit.languages import helpers
1010
from pre_commit.util import CalledProcessError
1111
from pre_commit.util import clean_path_on_failure
12-
from pre_commit.util import cmd_output
12+
from pre_commit.util import cmd_output_b
1313

1414

1515
ENVIRONMENT_DIR = 'docker'
@@ -29,9 +29,11 @@ def docker_tag(prefix): # pragma: windows no cover
2929

3030
def docker_is_running(): # pragma: windows no cover
3131
try:
32-
return cmd_output('docker', 'ps')[0] == 0
32+
cmd_output_b('docker', 'ps')
3333
except CalledProcessError:
3434
return False
35+
else:
36+
return True
3537

3638

3739
def assert_docker_available(): # pragma: windows no cover

pre_commit/languages/golang.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pre_commit.languages import helpers
1212
from pre_commit.util import clean_path_on_failure
1313
from pre_commit.util import cmd_output
14+
from pre_commit.util import cmd_output_b
1415
from pre_commit.util import rmtree
1516

1617

@@ -70,9 +71,9 @@ def install_environment(prefix, version, additional_dependencies):
7071
gopath = directory
7172
env = dict(os.environ, GOPATH=gopath)
7273
env.pop('GOBIN', None)
73-
cmd_output('go', 'get', './...', cwd=repo_src_dir, env=env)
74+
cmd_output_b('go', 'get', './...', cwd=repo_src_dir, env=env)
7475
for dependency in additional_dependencies:
75-
cmd_output('go', 'get', dependency, cwd=repo_src_dir, env=env)
76+
cmd_output_b('go', 'get', dependency, cwd=repo_src_dir, env=env)
7677
# Same some disk space, we don't need these after installation
7778
rmtree(prefix.path(directory, 'src'))
7879
pkgdir = prefix.path(directory, 'pkg')

pre_commit/languages/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import six
99

1010
import pre_commit.constants as C
11-
from pre_commit.util import cmd_output
11+
from pre_commit.util import cmd_output_b
1212
from pre_commit.xargs import xargs
1313

1414
FIXED_RANDOM_SEED = 1542676186
1515

1616

1717
def run_setup_cmd(prefix, cmd):
18-
cmd_output(*cmd, cwd=prefix.prefix_dir, encoding=None)
18+
cmd_output_b(*cmd, cwd=prefix.prefix_dir)
1919

2020

2121
def environment_dir(ENVIRONMENT_DIR, language_version):

pre_commit/languages/node.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pre_commit.languages.python import bin_dir
1212
from pre_commit.util import clean_path_on_failure
1313
from pre_commit.util import cmd_output
14+
from pre_commit.util import cmd_output_b
1415

1516

1617
ENVIRONMENT_DIR = 'node_env'
@@ -65,7 +66,7 @@ def install_environment(
6566
]
6667
if version != C.DEFAULT:
6768
cmd.extend(['-n', version])
68-
cmd_output(*cmd)
69+
cmd_output_b(*cmd)
6970

7071
with in_env(prefix, version):
7172
# https://npm.community/t/npm-install-g-git-vs-git-clone-cd-npm-install-g/5449

pre_commit/languages/python.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pre_commit.util import CalledProcessError
1414
from pre_commit.util import clean_path_on_failure
1515
from pre_commit.util import cmd_output
16+
from pre_commit.util import cmd_output_b
1617

1718

1819
ENVIRONMENT_DIR = 'py_env'
@@ -143,11 +144,10 @@ def in_env(prefix, language_version):
143144

144145
def healthy(prefix, language_version):
145146
with in_env(prefix, language_version):
146-
retcode, _, _ = cmd_output(
147+
retcode, _, _ = cmd_output_b(
147148
'python', '-c',
148149
'import ctypes, datetime, io, os, ssl, weakref',
149150
retcode=None,
150-
encoding=None,
151151
)
152152
return retcode == 0
153153

@@ -177,7 +177,7 @@ def install_environment(prefix, version, additional_dependencies):
177177
def make_venv(envdir, python):
178178
env = dict(os.environ, VIRTUALENV_NO_DOWNLOAD='1')
179179
cmd = (sys.executable, '-mvirtualenv', envdir, '-p', python)
180-
cmd_output(*cmd, env=env, cwd='/')
180+
cmd_output_b(*cmd, env=env, cwd='/')
181181

182182

183183
_interface = py_interface(ENVIRONMENT_DIR, make_venv)

0 commit comments

Comments
 (0)