Skip to content

Instantly share code, notes, and snippets.

@souzagab
Forked from ppdeassis/pre-commit
Created March 15, 2021 11:55
Show Gist options
  • Save souzagab/6d7ece838aac0c2cd9a163cb33143520 to your computer and use it in GitHub Desktop.
Save souzagab/6d7ece838aac0c2cd9a163cb33143520 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/e1b6a2d4c9257ed6eecefedebeafa3655c98bb7a/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
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