Created
March 8, 2013 10:11
-
-
Save magopian/5115504 to your computer and use it in GitHub Desktop.
git pre-commit hook
This file contains 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 | |
# | |
# This pre-commit hook tests various conditions to allow the commit. | |
# It can be disabled by using the "-n" option with "git commit". | |
# | |
# To use it, link it to .git/hooks/pre-commit: | |
# | |
# ln -s `pwd`/pre-commit .git/hooks/ | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
against=HEAD | |
else | |
# Initial commit: diff against an empty tree object | |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
# If you want to allow non-ascii filenames set this variable to true. | |
allownonascii=$(git config hooks.allownonascii) | |
# Redirect output to stderr. | |
exec 1>&2 | |
echo "> Checking for non-ascii filenames" | |
# Cross platform projects tend to avoid non-ascii filenames; prevent | |
# them from being added to the repository. We exploit the fact that the | |
# printable range starts at the space character and ends with tilde. | |
if [ "$allownonascii" != "true" ] && | |
# Note that the use of brackets around a tr range is ok here, (it's | |
# even required, for portability to Solaris 10's /usr/bin/tr), since | |
# the square bracket bytes happen to fall in the designated range. | |
test $(git diff --cached --name-only --diff-filter=A -z $against | | |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 | |
then | |
echo "Error: Attempt to add a non-ascii file name." | |
echo | |
echo "This can cause problems if you want to work" | |
echo "with people on other platforms." | |
echo | |
echo "To be portable it is advisable to rename the file ..." | |
echo | |
echo "If you know what you are doing you can disable this" | |
echo "check using:" | |
echo | |
echo " git config hooks.allownonascii true" | |
echo | |
exit 1 | |
fi | |
# If there are whitespace errors, print the offending file names and fail. | |
#exec git diff-index --check --cached $against -- | |
echo "> Checking for whitespace errors" | |
res=$(git diff-index --check --cached $against --) | |
if test -n "$res"; then # if $res isn't empty, then some files have been found guilty | |
echo "Blocking commit, found whitespace errors in files commited:" | |
echo $res | |
exit 1 | |
fi | |
# make sure there's no TODO, debugging... left | |
echo "> Checking for bad text" | |
errors=0 | |
for bad in 'import pdb' 'import ipdb' 'TODO' 'print' 'assert False'; do | |
echo ">> checking for '$bad'" | |
res=$(git diff-index --name-only --cached $against -S"$bad" --) | |
if test -n "$res"; then # if $res isn't empty, then some files have been found guilty | |
echo "ERROR, found '$bad' in files commited:" | |
echo $res | |
errors=$(($errors + 1)) | |
fi | |
done | |
exit $errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment