Skip to content

Instantly share code, notes, and snippets.

@koppen
Created March 28, 2022 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koppen/30a0c1919f66efd3df62e014f82101b0 to your computer and use it in GitHub Desktop.
Save koppen/30a0c1919f66efd3df62e014f82101b0 to your computer and use it in GitHub Desktop.
.git/hooks/
#!/bin/sh
#
# Check for Ruby style errors using Rubocop
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=`git rev-list --max-parents=0 HEAD`
fi
# Check if rubocop is installed for the current project
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
rubocop --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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment