Skip to content

Commit 84b38f7

Browse files
author
marsha
committed
Change cmd_output_bs retcode arg to a boolean check
1 parent 71925c4 commit 84b38f7

File tree

15 files changed

+28
-26
lines changed

15 files changed

+28
-26
lines changed

pre_commit/commands/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def _all_filenames(args: argparse.Namespace) -> Collection[str]:
263263

264264
def _get_diff() -> bytes:
265265
_, out, _ = cmd_output_b(
266-
'git', 'diff', '--no-ext-diff', '--ignore-submodules', retcode=None,
266+
'git', 'diff', '--no-ext-diff', '--ignore-submodules', check=False,
267267
)
268268
return out
269269

@@ -318,7 +318,7 @@ def _has_unmerged_paths() -> bool:
318318
def _has_unstaged_config(config_file: str) -> bool:
319319
retcode, _, _ = cmd_output_b(
320320
'git', 'diff', '--no-ext-diff', '--exit-code', config_file,
321-
retcode=None,
321+
check=False,
322322
)
323323
# be explicit, other git errors don't mean it has an unstaged config.
324324
return retcode == 1

pre_commit/error_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _log_and_exit(
2525
error_msg = f'{msg}: {type(exc).__name__}: '.encode() + force_bytes(exc)
2626
output.write_line_b(error_msg)
2727

28-
_, git_version_b, _ = cmd_output_b('git', '--version', retcode=None)
28+
_, git_version_b, _ = cmd_output_b('git', '--version', check=False)
2929
git_version = git_version_b.decode(errors='backslashreplace').rstrip()
3030

3131
storedir = Store().directory

pre_commit/git.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ def head_rev(remote: str) -> str:
187187

188188
def has_diff(*args: str, repo: str = '.') -> bool:
189189
cmd = ('git', 'diff', '--quiet', '--no-ext-diff', *args)
190-
return cmd_output_b(*cmd, cwd=repo, retcode=None)[0] == 1
190+
return cmd_output_b(*cmd, cwd=repo, check=False)[0] == 1
191191

192192

193193
def has_core_hookpaths_set() -> bool:
194-
_, out, _ = cmd_output_b('git', 'config', 'core.hooksPath', retcode=None)
194+
_, out, _ = cmd_output_b('git', 'config', 'core.hooksPath', check=False)
195195
return bool(out.strip())
196196

197197

pre_commit/languages/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def in_env(
7575

7676
def health_check(prefix: Prefix, language_version: str) -> str | None:
7777
with in_env(prefix, language_version):
78-
retcode, _, _ = cmd_output_b('node', '--version', retcode=None)
78+
retcode, _, _ = cmd_output_b('node', '--version', check=False)
7979
if retcode != 0: # pragma: win32 no cover
8080
return f'`node --version` returned {retcode}'
8181
else:

pre_commit/languages/rust.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_default_version() -> str:
3636
# Just detecting the executable does not suffice, because if rustup is
3737
# installed but no toolchain is available, then `cargo` exists but
3838
# cannot be used without installing a toolchain first.
39-
if cmd_output_b('cargo', '--version', retcode=None)[0] == 0:
39+
if cmd_output_b('cargo', '--version', check=False)[0] == 0:
4040
return 'system'
4141
else:
4242
return C.DEFAULT

pre_commit/staged_files_only.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _unstaged_changes_cleared(patch_dir: str) -> Generator[None, None, None]:
5252
retcode, diff_stdout_binary, _ = cmd_output_b(
5353
'git', 'diff-index', '--ignore-submodules', '--binary',
5454
'--exit-code', '--no-color', '--no-ext-diff', tree, '--',
55-
retcode=None,
55+
check=False,
5656
)
5757
if retcode and diff_stdout_binary.strip():
5858
patch_filename = f'patch{int(time.time())}-{os.getpid()}'

pre_commit/util.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def _oserror_to_output(e: OSError) -> tuple[int, bytes, None]:
124124

125125
def cmd_output_b(
126126
*cmd: str,
127-
retcode: int | None = 0,
127+
check: bool = True,
128128
**kwargs: Any,
129129
) -> tuple[int, bytes, bytes | None]:
130130
_setdefault_kwargs(kwargs)
@@ -142,8 +142,9 @@ def cmd_output_b(
142142
stdout_b, stderr_b = proc.communicate()
143143
returncode = proc.returncode
144144

145-
if retcode is not None and retcode != returncode:
146-
raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
145+
SUCCESS = 0
146+
if check and returncode != SUCCESS:
147+
raise CalledProcessError(returncode, cmd, SUCCESS, stdout_b, stderr_b)
147148

148149
return returncode, stdout_b, stderr_b
149150

@@ -196,10 +197,10 @@ def __exit__(
196197

197198
def cmd_output_p(
198199
*cmd: str,
199-
retcode: int | None = 0,
200+
check: bool = True,
200201
**kwargs: Any,
201202
) -> tuple[int, bytes, bytes | None]:
202-
assert retcode is None
203+
assert check is False
203204
assert kwargs['stderr'] == subprocess.STDOUT, kwargs['stderr']
204205
_setdefault_kwargs(kwargs)
205206

pre_commit/xargs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def run_cmd_partition(
154154
run_cmd: tuple[str, ...],
155155
) -> tuple[int, bytes, bytes | None]:
156156
return cmd_fn(
157-
*run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs,
157+
*run_cmd, check=False, stderr=subprocess.STDOUT, **kwargs,
158158
)
159159

160160
threads = min(len(partitions), target_concurrency)

tests/commands/init_templatedir_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_init_templatedir_skip_on_missing_config(
135135
retcode, output = git_commit(
136136
fn=cmd_output_mocked_pre_commit_home,
137137
tempdir_factory=tempdir_factory,
138-
retcode=None,
138+
check=False,
139139
)
140140

141141
assert retcode == commit_retcode

tests/commands/install_uninstall_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _get_commit_output(tempdir_factory, touch_file='foo', **kwargs):
126126
cmd_output('git', 'add', touch_file)
127127
return git_commit(
128128
fn=cmd_output_mocked_pre_commit_home,
129-
retcode=None,
129+
check=False,
130130
tempdir_factory=tempdir_factory,
131131
**kwargs,
132132
)
@@ -286,7 +286,7 @@ def test_environment_not_sourced(tempdir_factory, store):
286286
'GIT_AUTHOR_EMAIL': os.environ['GIT_AUTHOR_EMAIL'],
287287
'GIT_COMMITTER_EMAIL': os.environ['GIT_COMMITTER_EMAIL'],
288288
},
289-
retcode=None,
289+
check=False,
290290
)
291291
assert ret == 1
292292
assert out == (
@@ -551,7 +551,7 @@ def _get_push_output(tempdir_factory, remote='origin', opts=()):
551551
return cmd_output_mocked_pre_commit_home(
552552
'git', 'push', remote, 'HEAD:new_branch', *opts,
553553
tempdir_factory=tempdir_factory,
554-
retcode=None,
554+
check=False,
555555
)[:2]
556556

557557

0 commit comments

Comments
 (0)