Skip to content

follow-up: guard-git.sh detect_work_dir sed pattern fails on BSD sed (macOS) #1159

@carlos-alm

Description

@carlos-alm

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 ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    follow-upDeferred work from PR reviews that needs tracking

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions