mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-03 02:56:52 +00:00
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.
This commit is contained in:
parent
b95dcad616
commit
a8592669d9
6 changed files with 87 additions and 0 deletions
|
|
@ -129,6 +129,12 @@
|
|||
entry: name-tests-test
|
||||
language: python
|
||||
files: tests/.+\.py$
|
||||
- id: no-commit-to-branch
|
||||
name: "Don't commit to branch"
|
||||
entry: no-commit-to-branch
|
||||
language: python
|
||||
files: .*
|
||||
always_run: true
|
||||
- id: pyflakes
|
||||
name: Pyflakes (DEPRECATED, use flake8)
|
||||
description: This hook runs pyflakes. (This is deprecated, use flake8).
|
||||
|
|
|
|||
|
|
@ -56,6 +56,8 @@ Add this to your `.pre-commit-config.yaml`
|
|||
- `forbid-new-submodules` - Prevent addition of new git submodules.
|
||||
- `name-tests-test` - Assert that files in tests/ end in `_test.py`.
|
||||
- Use `args: ['--django']` to match `test*.py` instead.
|
||||
- `no-commit-to-branch` - Protect specific branches from direct checkins.
|
||||
- Use `args: -b <branch> ` to set the branch. `master` is the default if no argument is set.
|
||||
- `pyflakes` - Run pyflakes on your python files.
|
||||
- `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
|
||||
|
|
|
|||
|
|
@ -129,6 +129,12 @@
|
|||
entry: name-tests-test
|
||||
language: python
|
||||
files: tests/.+\.py$
|
||||
- id: no-commit-to-branch
|
||||
name: "Don't commit to branch"
|
||||
entry: no-commit-to-branch
|
||||
language: python
|
||||
files: .*
|
||||
always_run: true
|
||||
- id: pyflakes
|
||||
name: Pyflakes (DEPRECATED, use flake8)
|
||||
description: This hook runs pyflakes. (This is deprecated, use flake8).
|
||||
|
|
|
|||
25
pre_commit_hooks/no_commit_to_branch.py
Normal file
25
pre_commit_hooks/no_commit_to_branch.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
||||
def is_on_branch(protected):
|
||||
branch = cmd_output('git', 'symbolic-ref', 'HEAD')
|
||||
chunks = branch.strip().split('/')
|
||||
return '/'.join(chunks[2:]) == protected
|
||||
|
||||
|
||||
def main(argv=[]):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
'-b', '--branch', default='master', help='branch to disallow commits to')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
return int(is_on_branch(args.branch))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
||||
1
setup.py
1
setup.py
|
|
@ -52,6 +52,7 @@ setup(
|
|||
'fix-encoding-pragma = pre_commit_hooks.fix_encoding_pragma:main',
|
||||
'forbid-new-submodules = pre_commit_hooks.forbid_new_submodules:main',
|
||||
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files',
|
||||
'no-commit-to-branch = pre_commit_hooks.no_commit_to_branch:main',
|
||||
'pretty-format-json = pre_commit_hooks.pretty_format_json:pretty_format_json',
|
||||
'requirements-txt-fixer = pre_commit_hooks.requirements_txt_fixer:fix_requirements_txt',
|
||||
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
|
||||
|
|
|
|||
47
tests/check_no_commit_to_branch_test.py
Normal file
47
tests/check_no_commit_to_branch_test.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from pre_commit_hooks.no_commit_to_branch import is_on_branch
|
||||
from pre_commit_hooks.no_commit_to_branch import main
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
||||
def test_other_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
||||
assert is_on_branch('master') is False
|
||||
|
||||
|
||||
def test_multi_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'another/branch')
|
||||
assert is_on_branch('master') is False
|
||||
|
||||
|
||||
def test_multi_branch_fail(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'another/branch')
|
||||
assert is_on_branch('another/branch') is True
|
||||
|
||||
|
||||
def test_master_branch(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
assert is_on_branch('master') is True
|
||||
|
||||
|
||||
def test_main_b_call(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'other')
|
||||
assert main(['-b', 'other']) == 1
|
||||
|
||||
|
||||
def test_main_branch_call(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'other')
|
||||
assert main(['--branch', 'other']) == 1
|
||||
|
||||
|
||||
def test_main_default_call(temp_git_dir):
|
||||
with temp_git_dir.as_cwd():
|
||||
cmd_output('git', 'checkout', '-b', 'anotherbranch')
|
||||
assert main() == 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue