Skip to content

Instantly share code, notes, and snippets.

@JohannesFischer
Last active December 6, 2017 23:48
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 JohannesFischer/0f97409e495153d3172095430d31006d to your computer and use it in GitHub Desktop.
Save JohannesFischer/0f97409e495153d3172095430d31006d to your computer and use it in GitHub Desktop.
Git pre commit hook - test modified files with ESLint and Rubocop and for focus tags in spec files
#!/bin/sh
FILE_COUNT=$(git status | grep 'modified:' | wc -l)
if [ $FILE_COUNT -gt 100 ] ; then
echo "File count is 100+, skipping pre-commit validation"
exit 0
fi
FAILED=0
FILES=$(git diff --cached --name-only --diff-filter=M)
# Check modified Javascript files with ESLint
JS_FILES=$(echo "$FILES" | grep '\.js$')
if [[ ! $JS_FILES = "" ]]; then
echo "Running ESLint..."
for file in ${JS_FILES}; do
git show :$file | node_modules/.bin/eslint --quiet $file
if [[ $? != 0 ]] ; then
FAILED=1
fi
done;
fi
# Check modified Ruby files with Rubocop
RB_FILES=$(echo "$FILES" | grep '\.rb$')
if [[ $RB_FILES != '' ]]; then
echo "Running Rubocop..."
bundle exec rubocop --force-exclusion $RB_FILES
OUT=$?
if [ $OUT -eq 1 ];then
FAILED=1
fi
fi
# Test files in spec folder for focus tags
SPEC_FILES=$(echo "$FILES" | grep '^spec\?')
if [[ ! $SPEC_FILES = "" ]]; then
for file in ${SPEC_FILES}; do
egrep -q ',\s?(:focus|focus:\s?true|:focus\s?=>\s?true)' $file
if [[ $? != '' ]] ; then
echo "Please focus and remove your focus tags before committing."
echo $file
FAILED=1
fi
done;
fi
if [[ $FAILED != 0 ]]; then
echo "Pre-commit test failed, git commit denied!"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment