The Golden Spot

I hope this helps someone who is learning about Linux and web application programming with Python, Django, and Javascript.

Tuesday, February 04, 2014

git pre-commit hook for finding Python print statements

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}" | wc -l`
if [ $count -gt 0 ];
   then
    echo
    echo "###############################################################################"
    echo
    echo "###############################################################################"
    echo "       " $count "print statement(s) found in commit!"
    echo
    echo "  Abort : "
    echo "  Commit: commit "
    echo
    echo ">>> \c"
    exec < /dev/tty
    read command
    exec <&-
    if [ ${#command} -lt 6 ];
        then
        echo "aborting"
        exit 1
    fi
    # Lower input received
    lowered=`echo ${command} | tr '[:upper:]' '[:lower:]'`

    if [ "$command" = "commit" ];
        then
        echo "committing print statement"
        exit $TESTING
    fi
    echo "aborting"
    exit 1
fi