Skip to content

Instantly share code, notes, and snippets.

@franga2000
Created January 20, 2018 20:50
Show Gist options
  • Save franga2000/0fe85071c985bb93e6d05379be42c579 to your computer and use it in GitHub Desktop.
Save franga2000/0fe85071c985bb93e6d05379be42c579 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Run this before comitting new code
export C_CLEAR='\e[0m'
export C_GREEN='\e[1;32m'
export C_CYAN='\e[1;36m'
export C_RED='\e[1;31m'
export C_YELLOW='\e[1;33m'
function prompt_to_exit() {
exec < /dev/tty
while true; do
read -n 1 -p "Should I ignore this? (y/N) " choice
case "$choice" in
y|Y ) echo " As you wish..."; break;;
n|N ) echo " Exiting!"; exit 1;;
esac
done
}
function echo_status() {
echo -e $C_CYAN"$*"$C_CLEAR
}
function echo_error() {
echo -e $C_RED"$*"$C_CLEAR
}
# Change dir to code root
cd "$(dirname ${BASH_SOURCE[0]})/.."
# Activate virtualenv
source ../venv/bin/activate
# TODO: Somehow check requirements.txt - maybe even docker-based tests
# Check for leftover print statements
echo_status "Checking for leftover print statements..."
prints=$(grep -nr "print(.*)" . --include "*.py" --color=always)
if [ $? -eq 0 ]; then
echo_error "You have some print() statements in your code:"
echo $prints
exec < /dev/tty
prompt_to_exit
else
echo "None found"
fi
# Check for needed migrations
echo_status "Checking for missing migrations"
./manage.py makemigrations --dry-run --check
if [ $? -ne 0 ]; then
echo_error "You have some model changes missing migrations!"
prompt_to_exit
fi
staged=$(git diff --name-only --cached)
# Check if translations weren't updated
echo_status "Checking locale files..."
if [[ "$staged" == *"LC_MESSAGES"* && "$staged" != *"django.po"* ]]; then
echo_error "You are trying to commit new locale files without combining them"
echo_error "You should run "$C_YELLOW"updateTranslations.sh"$C_RED" to fix this!"
prompt_to_exit
fi
# Run tests
echo_status "Running tests..."
./manage.py test
if [ $? -ne 0 ]; then
echo_error "You should probably not be commiting this!"
exec < /dev/tty
prompt_to_exit
fi
echo_status "Pre-commit checks complete!"; echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment