Skip to content

Instantly share code, notes, and snippets.

@sfate
Last active May 16, 2017 08:17
Show Gist options
  • Save sfate/34eb39347e0175a0a182501250eed467 to your computer and use it in GitHub Desktop.
Save sfate/34eb39347e0175a0a182501250eed467 to your computer and use it in GitHub Desktop.
Git pre-commit hook
#!/bin/bash
#
# Pre-commit hook script to avoid debug statements to be commited.
# Place under `$PROJECT_DIR/.git/hooks/pre-commit`
# Make script executable `chmod +x $PROJECT_DIR/.git/hooks/pre-commit`
#
# https://github.com/sfate
STATEMENTS="puts\|debugger\|binding.pry\|alert(\|console.log("
OUTPUT=$(git diff --cached --name-only | xargs git grep -wnH " $STATEMENTS" | awk '$0="\t"$0')
if [ -n "$OUTPUT" ]; then
echo "On branch $(git rev-parse --abbrev-ref HEAD)"
echo 'Changes staged for commit with debug statements:'
echo ' (remove debug statements first)'
echo
echo -en "\033[0;31m$OUTPUT\033[0m"
echo
echo
echo 'no changes were commited! (remove debug lines and stage files back)'
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment