# Path: pre_commit_hooks/check_merge_conflict.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument('filenames', nargs='*')
#     parser.add_argument('--assume-in-merge', action='store_true')
#     args = parser.parse_args(argv)
# 
#     if not is_in_merge() and not args.assume_in_merge:
#         return 0
# 
#     retcode = 0
#     for filename in args.filenames:
#         with open(filename, 'rb') as inputfile:
#             for i, line in enumerate(inputfile):
#                 for pattern in CONFLICT_PATTERNS:
#                     if line.startswith(pattern):
#                         print(
#                             f'Merge conflict string "{pattern.decode()}" '
#                             f'found in {filename}:{i + 1}',
#                         )
#                         retcode = 1
# 
#     return retcode
# 
# 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 get_resource_path(path):
#     return os.path.join(TESTING_DIR, 'resources', path)
# 
# 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 os
import shutil
import pytest
from pre_commit_hooks.check_merge_conflict import main
from pre_commit_hooks.util import cmd_output
from testing.util import get_resource_path
from testing.util import git_commit
        # We should end up in a pending merge
        assert repo2_f1.read() == 'parent\n'
        assert repo2_f2.read() == 'child\n'
        assert os.path.exists(os.path.join('.git', 'MERGE_HEAD'))
        yield repo2


@pytest.mark.usefixtures('f1_is_a_conflict_file')
def test_merge_conflicts_git():
    assert main(['f1']) == 1


@pytest.mark.parametrize(
    'contents', (b'<<<<<<< HEAD\n', b'=======\n', b'>>>>>>> master\n'),
)
def test_merge_conflicts_failing(contents, repository_pending_merge):
    repository_pending_merge.join('f2').write_binary(contents)
    assert main(['f2']) == 1


@pytest.mark.parametrize(
    'contents', (b'# <<<<<<< HEAD\n', b'# =======\n', b'import mod', b''),
)
def test_merge_conflicts_ok(contents, f1_is_a_conflict_file):
    f1_is_a_conflict_file.join('f1').write_binary(contents)
    assert main(['f1']) == 0


@pytest.mark.usefixtures('f1_is_a_conflict_file')
def test_ignores_binary_files():

# Path: pre_commit_hooks/check_merge_conflict.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument('filenames', nargs='*')
#     parser.add_argument('--assume-in-merge', action='store_true')
#     args = parser.parse_args(argv)
# 
#     if not is_in_merge() and not args.assume_in_merge:
#         return 0
# 
#     retcode = 0
#     for filename in args.filenames:
#         with open(filename, 'rb') as inputfile:
#             for i, line in enumerate(inputfile):
#                 for pattern in CONFLICT_PATTERNS:
#                     if line.startswith(pattern):
#                         print(
#                             f'Merge conflict string "{pattern.decode()}" '
#                             f'found in {filename}:{i + 1}',
#                         )
#                         retcode = 1
# 
#     return retcode
# 
# 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 get_resource_path(path):
#     return os.path.join(TESTING_DIR, 'resources', path)
# 
# 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_merge_conflict_test.py
import os
import shutil
import pytest
from pre_commit_hooks.check_merge_conflict import main
from pre_commit_hooks.util import cmd_output
from testing.util import get_resource_path
from testing.util import git_commit

        # We should end up in a pending merge
        assert repo2_f1.read() == 'parent\n'
        assert repo2_f2.read() == 'child\n'
        assert os.path.exists(os.path.join('.git', 'MERGE_HEAD'))
        yield repo2


@pytest.mark.usefixtures('f1_is_a_conflict_file')
def test_merge_conflicts_git():
    assert main(['f1']) == 1


@pytest.mark.parametrize(
    'contents', (b'<<<<<<< HEAD\n', b'=======\n', b'>>>>>>> master\n'),
)
def test_merge_conflicts_failing(contents, repository_pending_merge):
    repository_pending_merge.join('f2').write_binary(contents)
    assert main(['f2']) == 1


@pytest.mark.parametrize(
    'contents', (b'# <<<<<<< HEAD\n', b'# =======\n', b'import mod', b''),
)
def test_merge_conflicts_ok(contents, f1_is_a_conflict_file):
    f1_is_a_conflict_file.join('f1').write_binary(contents)
    assert main(['f1']) == 0


@pytest.mark.usefixtures('f1_is_a_conflict_file')
def test_ignores_binary_files():

    shutil.copy(get_resource_path('img1.jpg'), 'f1')