Skip to content

guard-git.sh: BSD sed incompatibility breaks -C work_dir detection on macOS #1135

@carlos-alm

Description

@carlos-alm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfollow-upDeferred work from PR reviews that needs tracking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions