Skip to content

Instantly share code, notes, and snippets.

@vickeryj
Created March 2, 2017 18:15
Show Gist options
  • Save vickeryj/c06d8e8ed0c030daa99bfa72f5d44e8b to your computer and use it in GitHub Desktop.
Save vickeryj/c06d8e8ed0c030daa99bfa72f5d44e8b to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Check for ruby style errors
# https://gist.github.com/johnnyji/d1226c9467a51f7d8b39
# Put this in .git/hooks/pre-commit
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=04583883cf57a67971957e4de28b8a3218d1ccff
fi
# Check if rubocop is installed for the current project
bin/bundle exec rubocop -R -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 -R ${FILES}
if [ $? -ne 0 ]; then
echo "${red}[Ruby Style][Error]: ${NC}"
fi
else
echo "${green}[Ruby Style][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