Created
September 6, 2013 15:46
-
-
Save sindresorhus/6465739 to your computer and use it in GitHub Desktop.
Git hook to install npm dependencies after a `git pull`. Run `chmod +x post-merge` and put it in `.git/hooks/`. Though could really do whatever.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
npm install |
You could save the MD5 hashsum of package.json and check if it changes between merges, but that's really just overcomplicating it. Can't you just always run npm install & bower install
after every merge? If nothing has changd they won't do anything.
I think running npm install & bower install on any git pull is not really an elegant solution for three reasons:
- this really means that pulling a project every 10/15 minutes ( like often we do ) it will trigger many other useless npm and bower commands that could take at least ~30seconds per each ( imagine how many seconds wasted )
- npm install and bower install do not delete the old packages already fetched
- this solution does not work if you then add the composer commands ( we work mostly on Laravel projects )
I created a quick script for you. Let me know how it works: https://gist.github.com/sindresorhus/7996717
+1 to latest script
this doesn't work for me with zsh and nvm - can't find npm
@pajtai see nvm-sh/nvm#688 (comment) (googled for "git hook nvm path")... don't be so lazy and do some research 🤓 your problem is not related to this script.
#!/usr/bin/env bash
set -e
prevHEAD=$1
newHEAD=$2
checkoutType=$3
[[ $checkoutType == 1 ]] && checkoutType='branch' ||
checkoutType='file' ;
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
[[ $checkoutType = 'branch' ]] && {
changed_files="$(git diff-tree -r --name-only --no-commit-id $prevHEAD $newHEAD)"
# node - use whichever works for you
check_run yarn.lock "yarn install"
# check_run package-lock.json "npm install"
# composer
check_run composer.lock "composer install"
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your example is really easy, but I need something a bit more complicated:
how is it possible to trigger an
npm install
orbower install
on a post merge ONLY if my local package.json or bower.json gets updated?Working in big team, anytime someone installs a new grunt or bower dependency he must communicate that by voice or chat to all the other developers so it would be great to trigger it just by
git pull
if necessaryAnother idea could be also to trigger any npm or bower command by checking the commit messages example:
added the new jquery slider carousel :: bower install jquery
what's your opinion?