Skip to content

Instantly share code, notes, and snippets.

@aberezin
Last active July 6, 2020 15:40
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 aberezin/7078ec087c7e4f8a078dd518d30a75bf to your computer and use it in GitHub Desktop.
Save aberezin/7078ec087c7e4f8a078dd518d30a75bf to your computer and use it in GitHub Desktop.
# from https://medium.com/@ripoche.b/using-global-pre-commit-hook-to-prevent-committing-unwanted-code-edbbf957ad12
#
# To prevent debug code from being accidentally committed, simply add a comment near your
# debug code containing the keyword NO_GITCOMMIT (with underscore gone) and this script will abort the commit.
#
# I also made this available here
# https://gist.github.com/aberezin/7078ec087c7e4f8a078dd518d30a75bf
#
if git commit -av --dry-run | grep $(echo NO_GITCOMMIT | tr -d _) >/dev/null 2>&1
then
echo "This is a global pre-commit hook talking: Trying to commit non-committable code."
echo "Remove the NO_GITCOMMIT (The tag is with the _ gone) string and try again."
echo "You might have that tag in a file that is not yet added to staged but this hook rejects all bc it is not yet smart enought to know if you are doing a git commit -a or not. Also, because of the way the this hook is written, it will catch the previous commit that had a this tag. In either case, you can use --no--verify to force the commit."
exit 1
fi
if [ -d $(git rev-parse --show-toplevel)/.gitsecret ]; then
#This is flawed in that it will error out if any of the revealed files are already deleted
#We can deal with that by running true after calling it.
#But the bigger problem is that when it creates new .secret files, those wont be added to the index
#If it could give us a list of file paths that it created, we could add those
#We could parse the .gitsecret/paths/mapping.cfg
#git secret hide -d -P
true
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment