兩支閘改看「指令要動的那個 repo」,不看 hook 自己的環境/cwd(inkstone/ISEP#109 → comment 6629)
① line-needs-own-worktree.sh:出路 `WORKTREE_OK=1 git -C … checkout …` 改認指令字串裡的字面前綴
(舊版讀 hook 自己的環境變數,PreToolUse hook 跟指令不是同一個行程,那行永遠走不通)。
判準是位置不是字:前綴必須掛在會移動 HEAD 的那條 git 指令上。
lib/checkout_target_dir.py 加 `--escape NAME=1`;push_target_dir._classify 在 strip_env 時
把前綴留在 verb 事件裡(find_push_target 行為不變,主線閘 10+19 條照綠)。
② github-contact-guard.sh:remote 名在「這條指令實際會推的那個 repo」裡解(沿用 lib/push_target_dir.py
解 cd 鏈/-C/子殼),解不出來才退回 cwd。判準仍是 remote URL 主機,不是「有 -C 就放行」。
測試:A24 59→69(E 群把閘印的那一行原樣餵回去;舊閘 3 條紅)、
A39 14→26(payload 帶 cwd、cwd≠目標 repo;舊閘 7 條紅=4 誤攔+3 漏擋)。
盤點表:61 支/85 條,改判準不加閘,當場數的。
版本:待總管定版。
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0178ef1fGw3XeZtpN7LaZrm4
This commit is contained in:
@@ -30,13 +30,24 @@
|
||||
其他 目錄運算式,可能是相對的;呼叫端自己 `cd` 進去問 git(唯讀)。
|
||||
"." =「就是呼叫端自己的 cwd」(指令裡沒有 cd 也沒有 -C)。
|
||||
|
||||
用法:printf '%s' "$CMD" | python3 checkout_target_dir.py
|
||||
出路的判準也住在這裡(inkstone/ISEP#109 → comment 6587,2026-09-07)
|
||||
閘訊息教的出路是 `WORKTREE_OK=1 git -C … checkout …`。舊版讀的是 **hook 自己的
|
||||
環境變數**,而 PreToolUse hook 跑在指令之前、跟指令不是同一個行程——指令字串裡的
|
||||
`WORKTREE_OK=1` 是給 git 的前綴賦值,hook 的行程裡根本沒有它 ⇒ 總管三次照貼三次被擋。
|
||||
⇒ 改成認**指令字串裡的字面前綴**,而且判準是位置不是字:那個賦值必須是
|
||||
「會移動 HEAD 的那條 git 指令」自己的前綴(`echo WORKTREE_OK=1; git checkout` 不算、
|
||||
`WORKTREE_OK=1 echo hi && git checkout` 不算、寫在 commit 訊息裡不算)。
|
||||
留痕就是那個前綴本身——它在指令歷史上。
|
||||
|
||||
用法:printf '%s' "$CMD" | python3 checkout_target_dir.py → 目錄運算式(見上)
|
||||
printf '%s' "$CMD" | python3 checkout_target_dir.py --escape WORKTREE_OK=1
|
||||
→ 那條 checkout 帶著這個前綴就印 1,否則空
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from push_target_dir import _events, _join # noqa: E402
|
||||
from push_target_dir import _ENV_ASSIGN, _events, _join # noqa: E402
|
||||
|
||||
VERBS = ("checkout", "switch")
|
||||
UNKNOWN = "?"
|
||||
@@ -52,14 +63,19 @@ def _step(base, path):
|
||||
|
||||
|
||||
def _moves_head(tokens):
|
||||
"""tokens 是整串 `git …`。回傳 (會不會動 HEAD, -C 指的路徑 or None)。
|
||||
"""tokens 是整串 `[NAME=value …] git …`。
|
||||
回傳 (會不會動 HEAD, -C 指的路徑 or None, 前綴賦值清單)。
|
||||
|
||||
判準只有「這個動作動不動 HEAD」,不看任何措辭:
|
||||
git checkout -- <檔>/git checkout HEAD -- <檔> ⇒ 還原檔案,不動
|
||||
git checkout -p ⇒ 挑 hunk,不動
|
||||
git checkout(沒有任何目標) ⇒ git 自己會報錯
|
||||
"""
|
||||
tok = list(tokens[1:]) # 去掉 "git"
|
||||
tok = list(tokens)
|
||||
env = []
|
||||
while tok and _ENV_ASSIGN.match(tok[0]):
|
||||
env.append(tok.pop(0)) # `WORKTREE_OK=1 git …` 的前綴——出路就認這個
|
||||
tok = tok[1:] # 去掉 "git"
|
||||
c_path = None
|
||||
while tok and tok[0].startswith("-"):
|
||||
if tok[0] == "-C" and len(tok) > 1:
|
||||
@@ -72,24 +88,25 @@ def _moves_head(tokens):
|
||||
tok.pop(0)
|
||||
continue
|
||||
if tok[0].startswith("--git-dir"):
|
||||
return (False, None) # 自己指 git-dir 的花式用法,不猜
|
||||
return (False, None, env) # 自己指 git-dir 的花式用法,不猜
|
||||
tok.pop(0)
|
||||
if not tok or tok[0] not in VERBS:
|
||||
return (False, None)
|
||||
return (False, None, env)
|
||||
|
||||
rest = tok[1:]
|
||||
if "--" in rest or "-p" in rest or "--patch" in rest:
|
||||
return (False, None)
|
||||
return (False, None, env)
|
||||
if not [a for a in rest if not a.startswith("-")] and \
|
||||
not any(a in ("-b", "-B", "-c", "-C") for a in rest):
|
||||
return (False, None)
|
||||
return (True, c_path)
|
||||
return (False, None, env)
|
||||
return (True, c_path, env)
|
||||
|
||||
|
||||
def find_checkout_target(cmd):
|
||||
def find_checkout(cmd):
|
||||
"""回 (目錄運算式, 那條 checkout 的前綴賦值清單)。目錄運算式的三種值見檔頭。"""
|
||||
events = _events(cmd, VERBS, strip_env=True)
|
||||
if not events:
|
||||
return "" # 引號壞掉之類——沿用「不擋在不確定上」
|
||||
return ("", []) # 引號壞掉之類——沿用「不擋在不確定上」
|
||||
|
||||
stack = [None] # 每層括號各自的 cwd;None = 同呼叫端的 cwd
|
||||
for kind, val in events:
|
||||
@@ -101,7 +118,7 @@ def find_checkout_target(cmd):
|
||||
elif kind == "cd":
|
||||
stack[-1] = _step(stack[-1], val)
|
||||
elif kind == "verb":
|
||||
moves, c_path = _moves_head(val)
|
||||
moves, c_path, env = _moves_head(val)
|
||||
if not moves:
|
||||
continue
|
||||
where = stack[-1]
|
||||
@@ -110,13 +127,29 @@ def find_checkout_target(cmd):
|
||||
# (而且 -C 若是相對路徑,是接在 cd 之後算,不是接在 cwd)
|
||||
where = _step(where, c_path)
|
||||
if where == UNKNOWN:
|
||||
return UNKNOWN
|
||||
return where or "."
|
||||
return ""
|
||||
return (UNKNOWN, env)
|
||||
return (where or ".", env)
|
||||
return ("", [])
|
||||
|
||||
|
||||
def find_checkout_target(cmd):
|
||||
return find_checkout(cmd)[0]
|
||||
|
||||
|
||||
def has_escape(cmd, assignment):
|
||||
"""會移動 HEAD 的那條 git 指令,自己的前綴裡有沒有這個字面賦值(例 `WORKTREE_OK=1`)。
|
||||
只看**那條指令的前綴**:印在別處、掛在別的指令上、寫在引號裡都不算。"""
|
||||
_expr, env = find_checkout(cmd)
|
||||
return assignment in env
|
||||
|
||||
|
||||
def main():
|
||||
sys.stdout.write(find_checkout_target(sys.stdin.read()))
|
||||
argv = sys.argv[1:]
|
||||
cmd = sys.stdin.read()
|
||||
if len(argv) == 2 and argv[0] == "--escape":
|
||||
sys.stdout.write("1" if has_escape(cmd, argv[1]) else "")
|
||||
return
|
||||
sys.stdout.write(find_checkout_target(cmd))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -91,21 +91,31 @@ def _classify(tokens, verbs=("push",), strip_env=False):
|
||||
stays deliberately loose here (the verb may sit anywhere after `git`) --
|
||||
callers re-walk the returned token list to confirm command position, so
|
||||
a false positive at this layer costs nothing."""
|
||||
body = tokens
|
||||
if strip_env:
|
||||
# `FOO=1 git checkout …` -- an env prefix doesn't change which command
|
||||
# runs. Opt-in so find_push_target's behaviour stays byte-identical;
|
||||
# checkout_target_dir.py turns it on (its predecessor stripped these,
|
||||
# and dropping that would have been a silent regression -- caught by
|
||||
# the worktree gate's own case ㉒).
|
||||
#
|
||||
# 2026-09-07 (inkstone/ISEP#109 -> comment 6587): the prefix is skipped
|
||||
# for *classifying* the statement, but the "verb" event hands back the
|
||||
# full token list, prefix included. The worktree gate's escape hatch is
|
||||
# exactly that prefix (`WORKTREE_OK=1 git checkout …`), and a PreToolUse
|
||||
# hook can only see it in the command string -- it never reaches the
|
||||
# hook's own environment. Dropping it here made the printed way out
|
||||
# unwalkable. Callers that don't care simply skip leading NAME=value
|
||||
# tokens themselves (checkout_target_dir._moves_head does).
|
||||
i = 0
|
||||
while i < len(tokens) and _ENV_ASSIGN.match(tokens[i]):
|
||||
i += 1
|
||||
tokens = tokens[i:]
|
||||
if not tokens:
|
||||
body = tokens[i:]
|
||||
if not body:
|
||||
return ("other", None)
|
||||
if tokens[0] == "cd" and len(tokens) > 1:
|
||||
return ("cd", tokens[1])
|
||||
if tokens[0] == "git" and any(v in tokens[1:] for v in verbs):
|
||||
if body[0] == "cd" and len(body) > 1:
|
||||
return ("cd", body[1])
|
||||
if body[0] == "git" and any(v in body[1:] for v in verbs):
|
||||
return ("verb", tokens)
|
||||
return ("other", None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user