閘認得出「動的是哪個 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:
@@ -47,6 +47,9 @@
|
||||
# 2. 它不知道那個 repo 現在有沒有別條線在動——**不知道就一律隔離**,
|
||||
# 因為「現在剛好沒人」是會變的,而分支留在那裡是不會自己回去的。
|
||||
# 3. `git worktree add` 之後那份 worktree 裡的 checkout 全部放行(判準 ②)。
|
||||
# 4. **目錄解不出來就放行**(`cd $VAR && git checkout`、`cd -`、引號壞掉)。
|
||||
# 不是漏了,是選的:猜錯會指著別的 repo 叫人去開 worktree,而他會照做、
|
||||
# 然後以為自己隔離好了。**寧可漏擋,也不要給錯的下一步**(comment 5398)。
|
||||
set -eu
|
||||
|
||||
[ "${WORKTREE_OK:-}" = "1" ] && exit 0
|
||||
@@ -54,67 +57,46 @@ set -eu
|
||||
|
||||
PAYLOAD=$(cat 2>/dev/null || echo '{}')
|
||||
|
||||
TARGET=$(printf '%s' "$PAYLOAD" | python3 -c '
|
||||
import json, re, shlex, sys
|
||||
|
||||
# 這條指令會動到哪個目錄的 HEAD——**不是問「hook 站在哪」**。
|
||||
# 2026-08-29 實測(inkstone/ISEP#109 → comment 5398):`cd <別的 repo> && git checkout`
|
||||
# 舊版拿 payload 的 cwd 當答案,於是擋對了、卻**指著另一個 repo 叫人去開 worktree**。
|
||||
# 照著做的人會在錯的 repo 開一份用不到的,真正要隔離的那個沒開到,而他以為隔離好了。
|
||||
# 🔴 一道閘給錯下一步,比不擋更糟。解析住在 lib/checkout_target_dir.py(純 tokenize,
|
||||
# 不執行任何指令;cd 鏈、子殼不外洩、-C 贏過 cd 的優先序都在那裡,理由見該檔檔頭)。
|
||||
CMD=$(printf '%s' "$PAYLOAD" | python3 -c '
|
||||
import json, sys
|
||||
try:
|
||||
d = json.load(sys.stdin)
|
||||
except Exception:
|
||||
raise SystemExit
|
||||
|
||||
ti = d.get("tool_input") or {}
|
||||
cmd = (ti.get("command") or "") if isinstance(ti, dict) else ""
|
||||
if not cmd:
|
||||
raise SystemExit
|
||||
|
||||
cwd = d.get("cwd") or ""
|
||||
|
||||
# 逐段拆(; && || 換行),只看每一段的第一個 git
|
||||
for seg in re.split(r"(?:&&|\|\||;|\n)", cmd):
|
||||
seg = seg.strip()
|
||||
if not seg:
|
||||
continue
|
||||
try:
|
||||
tok = shlex.split(seg)
|
||||
except ValueError:
|
||||
continue
|
||||
# 允許前面掛 env(VAR=值 git …)
|
||||
while tok and re.match(r"^[A-Za-z_][A-Za-z0-9_]*=", tok[0]):
|
||||
tok.pop(0)
|
||||
if not tok or tok[0] != "git":
|
||||
continue
|
||||
tok = tok[1:]
|
||||
|
||||
# git -C <dir> …
|
||||
where = cwd
|
||||
while tok and tok[0].startswith("-"):
|
||||
if tok[0] == "-C" and len(tok) > 1:
|
||||
where = tok[1]; tok = tok[2:]; continue
|
||||
if tok[0].startswith("--git-dir"):
|
||||
raise SystemExit # 自己指 git-dir 的花式用法,不猜
|
||||
tok.pop(0)
|
||||
if not tok:
|
||||
continue
|
||||
|
||||
sub = tok[0]
|
||||
if sub not in ("checkout", "switch"):
|
||||
continue
|
||||
|
||||
rest = tok[1:]
|
||||
# 還原檔案的形狀,不動 HEAD ⇒ 放行
|
||||
if "--" in rest:
|
||||
continue
|
||||
if any(a in ("-p", "--patch", "--") for a in rest):
|
||||
continue
|
||||
# 沒有任何目標(`git checkout` 自己會報錯)⇒ 沒事
|
||||
if not [a for a in rest if not a.startswith("-")] and \
|
||||
not any(a in ("-b", "-B", "-c", "-C") for a in rest):
|
||||
continue
|
||||
|
||||
print(where or ".")
|
||||
break
|
||||
sys.stdout.write((ti.get("command") or "") if isinstance(ti, dict) else "")
|
||||
' 2>/dev/null) || exit 0
|
||||
[ -n "${CMD:-}" ] || exit 0
|
||||
|
||||
CWD=$(printf '%s' "$PAYLOAD" | python3 -c '
|
||||
import json, sys
|
||||
try:
|
||||
d = json.load(sys.stdin)
|
||||
except Exception:
|
||||
raise SystemExit
|
||||
sys.stdout.write(d.get("cwd") or "")
|
||||
' 2>/dev/null) || exit 0
|
||||
[ -n "${CWD:-}" ] && [ -d "$CWD" ] || CWD=$PWD
|
||||
|
||||
RESOLVER="$(dirname "$0")/lib/checkout_target_dir.py"
|
||||
if [ ! -f "$RESOLVER" ]; then
|
||||
# 解析器不見了=安裝壞了。**不靜默**:講出來再放行,因為這時候唯一能給的
|
||||
# 「下一步」就是錯的那一個(拿 cwd 當答案),而那正是本輪修掉的病。
|
||||
echo "⚠️ line-needs-own-worktree:找不到 lib/checkout_target_dir.py,這一次沒有檢查。" >&2
|
||||
exit 0
|
||||
fi
|
||||
|
||||
EXPR=$(printf '%s' "$CMD" | python3 "$RESOLVER" 2>/dev/null) || exit 0
|
||||
[ -n "${EXPR:-}" ] || exit 0 # 沒有會移動 HEAD 的 checkout/switch
|
||||
[ "$EXPR" = "?" ] && exit 0 # 目錄解不出來(cd $VAR / cd -)⇒ 不猜,見該檔檔頭
|
||||
|
||||
TARGET=$(cd "$CWD" 2>/dev/null && cd "$EXPR" 2>/dev/null && pwd) || exit 0
|
||||
[ -n "${TARGET:-}" ] || exit 0
|
||||
|
||||
# 那個目錄是不是 repo 的**主工作目錄**(=共用的那一份)——git 自己回答
|
||||
|
||||
Reference in New Issue
Block a user