Skip to content

Instantly share code, notes, and snippets.

@ppdeassis
Last active July 20, 2022 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ppdeassis/48387d9f49b41af23e7d to your computer and use it in GitHub Desktop.
Save ppdeassis/48387d9f49b41af23e7d to your computer and use it in GitHub Desktop.
Git pre-commit hook for a rails project. Add it to your `repo/.git/hooks/pre-commit` file and make sure it has +x permission.
#!/bin/bash
#
# install into git dir:
# curl \
# -fSL https://gist.githubusercontent.com/ppdeassis/48387d9f49b41af23e7d/raw/bfb0c8adb6fe57e965395dc2f4a6b3e6d0004128/pre-commit \
# -o .git/hooks/pre-commit \
# && chmod +x .git/hooks/pre-commit
#
#!/bin/bash
if git-rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
for FILE in $(git diff-index --name-only $against) ; do
# skipping removed files
if [ ! -f "$FILE" ]; then
continue
fi
if [[ $FILE =~ .rb$ ]]; then
if [[ $FILE =~ _spec.rb$ ]]; then
if grep -q "focus: true do" "$FILE"; then
echo "[ERROR] $FILE contains :focus meta key!"
exit 1
fi
if grep -q ":focus do" "$FILE"; then
echo "[ERROR] $FILE contains :focus meta key!"
exit 1
fi
if grep -q " fit " "$FILE"; then
echo "[ERROR] $FILE contains fit spec!"
exit 1
fi
if grep -q " fcontext " "$FILE"; then
echo "[ERROR] $FILE contains fcontext spec!"
exit 1
fi
if grep -q " fdescribe " "$FILE"; then
echo "[ERROR] $FILE contains fdescribe spec!"
exit 1
fi
if grep -q " fscenario " "$FILE"; then
echo "[ERROR] $FILE contains fscenario spec!"
exit 1
fi
if grep -q " ffeature " "$FILE"; then
echo "[ERROR] $FILE contains ffeature spec!"
exit 1
fi
if grep -q " puts " "$FILE"; then
echo "[ERROR] $FILE contains puts!"
exit 1
fi
fi
# Check if the file contains 'byebug'
if grep -q "byebug" "$FILE"; then
echo "[ERROR] $FILE contains byebug!"
exit 1
fi
# Check if the file contains 'binding.pry'
if grep -q "binding.pry" "$FILE"; then
echo "[ERROR] $FILE contains binding.pry!"
exit 1
fi
# Check if the file contains 'debugger'
if grep -q "debugger" "$FILE"; then
echo "[ERROR] $FILE contains debugger!"
exit 1
fi
fi # [end] .rb
if [[ $FILE =~ .(js|vue).*$ ]] && [[ ! $FILE =~ vendor/assets/javascripts ]] && [[ ! $FILE =~ node_modules ]]; then
# Check if the file contains 'console.log'
if grep -q "console.log" "$FILE"; then
echo "[ERROR] $FILE contains console.log()!"
exit 1
fi
if grep -q "alert(" "$FILE"; then
echo "[ERROR] $FILE contains alert()!"
exit 1
fi
if grep -q "debugger" "$FILE"; then
echo "[ERROR] $FILE contains debugger!"
exit 1
fi
# Check if the file contains query debugging
if grep -q ".debug(" "$FILE"; then
echo "[ERROR] $FILE contains debug()!"
exit 1
fi
# pry-js
if grep -q "pry.it" "$FILE"; then
echo "[ERROR] $FILE contains pry!"
exit 1
fi
fi # [end] .(js|vue)
if [[ $FILE =~ .spec.js$ ]]; then
if grep -q ".only(" "$FILE"; then
echo "[ERROR] $FILE contains only() filtering specs!"
exit 1
fi
fi
if [[ $FILE =~ structure.sql ]]; then
# Check for trailing whitespaces in structure.sql
if grep -q '[[:blank:]]$' "$FILE"; then
echo "[ERROR] $FILE contains trailing whitespaces!"
exit 1
fi
fi
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment