Skip to content

Instantly share code, notes, and snippets.

@jihonrado
Created February 22, 2016 09:50
Show Gist options
  • Save jihonrado/a9c0f4564cbfe2ac7007 to your computer and use it in GitHub Desktop.
Save jihonrado/a9c0f4564cbfe2ac7007 to your computer and use it in GitHub Desktop.
Git pre-commit hook to execute rubocop
#!/bin/sh
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
# Check if rubocop is installed
rubocop -v >/dev/null 2>&1 || { echo >&2 "${RED}[Ruby Style][Fatal]: Install rubocop in your project or system"; exit 1; }
# Get only 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 ] && [ ! -f ~/.rubocop.yml ]; then
echo "${YELLOW}[Ruby Style][Warning]: No .rubocop.yml nor ~/.rubocop.yml config file.${NC}"
fi
# Run rubocop on staged files
rubocop ${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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment