# 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/error_handler.py
# def _log_and_exit(
#     msg: str,
#     ret_code: int,
#     exc: BaseException,
#     formatted: str,
# ) -> None:
# def error_handler() -> Generator[None, None, None]:
# 
# 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: 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 os.path
import pytest
from pre_commit import git
from pre_commit.error_handler import FatalError
from pre_commit.util import cmd_output
from testing.util import git_commit
from __future__ import annotations





def test_get_root_at_root(in_git_dir):
    expected = os.path.normcase(in_git_dir.strpath)
    assert os.path.normcase(git.get_root()) == expected


def test_get_root_deeper(in_git_dir):
    expected = os.path.normcase(in_git_dir.strpath)
    with in_git_dir.join('foo').ensure_dir().as_cwd():
        assert os.path.normcase(git.get_root()) == expected


def test_in_exactly_dot_git(in_git_dir):
    with in_git_dir.join('.git').as_cwd(), pytest.raises(FatalError):
        git.get_root()


def test_get_root_bare_worktree(tmpdir):
    src = tmpdir.join('src').ensure_dir()

# 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/error_handler.py
# def _log_and_exit(
#     msg: str,
#     ret_code: int,
#     exc: BaseException,
#     formatted: str,
# ) -> None:
# def error_handler() -> Generator[None, None, None]:
# 
# 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: 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/git_test.py
import os.path
import pytest
from pre_commit import git
from pre_commit.error_handler import FatalError
from pre_commit.util import cmd_output
from testing.util import git_commit

from __future__ import annotations





def test_get_root_at_root(in_git_dir):
    expected = os.path.normcase(in_git_dir.strpath)
    assert os.path.normcase(git.get_root()) == expected


def test_get_root_deeper(in_git_dir):
    expected = os.path.normcase(in_git_dir.strpath)
    with in_git_dir.join('foo').ensure_dir().as_cwd():
        assert os.path.normcase(git.get_root()) == expected


def test_in_exactly_dot_git(in_git_dir):
    with in_git_dir.join('.git').as_cwd(), pytest.raises(FatalError):
        git.get_root()


def test_get_root_bare_worktree(tmpdir):
    src = tmpdir.join('src').ensure_dir()

    cmd_output('git', 'init', str(src))