Skip to content

Instantly share code, notes, and snippets.

@pmoranga
Forked from skwashd/pre-commit
Last active February 28, 2024 11:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmoranga/c6997d08fa1e7b51625bf532ca1c603e to your computer and use it in GitHub Desktop.
Save pmoranga/c6997d08fa1e7b51625bf532ca1c603e to your computer and use it in GitHub Desktop.
Git pre-commit hook that blocks commits for files that contain swear words.
## Example using https://pre-commit.com/
repos:
- repo: local
hooks:
- id: dontship
name: DONTSHIP check - Block words
entry: '\bdie\b'
language: pygrep # https://pre-commit.com/#pygrep
types: [php]
- id: check-swear
name: Check Swear Words
entry: ./scripts/check_swear_words.sh
language: script
#!/bin/bash
## This script should run under pre-commit control and return error in case of any bad word is present in the files staged
# If you want to commit something with swearing use FUCK_IT=1 git commit ...
if [ -n "$FUCK_IT" ]; then
exit 0
fi
ROOT_DIR=$(git rev-parse --show-toplevel)
EXIT=0
# Definitely NSFW
WORD_FILE_URL_BASE="https://raw.githubusercontent.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/master/"
WORD_FILE_PATH_BASE=~/.swearwords
TMPFILE=$(mktemp)
check_for_words(){
LANG=$1
if [ ! -f "${WORD_FILE_PATH_BASE}-${LANG}" ]; then
wget -q -O - "${WORD_FILE_URL_BASE}/${LANG}" | tr '\n' '|' | sed 's/\|$//g' > "${WORD_FILE_PATH_BASE}-${LANG}"
fi
PATTERN="$(cat ${WORD_FILE_PATH_BASE}-${LANG} )"
if [[ -n "$PATTERN" ]]; then
for file in $(git diff --cached --name-only --diff-filter=ACM); do
if egrep -Hinw "$PATTERN" "$file" > $TMPFILE ; then
echo "Swear word with language: ${LANG} found in file: $(cat $TMPFILE)"
rm $TMPFILE
return 1
fi
done
fi
return 0
}
for i in en fr pt ru it es; do
check_for_words $i || exit 1
done
rm $TMPFILE
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment