Skip to content

Commit eca7652

Browse files
committed
Add precommit check
1 parent 40aa6ec commit eca7652

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ repos:
7979
pass_filenames: false
8080
always_run: true
8181
entry: bash -c "uv run --frozen --all-extras --dev deptry src codegen-git/src --ignore DEP001"
82+
- repo: "local"
83+
hooks:
84+
- id: disallowed-words-check
85+
name: Check for disallowed words
86+
entry: scripts/disallowed-words-check.sh
87+
language: script
88+
files: '' # Check all files
8289
- repo: https://github.com/renovatebot/pre-commit-hooks
8390
rev: 39.120.3
8491
hooks:

scripts/disallowed-words-check.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
3+
DISALLOWED_WORDS_FILE=".github/disallowed-words.txt"
4+
5+
# 1) If the file doesn't exist, fail the commit.
6+
if [[ ! -f "$DISALLOWED_WORDS_FILE" ]]; then
7+
echo "ERROR: $DISALLOWED_WORDS_FILE not found."
8+
echo "Cannot proceed with disallowed word checks."
9+
exit 1
10+
fi
11+
12+
# 2) If Git LFS isn't installed, fail the commit.
13+
if ! command -v git-lfs &>/dev/null; then
14+
echo "ERROR: Git LFS not installed or not in PATH."
15+
echo "Cannot proceed with disallowed word checks."
16+
exit 1
17+
fi
18+
19+
# 3) If the file is still an LFS pointer (not synced), fail the commit.
20+
if grep -q "https://git-lfs.github.com/spec/v1" "$DISALLOWED_WORDS_FILE"; then
21+
echo "ERROR: $DISALLOWED_WORDS_FILE is an LFS pointer but not synced."
22+
echo "Cannot proceed with disallowed word checks."
23+
exit 1
24+
fi
25+
26+
# 4) Read the disallowed words (one per line).
27+
DISALLOWED_WORDS="$(grep -v '^[[:space:]]*$' "$DISALLOWED_WORDS_FILE")"
28+
if [[ -z "$DISALLOWED_WORDS" ]]; then
29+
echo "ERROR: No disallowed words found in $DISALLOWED_WORDS_FILE."
30+
echo "Cannot proceed with disallowed word checks."
31+
exit 1
32+
fi
33+
34+
# Build a single regex WITHOUT word boundaries.
35+
# NOTE: This is intentionally strict. For example, banning "cat" also flags "catastrophic".
36+
# Will tweak and change this to a more lenient regex later.
37+
DISALLOWED_REGEX="($(echo "$DISALLOWED_WORDS" | paste -s -d '|' -))"
38+
39+
# 5) Find staged files that are Added (A) or Modified (M).
40+
FILES_TO_CHECK=$(git diff --cached --name-status | egrep '^[AM]' | cut -f2)
41+
42+
FAILED=0
43+
44+
# 6) For each file:
45+
# - Check the filename itself.
46+
# - Check the file contents (if it exists).
47+
for FILE in $FILES_TO_CHECK; do
48+
FILENAME_MATCHES=$(echo "$FILE" | grep -i -E -o "$DISALLOWED_REGEX")
49+
if [[ -n "$FILENAME_MATCHES" ]]; then
50+
echo "ERROR: Filename '$FILE' contains these disallowed words:"
51+
echo "$FILENAME_MATCHES"
52+
FAILED=1
53+
fi
54+
55+
if [[ -f "$FILE" ]]; then
56+
CONTENT_MATCHES=$(grep -I -i -E -o "$DISALLOWED_REGEX" "$FILE" | sort -u)
57+
if [[ -n "$CONTENT_MATCHES" ]]; then
58+
echo "ERROR: File '$FILE' contains these disallowed words:"
59+
echo "$CONTENT_MATCHES"
60+
FAILED=1
61+
fi
62+
fi
63+
done
64+
65+
# 7) Block commit if any violations were found.
66+
if [[ $FAILED -eq 1 ]]; then
67+
exit 1
68+
fi
69+
70+
exit 0

0 commit comments

Comments
 (0)