Skip to content

Instantly share code, notes, and snippets.

@8bitDesigner

8bitDesigner/1.md

Last active Apr 16, 2018
Embed
What would you like to do?
Git post-merge hook which, when you run `git merge` or `git pull` will then `bundle` if the project's Gemfile changed, or `npm install` if the project's package.json changed.Inspired and based off of https://gist.github.com/bumi/5706550

Make bundleing and npm installing easy

This repo has some handy handy hooks to run bundle or npm install whenever you:

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

How can I has this!!?

  1. git clone https://gist.github.com/5869846.git hooks && cd hooks && chmod +x install
  2. ./install [path to folder where you keep all your repos]

This will install these hooks globally, and then re-init any repos it finds, installing your shiny new hooks along the way.

Things have gone wrong and I am sad!!1

You can uninstall these hooks by running ./install [path to your folders] remove.

#! /bin/bash
if [[ ! -d $1 ]]; then
echo "Usage: $0 ~/path/to/your/sites"
echo ""
echo "This script will take all the git hooks in your directory and"
echo "1. Install them in your git hook template directory so new repos you"
echo " start will get these hooks for free"
echo "2. Given the path to your sites, re-init any repos it finds, so they"
echo " now have the shiny new hooks installed"
exit 1
fi
hookDir="$(dirname $(which git))/../share/git-core/templates/hooks"
hooks=$(ls | grep -v $(basename $0) | grep -v '.md')
repos=$(find $1 -type d -name '.git' | sed 's/\/.git//')
if [[ $2 == 'remove' ]]; then
for hook in $hooks; do
rm "$hookDir/$hook"
done
for repo in $repos; do
cd $repo/.git/hooks
for hook in $hooks; do rm $hook; done
cd - > /dev/null 2>&1
done
else
for hook in $hooks; do
cp -f $hook "$hookDir/$hook"
chmod +x "$hookDir/$hook"
done
for repo in $repos; do
cd $repo && rm .git/hooks/* && git init && cd - > /dev/null 2>&1
done
fi
#! /bin/bash
# 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
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 'db/migrate'; then
echo "Migrations pending, migrating"
rake db:migrate RAILS_ENV=development
rake db:migrate RAILS_ENV=test
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/sh
# post-checkout hook - looks for changes to Gemfile[.lock] or package.json, and if
# found, reinstalls the given packages every time you `git merge` or `git pull`
# To install, copy to your project's .git/hooks folder, and `chmod +x post-merge`
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 'db/migrate'; then
echo "Migrations pending, migrating"
rake db:migrate RAILS_ENV=development
rake db:migrate RAILS_ENV=test
rake db:migrate RAILS_ENV=production
[[ -d 'tmp' ]] && touch 'tmp/restart.txt'
fi
if changed 'package.json'; then
npm install
fi
@stefansundin

This comment has been minimized.

Copy link

@stefansundin stefansundin commented Nov 5, 2013

rake db:migrate RAILS_ENV=development
rake db:migrate RAILS_ENV=test
rake db:migrate RAILS_ENV=production
@8bitDesigner

This comment has been minimized.

Copy link
Owner Author

@8bitDesigner 8bitDesigner commented Nov 20, 2013

Done, thanks @stefansundin!

@stefansundin

This comment has been minimized.

Copy link

@stefansundin stefansundin commented Jan 8, 2014

Another thing, you should update so the script uses bundle exec on the rake commands.

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.