|
#!/bin/bash |
|
# Looks for changes to Gemfile[.lock] or package.json, and automates running bundle and other tasks. |
|
# Does not run if your local branch is behind the remote. |
|
# https://gist.github.com/stefansundin/82051ad2c8565999b914 |
|
|
|
# post-checkout hook - looks for changes to Gemfile[.lock] or package.json, |
|
# when you change branches, and if found, reinstalls the given packages every |
|
|
|
# Exit early if this was only a file checkout, not a branch change ($3 == 1) |
|
[[ $3 == 0 ]] && exit 0 |
|
|
|
oldRef=$1 |
|
newRef=$2 |
|
|
|
# Exit early if the local branch is behind the remote |
|
LOCAL=$(git rev-parse @) |
|
REMOTE=$(git rev-parse @{u} 2> /dev/null) |
|
BASE=$(git merge-base @ @{u} 2> /dev/null) |
|
|
|
if [[ "$LOCAL" != "$REMOTE" && "$LOCAL" = "$BASE" ]]; then |
|
echo "You are behind origin, not running bundle/migrate post-checkout hook." |
|
exit 1 |
|
fi |
|
|
|
|
|
function changed { |
|
git diff --name-only $oldRef $newRef | grep "^$1" > /dev/null 2>&1 |
|
} |
|
|
|
if changed 'Gemfile.*'; then |
|
echo "Gemfile changed, bundling" |
|
bundle install |
|
[[ -d 'tmp' ]] && touch 'tmp/restart.txt' |
|
fi |
|
|
|
if changed 'bower.json'; then |
|
echo "bower.json changed, installing" |
|
bower install |
|
fi |
|
|
|
if changed 'db/migrate'; then |
|
echo "Migrations pending, migrating" |
|
bundle exec rake db:migrate RAILS_ENV=development |
|
bundle exec rake db:migrate RAILS_ENV=test |
|
# bundle exec rake db:migrate RAILS_ENV=production |
|
[[ -d 'tmp' ]] && touch 'tmp/restart.txt' |
|
fi |
|
|
|
if changed 'package.json'; then |
|
echo "Package.json changed, installing" |
|
npm install |
|
fi |