-
Notifications
You must be signed in to change notification settings - Fork 13
Git post checkout hook
Nicu Listana edited this page Jul 17, 2019
·
5 revisions
place this in:
.git/hooks/post-checkout
Upon switching branches, this will (as necessary):
- bundle install
- yarn install
- migrate or rollback any migrations relevant (or not relevant) to the branch, and checkout
db/schema.rb - git submodule update
If your repo does not have the submodule, run:
- git submodule update --init
- cd app/javascript/ui/shared
- git checkout shape
You can also decline the updates e.g. if you are just making a quick branch update don't want to rollback migrations (and lose data).
# modified from https://gist.github.com/brysgo/9980344
CHECKING_OUT_BRANCH=$3
OLD_BRANCH=$1
NEW_BRANCH=$2
if [ $CHECKING_OUT_BRANCH -eq 1 ]
then
FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status`
MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep 'D\tdb/migrate/([0-9]+)' | sort -r`
MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep 'A\tdb/migrate/([0-9]+)'`
exec < /dev/tty
read -p "Do you want to proceed with automated branch changes? y/n" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# check if we need to do a bundle
BUNDLE_CHANGED=`echo "$FILES_CHANGED" | egrep 'M\tGemfile.lock'`
if [ ! -z "$BUNDLE_CHANGED" ]; then
echo "Your Gemfile.lock has changed, running bundle"
bundle
fi
# check if we need to do a yarn install
PACKAGE_CHANGED=`echo "$FILES_CHANGED" | egrep 'M\tyarn.lock'`
if [ ! -z "$PACKAGE_CHANGED" ]; then
echo "Your yarn.lock has changed, running yarn"
yarn
fi
if [ ! -z "$MIGRATIONS_REMOVED" ]; then
echo "Rolling back missing migrations"
for migration in $MIGRATIONS_REMOVED
do
if [ $migration == "D" ]; then
continue
fi
git checkout "$OLD_BRANCH" -- "$migration"
VERSION=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
bundle exec rake db:migrate:down VERSION="$VERSION"
git reset
rm "$migration"
done
bundle exec rake db:test:prepare
git checkout db/schema.rb
fi
if [ ! -z "$MIGRATIONS_ADDED" ]; then
echo "New migrations have been added running migrations"
bundle exec rake db:migrate db:test:prepare
fi
fi
# always check out the branch's correct version of the submodule
git submodule update app/javascript/ui/shared
fi