Skip to content

Commit e6e26c3

Browse files
authored
Merge pull request #376 from marcjay/no-commit-to-branch-wildcard
Add regex matching to no-commit-to-branch hook
2 parents aa9c202 + 053feb1 commit e6e26c3

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ Add this to your `.pre-commit-config.yaml`
8080
- Use `args: ['--django']` to match `test*.py` instead.
8181
- `no-commit-to-branch` - Protect specific branches from direct checkins.
8282
- Use `args: [--branch, staging, --branch, master]` to set the branch.
83-
`master` is the default if no argument is set.
83+
`master` is the default if no branch argument is set.
8484
- `-b` / `--branch` may be specified multiple times to protect multiple
8585
branches.
86+
- `-p` / `--pattern` can be used to protect branches that match a supplied regex
87+
(e.g. `--pattern, release/.*`). May be specified multiple times.
8688
- `pretty-format-json` - Checks that all your JSON files are pretty. "Pretty"
8789
here means that keys are sorted and indented. You can configure this with
8890
the following commandline options:

pre_commit_hooks/no_commit_to_branch.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
from __future__ import print_function
22

33
import argparse
4+
import re
5+
from typing import AbstractSet
46
from typing import Optional
57
from typing import Sequence
6-
from typing import Set
78

89
from pre_commit_hooks.util import CalledProcessError
910
from pre_commit_hooks.util import cmd_output
1011

1112

12-
def is_on_branch(protected): # type: (Set[str]) -> bool
13+
def is_on_branch(protected, patterns=frozenset()):
14+
# type: (AbstractSet[str], AbstractSet[str]) -> bool
1315
try:
14-
branch = cmd_output('git', 'symbolic-ref', 'HEAD')
16+
ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
1517
except CalledProcessError:
1618
return False
17-
chunks = branch.strip().split('/')
18-
return '/'.join(chunks[2:]) in protected
19+
chunks = ref_name.strip().split('/')
20+
branch_name = '/'.join(chunks[2:])
21+
return branch_name in protected or any(
22+
re.match(p, branch_name) for p in patterns
23+
)
1924

2025

2126
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
@@ -24,10 +29,18 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
2429
'-b', '--branch', action='append',
2530
help='branch to disallow commits to, may be specified multiple times',
2631
)
32+
parser.add_argument(
33+
'-p', '--pattern', action='append',
34+
help=(
35+
'regex pattern for branch name to disallow commits to, '
36+
'may be specified multiple times'
37+
),
38+
)
2739
args = parser.parse_args(argv)
2840

29-
protected = set(args.branch or ('master',))
30-
return int(is_on_branch(protected))
41+
protected = frozenset(args.branch or ('master',))
42+
patterns = frozenset(args.pattern or ())
43+
return int(is_on_branch(protected, patterns))
3144

3245

3346
if __name__ == '__main__':

tests/no_commit_to_branch_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ def test_forbid_multiple_branches(temp_git_dir, branch_name):
4444
assert main(('--branch', 'b1', '--branch', 'b2'))
4545

4646

47+
def test_branch_pattern_fail(temp_git_dir):
48+
with temp_git_dir.as_cwd():
49+
cmd_output('git', 'checkout', '-b', 'another/branch')
50+
assert is_on_branch(set(), {'another/.*'}) is True
51+
52+
53+
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
54+
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
55+
with temp_git_dir.as_cwd():
56+
cmd_output('git', 'checkout', '-b', branch_name)
57+
assert main(('--branch', 'master', '--pattern', 'another/.*'))
58+
59+
4760
def test_main_default_call(temp_git_dir):
4861
with temp_git_dir.as_cwd():
4962
cmd_output('git', 'checkout', '-b', 'anotherbranch')

0 commit comments

Comments
 (0)