Last active
August 6, 2021 15:08
-
-
Save manuwell/4b67f5383fac2642848faedb1c6f0cb0 to your computer and use it in GitHub Desktop.
Rubocop no pre-commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Check for ruby style errors | |
# To install follow: | |
# 1. add this file to your repository .git/hooks/pre-commit | |
# 2. chmod +x .git/hooks/pre-commit | |
# | |
# To skip rubocop checks, before you commit things do: | |
# SKIP_RC=1 git commit -m 'my skipped commit' | |
red='\033[0;31m' | |
green='\033[0;32m' | |
yellow='\033[0;33m' | |
NC='\033[0m' | |
against=HEAD | |
# Check if rubocop is installed for the current project | |
bin/bundle exec rubocop -v >/dev/null 2>&1 || { echo -e >&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 -e "${green}[Ruby Style][Info]: Checking Ruby Style${NC}" | |
if [ -n "$FILES" ] | |
then | |
echo -e "${green}[Ruby Style][Info]: ${FILES}${NC}" | |
if [ ! -f '.rubocop.yml' ]; then | |
echo -e "${yellow}[Ruby Style][Warning]: No .rubocop.yml config file.${NC}" | |
fi | |
if [ "$SKIP_RC" = "1" ]; then | |
echo -e "${green}[Ruby Style][SKIP]: Skipped rubocop spec ${NC}" | |
exit 0 | |
fi | |
# Run rubocop on the staged files | |
bin/bundle exec rubocop -a ${FILES} | |
if [ $? -ne 0 ]; then | |
echo -e "${red}[Ruby Style][Error]: Fix the issues and commit again${NC}" | |
exit 1 | |
fi | |
else | |
echo -e "${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