-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
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
To install, download and paste into your
.git/hooks/pre-commit
file and set it as executable withchmod +x pre-commit
.