fix(git): treat rebase onto a named branch as a branch change
`git rebase <upstream> <branch>` checks out <branch>, so a later bare push was refused as if main were still checked out. Pushes through xargs get refspecs the hook cannot see, so only an explicit main is refused there. Refs: #178 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
1 parent
a988275347
commit
1ccf246598
3 files changed
+38
-3
No files matched your search
@@ -44,6 +44,15 @@ WRAPPER_ARGUMENT = re.compile(r"-|\d+[smhd]?$")
|
||||
|
||||
SHORT_PUSH_OPTIONS = {"h": "--help", "n": "--dry-run"}
|
||||
GIT_OPTIONS_WITH_VALUE = {"-c", "--git-dir", "--work-tree", "--namespace", "--config-env"}
|
||||
REBASE_OPTIONS_WITH_VALUE = {
|
||||
"--onto",
|
||||
"-s",
|
||||
"--strategy",
|
||||
"-X",
|
||||
"--strategy-option",
|
||||
"-x",
|
||||
"--exec",
|
||||
}
|
||||
PUSH_OPTIONS_WITH_VALUE = {
|
||||
"--repo",
|
||||
"--push-option",
|
||||
@@ -128,6 +137,14 @@ def resolve(directory, target):
|
||||
def changes_branch(subcommand, arguments):
|
||||
if subcommand == "branch":
|
||||
return not {"-m", "-M", "--move"}.isdisjoint(arguments)
|
||||
if subcommand == "rebase": # `git rebase <upstream> <branch>` checks out <branch>
|
||||
rest, names = iter(arguments), []
|
||||
for argument in rest:
|
||||
if argument in REBASE_OPTIONS_WITH_VALUE:
|
||||
next(rest, None)
|
||||
elif not argument.startswith("-"):
|
||||
names.append(argument)
|
||||
return len(names) > 1
|
||||
return subcommand == "switch" or (subcommand == "checkout" and "--" not in arguments)
|
||||
|
||||
|
||||
@@ -205,10 +222,13 @@ def pushes_to_main(command, directory):
|
||||
branch_change = (directory, False)
|
||||
elif name == "git":
|
||||
repo, subcommand, arguments = split_git_command(arguments, directory)
|
||||
other_git_dir = any(w.startswith(("GIT_DIR=", "--git-dir")) for w in all_words)
|
||||
# xargs adds refspecs, and another git dir has its own branch.
|
||||
unknown = "xargs" in all_words or any(
|
||||
w.startswith(("GIT_DIR=", "--git-dir")) for w in all_words
|
||||
)
|
||||
|
||||
def on_main():
|
||||
if other_git_dir:
|
||||
if unknown:
|
||||
return False
|
||||
if branch_change:
|
||||
return branch_change == (repo, True)
|
||||
|
||||
@@ -57,7 +57,11 @@ The hook is a guardrail against accidents. Server-side branch protection is the
|
||||
- git aliases, shell variables such as `$BRANCH`, scripts, `bash -c`, command substitution inside double quotes, or pushes made through an MCP server
|
||||
- git config that redirects a push (`push.default=upstream`, `remote.<name>.push`)
|
||||
- a push behind a wrapper option that takes a separate value, such as `sudo -u <user>` or `env -u <name>`
|
||||
- a bare `git push`, `HEAD` or `@` when the branch is not known: after a branch change in the same command other than a literal `git switch main` or `git checkout main`, after a `cd` to a path that is not literal, or with `--git-dir` or `GIT_DIR=`
|
||||
- a command line the hook cannot parse, for example `$'...'` with an escaped quote
|
||||
- a bare `git push`, `HEAD` or `@` in a command that makes the branch unknown:
|
||||
- an earlier `git switch` or `git checkout` to anything but a literal `main`, `git branch -m`, `git rebase <upstream> <branch>`, `gh pr checkout` or `tea pr checkout`
|
||||
- an earlier `cd` to a path that is not literal
|
||||
- `--git-dir`, `GIT_DIR=` or `xargs` on the push itself
|
||||
|
||||
### Opt-out
|
||||
|
||||
|
||||
Reference in new issue
Block a user