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:
Defame1297andClaude Opus 5.5 committed 2026-12-04 22:54:50 +00:00
1 parent a988275347
commit 1ccf246598
3 files changed
+38 -3

No files matched your search

+22 -2
View File
@@ -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)