Skip to content

Commit

Permalink
Merge pull request #376 from marcjay/no-commit-to-branch-wildcard
Browse files Browse the repository at this point in the history
Add regex matching to no-commit-to-branch hook
  • Loading branch information
asottile authored Apr 20, 2019
2 parents aa9c202 + 053feb1 commit e6e26c3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ Add this to your `.pre-commit-config.yaml`
- Use `args: ['--django']` to match `test*.py` instead.
- `no-commit-to-branch` - Protect specific branches from direct checkins.
- Use `args: [--branch, staging, --branch, master]` to set the branch.
`master` is the default if no argument is set.
`master` is the default if no branch argument is set.
- `-b` / `--branch` may be specified multiple times to protect multiple
branches.
- `-p` / `--pattern` can be used to protect branches that match a supplied regex
(e.g. `--pattern, release/.*`). May be specified multiple times.
- `pretty-format-json` - Checks that all your JSON files are pretty. "Pretty"
here means that keys are sorted and indented. You can configure this with
the following commandline options:
Expand Down
27 changes: 20 additions & 7 deletions pre_commit_hooks/no_commit_to_branch.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from __future__ import print_function

import argparse
import re
from typing import AbstractSet
from typing import Optional
from typing import Sequence
from typing import Set

from pre_commit_hooks.util import CalledProcessError
from pre_commit_hooks.util import cmd_output


def is_on_branch(protected): # type: (Set[str]) -> bool
def is_on_branch(protected, patterns=frozenset()):
# type: (AbstractSet[str], AbstractSet[str]) -> bool
try:
branch = cmd_output('git', 'symbolic-ref', 'HEAD')
ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
except CalledProcessError:
return False
chunks = branch.strip().split('/')
return '/'.join(chunks[2:]) in protected
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=None): # type: (Optional[Sequence[str]]) -> int
Expand All @@ -24,10 +29,18 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
'-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 = set(args.branch or ('master',))
return int(is_on_branch(protected))
protected = frozenset(args.branch or ('master',))
patterns = frozenset(args.pattern or ())
return int(is_on_branch(protected, patterns))


if __name__ == '__main__':
Expand Down
13 changes: 13 additions & 0 deletions tests/no_commit_to_branch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ def test_forbid_multiple_branches(temp_git_dir, branch_name):
assert main(('--branch', 'b1', '--branch', 'b2'))


def test_branch_pattern_fail(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'another/branch')
assert is_on_branch(set(), {'another/.*'}) is True


@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', branch_name)
assert main(('--branch', 'master', '--pattern', 'another/.*'))


def test_main_default_call(temp_git_dir):
with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'anotherbranch')
Expand Down

0 comments on commit e6e26c3

Please sign in to comment.