From a988275347fb13ee1bfc59b5958668e71fa47181 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Fri, 4 Dec 2026 22:19:49 +0000 Subject: [PATCH] fix(git): follow earlier branch changes in the push-to-main hook Review found pushes refused after `gh pr checkout` or `git branch -m`, and pushes to main allowed after `git switch main` or `cd `. The hook now follows literal branch and directory changes and treats any other as unknown. Heredoc stripping is linear and handles several per line. Refs: #178 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi --- plugins/git/.apm/hooks/refuse-push-to-main.py | 130 +++++++++++++----- plugins/git/README.md | 3 +- tests/test-git-push-main-hook.sh | 92 ++++++++++--- 3 files changed, 167 insertions(+), 58 deletions(-) diff --git a/plugins/git/.apm/hooks/refuse-push-to-main.py b/plugins/git/.apm/hooks/refuse-push-to-main.py index cf83c7d..d97a6b2 100755 --- a/plugins/git/.apm/hooks/refuse-push-to-main.py +++ b/plugins/git/.apm/hooks/refuse-push-to-main.py @@ -16,11 +16,11 @@ import sys MAIN_NAMES = ("main", "heads/main", "refs/heads/main") HEREDOC = re.compile( - r"""(?'"`\\]+))""" ) WORD = r"""(?:[^ \t\r\n;&|()`<>'"\\]|\\.|'[^']*'|"(?:[^"\\]|\\.)*")+""" TOKEN = re.compile( - rf"""(?P[ \t\r]+|\\\n|\#[^\n]*|(?:\d*|&)[<>]+[&|]?[ \t]*(?:{WORD})?) + rf"""(?P[ \t\r]+|\#[^\n]*|(?:\d*|&)[<>]+[&|]?[ \t]*(?:{WORD})?) |(?P[;&|()`\n]+) |(?P{WORD})""", re.S | re.X, @@ -28,9 +28,21 @@ TOKEN = re.compile( ASSIGNMENT = re.compile(r"\w+=") SHELL_KEYWORDS = {"!", "{", "if", "then", "elif", "else", "while", "until", "do"} -WRAPPERS = {"rtk", "proxy", "command", "env", "sudo", "time", "timeout", "nohup"} +WRAPPERS = { + "rtk", + "proxy", + "command", + "env", + "exec", + "nohup", + "sudo", + "time", + "timeout", + "xargs", +} 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"} PUSH_OPTIONS_WITH_VALUE = { "--repo", @@ -41,15 +53,38 @@ PUSH_OPTIONS_WITH_VALUE = { } +def strip_heredocs(command): + lines = command.split("\n") + # A `<<` that nothing closes is text or arithmetic, not a heredoc. + closing_lines = {line.lstrip("\t") for line in lines} + kept, pending = [], [] + for line in lines: + if pending: + dash, delimiter = pending[0] + if (line.lstrip("\t") if dash else line) == delimiter: + pending.pop(0) + continue + kept.append(line) + for dash, *quotings in HEREDOC.findall(line): + delimiter = "".join(quotings) + if delimiter in closing_lines: + pending.append((dash, delimiter)) + return "\n".join(kept) + + def simple_commands(command): - """Split a command line into word lists; None when it does not parse.""" - command = HEREDOC.sub(r"\1", command) + """Split a command line into word lists; None when it does not parse. + + A subshell's parentheses come out as the commands ["("] and [")"]. + """ + command = re.sub(r"(?.push`) -- a bare `git push`, `HEAD` or `@` after a `cd`, `git switch` or `git checkout` in the same command, because the branch is not known yet +- a push behind a wrapper option that takes a separate value, such as `sudo -u ` or `env -u ` +- 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=` ### Opt-out diff --git a/tests/test-git-push-main-hook.sh b/tests/test-git-push-main-hook.sh index f67b689..f005ed6 100755 --- a/tests/test-git-push-main-hook.sh +++ b/tests/test-git-push-main-hook.sh @@ -35,10 +35,15 @@ git init -q -b main "$WORK/on-main" git init -q -b feat "$WORK/on-feat" # decision