Skip to content

Instantly share code, notes, and snippets.

@faun
Last active August 29, 2015 14:09
Show Gist options
  • Save faun/6f051cf27704cbc9a2d6 to your computer and use it in GitHub Desktop.
Save faun/6f051cf27704cbc9a2d6 to your computer and use it in GitHub Desktop.
Pre-commit hook
#!/bin/bash
## START PRECOMMIT HOOK
# Install with:
# ln -s ../../bin/pre-commit .git/hooks/pre-commit
files_modified=`git status --porcelain | egrep "^(A |M |R ).*" | awk ' { if ($3 == "->") print $4; else print $2 } '`
for f in $files_modified; do
if [[ $f == vendor/* ]]; then
echo "skipping $f"
continue
fi
echo "Checking ${f}..."
if [[ $f == *.rb ]]; then
ruby -c $f
if [ $? != 0 ]; then
echo "File ${f} failed"
exit 1
fi
echo "Running Rubocop for $f"
rubocop $f
if [ $? != 0 ]; then
echo "Rubocop failed for $f"
exit 1
fi
if grep --color -n "binding.pry" $f; then
echo "File ${f} failed - found 'binding.pry'"
exit 1
fi
elif [[ $f == *.js ]]; then
bundle exec jshint $f
elif [[ $f == *.coffee ]]; then
bundle exec coffeelint $f
elif [[ $f == *.haml ]]; then
bundle exec haml --check $f
elif [[ $f == *.sass ]]; then
bundle exec sass --check $f
fi
if [ $? != 0 ]; then
echo "File ${f} failed"
exit 1
fi
done
exit
## END PRECOMMIT HOOK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment