Repository navigation
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·77 lines (74 loc) · 3.04 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·77 lines (74 loc) · 3.04 KB
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
#!/bin/sh
# pre-commit hook: hygiene checks + shellcheck
# Checks (ordered fastest-first):
# 1. ASCII-only on staged *.ps1, *.md, *.sh files
# 2. Refuse commits directly on develop/main/master
# 3. Shellcheck on staged .sh files
set -e
# ---------------------------------------------------------------------------
# Check 1: ASCII-only on staged *.ps1, *.md, *.sh files
#
# Scanned extensions:
# .ps1 -- PS 5.1 on Windows uses CP1252; non-ASCII breaks string literals
# .md -- docs are the dominant source of em-dash / smart-quote leakage
# .sh -- shell scripts; em-dashes from copy-paste can break here-docs / strings
# ---------------------------------------------------------------------------
ASCII_STAGED=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ps1|md|sh)$' || true)
if [ -n "$ASCII_STAGED" ]; then
ASCII_FAIL=0
for file in $ASCII_STAGED; do
# Use staged content (not working tree) via git show
# perl -ne is used for non-ASCII detection (grep -P is GNU-only; macOS BSD grep lacks PCRE)
NON_ASCII=$(git show ":$file" | perl -ne 'print "$.: $_" if /[^\x00-\x7f]/' 2>/dev/null || true)
if [ -n "$NON_ASCII" ]; then
if [ "$ASCII_FAIL" -eq 0 ]; then
echo ""
echo "ERROR: Non-ASCII bytes detected in staged file(s)."
echo " Scanned extensions: .ps1 (PowerShell), .md (Markdown), .sh (shell)."
echo " PS 5.1 on Windows uses CP1252 -- non-ASCII bytes (em dashes, smart quotes)"
echo " break string literals at byte 0x94. Markdown / shell sources should also"
echo " stay ASCII-clean to keep grep / sed / diff portable across locales."
echo ""
fi
echo " $file:"
echo "$NON_ASCII" | sed 's/^/ /'
echo ""
ASCII_FAIL=1
fi
done
if [ "$ASCII_FAIL" -ne 0 ]; then
echo " Fix: replace non-ASCII characters with ASCII equivalents."
echo " em dash (U+2014) -> --"
echo " en dash (U+2013) -> -"
echo " smart quotes -> straight quotes"
echo " ellipsis (U+2026) -> ..."
echo ""
exit 1
fi
fi
# ---------------------------------------------------------------------------
# Check 2: Refuse commits directly on develop / main / master
# ---------------------------------------------------------------------------
current_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
case "$current_branch" in
develop|main|master)
echo ""
echo "ERROR: refusing to commit directly on '$current_branch'."
echo " Create a feature branch first:"
echo " git checkout -b feat/<issue>-<slug>"
echo ""
exit 1
;;
esac
# ---------------------------------------------------------------------------
# Check 3: Shellcheck on staged .sh files
# ---------------------------------------------------------------------------
if ! command -v shellcheck >/dev/null 2>&1; then
exit 0
fi
STAGED=$(git diff --cached --name-only --diff-filter=ACM | grep '\.sh$' || true)
if [ -z "$STAGED" ]; then
exit 0
fi
echo "[pre-commit] Running shellcheck on staged .sh files..."
echo "$STAGED" | xargs shellcheck