File size: 7,334 Bytes
807119f
 
 
e3581c4
807119f
 
 
 
 
 
 
e3581c4
807119f
 
e3581c4
807119f
 
 
e3581c4
807119f
 
 
e3581c4
807119f
 
 
e3581c4
807119f
 
e3581c4
 
 
807119f
e3581c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807119f
 
e3581c4
 
 
 
 
 
 
 
807119f
e3581c4
 
 
807119f
 
e3581c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807119f
 
e3581c4
 
807119f
e3581c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807119f
 
 
e3581c4
 
807119f
e3581c4
 
 
 
 
 
 
 
 
 
 
807119f
 
e3581c4
 
 
807119f
e3581c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
807119f
 
 
e3581c4
 
807119f
e3581c4
 
 
 
 
 
 
 
 
 
 
807119f
 
 
e3581c4
 
 
 
 
 
 
 
 
807119f
 
 
 
e3581c4
 
 
 
 
807119f
 
 
 
e3581c4
 
 
 
 
 
807119f
 
e3581c4
 
 
 
 
 
 
 
 
 
 
 
 
807119f
 
e3581c4
 
 
 
 
 
 
 
 
 
 
 
 
807119f
 
e3581c4
 
 
807119f
 
 
e3581c4
 
807119f
e3581c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env bash

# PiFlash Pre-commit Hook
# Validates staged changes before commit

set -euo pipefail

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_status() {
  echo -e "${BLUE}[PiFlash]${NC} $1"
}

print_success() {
  echo -e "${GREEN}${NC} $1"
}

print_warning() {
  echo -e "${YELLOW}⚠️${NC} $1"
}

print_error() {
  echo -e "${RED}${NC} $1"
}

require_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    print_error "Required command not found: $1"
    exit 1
  fi
}

get_file_size() {
  local file="$1"
  stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null
}

cleanup_trailing_whitespace() {
  local file="$1"
  python3 - "$file" <<'PY'
from pathlib import Path
import sys

path = Path(sys.argv[1])
data = path.read_text(encoding="utf-8", errors="ignore").splitlines()
path.write_text("\n".join(line.rstrip() for line in data) + ("\n" if data else ""), encoding="utf-8")
PY
}

validate_css_braces() {
  local file="$1"
  node - "$file" <<'NODE'
const fs = require('fs');
const path = process.argv[2];
const css = fs.readFileSync(path, 'utf8');

const openBraces = (css.match(/\{/g) || []).length;
const closeBraces = (css.match(/\}/g) || []).length;

if (openBraces !== closeBraces) {
  console.error(`Mismatched braces in ${path}`);
  process.exit(1);
}
NODE
}

validate_json() {
  local file="$1"
  node - "$file" <<'NODE'
const fs = require('fs');
const path = process.argv[2];
JSON.parse(fs.readFileSync(path, 'utf8'));
NODE
}

scan_sensitive_content() {
  local file="$1"
  local content
  content="$(git show ":$file" 2>/dev/null || true)"

  if [[ -z "$content" ]]; then
    return 0
  fi

  local patterns=(
    "password[[:space:]]*[:=][[:space:]]*['\"][^'\"]+['\"]"
    "api[_-]?key[[:space:]]*[:=][[:space:]]*['\"][^'\"]+['\"]"
    "secret[[:space:]]*[:=][[:space:]]*['\"][^'\"]+['\"]"
    "token[[:space:]]*[:=][[:space:]]*['\"][^'\"]+['\"]"
    "private[_-]?key"
    "BEGIN[[:space:]]+PRIVATE[[:space:]]+KEY"
    "ghp_[A-Za-z0-9_]+"
    "sk-[A-Za-z0-9]+"
  )

  local pattern
  for pattern in "${patterns[@]}"; do
    if grep -iE "$pattern" <<<"$content" >/dev/null 2>&1; then
      print_error "Potential sensitive data found in staged content: $file"
      print_error "Matched pattern: $pattern"
      exit 1
    fi
  done
}

print_status "Running pre-commit checks for PiFlash..."

if ! git rev-parse --git-dir >/dev/null 2>&1; then
  print_error "Not in a git repository"
  exit 1
fi

require_cmd git
require_cmd grep
require_cmd file

STAGED_FILES=()
while IFS= read -r -d '' file; do
  STAGED_FILES+=("$file")
done < <(git diff --cached --name-only --diff-filter=ACM -z)

if [[ ${#STAGED_FILES[@]} -eq 0 ]]; then
  print_warning "No staged files found"
  exit 0
fi

print_status "Staged files: ${#STAGED_FILES[@]}"

HTML_FILES=()
CSS_FILES=()
JS_FILES=()
JSON_FILES=()

for file in "${STAGED_FILES[@]}"; do
  case "$file" in
    *.html|*.htm) HTML_FILES+=("$file") ;;
    *.css|*.scss|*.sass) CSS_FILES+=("$file") ;;
    *.js|*.jsx|*.ts|*.tsx|*.mjs|*.cjs) JS_FILES+=("$file") ;;
    *.json) JSON_FILES+=("$file") ;;
  esac
done

print_status "Validating HTML files..."
if [[ ${#HTML_FILES[@]} -eq 0 ]]; then
  print_status "No HTML files to validate"
else
  for file in "${HTML_FILES[@]}"; do
    [[ -f "$file" ]] || continue

    if ! grep -q "<!DOCTYPE html>" "$file"; then
      print_error "Missing DOCTYPE in $file"
      exit 1
    fi

    if ! grep -q "<html" "$file" || ! grep -q "</html>" "$file"; then
      print_error "Invalid HTML structure in $file"
      exit 1
    fi

    print_success "HTML validation passed for $file"
  done
fi

print_status "Validating CSS files..."
if [[ ${#CSS_FILES[@]} -eq 0 ]]; then
  print_status "No CSS files to validate"
else
  require_cmd node
  for file in "${CSS_FILES[@]}"; do
    [[ -f "$file" ]] || continue

    if ! validate_css_braces "$file" >/dev/null 2>&1; then
      print_error "CSS syntax error in $file"
      exit 1
    fi

    print_success "CSS validation passed for $file"
  done
fi

print_status "Validating JavaScript/TypeScript files..."
if [[ ${#JS_FILES[@]} -eq 0 ]]; then
  print_status "No JavaScript files to validate"
else
  require_cmd node
  for file in "${JS_FILES[@]}"; do
    [[ -f "$file" ]] || continue

    if [[ "$file" =~ \.(js|mjs|cjs)$ ]]; then
      if ! node -c "$file" >/dev/null 2>&1; then
        print_error "JavaScript syntax error in $file"
        exit 1
      fi
    else
      print_warning "Skipping syntax parse for $file (Node cannot directly parse TypeScript)"
    fi

    if grep -n "console\.log" "$file" >/dev/null 2>&1; then
      print_warning "console.log found in $file"
    fi

    if grep -niE "TODO|FIXME|HACK" "$file" >/dev/null 2>&1; then
      print_warning "TODO/FIXME/HACK comments found in $file"
    fi

    print_success "Script validation passed for $file"
  done
fi

print_status "Validating JSON files..."
if [[ ${#JSON_FILES[@]} -eq 0 ]]; then
  print_status "No JSON files to validate"
else
  require_cmd node
  for file in "${JSON_FILES[@]}"; do
    [[ -f "$file" ]] || continue

    if ! validate_json "$file" >/dev/null 2>&1; then
      print_error "Invalid JSON in $file"
      exit 1
    fi

    print_success "JSON validation passed for $file"
  done
fi

print_status "Checking file sizes..."
MAX_SIZE=1048576
for file in "${STAGED_FILES[@]}"; do
  [[ -f "$file" ]] || continue
  size="$(get_file_size "$file" 2>/dev/null || echo 0)"

  if [[ "$size" -gt "$MAX_SIZE" ]]; then
    print_error "File too large: $file ($(($size / 1024))KB > 1024KB)"
    exit 1
  fi
done
print_success "File size check passed"

print_status "Checking line endings..."
for file in "${STAGED_FILES[@]}"; do
  [[ -f "$file" ]] || continue
  if file "$file" | grep -q "CRLF"; then
    print_warning "CRLF line endings found in $file"
  fi
done
print_success "Line ending check completed"

print_status "Checking for trailing whitespace..."
WHITESPACE_FILES=()
for file in "${STAGED_FILES[@]}"; do
  [[ -f "$file" ]] || continue
  if grep -n '[[:blank:]]$' "$file" >/dev/null 2>&1; then
    WHITESPACE_FILES+=("$file")
  fi
done

if [[ ${#WHITESPACE_FILES[@]} -gt 0 ]]; then
  print_warning "Trailing whitespace found in ${#WHITESPACE_FILES[@]} file(s)"
  print_status "Fixing trailing whitespace and re-staging..."

  require_cmd python3
  for file in "${WHITESPACE_FILES[@]}"; do
    cleanup_trailing_whitespace "$file"
    git add "$file"
  done

  print_success "Trailing whitespace fixed and re-staged"
else
  print_success "No trailing whitespace found"
fi

if [[ -n "${1:-}" && -f "${1:-}" ]]; then
  print_status "Checking commit message..."
  commit_msg="$(head -n1 "$1" | tr -d '\r')"

  if [[ ${#commit_msg} -gt 72 ]]; then
    print_warning "Commit message is longer than 72 characters"
  fi

  if grep -qE '^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+' <<<"$commit_msg"; then
    print_success "Conventional commit format detected"
  else
    print_warning "Consider using conventional commit format"
  fi
fi

print_status "Scanning staged content for sensitive data..."
for file in "${STAGED_FILES[@]}"; do
  scan_sensitive_content "$file"
done
print_success "Security scan completed"

print_success "All pre-commit checks passed"
print_status "Ready to commit ${#STAGED_FILES[@]} file(s)"

exit 0