Skip to content

Instantly share code, notes, and snippets.

@xa-bi
Last active October 13, 2020 08:27
Show Gist options
  • Save xa-bi/4f2de943a4ef4281151dc035860a7ac5 to your computer and use it in GitHub Desktop.
Save xa-bi/4f2de943a4ef4281151dc035860a7ac5 to your computer and use it in GitHub Desktop.
pre-commit
#!/bin/bash
#
# Install:
# 1. Copy this file as `.git/hooks/pre-commit`
# 2. Make it executable `chmod +x .git/hooks/pre-commit`
# 3. Set your desired options in the `WORKFORCE_PRECOMMIT` ENV var
#
# Posible options: eslint, jstest, rubocop, rubytest
#
# Sample config: in your ~/.bashrc | ~/.zshrc | ...
# WORKFORCE_PRECOMMIT="eslint, jstest, rubocop, rubytest"
set -e
GREEN_COLOR=$(tput setaf 2)
RED_COLOR=$(tput setaf 1)
DEFAULT_COLOR=$(tput sgr0)
CHECK_NAME=(
'eslint'
'jstest'
'rubocop'
'rubytest'
)
CHECK_REGEXP=(
'\.jsx?$'
'(?!.*__helpers__.*)__test__.*\.jsx?$'
'\.rb$'
'_test\.rb$'
)
CHECK_CMD=(
"NODE_PATH='./node_modules:../app/javascript' eslint --ext .js,.jsx --report-unused-disable-directives --max-warnings 0"
"yarn test"
"rubocop"
"bundle exec rails test"
)
NUM_CHECKS=$((${#CHECK_NAME[@]} - 1))
CHECKS=",${WORKFORCE_PRECOMMIT},"
CHECKS="${CHECKS// /}"
for FILE in `git diff --cached --name-status | awk '$1 != "D" { print $2 }'`; do
for i in `seq 0 $NUM_CHECKS`; do
if [[ "$FILE" =~ ${CHECK_REGEXP[$i]} ]]; then
CHECK_FILES[$i]="${CHECK_FILES[$i]} $FILE"
fi
done
done
for i in `seq 0 $NUM_CHECKS`; do
if [ -n "${CHECK_FILES[$i]}" ] && [[ "${CHECKS}" == *",${CHECK_NAME[$i]},"* ]] ; then
echo -n "Executing [${CHECK_NAME[$i]}] ... "
/bin/bash -c "${CHECK_CMD[$i]} ${CHECK_FILES[$i]}"
echo "${GREEN_COLOR}OK${DEFAULT_COLOR}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment