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):
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))

View file

@ -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',
],
},
)

View file

@ -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')