Problem
detect_work_dir() in .claude/hooks/guard-git.sh (line 121) uses a sed expression with the t branch label syntax that GNU sed accepts but BSD sed (macOS default) rejects:
sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p;t;s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p'
On macOS this prints sed: 2: "s/.*-C[[:space:]]+\"([^\" ...": undefined label ';s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p', returns empty, and the hook falls back to the ambient cwd's branch for validation.
Impact
When an agent is running from a worktree like .claude/worktrees/agent-XXXX with a non-conforming branch name (e.g. worktree-agent-XXXX), every git -C /other/worktree push is denied with:
BLOCKED: Branch 'worktree-agent-XXXX' does not match required pattern.
…even though the targeted worktree's branch (fix/...) is valid. This forces agents to either rename the ambient worktree's branch or disable the hook.
Repro
printf '{"tool_input":{"command":"git -C /tmp push"}}' | bash .claude/hooks/guard-git.sh
Observes the sed error on macOS.
Fix
Either:
- Split into two sed commands (run quoted-form first, check output, then unquoted-form), or
- Use
-e with separate expressions and newlines instead of ;t; semicolon chaining, or
- Use a perl/awk one-liner that works identically on macOS and Linux.
Discovered while processing PR #1122.
Problem
detect_work_dir()in.claude/hooks/guard-git.sh(line 121) uses a sed expression with thetbranch label syntax that GNU sed accepts but BSD sed (macOS default) rejects:On macOS this prints
sed: 2: "s/.*-C[[:space:]]+\"([^\" ...": undefined label ';s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p', returns empty, and the hook falls back to the ambient cwd's branch for validation.Impact
When an agent is running from a worktree like
.claude/worktrees/agent-XXXXwith a non-conforming branch name (e.g.worktree-agent-XXXX), everygit -C /other/worktree pushis denied with:…even though the targeted worktree's branch (
fix/...) is valid. This forces agents to either rename the ambient worktree's branch or disable the hook.Repro
Observes the sed error on macOS.
Fix
Either:
-ewith separate expressions and newlines instead of;t;semicolon chaining, orDiscovered while processing PR #1122.