mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 05:14:18 +00:00
feat: [no_commit_to_branch] Add user friendly error message
This commit is contained in:
parent
848f775476
commit
c1efc9943f
2 changed files with 51 additions and 21 deletions
|
|
@ -9,29 +9,37 @@ 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(
|
def get_current_branch() -> str | None:
|
||||||
protected: AbstractSet[str],
|
|
||||||
patterns: AbstractSet[str] = frozenset(),
|
|
||||||
) -> bool:
|
|
||||||
try:
|
try:
|
||||||
ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
|
ref_name = cmd_output('git', 'symbolic-ref', 'HEAD')
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
return False
|
return None
|
||||||
chunks = ref_name.strip().split('/')
|
chunks = ref_name.strip().split('/')
|
||||||
branch_name = '/'.join(chunks[2:])
|
return '/'.join(chunks[2:])
|
||||||
return branch_name in protected or any(
|
|
||||||
re.match(p, branch_name) for p in patterns
|
|
||||||
|
def is_on_branch(
|
||||||
|
current_branch: str,
|
||||||
|
protected: AbstractSet[str],
|
||||||
|
patterns: AbstractSet[str] = frozenset(),
|
||||||
|
) -> bool:
|
||||||
|
return current_branch in protected or any(
|
||||||
|
re.match(p, current_branch) for p in patterns
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Sequence[str] | None = None) -> int:
|
def main(argv: Sequence[str] | None = None) -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-b', '--branch', action='append',
|
'-b',
|
||||||
|
'--branch',
|
||||||
|
action='append',
|
||||||
help='branch to disallow commits to, may be specified multiple times',
|
help='branch to disallow commits to, may be specified multiple times',
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-p', '--pattern', action='append',
|
'-p',
|
||||||
|
'--pattern',
|
||||||
|
action='append',
|
||||||
help=(
|
help=(
|
||||||
'regex pattern for branch name to disallow commits to, '
|
'regex pattern for branch name to disallow commits to, '
|
||||||
'may be specified multiple times'
|
'may be specified multiple times'
|
||||||
|
|
@ -41,7 +49,20 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||||
|
|
||||||
protected = frozenset(args.branch or ('master', 'main'))
|
protected = frozenset(args.branch or ('master', 'main'))
|
||||||
patterns = frozenset(args.pattern or ())
|
patterns = frozenset(args.pattern or ())
|
||||||
return int(is_on_branch(protected, patterns))
|
current_branch = get_current_branch()
|
||||||
|
|
||||||
|
if current_branch is None:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
on_branch = is_on_branch(current_branch, protected, patterns)
|
||||||
|
|
||||||
|
if on_branch:
|
||||||
|
print(
|
||||||
|
f'You are currently on {current_branch} branch, for which '
|
||||||
|
f'pre-commit script does not permit this action.',
|
||||||
|
)
|
||||||
|
|
||||||
|
return int(on_branch)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from pre_commit_hooks.no_commit_to_branch import get_current_branch
|
||||||
from pre_commit_hooks.no_commit_to_branch import is_on_branch
|
from pre_commit_hooks.no_commit_to_branch import is_on_branch
|
||||||
from pre_commit_hooks.no_commit_to_branch import main
|
from pre_commit_hooks.no_commit_to_branch import main
|
||||||
from pre_commit_hooks.util import cmd_output
|
from pre_commit_hooks.util import cmd_output
|
||||||
|
|
@ -11,24 +12,38 @@ from testing.util import git_commit
|
||||||
def test_other_branch(temp_git_dir):
|
def test_other_branch(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 is_on_branch({'master'}) is False
|
current_branch = get_current_branch()
|
||||||
|
assert current_branch == 'anotherbranch'
|
||||||
|
assert is_on_branch(current_branch, {'master'}) is False
|
||||||
|
|
||||||
|
|
||||||
def test_multi_branch(temp_git_dir):
|
def test_multi_branch(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
cmd_output('git', 'checkout', '-b', 'another/branch')
|
cmd_output('git', 'checkout', '-b', 'another/branch')
|
||||||
assert is_on_branch({'master'}) is False
|
current_branch = get_current_branch()
|
||||||
|
assert current_branch == 'another/branch'
|
||||||
|
assert is_on_branch(current_branch, {'master'}) is False
|
||||||
|
|
||||||
|
|
||||||
def test_multi_branch_fail(temp_git_dir):
|
def test_multi_branch_fail(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
cmd_output('git', 'checkout', '-b', 'another/branch')
|
cmd_output('git', 'checkout', '-b', 'another/branch')
|
||||||
assert is_on_branch({'another/branch'}) is True
|
current_branch = get_current_branch()
|
||||||
|
assert current_branch == 'another/branch'
|
||||||
|
assert is_on_branch(current_branch, {'another/branch'}) is True
|
||||||
|
|
||||||
|
|
||||||
def test_master_branch(temp_git_dir):
|
def test_master_branch(temp_git_dir):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
assert is_on_branch({'master'}) is True
|
current_branch = get_current_branch()
|
||||||
|
assert current_branch == 'master'
|
||||||
|
assert is_on_branch(current_branch, {'master'}) is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_branch_pattern_fail(temp_git_dir):
|
||||||
|
with temp_git_dir.as_cwd():
|
||||||
|
cmd_output('git', 'checkout', '-b', 'another/branch')
|
||||||
|
assert is_on_branch('another/branch', set(), {'another/.*'}) is True
|
||||||
|
|
||||||
|
|
||||||
def test_main_branch_call(temp_git_dir):
|
def test_main_branch_call(temp_git_dir):
|
||||||
|
|
@ -44,12 +59,6 @@ def test_forbid_multiple_branches(temp_git_dir, branch_name):
|
||||||
assert main(('--branch', 'b1', '--branch', 'b2'))
|
assert main(('--branch', 'b1', '--branch', 'b2'))
|
||||||
|
|
||||||
|
|
||||||
def test_branch_pattern_fail(temp_git_dir):
|
|
||||||
with temp_git_dir.as_cwd():
|
|
||||||
cmd_output('git', 'checkout', '-b', 'another/branch')
|
|
||||||
assert is_on_branch(set(), {'another/.*'}) is True
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
|
@pytest.mark.parametrize('branch_name', ('master', 'another/branch'))
|
||||||
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
|
def test_branch_pattern_multiple_branches_fail(temp_git_dir, branch_name):
|
||||||
with temp_git_dir.as_cwd():
|
with temp_git_dir.as_cwd():
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue