Skip to content

Instantly share code, notes, and snippets.

@kmcelwee
Forked from gmodarelli/pre-commit
Last active June 15, 2021 15:55
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 kmcelwee/ea87b4f978c04ccd168ca72ad31b0483 to your computer and use it in GitHub Desktop.
Save kmcelwee/ea87b4f978c04ccd168ca72ad31b0483 to your computer and use it in GitHub Desktop.
RuboCop with git pre-commit
#!/bin/sh
#
# Check for ruby style errors and autocorrect.
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
NC='\033[0m'
# 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 ${FILES} --out /dev/null
if [ $? -ne 0 ]; then
rubocop --auto-correct
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