mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2025-08-26 18:10:18 +08:00
Merge pull request #266 from pre-commit/fix_no_commit_to_branch
Fix no-commit-to-branch when not on a branch
This commit is contained in:
@ -1,21 +1,25 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
|
||||||
|
|
||||||
|
from pre_commit_hooks.util import CalledProcessError
|
||||||
from pre_commit_hooks.util import cmd_output
|
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')
|
try:
|
||||||
|
branch = cmd_output('git', 'symbolic-ref', 'HEAD')
|
||||||
|
except CalledProcessError:
|
||||||
|
return False
|
||||||
chunks = branch.strip().split('/')
|
chunks = branch.strip().split('/')
|
||||||
return '/'.join(chunks[2:]) == protected
|
return '/'.join(chunks[2:]) == protected
|
||||||
|
|
||||||
|
|
||||||
def main(argv=[]):
|
def main(argv=None):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-b', '--branch', default='master', help='branch to disallow commits to',
|
'-b', '--branch', default='master',
|
||||||
|
help='branch to disallow commits to',
|
||||||
)
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
@ -23,4 +27,4 @@ def main(argv=[]):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main(sys.argv))
|
exit(main())
|
||||||
|
@ -29,19 +29,22 @@ def test_master_branch(temp_git_dir):
|
|||||||
assert is_on_branch('master') is True
|
assert is_on_branch('master') is True
|
||||||
|
|
||||||
|
|
||||||
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):
|
def test_main_branch_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(['--branch', 'other']) == 1
|
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')
|
||||||
assert main() == 0
|
assert main(()) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_not_on_a_branch(temp_git_dir):
|
||||||
|
with temp_git_dir.as_cwd():
|
||||||
|
cmd_output('git', 'commit', '--no-gpg-sign', '--allow-empty', '-m1')
|
||||||
|
head = cmd_output('git', 'rev-parse', 'HEAD').strip()
|
||||||
|
cmd_output('git', 'checkout', head)
|
||||||
|
# we're not on a branch!
|
||||||
|
assert main(()) == 0
|
||||||
|
Reference in New Issue
Block a user