mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-05 19:46:54 +00:00
Auto-detect repository's default branch instead of hard-coding list
This commit is contained in:
parent
f1dff44d3a
commit
ea5f72dd61
3 changed files with 48 additions and 2 deletions
|
|
@ -9,6 +9,17 @@ from pre_commit_hooks.util import CalledProcessError
|
|||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
||||
def _default_branch() -> frozenset[str]:
|
||||
try:
|
||||
ref = cmd_output('git', 'rev-parse', '--abbrev-ref', 'origin/HEAD')
|
||||
branch = ref.strip().removeprefix('origin/')
|
||||
if branch:
|
||||
return frozenset((branch,))
|
||||
except CalledProcessError:
|
||||
pass
|
||||
return frozenset(('master', 'main'))
|
||||
|
||||
|
||||
def is_on_branch(
|
||||
protected: AbstractSet[str],
|
||||
patterns: AbstractSet[str] = frozenset(),
|
||||
|
|
@ -39,7 +50,7 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
protected = frozenset(args.branch or ('master', 'main'))
|
||||
protected = frozenset(args.branch) if args.branch else _default_branch()
|
||||
patterns = frozenset(args.pattern or ())
|
||||
return int(is_on_branch(protected, patterns))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue