# Path: pre_commit_hooks/check_case_conflict.py
# def find_conflicting_filenames(filenames: Sequence[str]) -> int:
#     repo_files = set(cmd_output('git', 'ls-files').splitlines())
#     repo_files |= directories_for(repo_files)
#     relevant_files = set(filenames) | added_files()
#     relevant_files |= directories_for(relevant_files)
#     repo_files -= relevant_files
#     retv = 0
# 
#     # new file conflicts with existing file
#     conflicts = lower_set(repo_files) & lower_set(relevant_files)
# 
#     # new file conflicts with other new file
#     lowercase_relevant_files = lower_set(relevant_files)
#     for filename in set(relevant_files):
#         if filename.lower() in lowercase_relevant_files:
#             lowercase_relevant_files.remove(filename.lower())
#         else:
#             conflicts.add(filename.lower())
# 
#     if conflicts:
#         conflicting_files = [
#             x for x in repo_files | relevant_files
#             if x.lower() in conflicts
#         ]
#         for filename in sorted(conflicting_files):
#             print(f'Case-insensitivity conflict found: {filename}')
#         retv = 1
# 
#     return retv
# 
# Path: pre_commit_hooks/check_case_conflict.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument(
#         'filenames', nargs='*',
#         help='Filenames pre-commit believes are changed.',
#     )
# 
#     args = parser.parse_args(argv)
# 
#     return find_conflicting_filenames(args.filenames)
# 
# Path: pre_commit_hooks/check_case_conflict.py
# def parents(file: str) -> Iterator[str]:
#     path_parts = file.split('/')
#     path_parts.pop()
#     while path_parts:
#         yield '/'.join(path_parts)
#         path_parts.pop()
# 
# Path: pre_commit_hooks/util.py
# def cmd_output(*cmd: str, retcode: int | None = 0, **kwargs: Any) -> str:
#     kwargs.setdefault('stdout', subprocess.PIPE)
#     kwargs.setdefault('stderr', subprocess.PIPE)
#     proc = subprocess.Popen(cmd, **kwargs)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode()
#     if retcode is not None and proc.returncode != retcode:
#         raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
#     return stdout
# 
# Path: testing/util.py
# def git_commit(*args, **kwargs):
#     cmd = ('git', 'commit', '--no-gpg-sign', '--no-verify', '--no-edit', *args)
#     subprocess.check_call(cmd, **kwargs)


import sys
import pytest
from pre_commit_hooks.check_case_conflict import find_conflicting_filenames
from pre_commit_hooks.check_case_conflict import main
from pre_commit_hooks.check_case_conflict import parents
from pre_commit_hooks.util import cmd_output
from testing.util import git_commit
from __future__ import annotations




skip_win32 = pytest.mark.skipif(
    sys.platform == 'win32',
    reason='case conflicts between directories and files',
)


def test_parents():
    assert set(parents('a')) == set()
    assert set(parents('a/b')) == {'a'}
    assert set(parents('a/b/c')) == {'a/b', 'a'}
    assert set(parents('a/b/c/d')) == {'a/b/c', 'a/b', 'a'}


def test_nothing_added(temp_git_dir):
    with temp_git_dir.as_cwd():

# Path: pre_commit_hooks/check_case_conflict.py
# def find_conflicting_filenames(filenames: Sequence[str]) -> int:
#     repo_files = set(cmd_output('git', 'ls-files').splitlines())
#     repo_files |= directories_for(repo_files)
#     relevant_files = set(filenames) | added_files()
#     relevant_files |= directories_for(relevant_files)
#     repo_files -= relevant_files
#     retv = 0
# 
#     # new file conflicts with existing file
#     conflicts = lower_set(repo_files) & lower_set(relevant_files)
# 
#     # new file conflicts with other new file
#     lowercase_relevant_files = lower_set(relevant_files)
#     for filename in set(relevant_files):
#         if filename.lower() in lowercase_relevant_files:
#             lowercase_relevant_files.remove(filename.lower())
#         else:
#             conflicts.add(filename.lower())
# 
#     if conflicts:
#         conflicting_files = [
#             x for x in repo_files | relevant_files
#             if x.lower() in conflicts
#         ]
#         for filename in sorted(conflicting_files):
#             print(f'Case-insensitivity conflict found: {filename}')
#         retv = 1
# 
#     return retv
# 
# Path: pre_commit_hooks/check_case_conflict.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument(
#         'filenames', nargs='*',
#         help='Filenames pre-commit believes are changed.',
#     )
# 
#     args = parser.parse_args(argv)
# 
#     return find_conflicting_filenames(args.filenames)
# 
# Path: pre_commit_hooks/check_case_conflict.py
# def parents(file: str) -> Iterator[str]:
#     path_parts = file.split('/')
#     path_parts.pop()
#     while path_parts:
#         yield '/'.join(path_parts)
#         path_parts.pop()
# 
# Path: pre_commit_hooks/util.py
# def cmd_output(*cmd: str, retcode: int | None = 0, **kwargs: Any) -> str:
#     kwargs.setdefault('stdout', subprocess.PIPE)
#     kwargs.setdefault('stderr', subprocess.PIPE)
#     proc = subprocess.Popen(cmd, **kwargs)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode()
#     if retcode is not None and proc.returncode != retcode:
#         raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
#     return stdout
# 
# Path: testing/util.py
# def git_commit(*args, **kwargs):
#     cmd = ('git', 'commit', '--no-gpg-sign', '--no-verify', '--no-edit', *args)
#     subprocess.check_call(cmd, **kwargs)


# Path: tests/check_case_conflict_test.py
import sys
import pytest
from pre_commit_hooks.check_case_conflict import find_conflicting_filenames
from pre_commit_hooks.check_case_conflict import main
from pre_commit_hooks.check_case_conflict import parents
from pre_commit_hooks.util import cmd_output
from testing.util import git_commit

from __future__ import annotations




skip_win32 = pytest.mark.skipif(
    sys.platform == 'win32',
    reason='case conflicts between directories and files',
)


def test_parents():
    assert set(parents('a')) == set()
    assert set(parents('a/b')) == {'a'}
    assert set(parents('a/b/c')) == {'a/b', 'a'}
    assert set(parents('a/b/c/d')) == {'a/b/c', 'a/b', 'a'}


def test_nothing_added(temp_git_dir):
    with temp_git_dir.as_cwd():

        assert find_conflicting_filenames(['f.py']) == 0