Skip to content

Instantly share code, notes, and snippets.

@jlgasparrini
Forked from tinogomes/pre-commit-complete
Created February 25, 2020 07:09
Show Gist options
  • Save jlgasparrini/4a5b83fab51adc4cb839f72785dbd541 to your computer and use it in GitHub Desktop.
Save jlgasparrini/4a5b83fab51adc4cb839f72785dbd541 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment