Skip to content

Instantly share code, notes, and snippets.

@nickbouton
Forked from stefansundin/git-bundle-hook.md
Created February 21, 2017 19:39
Show Gist options
  • Save nickbouton/343e375ee12aa45c06ec7c496f3edb2a to your computer and use it in GitHub Desktop.
Save nickbouton/343e375ee12aa45c06ec7c496f3edb2a to your computer and use it in GitHub Desktop.
Git post-checkout and post-merge hooks to simplify bundling and other tasks.

Make bundleing and npm installing easy

These git hooks runs bundle or npm install automatically whenever you:

  • git checkout a new branch with a different Gemfile or package.json.
  • git pull a change to Gemfile or package.json.

How to install

  1. cd awesome_git_repo
  2. curl -fL https://gist.githubusercontent.com/stefansundin/82051ad2c8565999b914/raw/install-hooks.sh | sh

How to uninstall

  1. cd awesome_git_repo
  2. rm .git/hooks/post-checkout .git/hooks/post-merge
#!/bin/sh
# This script will install Git post-checkout and post-merge hooks that will automate your bundling other tasks.
# Install in current Git repo:
# curl -fL https://gist.githubusercontent.com/stefansundin/82051ad2c8565999b914/raw/install-hooks.sh | sh
# Uninstall:
# rm .git/hooks/post-checkout .git/hooks/post-merge
# in each repository that you've added this to.
GITROOT=`git rev-parse --show-toplevel 2> /dev/null`
echo
echo
if [ "$GITROOT" == "" ]; then
echo This does not appear to be a git repo.
exit 1
fi
if [ -f "$GITROOT/.git/hooks/post-checkout" ]; then
echo There is already a post-checkout hook installed. Delete it first.
echo
echo " rm '$GITROOT/.git/hooks/post-checkout'"
echo
exit 2
fi
if [ -f "$GITROOT/.git/hooks/post-merge" ]; then
echo There is already a post-merge hook installed. Delete it first.
echo
echo " rm '$GITROOT/.git/hooks/post-merge'"
echo
exit 3
fi
echo Downloading post-checkout and post-merge hooks from https://gist.github.com/stefansundin/82051ad2c8565999b914
echo
curl -L -o "$GITROOT/.git/hooks/post-checkout" https://gist.githubusercontent.com/stefansundin/82051ad2c8565999b914/raw/post-checkout
if [ ! -f "$GITROOT/.git/hooks/post-checkout" ]; then
echo Error downloading post-checkout hook!
exit 4
fi
curl -L -o "$GITROOT/.git/hooks/post-merge" https://gist.githubusercontent.com/stefansundin/82051ad2c8565999b914/raw/post-merge
if [ ! -f "$GITROOT/.git/hooks/post-merge" ]; then
echo Error downloading post-merge hook!
exit 5
fi
chmod +x "$GITROOT/.git/hooks/post-checkout" "$GITROOT/.git/hooks/post-merge"
echo "You're all set! Happy hacking!"
exit 0
#!/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
#!/bin/bash
# Looks for changes to Gemfile[.lock] or package.json, and automates running bundle and other tasks.
# https://gist.github.com/stefansundin/82051ad2c8565999b914
function changed {
git diff --name-only HEAD@{2} HEAD | 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment