Skip to content

Instantly share code, notes, and snippets.

@8bitDesigner
Last active February 1, 2024 06:42
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save 8bitDesigner/5869846 to your computer and use it in GitHub Desktop.
Save 8bitDesigner/5869846 to your computer and use it in GitHub Desktop.
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
@8bitDesigner
Copy link
Author

Done, thanks @stefansundin!

@stefansundin
Copy link

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