Skip to content

no-commit-to-branch: Auto-detect repository's default branch#1245

Closed
emanb29 wants to merge 2 commits intopre-commit:mainfrom
emanb29:feature/auto-detect-main-branch
Closed

no-commit-to-branch: Auto-detect repository's default branch#1245
emanb29 wants to merge 2 commits intopre-commit:mainfrom
emanb29:feature/auto-detect-main-branch

Conversation

@emanb29
Copy link
Copy Markdown

@emanb29 emanb29 commented Apr 4, 2026

Many repos I work on use dev or develop for the trunk branch -- it'd be convenient for those to be protected as defaults. However, others may well use dev as an intentionally-volatile branch. Instead of guessing, we can infer the default branch by inspecting origin/HEAD. If that fails for whatever reason, fall back to the previous default of master+main

Comment on lines -155 to +157
Both `main` and `master` are protected by default if no branch argument is set.
If no branch argument is set, the hook auto-detects the repository's default
branch from `origin/HEAD`. Falls back to protecting both `main` and `master`
if `origin/HEAD` is not configured.
Copy link
Copy Markdown
Member

@asottile asottile Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as written this is a breaking change

please in the future discuss feature ideas before wasting time on implementation such that we can find the problems during the design phase rather than at the last mile of the coding phase

I suspect if this is going to be accepted at all this would need to be a separate option rather than an implicit behaviour -- especially because as written it is an inconsistent implicit behaviour (based on the repository state)

and with a required option to trigger this behaviour -- I kinda see little point to the option at all (since you could just specify the option that's already there with the proper value instead and accomplish the same thing)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's theoretically a breaking change, but not practically. I suspect anyone relying on the default is doing so because of an intent to protect their trunk branch.

As for the process quibble: the greater waste would have been a discussion -- I needed this behavior, now I have a fork with it. An unexpected 9-line PR isn't dropping a huge amount of work on you and other maintainers without any notice, so I decided to offer it to the community for a tool I've found generally delightful to use rather than keeping it in an isolated fork.

You're welcome to take the change, reject it, or modify it to your liking. Consider this PR a notice of interest from a community member and an offer to save you some work in the case your philosophy as a maintainer aligns with mine as a user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants