Skip to content

Commit 1ff4b54

Browse files
authored
fix(hooks): guard-git.sh sed patterns work on macOS BSD sed (#1146)
`\s` and the `s///;t;s///` flow-control chain are GNU sed extensions that BSD sed (macOS default) does not support. When a command like `cd /path/to/wt && git push` was issued from a different cwd, the cd-path extraction silently returned an empty string, the hook fell back to the calling shell's branch, and produced false "Branch does not match required pattern" denials. Changes: - Replace `\s` with `[[:space:]]` in all sed patterns (lines 36, 37, 124). - Split the `s/quoted/.../p;t;s/unquoted/.../p` chain on the `-C` extraction into two separate `sed -nE` invocations — BSD sed parses the chained `s///` after `t` as a label and errors with "undefined label". - Update the matching `grep -qE` cd/-C guards to `[[:space:]]` for consistency. Verified end-to-end on macOS with all four code paths the hook protects (cd && push, -C push, -C with quoted path, multi -C, cd + -C precedence). Closes #1145
1 parent f2f0571 commit 1ff4b54

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

.claude/hooks/guard-git.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ fi
3333
# the opening `"` of a quoted path (which would leave a trailing `path"` in
3434
# NCOMMAND). The pattern re-anchors on `git`, so multi-`-C` chains (e.g.
3535
# `git -C /a -C /b push`) need a second pass to collapse the residual `-C`.
36-
NCOMMAND=$(echo "$COMMAND" | sed -E 's/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g')
37-
NCOMMAND=$(echo "$NCOMMAND" | sed -E 's/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|\s|&&\s*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g')
36+
NCOMMAND=$(echo "$COMMAND" | sed -E 's/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g')
37+
NCOMMAND=$(echo "$NCOMMAND" | sed -E 's/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+"[^"]+"/\1git/g; s/(^|[[:space:]]|&&[[:space:]]*)git[[:space:]]+-C[[:space:]]+[^"[:space:]][^[:space:]]*/\1git/g')
3838

3939
deny() {
4040
local reason="$1"
@@ -117,11 +117,16 @@ detect_work_dir() {
117117
# `git -C` is the explicit git-level override and wins over any ambient cd prefix,
118118
# so check it first (e.g. `cd /tmp && git -C /worktree push` targets /worktree).
119119
# Greedy `.*-C` anchors on the LAST `-C` in the chosen segment.
120-
if echo "$search_str" | grep -qE 'git\s+([^&|;]*\s)?-C\s+'; then
121-
work_dir=$(echo "$search_str" | sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p;t;s/.*-C[[:space:]]+([^[:space:]]+).*/\1/p')
120+
# Two separate sed invocations (quoted path first, then unquoted fallback) instead
121+
# of a single `;t;s` chain — BSD sed parses chained s/// after `t` as a label.
122+
if echo "$search_str" | grep -qE 'git[[:space:]]+([^&|;]*[[:space:]])?-C[[:space:]]+'; then
123+
work_dir=$(echo "$search_str" | sed -nE 's/.*-C[[:space:]]+"([^"]+)".*/\1/p')
124+
if [ -z "$work_dir" ]; then
125+
work_dir=$(echo "$search_str" | sed -nE 's/.*-C[[:space:]]+([^[:space:]]+).*/\1/p')
126+
fi
122127
fi
123-
if [ -z "$work_dir" ] && echo "$COMMAND" | grep -qE '^\s*cd\s+'; then
124-
work_dir=$(echo "$COMMAND" | sed -nE 's/^\s*cd\s+"?([^"&]+)"?\s*&&.*/\1/p')
128+
if [ -z "$work_dir" ] && echo "$COMMAND" | grep -qE '^[[:space:]]*cd[[:space:]]+'; then
129+
work_dir=$(echo "$COMMAND" | sed -nE 's/^[[:space:]]*cd[[:space:]]+"?([^"&]+)"?[[:space:]]*&&.*/\1/p')
125130
fi
126131
# Trim trailing whitespace
127132
work_dir="${work_dir%"${work_dir##*[![:space:]]}"}"

0 commit comments

Comments
 (0)