Skip to content

follow-up: guard-git.sh \s regex fails on macOS BSD sed #1145

@carlos-alm

Description

@carlos-alm

Discovered while processing PR #1133.

The guard-git.sh hook's detect_work_dir function uses sed patterns with \s (Perl/GNU shorthand) that BSD sed on macOS does not recognize. As a result, when invoking cd <path> && git push from a different worktree directory, the cd-path extraction silently fails and the hook falls back to using the current shell's cwd to detect the branch — producing a false 'Branch does not match required pattern' block.

Affected patterns in .claude/hooks/guard-git.sh:

  • Line 124: sed -nE 's/^\s*cd\s+"?([^"&]+)"?\s*&&.*/\1/p'\s does not match whitespace on macOS BSD sed.
  • Line 121: sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p;t;s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p' — the ;t;s flow control fails on macOS BSD sed ("undefined label" error when given a chained s command after t).

Reproduction (macOS):

echo 'git -C /tmp push' | sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p;t;s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p'
# → sed: undefined label

Fix: replace \s with [[:space:]] throughout, and split the multi-pass -C sed into two separate sed -nE invocations (or use awk) to avoid BSD sed's t label parsing.

Workaround used in the PR-1133 sweep: invoke push via an external bash script (#!/bin/bash ... exec git push) to bypass the hook's command-string parsing.

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