Adding upstream version 4.5.0+dfsg.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
27cd5628db
commit
6bd375ed5f
108 changed files with 6514 additions and 0 deletions
48
pre_commit_hooks/no_commit_to_branch.py
Normal file
48
pre_commit_hooks/no_commit_to_branch.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
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 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
|
||||
)
|
||||
|
||||
|
||||
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))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
Loading…
Add table
Add a link
Reference in a new issue