diff --git a/hooks.yaml b/hooks.yaml index 0604585..f7e35d4 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -147,3 +147,9 @@ entry: trailing-whitespace-fixer language: python files: \.(asciidoc|adoc|coffee|cpp|css|c|ejs|erb|groovy|h|haml|hh|hpp|hxx|html|in|j2|jade|json|js|less|markdown|md|ml|mli|pp|py|rb|rs|R|scala|scss|sh|slim|tex|tmpl|ts|txt|yaml|yml)$ +- id: no-commit-to-branch + name: Dont commit to branch + entry: no-commit-to-branch + language: python + files: .* + always_run: true 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..01dee5a --- /dev/null +++ b/pre_commit_hooks/no_commit_to_branch.py @@ -0,0 +1,24 @@ +from __future__ import print_function + +import argparse +import sys + +import util + + +def main(argv=None): + parser = argparse.ArgumentParser() + parser.add_argument('-b', default='master', help='branch to disallow commits to') + parser.add_argument('filenames', nargs='*', help='filenames to check.') + args = parser.parse_args(argv) + + retval = -1 + branch = util.cmd_output('git', 'symbolic-ref', 'HEAD') + chunks = branch.split('/') + if chunks[2] == args.b: + retval = 0 + return retval + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/setup.py b/setup.py index 68a8664..3d96bda 100644 --- a/setup.py +++ b/setup.py @@ -55,6 +55,7 @@ setup( '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', + 'no-commit-to-branch = pre_commit_hooks.no_commit_to_branch:main', ], }, )