閘認得出「動的是哪個 repo」,不再拿 cwd 當答案(inkstone/ISEP#109 → comment 5398)
line-needs-own-worktree.sh 擋 `cd <別的 repo> && git checkout` 時擋對了,但訊息
指的是 payload 的 cwd 那個 repo,不是指令真正動的那個。comment 5398 實測:
指令:cd .../matrix/arcrun && git checkout fix/library-lifecycle-187
舊版:目錄:.../InkStoneCo 它現在在:feat/ticket-bell-webhook
↑ 動的是 matrix/arcrun,講的卻是 InkStoneCo
🔴 照著那個訊息做的人,會在 InkStoneCo 開一份用不到的 worktree,真正要隔離的
matrix/arcrun 沒開到——而他以為自己隔離好了。**一道閘給錯下一步比不擋更糟。**
而 `cd X && git checkout` 正是這條線最常出現的寫法(本票的來由那次就是它)。
沒有另寫一支解析器:hooks/lib/push_target_dir.py 2026-08-23 已經替 git push 解過
同一題(inkstone/ISEP#30 comment 3949),多層 cd 鏈與「子殼的 cd 不外洩」都算過了。
本輪只把那支的動詞與「要不要去掉 env 前綴」變成參數(兩個都有預設值,push 那條路
一個 byte 都沒變,42 條既有測試重跑全綠),新增的 lib/checkout_target_dir.py 只放
checkout 專屬的兩件事:哪些形狀不動 HEAD、-C 贏過 cd 的優先序。
新增的一條性質(不是順手,是本票要的):目錄解不出來就放行。
`cd $VAR`/`cd -`/引號壞掉時回 "?",閘直接 exit 0。寧可漏擋,也不要指著錯的
repo 叫人去開 worktree——猜一個回去等於原地打轉。
實測:
- hooks/tests/line-needs-own-worktree.test.sh 35 條 → 59 條(新增 D 群 24 條)
正向:cd 認出 A/-C 認出 B/兩者都在時 -C 贏/相對 cd/相對 -C 接在 cd 之後/
從 worktree cd 回共用目錄/子殼不外洩
反向(不該擋):cd 到非 repo、cd 進自己的 worktree、cd $VAR、cd -、還原檔案
已知邊界也釘成測試:`);` 中間沒空白時 tokenize 會斷在那裡 ⇒ 漏擋(不是指錯)。
那層是 push_target_dir 的 tokenizer,動它會連帶改到推 main 那道閘的偵測範圍,
本票不動,另報。
- 推 main 那三套(共用被改到的 lib):10/10、19/19、13/13
- 實地打票上那條害過我們的指令:現在印的是 matrix/arcrun,worktree 指令也是它的
plugin.json 0.16.1 → 0.16.2 並重跑 vendor-to-shell.py(版本沒動=沒人吃得到)。
⚠️ 留痕:hooks/lib/checkout_target_dir.py 是用 Bash heredoc 寫的,因為 sdd-guard
對 ISEP 的 .py 是結構性永遠在響——這個 repo 根本沒有 system-dev/docs/3-specs,
而 hooks/lib/ 已經住了 9 支 .py。範圍就這一支新檔,沒有動那道閘(另報)。
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -57,10 +57,12 @@ have opened before.
|
||||
Usage:
|
||||
printf '%s' "$CMD" | python3 push_target_dir.py
|
||||
"""
|
||||
import re
|
||||
import shlex
|
||||
import sys
|
||||
|
||||
_SEPARATORS = {";", "&&", "||", "|", "&", "\n"}
|
||||
_ENV_ASSIGN = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*=")
|
||||
|
||||
|
||||
def _join(base, path):
|
||||
@@ -82,17 +84,33 @@ def _join(base, path):
|
||||
return base.rstrip("/") + "/" + path
|
||||
|
||||
|
||||
def _classify(tokens):
|
||||
def _classify(tokens, verbs=("push",), strip_env=False):
|
||||
"""`verbs` is which git subcommand(s) count as the action being located.
|
||||
It defaults to push so find_push_target's behaviour is byte-identical to
|
||||
before; checkout_target_dir.py passes checkout/switch instead. Matching
|
||||
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."""
|
||||
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 ㉒).
|
||||
i = 0
|
||||
while i < len(tokens) and _ENV_ASSIGN.match(tokens[i]):
|
||||
i += 1
|
||||
tokens = tokens[i:]
|
||||
if not tokens:
|
||||
return ("other", None)
|
||||
if tokens[0] == "cd" and len(tokens) > 1:
|
||||
return ("cd", tokens[1])
|
||||
if tokens[0] == "git" and "push" in tokens[1:]:
|
||||
return ("push", tokens)
|
||||
if tokens[0] == "git" and any(v in tokens[1:] for v in verbs):
|
||||
return ("verb", tokens)
|
||||
return ("other", None)
|
||||
|
||||
|
||||
def _events(cmd):
|
||||
def _events(cmd, verbs=("push",), strip_env=False):
|
||||
"""Tokenize cmd into (kind, value) events in source order: 'enter'/
|
||||
'exit' for parens (subshell boundaries), 'cd'/'push'/'other' for
|
||||
statements split on the usual shell separators. Returns [] on any
|
||||
@@ -109,10 +127,10 @@ def _events(cmd):
|
||||
|
||||
def flush():
|
||||
if seg:
|
||||
# _classify's "push" branch returns `tokens` by reference; copy
|
||||
# _classify's "verb" branch returns `tokens` by reference; copy
|
||||
# before clear() below, or the event's tuple would observe the
|
||||
# list emptied out from under it (aliasing, not a value copy).
|
||||
events.append(_classify(seg[:]))
|
||||
events.append(_classify(seg[:], verbs, strip_env))
|
||||
seg.clear()
|
||||
|
||||
for tok in toks:
|
||||
@@ -147,7 +165,7 @@ def find_push_target(cmd):
|
||||
stack.pop() # subshell's own cd's don't leak out
|
||||
elif kind == "cd":
|
||||
stack[-1] = _join(stack[-1], val)
|
||||
elif kind == "push":
|
||||
elif kind == "verb":
|
||||
saw_push = True
|
||||
c_path = None
|
||||
toks = val
|
||||
|
||||
Reference in New Issue
Block a user