Skip to content

Commit ea5f72d

Browse files
committed
Auto-detect repository's default branch instead of hard-coding list
1 parent f1dff44 commit ea5f72d

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ verifies that test files are named correctly.
152152
#### `no-commit-to-branch`
153153
Protect specific branches from direct checkins.
154154
- Use `args: [--branch, staging, --branch, main]` to set the branch.
155-
Both `main` and `master` are protected by default if no branch argument is set.
155+
If no branch argument is set, the hook auto-detects the repository's default
156+
branch from `origin/HEAD`. Falls back to protecting both `main` and `master`
157+
if `origin/HEAD` is not configured.
156158
- `-b` / `--branch` may be specified multiple times to protect multiple
157159
branches.
158160
- `-p` / `--pattern` can be used to protect branches that match a supplied regex

pre_commit_hooks/no_commit_to_branch.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
from pre_commit_hooks.util import cmd_output
1010

1111

12+
def _default_branch() -> frozenset[str]:
13+
try:
14+
ref = cmd_output('git', 'rev-parse', '--abbrev-ref', 'origin/HEAD')
15+
branch = ref.strip().removeprefix('origin/')
16+
if branch:
17+
return frozenset((branch,))
18+
except CalledProcessError:
19+
pass
20+
return frozenset(('master', 'main'))
21+
22+
1223
def is_on_branch(
1324
protected: AbstractSet[str],
1425
patterns: AbstractSet[str] = frozenset(),
@@ -39,7 +50,7 @@ def main(argv: Sequence[str] | None = None) -> int:
3950
)
4051
args = parser.parse_args(argv)
4152

42-
protected = frozenset(args.branch or ('master', 'main'))
53+
protected = frozenset(args.branch) if args.branch else _default_branch()
4354
patterns = frozenset(args.pattern or ())
4455
return int(is_on_branch(protected, patterns))
4556

tests/no_commit_to_branch_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
from pre_commit_hooks.no_commit_to_branch import _default_branch
56
from pre_commit_hooks.no_commit_to_branch import is_on_branch
67
from pre_commit_hooks.no_commit_to_branch import main
78
from pre_commit_hooks.util import cmd_output
@@ -78,3 +79,35 @@ def test_default_branch_names(temp_git_dir, branch_name):
7879
with temp_git_dir.as_cwd():
7980
cmd_output('git', 'checkout', '-b', branch_name)
8081
assert main(()) == 1
82+
83+
84+
def test_default_branch_detects_from_origin(tmpdir):
85+
remote = tmpdir.join('remote')
86+
cmd_output('git', 'init', '--', str(remote))
87+
with remote.as_cwd():
88+
cmd_output('git', 'checkout', '-b', 'develop')
89+
git_commit('--allow-empty', '-m', 'init')
90+
91+
local = tmpdir.join('local')
92+
cmd_output('git', 'clone', str(remote), str(local))
93+
94+
with local.as_cwd():
95+
assert _default_branch() == frozenset({'develop'})
96+
97+
98+
def test_main_blocks_detected_default_branch(tmpdir):
99+
remote = tmpdir.join('remote')
100+
cmd_output('git', 'init', '--', str(remote))
101+
with remote.as_cwd():
102+
cmd_output('git', 'checkout', '-b', 'develop')
103+
git_commit('--allow-empty', '-m', 'init')
104+
105+
local = tmpdir.join('local')
106+
cmd_output('git', 'clone', str(remote), str(local))
107+
108+
with local.as_cwd():
109+
# On detected default branch — should be blocked
110+
assert main(()) == 1
111+
# On a feature branch — should pass
112+
cmd_output('git', 'checkout', '-b', 'feature')
113+
assert main(()) == 0

0 commit comments

Comments
 (0)