Skip to content

Instantly share code, notes, and snippets.

@andrewjmeier
Created March 26, 2018 22:27
Show Gist options
  • Save andrewjmeier/a119497bdca515245f32222c288109a3 to your computer and use it in GitHub Desktop.
Save andrewjmeier/a119497bdca515245f32222c288109a3 to your computer and use it in GitHub Desktop.
Ruby/JavaScript git pre commit hook
#!/bin/sh
# To enable this hook, rename this file to "pre-commit".
###############################################################################
# Forked from https://gist.github.com/stuntgoat/8800170
# Git pre-commit hook for finding and warning about print statements in Ruby
# and JavaScript code.
#
# Set this to 0 to turn off testing.
TESTING=1
# Get the current git HEAD
head=`git rev-parse --verify HEAD`
# BSD regex for finding print statements
find_print='\+[[:space:]]*[p|puts|console.log][[:space:]]*'
# Save output to $out var
out=`git diff ${head} | grep -e ${find_print}`
# Count number of prints
count=`echo "${out}" | grep -e '\w' | wc -l`
if [ $count -gt 0 ];
then
echo
echo "###############################################################################"
echo "$out"
echo "###############################################################################"
echo " " $count "print statement(s) found in commit!"
echo
echo " Abort : <enter>"
echo " Commit: commit <enter>"
echo
echo ">>> \c"
## Get stdin from a keyboard:
## http://stackoverflow.com/a/10015707
exec < /dev/tty
# Let user type option.
read command
## Close stdin.
exec <&-
# Lower input received
lowered=`echo ${command} | tr '[:upper:]' '[:lower:]'`
if [ "$command" = "commit" ];
then
echo "committing print statement(s)"
exit $TESTING
fi
echo "aborting"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment