Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git post checkout hook that reminds you of South migration changes when changing branches. Can be useful when you are when you are testing out a branch from someone else that requires migrations. Put the file in .git/hooks/post-checkout
#!/bin/bash
# Git post checkout hook.
# Reminds you of South migration changes when switching branches.
# Can be useful when you are when you are testing out a branch from
# someone else that requires migrations.
# Put the file in .git/hooks/post-checkout
PREVIOUS_HEAD=$1
NEW_HEAD=$2
BRANCH_SWITCH=$3
if [ $BRANCH_SWITCH == "1" -a $PREVIOUS_HEAD != $NEW_HEAD ]; then
# Start from the repository root.
cd ./$(git rev-parse --show-cdup)
# Check if any migrations have changed.
CHANGED=`git diff $PREVIOUS_HEAD $NEW_HEAD --name-status | grep "^M.*migrations"`
if [ $? -eq "0" ]; then
echo "Beware. These migrations have changed:"
echo "$CHANGED"
fi
# Check if any migrations have been deleted.
DELETED=`git diff $PREVIOUS_HEAD $NEW_HEAD --name-status | grep "^D.*migrations"`
if [ $? -eq "0" ]; then
echo "These migrations in the old branch were deleted, you should consider if you need to undo them."
echo "$DELETED"
fi
if [ -n "$CHANGED" -o -n "$DELETED" ]; then
echo "Run these commands to roll back these migrations:"
echo " git checkout $PREVIOUS_HEAD"
echo "$CHANGED\n$DELETED" | sort -r | sed -e "s/_/ /g" -e "s/\// /g" | awk '/[a-z]+ migrations [0-9]+/ { printf "\tpython manage.py migrate %s %04d\n", $3, $5-1; }'
fi
# Check if any migrations have been added.
ADDED=`git diff $PREVIOUS_HEAD $NEW_HEAD --name-status | grep "^A.*migrations"`
if [ $? -eq "0" ]; then
echo "You also have some new migrations. Run them by using:"
echo " python manage.py migrate"
fi
# Check if submodules have been changed
MODULES=`git diff $PREVIOUS_HEAD $NEW_HEAD --name-status | grep "\.gitmodules$"`
if [ $? -eq "0" ]; then
echo "The submodules in this project has changed. Update submodules by running:"
echo " git submodule update --init"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.