Skip to content

Instantly share code, notes, and snippets.

@ppdeassis
Last active February 19, 2026 17:45
Show Gist options
  • Select an option

  • Save ppdeassis/48387d9f49b41af23e7d to your computer and use it in GitHub Desktop.

Select an option

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
#
# ref: https://gist.github.com/ppdeassis/48387d9f49b41af23e7d
#
# install into git dir:
# curl \
# -fSL https://gist.githubusercontent.com/ppdeassis/48387d9f49b41af23e7d/raw/64af0d58321cd1d89fdb461995bc3560153cb7e2/pre-commit \
# -o .git/hooks/pre-commit \
# && chmod +x .git/hooks/pre-commit
#
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|_test).rb$ ]]; then
# rspec/focus:
if grep "focus: true do" "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains :focus meta key!"
exit 1
fi
# rspec/focus:
if grep ":focus do" "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains :focus meta key!"
exit 1
fi
# rspec/focus:
if grep " fit " "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains fit spec!"
exit 1
fi
# rspec/focus:
if grep " fcontext " "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains fcontext spec!"
exit 1
fi
# rspec/focus:
if grep " fdescribe " "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains fdescribe spec!"
exit 1
fi
# rspec/focus:
if grep " fscenario " "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains fscenario spec!"
exit 1
fi
# rspec/focus:
if grep " ffeature " "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains ffeature spec!"
exit 1
fi
# minitest/focus: grep para "focus\n"
if grep "focus$" "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains :focus meta key!"
exit 1
fi
# minitest/focus:
if grep " ftest " "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains :focus meta key!"
exit 1
fi
if grep " puts " "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains puts!"
exit 1
fi
fi
# Check if the file contains 'byebug'
if grep "byebug" "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains byebug!"
exit 1
fi
# Check if the file contains 'binding.pry'
if grep "binding.pry" "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains binding.pry!"
exit 1
fi
# Check if the file contains 'debugger'
if grep "debugger" "$FILE" | grep -q -v '^[[:space:]]*#'; then
echo "[ERROR] $FILE contains debugger!"
exit 1
fi
fi # [end] .rb
if [[ $FILE =~ .(js|ts|vue).*$ ]] && [[ ! $FILE =~ vendor/assets/javascripts ]] && [[ ! $FILE =~ node_modules ]]; then
# Check if the file contains 'console.log'
if grep "console.log" "$FILE" | grep -q -v '^[[:space:]]*//'; then
echo "[ERROR] $FILE contains console.log()!"
exit 1
fi
if grep "alert(" "$FILE" | grep -q -v '^[[:space:]]*//'; then
echo "[ERROR] $FILE contains alert()!"
exit 1
fi
if grep "debugger" "$FILE" | grep -q -v '^[[:space:]]*//'; then
echo "[ERROR] $FILE contains debugger!"
exit 1
fi
# Check if the file contains query debugging
if grep ".debug(" "$FILE" | grep -q -v '^[[:space:]]*//'; then
echo "[ERROR] $FILE contains debug()!"
exit 1
fi
# pry-js
if grep "pry.it" "$FILE" | grep -q -v '^[[:space:]]*//'; then
echo "[ERROR] $FILE contains pry!"
exit 1
fi
fi # [end] .(ts|js|vue)
# if [[ $FILE =~ .spec.js$ ]]; then
if [[ $FILE =~ (spec|test).(js|ts)$ ]]; then
if grep -q ".only(" "$FILE" | grep -q -v '^[[:space:]]*//'; 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment