Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martinwheeler/f03147e14f9d67eb4fd0a3437cdfd3e0 to your computer and use it in GitHub Desktop.
Save martinwheeler/f03147e14f9d67eb4fd0a3437cdfd3e0 to your computer and use it in GitHub Desktop.
#/usr/bin/env bash
git config --global init.templatedir '~/.git-templates' && \
mkdir -p ~/.git-templates/hooks && \
cd ~/.git-templates/hooks
cat >post-checkout <<'EOL'
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep -E --quiet "$1" && eval "$2"
}
# `npm install` and `npm prune` if the `package.json` file gets changed
# to update all the nodejs ( grunt ) dependencies deleting the unused packages (not listed into the `package.json` file)
check_run package.json "yarn install"
# `composer install` if the `composer.json` file gets changed
# to update all the php dependencies
check_run composer "composer install"
EOL
cp post-checkout ./post-merge && cp post-checkout ./post-fetch
chmod -R a+x .
@martinwheeler
Copy link
Author

martinwheeler commented May 25, 2018

Yarn & Composer Git Hooks

This snippet creates two simple post git hooks that will make your life as a web developer much easier.

After every 'merge' (runs on git pull), 'checkout' & 'fetch' the snippet will check the changes from your previous branch to the current one for your package.json and composer file. If there are changes it will then run yarn install and/or composer install for you.

Setup

  1. Run curl -L https://gist.githubusercontent.com/martinwheeler/f03147e14f9d67eb4fd0a3437cdfd3e0/raw/624c10f93a8c909dd19e82a97a453f90ddad157d/yarn-composer-post-merge-and-checkout-hooks.sh | bash
  2. then git init inside your local repo

Note

The git init will re-initialise the local hooks from the global .git-templates folder but it won't override any hooks that are already created with the same name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment