Skip to content

Instantly share code, notes, and snippets.

@thaliaarchi
Forked from guilherme/gist:9604324
Last active June 7, 2018 20:03
Show Gist options
  • Save thaliaarchi/4fd2c934fcc620ad8da75f567c1ba6c4 to your computer and use it in GitHub Desktop.
Save thaliaarchi/4fd2c934fcc620ad8da75f567c1ba6c4 to your computer and use it in GitHub Desktop.
Git pre-commit hook that detects if the developer forget to remove all javascript console.log statements before commit.
#!/bin/sh
# Prevent console.log from appearing in commits
# https://gist.github.com/guilherme/9604324
# Redirect output to stderr
exec 1>&2
# Enable user input
exec < /dev/tty
consoleregexp='^\+.*console\.log'
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0; then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some occurrences of console.log in your modification. Are you sure want to continue? (y/n) " yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]; then
exit 0; # Continue
else
exit 1; # Halt
fi
fi
#!/bin/sh
# Prevent console.log from appearing in commits without user input
# https://gist.github.com/guilherme/9604324
# Redirect output to stderr
exec 1>&2
consoleregexp='^\+.*console\.log'
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0; then
echo "There are some occurrences of console.log in your modification. Remove them to commit."
exec git diff --cached | grep -ne $consoleregexp
exit 1;
fi
exit 0;
@thaliaarchi
Copy link
Author

To install, download and paste into your .git/hooks/pre-commit file and set it as executable with chmod +x pre-commit.

@thaliaarchi
Copy link
Author

In VS Code, and some other tools with git integration, user input through /dev/tty is not supported, so an alternate hook is included that omits user confirmation and always blocks commits including console.log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment