forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·132 lines (116 loc) · 4.27 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·132 lines (116 loc) · 4.27 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
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
#!/bin/sh
# Universal source hygiene: fail fast on unresolved merge conflict markers.
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
if [ -n "$STAGED_FILES" ]; then
conflict_found=0
while IFS= read -r file; do
[ -n "$file" ] || continue
if git grep --cached -n -I -E '^(<<<<<<<|>>>>>>>)' -- "$file"; then
conflict_found=1
fi
done <<EOF
$STAGED_FILES
EOF
if [ "$conflict_found" -eq 1 ]; then
echo "Unresolved merge conflict markers found in staged files" >&2
exit 1
fi
fi
# Dart formatting for app/
STAGED_DART_FILES=$(git diff --cached --name-only --diff-filter=ACM "app/**.dart" | grep -v -e '\.gen\.dart$' -e '\.g\.dart$')
if [ -n "$STAGED_DART_FILES" ]; then
echo "Formatting staged Dart files in app/..."
echo "$STAGED_DART_FILES" | xargs dart format --line-length 120
echo "$STAGED_DART_FILES" | xargs git add
fi
# Python formatting for backend/
STAGED_PYTHON_FILES=$(git diff --cached --name-only --diff-filter=ACM "backend/**.py")
if [ -n "$STAGED_PYTHON_FILES" ]; then
echo "Formatting staged Python files in backend/..."
if command -v black >/dev/null 2>&1; then
BLACK_CMD="black"
elif python3 -m black --version >/dev/null 2>&1; then
BLACK_CMD="python3 -m black"
else
echo "black is not installed. Please run 'pip install black'" >&2
exit 1
fi
echo "$STAGED_PYTHON_FILES" | xargs $BLACK_CMD --line-length 120 --skip-string-normalization
echo "$STAGED_PYTHON_FILES" | xargs git add
fi
# ARB (l10n JSON) formatting for app/lib/l10n/
STAGED_ARB_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.arb$')
if [ -n "$STAGED_ARB_FILES" ]; then
if command -v jq >/dev/null 2>&1; then
echo "Formatting ARB files to 4-space JSON indent..."
for f in $STAGED_ARB_FILES; do
jq --indent 4 '.' "$f" > "$f.tmp" && mv "$f.tmp" "$f"
done
echo "$STAGED_ARB_FILES" | xargs git add
else
echo "jq is not installed. Please run 'sudo apt install jq' or 'brew install jq'" >&2
exit 1
fi
fi
# Web (JS/TS/JSX/TSX) formatting with Prettier
STAGED_WEB_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(js|jsx|ts|tsx)$' | grep -E '^web/')
if [ -n "$STAGED_WEB_FILES" ]; then
echo "Formatting staged web files with Prettier..."
for dir in web/frontend web/app web/personas-open-source; do
DIR_FILES=$(echo "$STAGED_WEB_FILES" | grep "^$dir/")
if [ -z "$DIR_FILES" ]; then
continue
fi
if [ -x "$dir/node_modules/.bin/prettier" ]; then
echo "$DIR_FILES" | xargs "$dir/node_modules/.bin/prettier" --write
elif command -v npx >/dev/null 2>&1; then
echo "$DIR_FILES" | xargs npx --prefix "$dir" prettier --write
elif command -v prettier >/dev/null 2>&1; then
echo "$DIR_FILES" | xargs prettier --write
else
echo "prettier not found for $dir. Run 'npm install' in $dir or install prettier globally." >&2
exit 1
fi
echo "$DIR_FILES" | xargs git add
done
fi
# C/C++ formatting for firmware/
STAGED_C_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(c|cpp|cc|cxx|h|hpp)$' | grep -E '^(omi/|omiGlass/)')
if [ -n "$STAGED_C_FILES" ]; then
echo "Formatting staged C/C++ files in firmware..."
if ! command -v clang-format >/dev/null 2>&1; then
echo "clang-format is not installed. Please install it first" >&2
exit 1
fi
echo "$STAGED_C_FILES" | xargs clang-format -i
echo "$STAGED_C_FILES" | xargs git add
fi
# Rust formatting/checking for desktop backend.
STAGED_DESKTOP_RUST_INPUTS=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^desktop/macos/Backend-Rust/' || true)
STAGED_DESKTOP_RUST_FILES=$(printf '%s\n' "$STAGED_DESKTOP_RUST_INPUTS" | grep -E '\.rs$' || true)
if [ -n "$STAGED_DESKTOP_RUST_INPUTS" ]; then
echo "Checking desktop Rust backend..."
if ! command -v cargo >/dev/null 2>&1; then
echo "cargo is not installed. Please install Rust/Cargo first" >&2
exit 1
fi
if [ -n "$STAGED_DESKTOP_RUST_FILES" ]; then
while IFS= read -r file; do
[ -n "$file" ] || continue
if ! rustfmt --edition 2021 "$file"; then
echo "rustfmt failed for $file" >&2
exit 1
fi
git add "$file"
done <<EOF
$STAGED_DESKTOP_RUST_FILES
EOF
fi
if ! (
cd desktop/macos/Backend-Rust && \
cargo check --locked
); then
exit 1
fi
fi
exit 0