From a8592669d958b27368e4621741c1f0cb5e538f36 Mon Sep 17 00:00:00 2001 From: Evan Felix Date: Mon, 20 Mar 2017 10:36:51 -0700 Subject: [PATCH] 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. --- .pre-commit-hooks.yaml | 6 ++++ README.md | 2 ++ hooks.yaml | 6 ++++ pre_commit_hooks/no_commit_to_branch.py | 25 +++++++++++++ setup.py | 1 + tests/check_no_commit_to_branch_test.py | 47 +++++++++++++++++++++++++ 6 files changed, 87 insertions(+) create mode 100644 pre_commit_hooks/no_commit_to_branch.py create mode 100644 tests/check_no_commit_to_branch_test.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 0604585..bda3f76 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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). diff --git a/README.md b/README.md index 031edfd..723136d 100644 --- a/README.md +++ b/README.md @@ -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 ` 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 diff --git a/hooks.yaml b/hooks.yaml index 0604585..bda3f76 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -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). diff --git a/pre_commit_hooks/no_commit_to_branch.py b/pre_commit_hooks/no_commit_to_branch.py new file mode 100644 index 0000000..4aa3535 --- /dev/null +++ b/pre_commit_hooks/no_commit_to_branch.py @@ -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)) diff --git a/setup.py b/setup.py index 68a8664..9db61a4 100644 --- a/setup.py +++ b/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', diff --git a/tests/check_no_commit_to_branch_test.py b/tests/check_no_commit_to_branch_test.py new file mode 100644 index 0000000..99af938 --- /dev/null +++ b/tests/check_no_commit_to_branch_test.py @@ -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