mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +00:00
Add check branch meets naming convention hook
This commit is contained in:
parent
c03226380c
commit
7abaf07fb5
1 changed files with 47 additions and 0 deletions
47
pre_commit_hooks/check_branch_meets_naming_convention.py
Normal file
47
pre_commit_hooks/check_branch_meets_naming_convention.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import re
|
||||
from typing import AbstractSet
|
||||
from typing import Sequence
|
||||
|
||||
from pre_commit_hooks.util import CalledProcessError
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
def has_naming_convention(
|
||||
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 any(
|
||||
re.match(p, branch_name) for p in patterns
|
||||
)
|
||||
|
||||
|
||||
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 allow commits to, '
|
||||
'may be specified multiple times'
|
||||
),
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
patterns = frozenset(args.pattern or ())
|
||||
|
||||
return int(has_naming_convention(patterns))
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue