Deferred from PR #1151 review.
Context: While processing PR #1151 from an agent worktree, the push call (using both git -C <path> and cd <path> && style) was incorrectly blocked by guard-git.sh with:
BLOCKED: Branch 'worktree-agent-XXX' does not match required pattern.
The agent ran the command from a parent worktree branch (worktree-agent-XXX), but used git -C / cd to target the PR worktree which was on the valid fix/1136-cargo-rustup-init-shim branch. The hook should have detected the target work dir and validated its branch.
Root cause: detect_work_dir() in .claude/hooks/guard-git.sh uses this sed pattern at line 121:
sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p;t;s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p'
The ;t; (test-and-branch) syntax is not portable to BSD sed shipped with macOS:
sed: 2: "s/.*-C[[:space:]]+"([^" ...": undefined label ';s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p'
Result: work_dir is empty, the fallback rev-parse --abbrev-ref HEAD runs from the agent's cwd, and the wrong branch is reported.
PR #1146 fixed several BSD sed issues but this ;t; chained pattern was missed.
Fix: Split the two s/.../\1/p patterns into separate -e arguments (or use grep+conditional). The branch was extracted manually inside #1151's flow as a workaround.
Repro:
printf 'gXt -C /tmp push origin foo\n' | \
sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p;t;s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p'
# sed: 2: ...: undefined label ...
Deferred from PR #1151 review.
Context: While processing PR #1151 from an agent worktree, the push call (using both
git -C <path>andcd <path> &&style) was incorrectly blocked byguard-git.shwith:The agent ran the command from a parent worktree branch (
worktree-agent-XXX), but usedgit -C/cdto target the PR worktree which was on the validfix/1136-cargo-rustup-init-shimbranch. The hook should have detected the target work dir and validated its branch.Root cause:
detect_work_dir()in.claude/hooks/guard-git.shuses this sed pattern at line 121:The
;t;(test-and-branch) syntax is not portable to BSD sed shipped with macOS:Result:
work_diris empty, the fallbackrev-parse --abbrev-ref HEADruns from the agent's cwd, and the wrong branch is reported.PR #1146 fixed several BSD sed issues but this
;t;chained pattern was missed.Fix: Split the two
s/.../\1/ppatterns into separate-earguments (or use grep+conditional). The branch was extracted manually inside #1151's flow as a workaround.Repro: