blob: 31310595d9272bccf5fd1810fe22434e2a9f87a8 [file] [log] [blame]
from __future__ import print_function
import argparse
import re
from typing import AbstractSet
from typing import Optional
from typing import Sequence
from pre_commit_hooks.util import CalledProcessError
from pre_commit_hooks.util import cmd_output
def is_on_branch(protected, patterns=frozenset()):
# type: (AbstractSet[str], AbstractSet[str]) -> bool
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
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
parser = argparse.ArgumentParser()
'-b', '--branch', action='append',
help='branch to disallow commits to, may be specified multiple times',
'-p', '--pattern', action='append',
'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',))
patterns = frozenset(args.pattern or ())
return int(is_on_branch(protected, patterns))
if __name__ == '__main__':