Skip to content

Instantly share code, notes, and snippets.

@andrewdieken
Created November 12, 2020 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewdieken/b06c0bd851921f2aa1622c6004742bc4 to your computer and use it in GitHub Desktop.
Save andrewdieken/b06c0bd851921f2aa1622c6004742bc4 to your computer and use it in GitHub Desktop.
pre-commit hook for JavaScript projects
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
is_commit_clean=true
# invalid strings
consoleregexp='^\+.*console\.log\|fdescribe\|debugger'
# list all the files staged for commit
files_changed=$(git diff --cached --name-only 2>&1)
for file in $files_changed
do
if git diff --cached $file | grep $consoleregexp >/dev/null; then
echo "* $file"
is_commit_clean=false
fi
done
if [ $is_commit_clean == false ]; then
echo ""
echo "Errors, aborting. Ensure the following statements are removed from the files above before commiting"
echo "**** console.log ****"
echo "**** fdescribe ****"
echo "**** debugger ****"
exit 1
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment