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

Skip to content

Commit 49cf490

Browse files
committed
Remove noop_context
1 parent 2a9893d commit 49cf490

2 files changed

Lines changed: 9 additions & 13 deletions

File tree

pre_commit/commands/run.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import contextlib
23
import functools
34
import logging
45
import os
@@ -27,7 +28,6 @@
2728
from pre_commit.store import Store
2829
from pre_commit.util import cmd_output_b
2930
from pre_commit.util import EnvironT
30-
from pre_commit.util import noop_context
3131

3232

3333
logger = logging.getLogger('pre_commit')
@@ -272,7 +272,7 @@ def run(
272272
args: argparse.Namespace,
273273
environ: EnvironT = os.environ,
274274
) -> int:
275-
no_stash = args.all_files or bool(args.files)
275+
stash = not args.all_files and not args.files
276276

277277
# Check if we have unresolved merge conflict files and fail fast.
278278
if _has_unmerged_paths():
@@ -281,7 +281,7 @@ def run(
281281
if bool(args.source) != bool(args.origin):
282282
logger.error('Specify both --origin and --source.')
283283
return 1
284-
if _has_unstaged_config(config_file) and not no_stash:
284+
if stash and _has_unstaged_config(config_file):
285285
logger.error(
286286
f'Your pre-commit configuration is unstaged.\n'
287287
f'`git add {config_file}` to fix this.',
@@ -293,12 +293,10 @@ def run(
293293
environ['PRE_COMMIT_ORIGIN'] = args.origin
294294
environ['PRE_COMMIT_SOURCE'] = args.source
295295

296-
if no_stash:
297-
ctx = noop_context()
298-
else:
299-
ctx = staged_files_only(store.directory)
296+
with contextlib.ExitStack() as exit_stack:
297+
if stash:
298+
exit_stack.enter_context(staged_files_only(store.directory))
300299

301-
with ctx:
302300
config = load_config(config_file)
303301
hooks = [
304302
hook
@@ -316,3 +314,6 @@ def run(
316314
install_hook_envs(hooks, store)
317315

318316
return _run_hooks(config, hooks, args, environ)
317+
318+
# https://github.com/python/mypy/issues/7726
319+
raise AssertionError('unreachable')

pre_commit/util.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ def clean_path_on_failure(path: str) -> Generator[None, None, None]:
4040
raise
4141

4242

43-
@contextlib.contextmanager
44-
def noop_context() -> Generator[None, None, None]:
45-
yield
46-
47-
4843
@contextlib.contextmanager
4944
def tmpdir() -> Generator[str, None, None]:
5045
"""Contextmanager to create a temporary directory. It will be cleaned up

0 commit comments

Comments
 (0)