Skip to content

fix(hooks): use POSIX [[:space:]] in guard-git.sh grep patterns#1170

Merged
carlos-alm merged 2 commits into
mainfrom
fix/1157-guard-git-grep-posix-portability
May 20, 2026
Merged

fix(hooks): use POSIX [[:space:]] in guard-git.sh grep patterns#1170
carlos-alm merged 2 commits into
mainfrom
fix/1157-guard-git-grep-posix-portability

Conversation

@carlos-alm
Copy link
Copy Markdown
Contributor

Summary

  • Replace all remaining \s shorthand with POSIX [[:space:]] in the grep -qE patterns of .claude/hooks/guard-git.sh.
  • BSD/GNU grep accept \s in ERE mode as a vendor extension, but it is not POSIX. Stricter graps (e.g. busybox in Alpine) treat the pattern literally and miss matches.
  • Mirrors the convention established for the sed patterns in fix(hooks): guard-git.sh sed patterns work on macOS BSD sed #1146 — the file is now uniformly portable.

Context

Follow-up to Greptile feedback on PR #1153, which was closed as superseded by #1146 before this suggestion could be addressed. Tracked in #1157.

No behavior change on macOS or stock Linux runners (BSD/GNU grep both keep matching). The fix only affects POSIX-strict environments where \s was silently treated as a literal s.

Test plan

  • bash -n .claude/hooks/guard-git.sh — syntax check
  • Manual pattern checks on macOS BSD grep: git add ., git add -A, git add --all, git reset, git restore --staged, git push, gh pr create, git commit, cd /tmp && git status, and tab-separated variants — all match as before; git add file.txt and echo hello still no-match.
  • CI green

Closes #1157

PR #1146 fixed BSD sed portability by replacing \s with [[:space:]] in
sed patterns, but the grep -qE patterns in the same file still used \s.
\s is a vendor extension (GNU/BSD grep accept it in ERE mode) but is not
POSIX ERE, so it can fail on stricter graps such as busybox in Alpine
containers. Replace all occurrences with [[:space:]] for uniform POSIX
compliance and consistency with the existing sed patterns.

Closes #1157
@claude
Copy link
Copy Markdown

claude Bot commented May 19, 2026

Claude finished @carlos-alm's task —— View job


PR Review Complete

Root Cause Analysis

Problem Being Solved: The guard-git.sh hook uses \s shorthand in grep -qE patterns, which is a vendor extension (GNU/BSD grep) but not POSIX-compliant. This causes silent failures on stricter grep implementations like busybox in Alpine containers, where \s is treated literally as the character 's' rather than as a whitespace matcher.

Why This Approach: This is a legitimate portability fix, not a workaround. The PR correctly addresses the root cause by replacing non-portable regex syntax with POSIX-compliant equivalents. It follows the same pattern established in #1146 for sed patterns, ensuring uniform compliance across the entire file.

Risk Assessment: ✅ LOW RISK - Pure compatibility improvement with no behavior change on existing systems.

Phase Analysis

Phase 1: ✅ Root Cause - LEGITIMATE FIX

This addresses a real portability issue, not symptom masking. The \s shorthand fails silently on strict POSIX systems, allowing dangerous git commands to pass through unchecked.

Phase 2: ✅ Configuration Changes - NOT APPLICABLE

No configuration files, linting rules, or test thresholds modified. This is purely a pattern syntax fix.

Phase 3: ✅ Test Integrity - NOT APPLICABLE

No test files modified or assertions weakened.

Phase 4: ✅ Code Quality Review

Excellent quality:

  • Consistent transformation: All 12 occurrences systematically replaced
  • Preserves functionality: [[:space:]] is functionally equivalent to \s but POSIX-compliant
  • Pattern accuracy: Each replacement correctly handles word boundaries and spacing
  • Comment updates: Line 30 comment updated to reflect the new syntax
  • No regressions: BSD/GNU grep continue to work identically

Key changes verified:

  • Line 25: Git command detection pattern
  • Lines 56, 61, 66, 71-72, 78, 83: Dangerous command blocking patterns
  • Lines 161, 167, 175: Branch validation and commit patterns
  • All maintain identical matching semantics with better portability

Phase 5: ✅ Backlog Compliance

Backlog Compliance

  • Zero-dep: ✅ — No new dependencies, pure shell script fix
  • Foundation-aligned: ✅ — Improves P4 (zero-cost core) by ensuring hooks work across all POSIX environments
  • Problem-fit: 3/5 — Infrastructure improvement that ensures hooks function correctly for AI agents, but doesn't directly address core navigation/orientation problems
  • Breaking: No — Purely additive compatibility, no API/CLI changes
  • Tier: 1 (zero-dep ✓ + foundation-aligned ✓)

Critical Concerns

None identified. This is a well-executed portability fix.

Final Recommendation

  • Rating: ⭐⭐⭐⭐☆ (4/5)
  • Action: APPROVE
  • Reasoning: High-quality portability fix that eliminates silent failures on strict POSIX systems. Consistent implementation, no behavior change on existing systems, and follows established patterns from fix(hooks): guard-git.sh sed patterns work on macOS BSD sed #1146. The only reason this doesn't get 5 stars is that it's infrastructure maintenance rather than a breakthrough feature.

Recommendation: Approve and merge. This fix ensures the guard-git.sh hook works reliably across all deployment environments.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 19, 2026

Greptile Summary

This PR completes the POSIX portability cleanup of guard-git.sh by replacing all remaining \s ERE shorthands in grep -qE patterns with the POSIX character class [[:space:]]. The sed patterns were already updated in PR #1146; this follow-up makes the grep patterns consistent and ensures the hook functions correctly on POSIX-strict environments (e.g. busybox grep in Alpine) where \s is treated as a literal s.

  • All 11 occurrences of \s in grep -qE patterns replaced with [[:space:]] — no logic change, purely a portability fix.
  • The inline code comment on line 30 was also updated to match the new notation, keeping documentation in sync with the patterns.

Confidence Score: 5/5

Safe to merge — purely mechanical substitution of non-POSIX shorthand with its portable equivalent, with no changes to matching logic.

Every \s in grep -qE patterns has been replaced with [[:space:]], which is the correct POSIX ERE equivalent. The sed patterns were already using [[:space:]], and the remaining grep calls in detect_work_dir (lines 122, 128) were never using \s and are untouched. The test plan covers the full set of command patterns the hook guards. No behavioral change on BSD/GNU grep; the fix only matters on POSIX-strict grep implementations where \s was silently treated as literal s.

No files require special attention.

Important Files Changed

Filename Overview
.claude/hooks/guard-git.sh All \s shorthands in grep -qE patterns replaced with POSIX [[:space:]]; sed patterns were already POSIX-compliant from PR #1146, so the file is now uniformly portable with no logic changes.

Reviews (2): Last reviewed commit: "Merge branch 'main' into fix/1157-guard-..." | Re-trigger Greptile

@carlos-alm carlos-alm merged commit c9efaca into main May 20, 2026
21 checks passed
@carlos-alm carlos-alm deleted the fix/1157-guard-git-grep-posix-portability branch May 20, 2026 03:41
@github-actions github-actions Bot locked and limited conversation to collaborators May 20, 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.

follow-up: guard-git.sh grep patterns still use \s (BSD portability)

1 participant