Skip to content

Instantly share code, notes, and snippets.

@venkatesh22
Last active August 12, 2019 08:21
Show Gist options
  • Save venkatesh22/5972749 to your computer and use it in GitHub Desktop.
Save venkatesh22/5972749 to your computer and use it in GitHub Desktop.
Git pre-commit hook with django for checking debugging lines and run tests and jenkins

Keep your hook script in source control

Commit your hook script (say pre-commit.sh) at the root of your project and include the installation instructions in your README/documentation to encourage all developers use it.

Installation is nothing more than:

ln -s ../../pre-commit.sh .git/hooks/pre-commit
chmod a+x .git/hooks/pre-commit

in pre-commit.sh

#!/bin/sh
echo "Check for debug lines"

FILES_PATTERN='\.(py)(\..+)?$'
FORBIDDEN='pdb.set_trace()'
git diff --cached --name-only | \
grep -E $FILES_PATTERN | \
GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $FORBIDDEN && echo "COMMIT REJECTED Found '$FORBIDDEN' references. Please remove them before commiting" && exit 1
echo "run Jenkins or tests"
/home/venv/bin/python ./manage.py jenkins --settings=test.settings
RESULT=$?
[ $RESULT -ne 0 ] && echo "COMMIT REJECTED Found Failed Test Cases. Please fix them before commiting" && exit 1
exit 0

to skip all the above checks

git commit --no-verify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment