|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
4 | | -# Only check formatting when Rust files are staged |
5 | | -if git diff --cached --name-only --diff-filter=ACM | grep -q '\.rs$'; then |
6 | | - echo "pre-commit: checking Rust formatting..." |
7 | | - cargo fmt --all -- --check |
| 4 | +# ────────────────────────────────────────────────────────────── |
| 5 | +# Pre-commit hook for PurePoint |
| 6 | +# |
| 7 | +# Checks: rust-fmt, swift-fmt, hygiene |
| 8 | +# Skip individual: SKIP_HOOKS=rust-fmt,hygiene git commit ... |
| 9 | +# Skip all: SKIP_HOOKS=all git commit ... |
| 10 | +# Bypass entirely: git commit --no-verify |
| 11 | +# ────────────────────────────────────────────────────────────── |
| 12 | + |
| 13 | +FAILED=0 |
| 14 | + |
| 15 | +# Helpers ───────────────────────────────────────────────────── |
| 16 | + |
| 17 | +skip_check() { |
| 18 | + local name="$1" |
| 19 | + [[ "${SKIP_HOOKS:-}" == "all" ]] && return 0 |
| 20 | + echo ",${SKIP_HOOKS:-}," | grep -q ",$name," && return 0 |
| 21 | + return 1 |
| 22 | +} |
| 23 | + |
| 24 | +now_ms() { |
| 25 | + perl -MTime::HiRes=time -e 'printf "%.0f\n", time * 1000' |
| 26 | +} |
| 27 | + |
| 28 | +print_timing() { |
| 29 | + local label="$1" start="$2" end="$3" |
| 30 | + local elapsed=$(( end - start )) |
| 31 | + if (( elapsed >= 1000 )); then |
| 32 | + printf " [%s: %d.%03ds]\n" "$label" $((elapsed / 1000)) $((elapsed % 1000)) |
| 33 | + else |
| 34 | + printf " [%s: %dms]\n" "$label" "$elapsed" |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +# Staged files (cached once) ────────────────────────────────── |
| 39 | + |
| 40 | +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM) |
| 41 | + |
| 42 | +has_staged() { |
| 43 | + echo "$STAGED_FILES" | grep -q "$1" |
| 44 | +} |
| 45 | + |
| 46 | +# ══════════════════════════════════════════════════════════════ |
| 47 | +# Check: Rust formatting |
| 48 | +# ══════════════════════════════════════════════════════════════ |
| 49 | + |
| 50 | +if ! skip_check "rust-fmt" && has_staged '\.rs$'; then |
| 51 | + echo "pre-commit: checking Rust formatting..." |
| 52 | + start=$(now_ms) |
| 53 | + |
| 54 | + if ! cargo fmt --all -- --check; then |
| 55 | + echo "" |
| 56 | + echo " FAILED: Rust formatting" |
| 57 | + echo " Fix: cargo fmt --all" |
| 58 | + echo "" |
| 59 | + FAILED=1 |
| 60 | + fi |
| 61 | + |
| 62 | + end=$(now_ms) |
| 63 | + print_timing "rust-fmt" "$start" "$end" |
| 64 | +fi |
| 65 | + |
| 66 | +# ══════════════════════════════════════════════════════════════ |
| 67 | +# Check: Swift formatting |
| 68 | +# ══════════════════════════════════════════════════════════════ |
| 69 | + |
| 70 | +if ! skip_check "swift-fmt" && has_staged '\.swift$'; then |
| 71 | + SWIFT_FORMAT="" |
| 72 | + |
| 73 | + if command -v swift-format &>/dev/null; then |
| 74 | + SWIFT_FORMAT="swift-format" |
| 75 | + elif xcrun --find swift-format &>/dev/null 2>&1; then |
| 76 | + SWIFT_FORMAT="$(xcrun --find swift-format)" |
| 77 | + fi |
| 78 | + |
| 79 | + if [[ -z "$SWIFT_FORMAT" ]]; then |
| 80 | + echo "pre-commit: swift-format not found, skipping Swift format check" |
| 81 | + echo " Install: brew install swift-format" |
| 82 | + else |
| 83 | + echo "pre-commit: checking Swift formatting..." |
| 84 | + start=$(now_ms) |
| 85 | + |
| 86 | + SWIFT_STAGED=$(echo "$STAGED_FILES" | grep '\.swift$' || true) |
| 87 | + SWIFT_FAILED=0 |
| 88 | + |
| 89 | + while IFS= read -r file; do |
| 90 | + [[ -z "$file" ]] && continue |
| 91 | + if ! "$SWIFT_FORMAT" lint --strict "$file" 2>&1; then |
| 92 | + SWIFT_FAILED=1 |
| 93 | + fi |
| 94 | + done <<< "$SWIFT_STAGED" |
| 95 | + |
| 96 | + if (( SWIFT_FAILED )); then |
| 97 | + echo "" |
| 98 | + echo " FAILED: Swift formatting" |
| 99 | + echo " Fix: just fmt-swift" |
| 100 | + echo "" |
| 101 | + FAILED=1 |
| 102 | + fi |
| 103 | + |
| 104 | + end=$(now_ms) |
| 105 | + print_timing "swift-fmt" "$start" "$end" |
| 106 | + fi |
| 107 | +fi |
| 108 | + |
| 109 | +# ══════════════════════════════════════════════════════════════ |
| 110 | +# Check: File hygiene |
| 111 | +# ══════════════════════════════════════════════════════════════ |
| 112 | + |
| 113 | +if ! skip_check "hygiene" && [[ -n "$STAGED_FILES" ]]; then |
| 114 | + echo "pre-commit: checking file hygiene..." |
| 115 | + start=$(now_ms) |
| 116 | + HYGIENE_FAILED=0 |
| 117 | + |
| 118 | + # --- Large files (>1MB) --- |
| 119 | + while IFS= read -r file; do |
| 120 | + [[ -z "$file" ]] && continue |
| 121 | + [[ ! -f "$file" ]] && continue |
| 122 | + size=$(wc -c < "$file" | tr -d ' ') |
| 123 | + if (( size > 1048576 )); then |
| 124 | + size_mb=$(echo "scale=1; $size / 1048576" | bc) |
| 125 | + echo " BLOCKED: $file is ${size_mb}MB (limit: 1MB)" |
| 126 | + HYGIENE_FAILED=1 |
| 127 | + fi |
| 128 | + done <<< "$STAGED_FILES" |
| 129 | + |
| 130 | + # --- Merge conflict markers (text files only) --- |
| 131 | + while IFS= read -r file; do |
| 132 | + [[ -z "$file" ]] && continue |
| 133 | + [[ ! -f "$file" ]] && continue |
| 134 | + # Skip binary files |
| 135 | + if file --brief --mime "$file" | grep -q 'charset=binary'; then |
| 136 | + continue |
| 137 | + fi |
| 138 | + if grep -nE '^(<{7}|>{7}|={7})( |$)' "$file" > /dev/null 2>&1; then |
| 139 | + echo " BLOCKED: $file contains merge conflict markers" |
| 140 | + HYGIENE_FAILED=1 |
| 141 | + fi |
| 142 | + done <<< "$STAGED_FILES" |
| 143 | + |
| 144 | + # --- Secret patterns --- |
| 145 | + SECRET_PATTERNS=( |
| 146 | + 'AKIA[0-9A-Z]{16}' # AWS access key |
| 147 | + '-----BEGIN (RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----' # Private keys |
| 148 | + 'ghp_[0-9a-zA-Z]{36}' # GitHub PAT |
| 149 | + 'gho_[0-9a-zA-Z]{36}' # GitHub OAuth |
| 150 | + 'ghu_[0-9a-zA-Z]{36}' # GitHub user token |
| 151 | + 'ghs_[0-9a-zA-Z]{36}' # GitHub server token |
| 152 | + 'xoxb-[0-9a-zA-Z-]+' # Slack bot token |
| 153 | + 'xoxp-[0-9a-zA-Z-]+' # Slack user token |
| 154 | + 'xoxa-[0-9a-zA-Z-]+' # Slack app token |
| 155 | + 'sk-[0-9a-zA-Z]{20,}' # API keys (OpenAI, etc.) |
| 156 | + 'password\s*=\s*["\x27][^"\x27]{4,}' # password assignments |
| 157 | + 'api[_-]?key\s*=\s*["\x27][^"\x27]{8,}' # api key assignments |
| 158 | + ) |
| 159 | + |
| 160 | + while IFS= read -r file; do |
| 161 | + [[ -z "$file" ]] && continue |
| 162 | + [[ ! -f "$file" ]] && continue |
| 163 | + # Skip binary files |
| 164 | + if file --brief --mime "$file" | grep -q 'charset=binary'; then |
| 165 | + continue |
| 166 | + fi |
| 167 | + for pattern in "${SECRET_PATTERNS[@]}"; do |
| 168 | + if grep -nEi "$pattern" "$file" > /dev/null 2>&1; then |
| 169 | + echo " BLOCKED: $file may contain secrets (matched: ${pattern:0:30}...)" |
| 170 | + HYGIENE_FAILED=1 |
| 171 | + break |
| 172 | + fi |
| 173 | + done |
| 174 | + done <<< "$STAGED_FILES" |
| 175 | + |
| 176 | + if (( HYGIENE_FAILED )); then |
| 177 | + echo "" |
| 178 | + echo " FAILED: File hygiene checks" |
| 179 | + echo " Skip: SKIP_HOOKS=hygiene git commit ..." |
| 180 | + echo "" |
| 181 | + FAILED=1 |
| 182 | + fi |
| 183 | + |
| 184 | + end=$(now_ms) |
| 185 | + print_timing "hygiene" "$start" "$end" |
| 186 | +fi |
| 187 | + |
| 188 | +# ══════════════════════════════════════════════════════════════ |
| 189 | + |
| 190 | +if (( FAILED )); then |
| 191 | + echo "pre-commit: BLOCKED (see above)" |
| 192 | + echo " Skip individual checks: SKIP_HOOKS=<name>[,<name>] git commit ..." |
| 193 | + echo " Skip all pre-commit: SKIP_HOOKS=all git commit ..." |
| 194 | + echo " Bypass entirely: git commit --no-verify" |
| 195 | + exit 1 |
8 | 196 | fi |
0 commit comments