From 3abbd4785ccb2614b84012d5a6f1e2a98d52a7de Mon Sep 17 00:00:00 2001 From: Nicholas Devenish Date: Thu, 4 Mar 2021 13:41:04 +0000 Subject: [PATCH] no-commit-to-branch: Default to both master and main --- README.md | 2 +- pre_commit_hooks/no_commit_to_branch.py | 2 +- tests/no_commit_to_branch_test.py | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f66d2657..bf36ecf1 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Assert that files in tests/ end in `_test.py`. #### `no-commit-to-branch` Protect specific branches from direct checkins. - Use `args: [--branch, staging, --branch, master]` to set the branch. - `master` is the default if no branch argument is set. + Both `master` and `main` are protected by default if no branch argument is set. - `-b` / `--branch` may be specified multiple times to protect multiple branches. - `-p` / `--pattern` can be used to protect branches that match a supplied regex diff --git a/pre_commit_hooks/no_commit_to_branch.py b/pre_commit_hooks/no_commit_to_branch.py index fb1506f9..49ffecf7 100644 --- a/pre_commit_hooks/no_commit_to_branch.py +++ b/pre_commit_hooks/no_commit_to_branch.py @@ -38,7 +38,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int: ) args = parser.parse_args(argv) - protected = frozenset(args.branch or ('master',)) + protected = frozenset(args.branch or ('master', 'main')) patterns = frozenset(args.pattern or ()) return int(is_on_branch(protected, patterns)) diff --git a/tests/no_commit_to_branch_test.py b/tests/no_commit_to_branch_test.py index 72b32e64..610e660e 100644 --- a/tests/no_commit_to_branch_test.py +++ b/tests/no_commit_to_branch_test.py @@ -67,3 +67,10 @@ def test_not_on_a_branch(temp_git_dir): cmd_output('git', 'checkout', head) # we're not on a branch! assert main(()) == 0 + + +@pytest.mark.parametrize('branch_name', ('master', 'main')) +def test_default_branch_names(temp_git_dir, branch_name): + with temp_git_dir.as_cwd(): + cmd_output('git', 'checkout', '-b', branch_name) + assert main(()) == 1