Skip to content

Commit a859266

Browse files
karcawasottile
authored andcommitted
Add a no commit to specific branch hook. (#185)
* add no commit code and config * add the code * remove version tweak * fix logic, remove newline * add Tests and cleanup testing issues * remove extraneous modules * cleanup some pep8 and flake issues * reorder imports * more fixes for syntax checking * code cleanup based off asottile comments * Use Contractions Properly, alphabatize new hook. * Adding support for branches with a slash in them.
1 parent b95dcad commit a859266

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@
129129
entry: name-tests-test
130130
language: python
131131
files: tests/.+\.py$
132+
- id: no-commit-to-branch
133+
name: "Don't commit to branch"
134+
entry: no-commit-to-branch
135+
language: python
136+
files: .*
137+
always_run: true
132138
- id: pyflakes
133139
name: Pyflakes (DEPRECATED, use flake8)
134140
description: This hook runs pyflakes. (This is deprecated, use flake8).

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ Add this to your `.pre-commit-config.yaml`
5656
- `forbid-new-submodules` - Prevent addition of new git submodules.
5757
- `name-tests-test` - Assert that files in tests/ end in `_test.py`.
5858
- Use `args: ['--django']` to match `test*.py` instead.
59+
- `no-commit-to-branch` - Protect specific branches from direct checkins.
60+
- Use `args: -b <branch> ` to set the branch. `master` is the default if no argument is set.
5961
- `pyflakes` - Run pyflakes on your python files.
6062
- `pretty-format-json` - Checks that all your JSON files are pretty. "Pretty"
6163
here means that keys are sorted and indented. You can configure this with

hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@
129129
entry: name-tests-test
130130
language: python
131131
files: tests/.+\.py$
132+
- id: no-commit-to-branch
133+
name: "Don't commit to branch"
134+
entry: no-commit-to-branch
135+
language: python
136+
files: .*
137+
always_run: true
132138
- id: pyflakes
133139
name: Pyflakes (DEPRECATED, use flake8)
134140
description: This hook runs pyflakes. (This is deprecated, use flake8).
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import print_function
2+
3+
import argparse
4+
import sys
5+
6+
from pre_commit_hooks.util import cmd_output
7+
8+
9+
def is_on_branch(protected):
10+
branch = cmd_output('git', 'symbolic-ref', 'HEAD')
11+
chunks = branch.strip().split('/')
12+
return '/'.join(chunks[2:]) == protected
13+
14+
15+
def main(argv=[]):
16+
parser = argparse.ArgumentParser()
17+
parser.add_argument(
18+
'-b', '--branch', default='master', help='branch to disallow commits to')
19+
args = parser.parse_args(argv)
20+
21+
return int(is_on_branch(args.branch))
22+
23+
24+
if __name__ == '__main__':
25+
sys.exit(main(sys.argv))

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
'fix-encoding-pragma = pre_commit_hooks.fix_encoding_pragma:main',
5353
'forbid-new-submodules = pre_commit_hooks.forbid_new_submodules:main',
5454
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
55+
'no-commit-to-branch = pre_commit_hooks.no_commit_to_branch:main',
5556
'pretty-format-json = pre_commit_hooks.pretty_format_json:pretty_format_json',
5657
'requirements-txt-fixer = pre_commit_hooks.requirements_txt_fixer:fix_requirements_txt',
5758
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import absolute_import
2+
from __future__ import unicode_literals
3+
4+
from pre_commit_hooks.no_commit_to_branch import is_on_branch
5+
from pre_commit_hooks.no_commit_to_branch import main
6+
from pre_commit_hooks.util import cmd_output
7+
8+
9+
def test_other_branch(temp_git_dir):
10+
with temp_git_dir.as_cwd():
11+
cmd_output('git', 'checkout', '-b', 'anotherbranch')
12+
assert is_on_branch('master') is False
13+
14+
15+
def test_multi_branch(temp_git_dir):
16+
with temp_git_dir.as_cwd():
17+
cmd_output('git', 'checkout', '-b', 'another/branch')
18+
assert is_on_branch('master') is False
19+
20+
21+
def test_multi_branch_fail(temp_git_dir):
22+
with temp_git_dir.as_cwd():
23+
cmd_output('git', 'checkout', '-b', 'another/branch')
24+
assert is_on_branch('another/branch') is True
25+
26+
27+
def test_master_branch(temp_git_dir):
28+
with temp_git_dir.as_cwd():
29+
assert is_on_branch('master') is True
30+
31+
32+
def test_main_b_call(temp_git_dir):
33+
with temp_git_dir.as_cwd():
34+
cmd_output('git', 'checkout', '-b', 'other')
35+
assert main(['-b', 'other']) == 1
36+
37+
38+
def test_main_branch_call(temp_git_dir):
39+
with temp_git_dir.as_cwd():
40+
cmd_output('git', 'checkout', '-b', 'other')
41+
assert main(['--branch', 'other']) == 1
42+
43+
44+
def test_main_default_call(temp_git_dir):
45+
with temp_git_dir.as_cwd():
46+
cmd_output('git', 'checkout', '-b', 'anotherbranch')
47+
assert main() == 0

0 commit comments

Comments
 (0)