Skip to content

Instantly share code, notes, and snippets.

@alexcouper
Last active December 23, 2020 07:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcouper/9421502 to your computer and use it in GitHub Desktop.
Save alexcouper/9421502 to your computer and use it in GitHub Desktop.
My pre commit hook. No more pdb please!
# This hook was inspired by the work done on these two blogs:
# - http://blog.keul.it/2013/11/no-more-pdbsettrace-committed-git-pre.html
# - http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/
# Ensure that code that isn't part of the prospective commit isn't tested
# within the pre-commit script using git stash.
git stash -q --keep-index
# Test prospective commit
FILES_PATTERN='\.py(\..+)?$'
FORBIDDEN_PATTERN='^[^#]*pdb.set_trace()'
FORBIDDEN_FILES=`git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n \
-e $FORBIDDEN_PATTERN`
[ -n "$FORBIDDEN_FILES" ] && echo "COMMIT REJECTED \n$FORBIDDEN_FILES"
RETVAL=$?
git stash pop -q
[ $RETVAL -eq 1 ] && exit 0
@arnlaugsson
Copy link

Nice!

@koddsson
Copy link

koddsson commented Mar 8, 2014

I would have thought you would have used the grep command --colour instead of --color 🇬🇧 😏

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