# Path: pre_commit_hooks/no_commit_to_branch.py
# def is_on_branch(
#         protected: AbstractSet[str],
#         patterns: AbstractSet[str] = frozenset(),
# ) -> bool:
#     try:
#         ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
#     except CalledProcessError:
#         return False
#     chunks = ref_name.strip().split('/')
#     branch_name = '/'.join(chunks[2:])
#     return branch_name in protected or any(
#         re.match(p, branch_name) for p in patterns
#     )
# 
# Path: pre_commit_hooks/no_commit_to_branch.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument(
#         '-b', '--branch', action='append',
#         help='branch to disallow commits to, may be specified multiple times',
#     )
#     parser.add_argument(
#         '-p', '--pattern', action='append',
#         help=(
#             'regex pattern for branch name to disallow commits to, '
#             'may be specified multiple times'
#         ),
#     )
#     args = parser.parse_args(argv)
# 
#     protected = frozenset(args.branch or ('master', 'main'))
#     patterns = frozenset(args.pattern or ())
#     return int(is_on_branch(protected, patterns))
# 
# 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 pytest
from pre_commit_hooks.no_commit_to_branch import is_on_branch
from pre_commit_hooks.no_commit_to_branch import main
from pre_commit_hooks.util import cmd_output
from testing.util import git_commit


@pytest.mark.parametrize('branch_name', ('b1', 'b2'))
def test_forbid_multiple_branches(temp_git_dir, branch_name):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', branch_name)
        assert main(('--branch', 'b1', '--branch', 'b2'))


def test_branch_pattern_fail(temp_git_dir):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', 'another/branch')
        assert is_on_branch(set(), {'another/.*'}) is True


@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', branch_name)
        assert main(('--branch', 'master', '--pattern', 'another/.*'))


def test_main_default_call(temp_git_dir):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', 'anotherbranch')
        assert main(()) == 0


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

# Path: pre_commit_hooks/no_commit_to_branch.py
# def is_on_branch(
#         protected: AbstractSet[str],
#         patterns: AbstractSet[str] = frozenset(),
# ) -> bool:
#     try:
#         ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
#     except CalledProcessError:
#         return False
#     chunks = ref_name.strip().split('/')
#     branch_name = '/'.join(chunks[2:])
#     return branch_name in protected or any(
#         re.match(p, branch_name) for p in patterns
#     )
# 
# Path: pre_commit_hooks/no_commit_to_branch.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser()
#     parser.add_argument(
#         '-b', '--branch', action='append',
#         help='branch to disallow commits to, may be specified multiple times',
#     )
#     parser.add_argument(
#         '-p', '--pattern', action='append',
#         help=(
#             'regex pattern for branch name to disallow commits to, '
#             'may be specified multiple times'
#         ),
#     )
#     args = parser.parse_args(argv)
# 
#     protected = frozenset(args.branch or ('master', 'main'))
#     patterns = frozenset(args.pattern or ())
#     return int(is_on_branch(protected, patterns))
# 
# 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/no_commit_to_branch_test.py
import pytest
from pre_commit_hooks.no_commit_to_branch import is_on_branch
from pre_commit_hooks.no_commit_to_branch import main
from pre_commit_hooks.util import cmd_output
from testing.util import git_commit



@pytest.mark.parametrize('branch_name', ('b1', 'b2'))
def test_forbid_multiple_branches(temp_git_dir, branch_name):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', branch_name)
        assert main(('--branch', 'b1', '--branch', 'b2'))


def test_branch_pattern_fail(temp_git_dir):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', 'another/branch')
        assert is_on_branch(set(), {'another/.*'}) is True


@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', branch_name)
        assert main(('--branch', 'master', '--pattern', 'another/.*'))


def test_main_default_call(temp_git_dir):
    with temp_git_dir.as_cwd():
        cmd_output('git', 'checkout', '-b', 'anotherbranch')
        assert main(()) == 0


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

        git_commit('--allow-empty', '-m1')