diff --git a/pre_commit_hooks/no_commit_to_branch.py b/pre_commit_hooks/no_commit_to_branch.py index ed66856..a97b103 100644 --- a/pre_commit_hooks/no_commit_to_branch.py +++ b/pre_commit_hooks/no_commit_to_branch.py @@ -7,26 +7,19 @@ from pre_commit_hooks.util import cmd_output def is_on_branch(protected): - retval = False branch = cmd_output('git', 'symbolic-ref', 'HEAD') chunks = branch.strip().split('/') - if chunks[2] == protected: - retval = True - return retval + return chunks[2] == protected -def main(argv=None): +def main(argv=[]): parser = argparse.ArgumentParser() parser.add_argument( - '-b', default='master', help='branch to disallow commits to') - parser.add_argument('filenames', nargs='*', help='filenames to check.') + '-b', '--branch', default='master', help='branch to disallow commits to') args = parser.parse_args(argv) - if is_on_branch(args.b): - return 1 - else: - return 0 + return int(is_on_branch(args.branch)) if __name__ == '__main__': - sys.exit(main()) + sys.exit(main(sys.argv)) diff --git a/setup.py b/setup.py index 3d96bda..9db61a4 100644 --- a/setup.py +++ b/setup.py @@ -52,10 +52,10 @@ 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', - 'no-commit-to-branch = pre_commit_hooks.no_commit_to_branch:main', ], }, ) diff --git a/tests/check_no_commit_to_branch_test.py b/tests/check_no_commit_to_branch_test.py index 91df387..e8b09fd 100644 --- a/tests/check_no_commit_to_branch_test.py +++ b/tests/check_no_commit_to_branch_test.py @@ -17,12 +17,18 @@ def test_master_branch(temp_git_dir): assert is_on_branch('master') is True -def test_main_other_call(temp_git_dir): +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')