Skip to content

Instantly share code, notes, and snippets.

@vadakattu
Forked from milancermak/pre-commit.sh
Last active September 18, 2018 15:54
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 vadakattu/c063a8505c1f469be873b316a21d8871 to your computer and use it in GitHub Desktop.
Save vadakattu/c063a8505c1f469be873b316a21d8871 to your computer and use it in GitHub Desktop.
Python pre-commit hook feature to check if python package requirements have changed. Useful for maintaining consistency across virtualenvs. Can be skipped using git commit --no-verify
#!/bin/sh
# To version control your hook, save this script inside your repository e.g. .githooks/
# Then run 'git config core.hooksPath .githooks' to point your local repository to it
# Check if the python package requirements match the existing requirements
pip freeze | diff requirements.txt -
if [ $? -ne 0 ]
then
echo "The requirements have changed!"
echo "Update the requirements file and commit it"
exit 1
fi
# Check that the updated requirements file has been staged
CHANGED=`git diff --stat -- requirements.txt | wc -l`
if [ $CHANGED -ne 0 ];
then
echo "The requirements have changed!"
echo "Stage the new requirements file"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment