Skip to content

Instantly share code, notes, and snippets.

@faun
Last active October 22, 2022 14:54
Show Gist options
  • Save faun/3802690 to your computer and use it in GitHub Desktop.
Save faun/3802690 to your computer and use it in GitHub Desktop.
Pre-commit hook to prevent committing invalid ruby files or binding.pry Runs cane on *.ruby files, jshint on *.js files and coffeelint on *.coffee files # place this file in ./script/pre-commit # and install with: # chmod +x ./script/pre-commit # ln -s ../../script/pre-commit .git/hooks/pre-commit
#!/bin/bash
## START PRECOMMIT HOOK
files_modified=`git status --porcelain | egrep "^(A |M |R ).*" | awk ' { if ($3 == "->") print $4; else print $2 } '`
[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm"
## use ruby defined in project
source .rvmrc
for f in $files_modified; do
echo "Checking ${f}..."
if [[ $f == *.rb ]]; then
ruby -c $f
if [ $? != 0 ]; then
echo "File ${f} failed"
exit 1
fi
echo "Running cane for $f"
cane -f $f
if [ $? != 0 ]; then
echo "Cane 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment