Skip to content

Commit 8d2785b

Browse files
committed
Amend approach for no-commit-to-branch to use regex matching based on
feedback. Adds --pattern optional argument which can be used alongside --branch to block commits to a branch which matches a supplied regex expression
1 parent d6847c4 commit 8d2785b

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ Add this to your `.pre-commit-config.yaml`
7979
- `name-tests-test` - Assert that files in tests/ end in `_test.py`.
8080
- Use `args: ['--django']` to match `test*.py` instead.
8181
- `no-commit-to-branch` - Protect specific branches from direct checkins.
82-
- Use `args: [--branch, staging, --branch, master, --branch, release/*]` to set the branch.
82+
- Use `args: [--branch, staging, --branch, master]` to set the branch.
8383
`master` is the default if no 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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import print_function
22

33
import argparse
4-
import fnmatch
4+
import re
55
from typing import Optional
66
from typing import Sequence
77
from typing import Set
@@ -10,14 +10,17 @@
1010
from pre_commit_hooks.util import cmd_output
1111

1212

13-
def is_on_branch(protected): # type: (Set[str]) -> bool
13+
def is_on_branch(protected, patterns=set()):
14+
# type: (Set[str], Set[str]) -> bool
1415
try:
1516
ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
1617
except CalledProcessError:
1718
return False
1819
chunks = ref_name.strip().split('/')
1920
branch_name = '/'.join(chunks[2:])
20-
return any(fnmatch.fnmatch(branch_name, s) for s in protected)
21+
return branch_name in protected or any(
22+
re.match(p, branch_name) for p in patterns
23+
)
2124

2225

2326
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
@@ -26,10 +29,18 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
2629
'-b', '--branch', action='append',
2730
help='branch to disallow commits to, may be specified multiple times',
2831
)
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+
)
2939
args = parser.parse_args(argv)
3040

3141
protected = set(args.branch or ('master',))
32-
return int(is_on_branch(protected))
42+
patterns = set(args.pattern or ())
43+
return int(is_on_branch(protected, patterns))
3344

3445

3546
if __name__ == '__main__':

tests/no_commit_to_branch_test.py

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

4646

47-
def test_branch_wildcard_fail(temp_git_dir):
47+
def test_branch_pattern_fail(temp_git_dir):
4848
with temp_git_dir.as_cwd():
4949
cmd_output('git', 'checkout', '-b', 'another/branch')
50-
assert is_on_branch({'another/*'}) is True
50+
assert is_on_branch(set(), {'another/.*'}) is True
5151

5252

5353
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
54-
def test_branch_wildcard_multiple_branches_fail(temp_git_dir, branch_name):
54+
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
5555
with temp_git_dir.as_cwd():
5656
cmd_output('git', 'checkout', '-b', branch_name)
57-
assert main(('--branch', 'master', '--branch', 'another/*'))
57+
assert main(('--branch', 'master', '--pattern', 'another/.*'))
5858

5959

6060
def test_main_default_call(temp_git_dir):

0 commit comments

Comments
 (0)