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"}
|
SHORT_PUSH_OPTIONS = {"h": "--help", "n": "--dry-run"}
|
||||||
GIT_OPTIONS_WITH_VALUE = {"-c", "--git-dir", "--work-tree", "--namespace", "--config-env"}
|
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 = {
|
PUSH_OPTIONS_WITH_VALUE = {
|
||||||
"--repo",
|
"--repo",
|
||||||
"--push-option",
|
"--push-option",
|
||||||
@@ -128,6 +137,14 @@ def resolve(directory, target):
|
|||||||
def changes_branch(subcommand, arguments):
|
def changes_branch(subcommand, arguments):
|
||||||
if subcommand == "branch":
|
if subcommand == "branch":
|
||||||
return not {"-m", "-M", "--move"}.isdisjoint(arguments)
|
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)
|
return subcommand == "switch" or (subcommand == "checkout" and "--" not in arguments)
|
||||||
|
|
||||||
|
|
||||||
@@ -205,10 +222,13 @@ def pushes_to_main(command, directory):
|
|||||||
branch_change = (directory, False)
|
branch_change = (directory, False)
|
||||||
elif name == "git":
|
elif name == "git":
|
||||||
repo, subcommand, arguments = split_git_command(arguments, directory)
|
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():
|
def on_main():
|
||||||
if other_git_dir:
|
if unknown:
|
||||||
return False
|
return False
|
||||||
if branch_change:
|
if branch_change:
|
||||||
return branch_change == (repo, True)
|
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 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`)
|
- 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 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
|
### Opt-out
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ origin :main
|
|||||||
-d origin main
|
-d origin main
|
||||||
--delete origin main
|
--delete origin main
|
||||||
origin --all
|
origin --all
|
||||||
|
origin --branches
|
||||||
--mirror origin
|
--mirror origin
|
||||||
origin :
|
origin :
|
||||||
origin +:
|
origin +:
|
||||||
@@ -106,6 +107,7 @@ done << 'CASES'
|
|||||||
|
|
||||||
origin
|
origin
|
||||||
--follow-tags
|
--follow-tags
|
||||||
|
-o ci.skip origin
|
||||||
--repo=origin
|
--repo=origin
|
||||||
origin HEAD
|
origin HEAD
|
||||||
origin @
|
origin @
|
||||||
@@ -160,6 +162,15 @@ allow main git branch --move feat && git push -u origin HEAD
|
|||||||
allow main git switch feat && cd sub && git push
|
allow main git switch feat && cd sub && git push
|
||||||
allow main git checkout main && git pull && git checkout -b x && git push -u origin HEAD
|
allow main git checkout main && git pull && git checkout -b x && git push -u origin HEAD
|
||||||
refuse main git branch -a && git push
|
refuse main git branch -a && git push
|
||||||
|
allow feat git switch main && git pull && git rebase main feat && git push
|
||||||
|
allow main git fetch && git rebase origin/main feat && git push --force-with-lease
|
||||||
|
allow main git rebase --onto main old feat && git push
|
||||||
|
refuse main git rebase origin/main && git push
|
||||||
|
refuse main git rebase --onto origin/main old && git push
|
||||||
|
refuse main git rebase -x true origin/main && git push
|
||||||
|
allow feat cd WORK/on-main && git switch main && git pull; cd WORK/on-feat && git push
|
||||||
|
allow main echo feat | xargs git push origin
|
||||||
|
allow main echo feat | xargs -n 1 git push origin HEAD
|
||||||
refuse feat git checkout main && git merge feat && git push
|
refuse feat git checkout main && git merge feat && git push
|
||||||
refuse feat git switch main && git pull && git push origin HEAD
|
refuse feat git switch main && git pull && git push origin HEAD
|
||||||
refuse feat git -C WORK/on-feat switch main && git -C WORK/on-feat push
|
refuse feat git -C WORK/on-feat switch main && git -C WORK/on-feat push
|
||||||
|
|||||||
Reference in new issue
Block a user