Skip to content

Instantly share code, notes, and snippets.

@matiasgarciaisaia
Created January 29, 2014 21:11
Show Gist options
  • Save matiasgarciaisaia/8697278 to your computer and use it in GitHub Desktop.
Save matiasgarciaisaia/8697278 to your computer and use it in GitHub Desktop.
A simple hook to prevent commits adding Ruby string interpolations in single quotes, that will fail
#!/bin/sh
#
# A simple hook to prevent commits adding Ruby string interpolations
# in single quotes, that will fail.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
if [ $(git diff --cached -z $against |
grep -c "^\+.*'.*#{.*") != 0 ]
then
echo "ERROR: Attempt to commit a single quoted interpolation."
echo
echo "This won't work, as interpolations don't get expanded in"
echo "single quotes. Use double quotes instead:"
echo
echo " puts '#{object.message} won\\'t work'"
echo
echo "should become:"
# echo
echo " puts \"#{object.message} will work\""
echo
echo "You can"
# echo
echo " git commit --no-verify"
# echo
echo "to avoid this check and get your commit done anyway"
# echo
echo
echo "Offending lines are:"
echo
git diff --cached -z $against | grep "^\+.*'.*#{.*"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment