# Path: pre_commit/git.py
# NO_FS_MONITOR = ('-c', 'core.useBuiltinFSMonitor=false')
# def zsplit(s: str) -> list[str]:
# def no_git_env(
#         _env: MutableMapping[str, str] | None = None,
# ) -> dict[str, str]:
# def get_root() -> str:
# def get_git_dir(git_root: str = '.') -> str:
# def get_remote_url(git_root: str) -> str:
# def is_in_merge_conflict() -> bool:
# def parse_merge_msg_for_conflicts(merge_msg: bytes) -> list[str]:
# def get_conflicted_files() -> set[str]:
# def get_staged_files(cwd: str | None = None) -> list[str]:
# def intent_to_add_files() -> list[str]:
# def get_all_files() -> list[str]:
# def get_changed_files(old: str, new: str) -> list[str]:
# def head_rev(remote: str) -> str:
# def has_diff(*args: str, repo: str = '.') -> bool:
# def has_core_hookpaths_set() -> bool:
# def init_repo(path: str, remote: str) -> None:
# def commit(repo: str = '.') -> None:
# def git_path(name: str, repo: str = '.') -> str:
# def check_for_cygwin_mismatch() -> None:
# 
# Path: pre_commit/commands/hook_impl.py
# def hook_impl(
#         store: Store,
#         *,
#         config: str,
#         color: bool,
#         hook_type: str,
#         hook_dir: str,
#         skip_on_missing_config: bool,
#         args: Sequence[str],
# ) -> int:
#     retv, stdin = _run_legacy(hook_type, hook_dir, args)
#     _validate_config(retv, config, skip_on_missing_config)
#     ns = _run_ns(hook_type, color, args, stdin)
#     if ns is None:
#         return retv
#     else:
#         return retv | run(config, store, ns)
# 
# Path: pre_commit/envcontext.py
# @contextlib.contextmanager
# def envcontext(
#         patch: PatchesT,
#         _env: MutableMapping[str, str] | None = None,
# ) -> Generator[None, None, None]:
#     """In this context, `os.environ` is modified according to `patch`.
# 
#     `patch` is an iterable of 2-tuples (key, value):
#         `key`: string
#         `value`:
#             - string: `environ[key] == value` inside the context.
#             - UNSET: `key not in environ` inside the context.
#             - template: A template is a tuple of strings and Var which will be
#               replaced with the previous environment
#     """
#     env = os.environ if _env is None else _env
#     before = dict(env)
# 
#     for k, v in patch:
#         if v is UNSET:
#             env.pop(k, None)
#         elif isinstance(v, tuple):
#             env[k] = format_env(v, before)
#         else:
#             env[k] = v
# 
#     try:
#         yield
#     finally:
#         env.clear()
#         env.update(before)
# 
# Path: pre_commit/util.py
# def cmd_output(*cmd: str, **kwargs: Any) -> tuple[int, str, str | None]:
#     returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
#     stdout = stdout_b.decode() if stdout_b is not None else None
#     stderr = stderr_b.decode() if stderr_b is not None else None
#     return returncode, stdout, stderr
# 
# Path: pre_commit/util.py
# def make_executable(filename: str) -> None:
#     original_mode = os.stat(filename).st_mode
#     new_mode = original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
#     os.chmod(filename, new_mode)
# 
# Path: testing/fixtures.py
# def git_dir(tempdir_factory):
#     path = tempdir_factory.get()
#     cmd_output('git', 'init', path)
#     return path
# 
# Path: testing/fixtures.py
# def sample_local_config():
#     return {
#         'repo': 'local',
#         'hooks': [{
#             'id': 'do_not_commit',
#             'name': 'Block if "DO NOT COMMIT" is found',
#             'entry': 'DO NOT COMMIT',
#             'language': 'pygrep',
#         }],
#     }
# 
# Path: testing/fixtures.py
# def write_config(directory, config, config_file=C.CONFIG_FILE):
#     if type(config) is not list and 'repos' not in config:
#         assert isinstance(config, dict), config
#         config = {'repos': [config]}
#     with open(os.path.join(directory, config_file), 'w') as outfile:
#         outfile.write(yaml_dump(config))
# 
# Path: testing/util.py
# @contextlib.contextmanager
# def cwd(path):
#     original_cwd = os.getcwd()
#     os.chdir(path)
#     try:
#         yield
#     finally:
#         os.chdir(original_cwd)
# 
# Path: testing/util.py
# def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs):
#     kwargs.setdefault('stderr', subprocess.STDOUT)
# 
#     cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', *args)
#     if all_files:  # allow skipping `-a` with `all_files=False`
#         cmd += ('-a',)
#     if msg is not None:  # allow skipping `-m` with `msg=None`
#         cmd += ('-m', msg)
#     ret, out, _ = fn(*cmd, **kwargs)
#     return ret, out.replace('\r\n', '\n')


import subprocess
import sys
import pytest
import pre_commit.constants as C
from unittest import mock
from pre_commit import git
from pre_commit.commands import hook_impl
from pre_commit.envcontext import envcontext
from pre_commit.util import cmd_output
from pre_commit.util import make_executable
from testing.fixtures import git_dir
from testing.fixtures import sample_local_config
from testing.fixtures import write_config
from testing.util import cwd
from testing.util import git_commit
from __future__ import annotations





def test_validate_config_file_exists(tmpdir):
    cfg = tmpdir.join(C.CONFIG_FILE).ensure()

# Path: pre_commit/git.py
# NO_FS_MONITOR = ('-c', 'core.useBuiltinFSMonitor=false')
# def zsplit(s: str) -> list[str]:
# def no_git_env(
#         _env: MutableMapping[str, str] | None = None,
# ) -> dict[str, str]:
# def get_root() -> str:
# def get_git_dir(git_root: str = '.') -> str:
# def get_remote_url(git_root: str) -> str:
# def is_in_merge_conflict() -> bool:
# def parse_merge_msg_for_conflicts(merge_msg: bytes) -> list[str]:
# def get_conflicted_files() -> set[str]:
# def get_staged_files(cwd: str | None = None) -> list[str]:
# def intent_to_add_files() -> list[str]:
# def get_all_files() -> list[str]:
# def get_changed_files(old: str, new: str) -> list[str]:
# def head_rev(remote: str) -> str:
# def has_diff(*args: str, repo: str = '.') -> bool:
# def has_core_hookpaths_set() -> bool:
# def init_repo(path: str, remote: str) -> None:
# def commit(repo: str = '.') -> None:
# def git_path(name: str, repo: str = '.') -> str:
# def check_for_cygwin_mismatch() -> None:
# 
# Path: pre_commit/commands/hook_impl.py
# def hook_impl(
#         store: Store,
#         *,
#         config: str,
#         color: bool,
#         hook_type: str,
#         hook_dir: str,
#         skip_on_missing_config: bool,
#         args: Sequence[str],
# ) -> int:
#     retv, stdin = _run_legacy(hook_type, hook_dir, args)
#     _validate_config(retv, config, skip_on_missing_config)
#     ns = _run_ns(hook_type, color, args, stdin)
#     if ns is None:
#         return retv
#     else:
#         return retv | run(config, store, ns)
# 
# Path: pre_commit/envcontext.py
# @contextlib.contextmanager
# def envcontext(
#         patch: PatchesT,
#         _env: MutableMapping[str, str] | None = None,
# ) -> Generator[None, None, None]:
#     """In this context, `os.environ` is modified according to `patch`.
# 
#     `patch` is an iterable of 2-tuples (key, value):
#         `key`: string
#         `value`:
#             - string: `environ[key] == value` inside the context.
#             - UNSET: `key not in environ` inside the context.
#             - template: A template is a tuple of strings and Var which will be
#               replaced with the previous environment
#     """
#     env = os.environ if _env is None else _env
#     before = dict(env)
# 
#     for k, v in patch:
#         if v is UNSET:
#             env.pop(k, None)
#         elif isinstance(v, tuple):
#             env[k] = format_env(v, before)
#         else:
#             env[k] = v
# 
#     try:
#         yield
#     finally:
#         env.clear()
#         env.update(before)
# 
# Path: pre_commit/util.py
# def cmd_output(*cmd: str, **kwargs: Any) -> tuple[int, str, str | None]:
#     returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
#     stdout = stdout_b.decode() if stdout_b is not None else None
#     stderr = stderr_b.decode() if stderr_b is not None else None
#     return returncode, stdout, stderr
# 
# Path: pre_commit/util.py
# def make_executable(filename: str) -> None:
#     original_mode = os.stat(filename).st_mode
#     new_mode = original_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
#     os.chmod(filename, new_mode)
# 
# Path: testing/fixtures.py
# def git_dir(tempdir_factory):
#     path = tempdir_factory.get()
#     cmd_output('git', 'init', path)
#     return path
# 
# Path: testing/fixtures.py
# def sample_local_config():
#     return {
#         'repo': 'local',
#         'hooks': [{
#             'id': 'do_not_commit',
#             'name': 'Block if "DO NOT COMMIT" is found',
#             'entry': 'DO NOT COMMIT',
#             'language': 'pygrep',
#         }],
#     }
# 
# Path: testing/fixtures.py
# def write_config(directory, config, config_file=C.CONFIG_FILE):
#     if type(config) is not list and 'repos' not in config:
#         assert isinstance(config, dict), config
#         config = {'repos': [config]}
#     with open(os.path.join(directory, config_file), 'w') as outfile:
#         outfile.write(yaml_dump(config))
# 
# Path: testing/util.py
# @contextlib.contextmanager
# def cwd(path):
#     original_cwd = os.getcwd()
#     os.chdir(path)
#     try:
#         yield
#     finally:
#         os.chdir(original_cwd)
# 
# Path: testing/util.py
# def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs):
#     kwargs.setdefault('stderr', subprocess.STDOUT)
# 
#     cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', *args)
#     if all_files:  # allow skipping `-a` with `all_files=False`
#         cmd += ('-a',)
#     if msg is not None:  # allow skipping `-m` with `msg=None`
#         cmd += ('-m', msg)
#     ret, out, _ = fn(*cmd, **kwargs)
#     return ret, out.replace('\r\n', '\n')


# Path: tests/commands/hook_impl_test.py
import subprocess
import sys
import pytest
import pre_commit.constants as C
from unittest import mock
from pre_commit import git
from pre_commit.commands import hook_impl
from pre_commit.envcontext import envcontext
from pre_commit.util import cmd_output
from pre_commit.util import make_executable
from testing.fixtures import git_dir
from testing.fixtures import sample_local_config
from testing.fixtures import write_config
from testing.util import cwd
from testing.util import git_commit

from __future__ import annotations





def test_validate_config_file_exists(tmpdir):
    cfg = tmpdir.join(C.CONFIG_FILE).ensure()

    hook_impl._validate_config(0, cfg, True)