Skip to content

Instantly share code, notes, and snippets.

@scottserok
Last active March 20, 2020 13:55
Show Gist options
  • Save scottserok/0838cdda3f07a6eb30b44ee475b1e57c to your computer and use it in GitHub Desktop.
Save scottserok/0838cdda3f07a6eb30b44ee475b1e57c to your computer and use it in GitHub Desktop.
A Git pre-commit hook for Ruby projects that validates syntax, looks for any lingering puts or byebug statements.
#!/bin/sh
#
# A pre-commit hook for Ruby projects.
#
# == Installation
#
# From within a Ruby project directory in your terminal:
#
# curl https://gist.github.com/scottserok/0838cdda3f07a6eb30b44ee475b1e57c/raw/fe00beb4a69c529c6391def23a4dc61e2711c2cb/ruby-pre-commit >> .git/hooks/pre-commit && chmod +x .git/hooks/pre-commit
# Redirect output to stderr.
exec 1>&2
# check the syntax for any changed ruby files
paths=$(git diff --cached --name-only | grep '.rb$')
for path in $paths; do
if test -f $path && ! ruby -c $path
then
cat <<\EOF
Error: Attempt to commit a Ruby file that does not pass syntax validation:
EOF
echo $path
exit 1
fi
done
# check for byebug statements
if test $(git grep -l ' byebug ' spec lib config | wc -c) != 0
then
cat <<\EOF
Error: Attempted to commit with a byebug statement
EOF
echo $(git grep -l ' byebug ' spec lib config)
exit 1
fi
# check for puts statements
if test $(git grep -l ' puts ' spec/lib spec/graphql lib config | grep -v lib/tasks | wc -c) != 0
then
cat <<\EOF
Error: Attempted to commit with a "puts" statement
EOF
echo $(git grep -l ' puts ' spec/lib spec/graphql lib config)
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment