Last active
October 10, 2024 18:27
-
Star
(108)
You must be signed in to star a gist -
Fork
(30)
You must be signed in to fork a gist
-
-
Save guilherme/9604324 to your computer and use it in GitHub Desktop.
Git pre-commit hook that detects if the developer forget to remove all the javascript console.log 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 | |
# Redirect output to stderr. | |
exec 1>&2 | |
# enable user input | |
exec < /dev/tty | |
consoleregexp='console.log' | |
# CHECK | |
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 at your modification. Are you sure want to continue? (y/n)" yn | |
echo $yn | grep ^[Yy]$ | |
if [ $? -eq 0 ] | |
then | |
exit 0; #THE USER WANTS TO CONTINUE | |
else | |
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK | |
fi | |
fi |
@TimCreasman thanks for this script, I tested it on Windows / Linux and it seems that the -r
argument on tail -r
in your OffendingDiff is unkown.
I found the tac
command that works just fine
git diff --cached --color=always | tac | awk "/$1/{flag=1}/diff/{flag=0}flag" | tac
Update : on MacOS, make sure you have coreutils installed (brew install coreutils
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been working on a more general solution that takes a text file of blocked phrases as input and will detect if any of these phrases are in your commit. I also incorporated the color changes @TheophileMot added and formatted some additional things.
If you have a file
block-list.txt
with the following:It will detect these phrases and print out the offending diff statement.
Also disclaimer: I only tested this on OSX, but anyway here's the code: