Skip to content

fix(hooks): guard-git.sh uses [[:space:]] in sed for BSD compatibility#1153

Closed
carlos-alm wants to merge 1 commit into
mainfrom
fix/1132-guard-git-bsd-sed
Closed

fix(hooks): guard-git.sh uses [[:space:]] in sed for BSD compatibility#1153
carlos-alm wants to merge 1 commit into
mainfrom
fix/1132-guard-git-bsd-sed

Conversation

@carlos-alm
Copy link
Copy Markdown
Contributor

Summary

  • BSD sed (macOS default) does not interpret \s as whitespace, so the cd <path> && git ... extractor and the git -C normalization regexes in .claude/hooks/guard-git.sh silently produced no output.
  • detect_work_dir then fell back to the shell cwd's branch, blocking valid pushes from worktrees whose cwd had a non-conformant branch name (e.g. worktree-agent-<hash>).
  • Replaced all three \s sites in sed regexes with [[:space:]], which is portable across BSD and GNU sed.

Test plan

  • Manual: feed the hook a JSON payload with {"command":"cd /tmp/fake-wt && git push origin HEAD:fix/some-fix"} while cwd is on a non-conformant branch — hook now exits 0 (previously denied).
  • Manual: confirm BSD sed locally treats \s as literal s and [[:space:]] as expected.

Closes #1132

BSD sed (default on macOS) does not interpret \s as whitespace, so
the cd-prefix and git -C normalization regexes silently produced no
output. detect_work_dir then fell back to the shell cwd's branch,
blocking valid pushes from worktrees with non-conformant cwd names.

Closes #1132
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 18, 2026

Greptile Summary

This PR fixes a macOS/BSD compatibility bug in guard-git.sh where BSD sed silently treats \s as a literal s rather than a whitespace shorthand, causing detect_work_dir to return an empty path and fall back to the shell cwd's branch — incorrectly blocking valid pushes from worktrees.

  • Replaced \s with [[:space:]] in the two git -C normalization sed expressions (lines 36–37) and the cd-path extraction sed expression (line 124), covering every sed-based regex in the file.
  • All grep -qE patterns still use \s; BSD grep supports \s as an extension, so these remain functional on macOS.

Confidence Score: 4/5

Safe to merge — the change is a targeted, correctly scoped portability fix with no behavioral change on GNU/Linux and a verified fix on macOS.

The three sed fixes are complete and correct for the reported issue. The only follow-up consideration is that roughly a dozen grep -qE patterns still use \s, which works on BSD grep in practice but is a minor consistency gap worth addressing in a follow-up.

.claude/hooks/guard-git.sh — straightforward change; the grep patterns left untouched are worth a follow-up pass for uniformity.

Important Files Changed

Filename Overview
.claude/hooks/guard-git.sh Three sed regex patterns updated to replace \s with [[:space:]] for BSD/macOS compatibility; all grep patterns still use \s, which is fine since BSD grep supports it, but creates a minor style inconsistency.

Comments Outside Diff (1)

  1. .claude/hooks/guard-git.sh, line 25 (link)

    P2 Remaining \s in grep patterns

    The three sed occurrences are correctly fixed, but the hook still uses \s in all grep -qE patterns (lines 25, 56, 61, 66, 71–72, 78, 83, 120, 123, 156, 162, 170). BSD grep on macOS does support \s in extended-regex mode, so these work in practice — but strictly speaking \s is not a POSIX ERE construct. Replacing them with [[:space:]] would make the script uniformly portable across any POSIX-compliant shell environment (e.g., Alpine containers, some CI runners) and consistent with the convention established by this PR.

    Fix in Claude Code

Fix All in Claude Code

Reviews (1): Last reviewed commit: "fix(hooks): guard-git.sh uses [[:space:]..." | Re-trigger Greptile

@carlos-alm
Copy link
Copy Markdown
Contributor Author

Thanks for the review @greptileai. This PR is being closed as superseded — PR #1146 (already merged on main) applied a more comprehensive fix for the same BSD-sed root cause, covering both this issue (#1132) and the related ;t; problem (#1135).

The remaining \s in grep -qE patterns that you flagged is a real follow-up: tracked as issue #1157 against current main.

@carlos-alm
Copy link
Copy Markdown
Contributor Author

Closing as superseded by #1146 (already merged on main).

PR #1146 fixed both BSD-sed root causes in .claude/hooks/guard-git.sh in one commit:

The diff in this PR would partially revert #1146 (it restores \s in two grep guards and the ;t; chain). Closing without merge.

Follow-up filed as #1157 for the remaining \s usages in the file's grep -qE patterns.

@carlos-alm carlos-alm closed this May 18, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators May 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

guard-git.sh: detect_work_dir regex fails on macOS sed, blocks valid pushes from worktrees

1 participant