# Path: pre_commit/errors.py
# class FatalError(RuntimeError):
#     pass
# 
# Path: pre_commit/util.py
# class CalledProcessError(RuntimeError):
#     def __init__(
#             self,
#             returncode: int,
#             cmd: tuple[str, ...],
#             expected_returncode: int,
#             stdout: bytes,
#             stderr: bytes | None,
#     ) -> None:
#         super().__init__(returncode, cmd, expected_returncode, stdout, stderr)
#         self.returncode = returncode
#         self.cmd = cmd
#         self.expected_returncode = expected_returncode
#         self.stdout = stdout
#         self.stderr = stderr
# 
#     def __bytes__(self) -> bytes:
#         def _indent_or_none(part: bytes | None) -> bytes:
#             if part:
#                 return b'\n    ' + part.replace(b'\n', b'\n    ')
#             else:
#                 return b' (none)'
# 
#         return b''.join((
#             f'command: {self.cmd!r}\n'.encode(),
#             f'return code: {self.returncode}\n'.encode(),
#             f'expected return code: {self.expected_returncode}\n'.encode(),
#             b'stdout:', _indent_or_none(self.stdout), b'\n',
#             b'stderr:', _indent_or_none(self.stderr),
#         ))
# 
#     def __str__(self) -> str:
#         return self.__bytes__().decode()
# 
# 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 cmd_output_b(
#         *cmd: str,
#         retcode: int | None = 0,
#         **kwargs: Any,
# ) -> tuple[int, bytes, bytes | None]:
#     _setdefault_kwargs(kwargs)
# 
#     try:
#         cmd = parse_shebang.normalize_cmd(cmd)
#     except parse_shebang.ExecutableNotFoundError as e:
#         returncode, stdout_b, stderr_b = e.to_output()
#     else:
#         try:
#             proc = subprocess.Popen(cmd, **kwargs)
#         except OSError as e:
#             returncode, stdout_b, stderr_b = _oserror_to_output(e)
#         else:
#             stdout_b, stderr_b = proc.communicate()
#             returncode = proc.returncode
# 
#     if retcode is not None and retcode != returncode:
#         raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
# 
#     return returncode, stdout_b, stderr_b


import logging
import os.path
import sys
from typing import MutableMapping
from pre_commit.errors import FatalError
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
from pre_commit.util import cmd_output_b
    # In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
    # pre-commit hooks
    # In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
    # while running pre-commit hooks in submodules.
    # GIT_DIR: Causes git clone to clone wrong thing
    # GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
    _env = _env if _env is not None else os.environ
    return {
        k: v for k, v in _env.items()
        if not k.startswith('GIT_') or
        k.startswith(('GIT_CONFIG_KEY_', 'GIT_CONFIG_VALUE_')) or
        k in {
            'GIT_EXEC_PATH', 'GIT_SSH', 'GIT_SSH_COMMAND', 'GIT_SSL_CAINFO',
            'GIT_SSL_NO_VERIFY', 'GIT_CONFIG_COUNT',
            'GIT_HTTP_PROXY_AUTHMETHOD',
        }
    }


def get_root() -> str:
    # Git 2.25 introduced a change to "rev-parse --show-toplevel" that exposed
    # underlying volumes for Windows drives mapped with SUBST.  We use
    # "rev-parse --show-cdup" to get the appropriate path, but must perform
    # an extra check to see if we are in the .git directory.
    try:
        root = os.path.abspath(
            cmd_output('git', 'rev-parse', '--show-cdup')[1].strip(),
        )
        git_dir = os.path.abspath(get_git_dir())
    except CalledProcessError:

# Path: pre_commit/errors.py
# class FatalError(RuntimeError):
#     pass
# 
# Path: pre_commit/util.py
# class CalledProcessError(RuntimeError):
#     def __init__(
#             self,
#             returncode: int,
#             cmd: tuple[str, ...],
#             expected_returncode: int,
#             stdout: bytes,
#             stderr: bytes | None,
#     ) -> None:
#         super().__init__(returncode, cmd, expected_returncode, stdout, stderr)
#         self.returncode = returncode
#         self.cmd = cmd
#         self.expected_returncode = expected_returncode
#         self.stdout = stdout
#         self.stderr = stderr
# 
#     def __bytes__(self) -> bytes:
#         def _indent_or_none(part: bytes | None) -> bytes:
#             if part:
#                 return b'\n    ' + part.replace(b'\n', b'\n    ')
#             else:
#                 return b' (none)'
# 
#         return b''.join((
#             f'command: {self.cmd!r}\n'.encode(),
#             f'return code: {self.returncode}\n'.encode(),
#             f'expected return code: {self.expected_returncode}\n'.encode(),
#             b'stdout:', _indent_or_none(self.stdout), b'\n',
#             b'stderr:', _indent_or_none(self.stderr),
#         ))
# 
#     def __str__(self) -> str:
#         return self.__bytes__().decode()
# 
# 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 cmd_output_b(
#         *cmd: str,
#         retcode: int | None = 0,
#         **kwargs: Any,
# ) -> tuple[int, bytes, bytes | None]:
#     _setdefault_kwargs(kwargs)
# 
#     try:
#         cmd = parse_shebang.normalize_cmd(cmd)
#     except parse_shebang.ExecutableNotFoundError as e:
#         returncode, stdout_b, stderr_b = e.to_output()
#     else:
#         try:
#             proc = subprocess.Popen(cmd, **kwargs)
#         except OSError as e:
#             returncode, stdout_b, stderr_b = _oserror_to_output(e)
#         else:
#             stdout_b, stderr_b = proc.communicate()
#             returncode = proc.returncode
# 
#     if retcode is not None and retcode != returncode:
#         raise CalledProcessError(returncode, cmd, retcode, stdout_b, stderr_b)
# 
#     return returncode, stdout_b, stderr_b


# Path: pre_commit/git.py
import logging
import os.path
import sys
from typing import MutableMapping
from pre_commit.errors import FatalError
from pre_commit.util import CalledProcessError
from pre_commit.util import cmd_output
from pre_commit.util import cmd_output_b

    # In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
    # pre-commit hooks
    # In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
    # while running pre-commit hooks in submodules.
    # GIT_DIR: Causes git clone to clone wrong thing
    # GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
    _env = _env if _env is not None else os.environ
    return {
        k: v for k, v in _env.items()
        if not k.startswith('GIT_') or
        k.startswith(('GIT_CONFIG_KEY_', 'GIT_CONFIG_VALUE_')) or
        k in {
            'GIT_EXEC_PATH', 'GIT_SSH', 'GIT_SSH_COMMAND', 'GIT_SSL_CAINFO',
            'GIT_SSL_NO_VERIFY', 'GIT_CONFIG_COUNT',
            'GIT_HTTP_PROXY_AUTHMETHOD',
        }
    }


def get_root() -> str:
    # Git 2.25 introduced a change to "rev-parse --show-toplevel" that exposed
    # underlying volumes for Windows drives mapped with SUBST.  We use
    # "rev-parse --show-cdup" to get the appropriate path, but must perform
    # an extra check to see if we are in the .git directory.
    try:
        root = os.path.abspath(
            cmd_output('git', 'rev-parse', '--show-cdup')[1].strip(),
        )
        git_dir = os.path.abspath(get_git_dir())
    except CalledProcessError:

        raise FatalError(