code cleanup based off asottile comments

This commit is contained in:
Evan J. Felix 2017-03-17 15:34:23 -07:00
parent 44c6676070
commit 7c6b842a92
3 changed files with 13 additions and 14 deletions

View file

@ -7,26 +7,19 @@ from pre_commit_hooks.util import cmd_output
def is_on_branch(protected): def is_on_branch(protected):
retval = False
branch = cmd_output('git', 'symbolic-ref', 'HEAD') branch = cmd_output('git', 'symbolic-ref', 'HEAD')
chunks = branch.strip().split('/') chunks = branch.strip().split('/')
if chunks[2] == protected: return chunks[2] == protected
retval = True
return retval
def main(argv=None): def main(argv=[]):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
'-b', default='master', help='branch to disallow commits to') '-b', '--branch', default='master', help='branch to disallow commits to')
parser.add_argument('filenames', nargs='*', help='filenames to check.')
args = parser.parse_args(argv) args = parser.parse_args(argv)
if is_on_branch(args.b): return int(is_on_branch(args.branch))
return 1
else:
return 0
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main(sys.argv))

View file

@ -52,10 +52,10 @@ setup(
'fix-encoding-pragma = pre_commit_hooks.fix_encoding_pragma:main', 'fix-encoding-pragma = pre_commit_hooks.fix_encoding_pragma:main',
'forbid-new-submodules = pre_commit_hooks.forbid_new_submodules:main', 'forbid-new-submodules = pre_commit_hooks.forbid_new_submodules:main',
'name-tests-test = pre_commit_hooks.tests_should_end_in_test:validate_files', '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', 'pretty-format-json = pre_commit_hooks.pretty_format_json:pretty_format_json',
'requirements-txt-fixer = pre_commit_hooks.requirements_txt_fixer:fix_requirements_txt', 'requirements-txt-fixer = pre_commit_hooks.requirements_txt_fixer:fix_requirements_txt',
'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace', 'trailing-whitespace-fixer = pre_commit_hooks.trailing_whitespace_fixer:fix_trailing_whitespace',
'no-commit-to-branch = pre_commit_hooks.no_commit_to_branch:main',
], ],
}, },
) )

View file

@ -17,12 +17,18 @@ def test_master_branch(temp_git_dir):
assert is_on_branch('master') is True 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(): with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'other') cmd_output('git', 'checkout', '-b', 'other')
assert main(['-b', 'other']) == 1 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): def test_main_default_call(temp_git_dir):
with temp_git_dir.as_cwd(): with temp_git_dir.as_cwd():
cmd_output('git', 'checkout', '-b', 'anotherbranch') cmd_output('git', 'checkout', '-b', 'anotherbranch')