Skip to content

Instantly share code, notes, and snippets.

@insoul
Last active August 29, 2015 13:57
Show Gist options
  • Save insoul/9361825 to your computer and use it in GitHub Desktop.
Save insoul/9361825 to your computer and use it in GitHub Desktop.
git pre-commit hook to protect debugging code and syntax error
#!/usr/bin/env bash
changed_files=`git status --short | grep '^[AMCU]' | awk '{print $2}' | grep -P '\.(rb|erb)'`
if [ -z "$changed_files" ]; then
exit 0
fi
while read -r file; do
result=`grep -rin 'binding.pry\|debugger' $file`
if [ -n "$result" ]
then
echo "Cannot be commited because: $result ."
exit 1
fi
done <<< "$changed_files"
ruby_files=`git status --short | grep -v '^D' | awk '{print $2}' | grep '\.rb$'`
if [ -z "$ruby_files" ]; then
exit 0
fi
while read -r file; do
ruby_syntax=`ruby -c $file 2>&1`
if [ $? -ne 0 ]
then
echo "Cannot be commited because ruby syntax error:"
echo "$ruby_syntax"
exit 1
fi
done <<< "$ruby_files"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment