Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define more branch protected #190

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions pre_commit_hooks/no_commit_to_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
from pre_commit_hooks.util import cmd_output


def is_on_branch(protected):
def is_on_branch(protected=()):
branch = cmd_output('git', 'symbolic-ref', 'HEAD')
chunks = branch.strip().split('/')
return '/'.join(chunks[2:]) == protected
position = '/'.join(chunks[2:])
return position in (protected or ('master',))


def main(argv=[]):
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument(
'-b', '--branch', default='master', help='branch to disallow commits to')
'-b',
'--branch',
action='append',
dest='branches',
help='branch to disallow commits to'
)
args = parser.parse_args(argv)

return int(is_on_branch(args.branch))
return int(is_on_branch(args.branches))


if __name__ == '__main__':
sys.exit(main(sys.argv))
sys.exit(main(sys.argv[1:]))
8 changes: 4 additions & 4 deletions tests/check_no_commit_to_branch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
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
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
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
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
assert is_on_branch(('master', )) is True


def test_main_b_call(temp_git_dir):
Expand Down