Skip to content

Instantly share code, notes, and snippets.

@tinogomes
Last active March 24, 2021 13:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tinogomes/4bd4344d72ccd133cb0dc5059ceaac24 to your computer and use it in GitHub Desktop.
Save tinogomes/4bd4344d72ccd133cb0dc5059ceaac24 to your computer and use it in GitHub Desktop.
Git Hook pre-commit to pass Rubocop and Brakeman on Rails application for validations
#!/bin/sh
#
# Check for ruby style errors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
NC='\033[0m'
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
# Change it to match your initial commit sha
against=123acdac4c698f24f2352cf34c3b12e246b48af1
fi
# Check if brakeman is installed for the current project
bin/bundle exec brakeman -v >/dev/null 2>&1 || \
{ echo >&2 "${red}[Brakeman][Fatal]: Add 'gem \"brakeman\", require: false, group: :development' to your Gemfile"; exit 1; }
# Check if rails_best_practices is installed for the current project
bin/bundle exec rails_best_practices -v >/dev/null 2>&1 || \
{ echo >&2 "${red}[RBP][Fatal]: Add 'gem \"rails_best_practices\", require: false, group: :development' to your Gemfile"; exit 1; }
# Check if rubycritic is installed for the current project
bin/bundle exec rubycritic -v >/dev/null 2>&1 || \
{ echo >&2 "${red}[RubyCritic][Fatal]: Add 'gem \"rubycritic\", require: false, group: :development' to your Gemfile"; exit 1; }
# Check if rubocop is installed for the current project
bin/bundle exec rubocop -v >/dev/null 2>&1 || \
{ echo >&2 "${red}[Rubocop][Fatal]: Add 'gem \"rubocop\", require: false, group: :development' to your Gemfile"; exit 1; }
# Get only the staged files
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.rb$" | tr '\n' ' ')"
if [ -n "$FILES" ]
then
echo "${green}[Rubocop][Info]: Checking Rubocop${NC}"
any_fail=0
bin/bundle exec brakeman -Az
if [ $? -ne 0]; then
echo "${red}[Brakeman][Error]: Fix the issues and commit again${NC}"
any_fail=1
else
echo "${green}[Brakeman][Info]: Everything Okay${NC}"
fi
bin/bundler exec rails_best_practices
if [ $? -ne 0 ]; then
echo "${red}[RBP][Error]: Fix the issues and commit again${NC}"
any_fail=1
else
echo "${green}[RBP][Info]: Everything Okay${NC}"
fi
bin/bundler exec rubycritic
if [ $? -ne 0 ]; then
echo "${red}[RubyCritic][Error]: Fix the issues and commit again${NC}"
any_fail=1
else
echo "${green}[RubyCritic][Info]: Everything Okay${NC}"
fi
if [ ! -f '.rubocop.yml' ]; then
echo "${yellow}[Rubocop][Warning]: No .rubocop.yml config file.${NC}"
fi
echo "${green}[Rubocop][Info]: ${FILES}${NC}"
bin/bundle exec rubocop -R ${FILES}
if [ $? -ne 0 ]; then
echo "${red}[Rubocop][Error]: Fix the issues and commit again${NC}"
any_fail=1
else
echo "${green}[Rubocop][Info]: Congrats${NC}"
fi
if [ $any_fail -ne 0]; then
exit 1
fi
else
echo "${green}[Rubocop][Info]: No files to check${NC}"
fi
exit 0
#!/bin/sh
#
# Check for ruby style errors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
NC='\033[0m'
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
# Change it to match your initial commit sha
against=123acdac4c698f24f2352cf34c3b12e246b48af1
fi
# Check if rubocop is installed for the current project
bin/bundle exec rubocop -v >/dev/null 2>&1 || { echo >&2 "${red}[Ruby Style][Fatal]: Add rubocop to your Gemfile"; exit 1; }
# Get only the staged files
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.rb$" | tr '\n' ' ')"
echo "${green}[Ruby Style][Info]: Checking Ruby Style${NC}"
if [ -n "$FILES" ]
then
echo "${green}[Ruby Style][Info]: ${FILES}${NC}"
if [ ! -f '.rubocop.yml' ]; then
echo "${yellow}[Ruby Style][Warning]: No .rubocop.yml config file.${NC}"
fi
# Run rubocop on the staged files
bin/bundle exec rubocop -P --force-exclusion ${FILES}
if [ $? -ne 0 ]; then
echo "${red}[Ruby Style][Error]: Fix the issues and commit again${NC}"
exit 1
fi
else
echo "${green}[Ruby Style][Info]: No files to check${NC}"
fi
exit 0
@thegreyfellow
Copy link

thegreyfellow commented Jul 22, 2018

Thanks that helped 🙏

edit: though I needed to put a space before ']', because it was throwing an error of missing ']'.
edit 2: also needed to modify 'bin/bundler' to 'bin/bundle'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment