Skip to content

Instantly share code, notes, and snippets.

@brownfoxsir
Forked from stuntgoat/pre-commit.sh
Created April 30, 2024 02:11
Show Gist options
  • Save brownfoxsir/869c225fb0337e3167b041efb72fabe8 to your computer and use it in GitHub Desktop.
Save brownfoxsir/869c225fb0337e3167b041efb72fabe8 to your computer and use it in GitHub Desktop.
Git pre-commit hook for finding Python added 'print' statements.
#!/bin/sh
# To enable this hook, rename this file to "pre-commit".
###############################################################################
# https://gist.github.com/stuntgoat/8800170
# Git pre-commit hook for finding and warning about Python print
# statements.
#
# 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 Python print statements
find_print='\+[[:space:]]*print[[: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