Skip to content

Commit d6847c4

Browse files
committed
Add wildcard matching to no-commit-to-branch hook so that commits can
be blocked on, for example, all release branches with 'release/*'
1 parent aa9c202 commit d6847c4

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ 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]` to set the branch.
82+
- Use `args: [--branch, staging, --branch, master, --branch, release/*]` 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.

pre_commit_hooks/no_commit_to_branch.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import print_function
22

33
import argparse
4+
import fnmatch
45
from typing import Optional
56
from typing import Sequence
67
from typing import Set
@@ -11,11 +12,12 @@
1112

1213
def is_on_branch(protected): # type: (Set[str]) -> bool
1314
try:
14-
branch = cmd_output('git', 'symbolic-ref', 'HEAD')
15+
ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
1516
except CalledProcessError:
1617
return False
17-
chunks = branch.strip().split('/')
18-
return '/'.join(chunks[2:]) in protected
18+
chunks = ref_name.strip().split('/')
19+
branch_name = '/'.join(chunks[2:])
20+
return any(fnmatch.fnmatch(branch_name, s) for s in protected)
1921

2022

2123
def main(argv=None): # type: (Optional[Sequence[str]]) -> int

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_wildcard_fail(temp_git_dir):
48+
with temp_git_dir.as_cwd():
49+
cmd_output('git', 'checkout', '-b', 'another/branch')
50+
assert is_on_branch({'another/*'}) is True
51+
52+
53+
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
54+
def test_branch_wildcard_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', '--branch', '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)