Last active
April 20, 2017 07:12
-
-
Save elct9620/0f300801058a8db54460827f96c183db to your computer and use it in GitHub Desktop.
.git/hooks/pre-commit
This file contains 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 | |
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=123acdac4c698f24f2352cf34c3b12e246b48af1 | |
fi | |
# Check if rubocop is installed for the current project | |
bin/bundle exec 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 | |
bin/bundle exec rubocop ${FILES} --force-exclusion db/schema.rb | |
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 | |
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.\(js\|jsx\)$" | tr '\n' ' ')" | |
echo "${green}[ESLint][Info]: Checking JavaScript Style${NC}" | |
if [ -n "$FILES" ] | |
then | |
echo "${green}[ESLint][Info]: ${FILES}${NC}" | |
npm run eslint | |
if [ ! -f '.eslintrc.json' ]; then | |
echo "${yellow}[ESLint][Warning]: No .eslintrc.json config file.${NC}" | |
fi | |
if [ $? -ne 0 ]; then | |
echo "${red}[ESLint][Error]: Fix the issue and commit again${NC}" | |
exit 1 | |
fi | |
else | |
echo "${green}[ESLint][Info]: No files to check${NC}" | |
fi | |
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.\(css\|scss\|sass\)$" | tr '\n' ' ')" | |
echo "${green}[SCSSLint][Info]: Checking SCSS Style${NC}" | |
if [ -n "$FILES" ] | |
then | |
echo "${green}[SCSSLint][Info]: ${FILES}${NC}" | |
if [ ! -f '.scss-lint.yml' ]; then | |
echo "${yellow}[SCSSLint][Warning]: No .scss-lint.yml config file.${NC}" | |
fi | |
bundle exec scss-lint app/assets/stylesheets | |
if [ $? -ne 0 ]; then | |
echo "${red}[SCSSLint][Error]: Fix the issue and commit again${NC}" | |
exit 1 | |
fi | |
else | |
echo "${green}[SCSSLint][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