Last active
September 15, 2020 08:39
-
-
Save waqashamid/4b183dba1b1fde2e9fd116f61818bdba to your computer and use it in GitHub Desktop.
pre-commit-hook.sh
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
#### Important Git information | |
Before you start committing code, please install our pre-commit hook for Git, which will check for code quality issues | |
(as per PEP8 conventions) and imports sorted incorrectly. | |
The file `pre-commit-hook.sh` is the hook script. To install this in your local repository, you need to symlink the script | |
in your ``.git/hooks`` folder. | |
The following commands will create the link. | |
``` | |
$ sudo chmod +x pre-commit-hook.sh | |
$ cd .git/hooks/ | |
$ ln -s ../../pre-commit-hook.sh pre-commit | |
$ cd ../.. | |
``` | |
#!/bin/bash | |
# Git pre-commit hook | |
##################### | |
# Terminal output formatting stuff | |
MIN_COVERAGE=95 | |
# Check, if any changes have been made | |
changes_count=$(git diff --cached | wc -l) | |
if [ "$changes_count" -ne "0" ]; then | |
######################################################################## | |
# Run code style check | |
old_stash=$(git rev-parse -q --verify refs/stash) | |
git stash save -q --keep-index | |
new_stash=$(git rev-parse -q --verify refs/stash) | |
echo | |
echo "$(tput setaf 6)$(tput bold)Checking your code against the PEP8 style guide to ensure code quality..$(tput sgr 0)" | |
modified_files=$(git diff --cached --name-status | grep -v ^D | awk '$1 $2 { print $2}' | grep -e .py$) | |
if [ -n "$modified_files" ]; then | |
flake8 $modified_files --max-line-length=120 | |
fi | |
code=$? | |
if [ "$code" -eq "0" ]; then | |
echo | |
echo -e "$(tput setaf 2)$(tput bold)Code quality looks fine.. $(tput sgr 0)" | |
echo | |
echo | |
echo | |
else | |
echo | |
echo "$(tput setaf 1)$(tput bold)Your changes violate PEP8 conventions! Please fix these and then push. $(tput sgr 0)" | |
echo | |
git stash pop -q | |
exit $code | |
fi; | |
######################################################################## | |
# Check proper ordering of imports | |
echo "$(tput setaf 6)$(tput bold)Checking if all imports are ordered properly..$(tput sgr 0)" | |
modified_files=$(git diff --cached --name-status | grep -v ^D | awk '$1 $2 { print $2}' | grep -e .py$) | |
if [ -n "$modified_files" ]; then | |
isort -c $modified_files | |
fi | |
code=$? | |
if [ "$code" -eq "0" ]; then | |
echo | |
echo -e "$(tput setaf 2)$(tput bold)Imports are ordered properly, continuing.. $(tput sgr 0)" | |
echo | |
echo | |
echo | |
else | |
echo | |
echo "$(tput setaf 1)$(tput bold)Imports are not ordered correctly! Please fix these and then push. $(tput sgr 0)" | |
echo | |
git stash pop -q | |
exit $code | |
fi; | |
######################################################################## | |
# Check for stray print statements or PDBs | |
echo "$(tput setaf 6)$(tput bold)Checking for stray PDB imports or print statements..$(tput sgr 0)" | |
modified_files=$(git diff --cached --name-status | grep -v ^D | awk '$1 $2 { print $2}' | grep -e .py$) | |
code=$? | |
if [ -n "$modified_files" ]; then | |
output=$(grep -HIrn --color=always 'print(\|import pdb\|set_trace' $modified_files | grep -v --color=always '# noqa') | |
code=$? | |
fi | |
code=$((1 - $code)) | |
if [ "$code" -eq "0" ]; then | |
echo | |
echo -e "$(tput setaf 2)$(tput bold)No PDB imports or print statements found, great job! Continuing.. $(tput sgr 0)" | |
echo | |
echo | |
echo | |
sleep 1.5 | |
else | |
echo | |
echo "$(tput setaf 1)$(tput bold)You missed the following lines which should NOT be committed! Please verify and commit again.. $(tput sgr 0)" | |
echo | |
echo "$output" | |
echo | |
git stash pop -q | |
exit 1 | |
fi; | |
git stash pop -q | |
fi; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment