Skip to content

Commit a193eab

Browse files
authored
Merge pull request #294 from pre-commit/more_protected_branches
Allow multiple branches to be protected
2 parents fa7a3b5 + baec308 commit a193eab

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ Add this to your `.pre-commit-config.yaml`
8282
- `name-tests-test` - Assert that files in tests/ end in `_test.py`.
8383
- Use `args: ['--django']` to match `test*.py` instead.
8484
- `no-commit-to-branch` - Protect specific branches from direct checkins.
85-
- Use `args: -b <branch> ` to set the branch. `master` is the default if no argument is set.
85+
- Use `args: [--branch <branch>]` to set the branch. `master` is the
86+
default if no argument is set.
87+
- `-b` / `--branch` may be specified multiple times to protect multiple
88+
branches.
8689
- `pyflakes` - Run pyflakes on your python files.
8790
- `pretty-format-json` - Checks that all your JSON files are pretty. "Pretty"
8891
here means that keys are sorted and indented. You can configure this with

pre_commit_hooks/no_commit_to_branch.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ def is_on_branch(protected):
1212
except CalledProcessError:
1313
return False
1414
chunks = branch.strip().split('/')
15-
return '/'.join(chunks[2:]) == protected
15+
return '/'.join(chunks[2:]) in protected
1616

1717

1818
def main(argv=None):
1919
parser = argparse.ArgumentParser()
2020
parser.add_argument(
21-
'-b', '--branch', default='master',
22-
help='branch to disallow commits to',
21+
'-b', '--branch', action='append',
22+
help='branch to disallow commits to, may be specified multiple times',
2323
)
2424
args = parser.parse_args(argv)
2525

26-
return int(is_on_branch(args.branch))
26+
protected = set(args.branch or ('master',))
27+
return int(is_on_branch(protected))
2728

2829

2930
if __name__ == '__main__':
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4+
import pytest
5+
46
from pre_commit_hooks.no_commit_to_branch import is_on_branch
57
from pre_commit_hooks.no_commit_to_branch import main
68
from pre_commit_hooks.util import cmd_output
@@ -9,24 +11,24 @@
911
def test_other_branch(temp_git_dir):
1012
with temp_git_dir.as_cwd():
1113
cmd_output('git', 'checkout', '-b', 'anotherbranch')
12-
assert is_on_branch('master') is False
14+
assert is_on_branch(('master',)) is False
1315

1416

1517
def test_multi_branch(temp_git_dir):
1618
with temp_git_dir.as_cwd():
1719
cmd_output('git', 'checkout', '-b', 'another/branch')
18-
assert is_on_branch('master') is False
20+
assert is_on_branch(('master',)) is False
1921

2022

2123
def test_multi_branch_fail(temp_git_dir):
2224
with temp_git_dir.as_cwd():
2325
cmd_output('git', 'checkout', '-b', 'another/branch')
24-
assert is_on_branch('another/branch') is True
26+
assert is_on_branch(('another/branch',)) is True
2527

2628

2729
def test_master_branch(temp_git_dir):
2830
with temp_git_dir.as_cwd():
29-
assert is_on_branch('master') is True
31+
assert is_on_branch(('master',)) is True
3032

3133

3234
def test_main_branch_call(temp_git_dir):
@@ -35,6 +37,13 @@ def test_main_branch_call(temp_git_dir):
3537
assert main(('--branch', 'other')) == 1
3638

3739

40+
@pytest.mark.parametrize('branch_name', ('b1', 'b2'))
41+
def test_forbid_multiple_branches(temp_git_dir, branch_name):
42+
with temp_git_dir.as_cwd():
43+
cmd_output('git', 'checkout', '-b', branch_name)
44+
assert main(('--branch', 'b1', '--branch', 'b2'))
45+
46+
3847
def test_main_default_call(temp_git_dir):
3948
with temp_git_dir.as_cwd():
4049
cmd_output('git', 'checkout', '-b', 'anotherbranch')

0 commit comments

Comments
 (0)