Skip to content

Instantly share code, notes, and snippets.

@tozwierz
Forked from GianlucaGuarini/post-merge
Last active April 16, 2018 11:41
Show Gist options
  • Save tozwierz/9e98e8167bd40c220861 to your computer and use it in GitHub Desktop.
Save tozwierz/9e98e8167bd40c220861 to your computer and use it in GitHub Desktop.

How to create a global git commit hook by Matt Venables

1 Enable git templates (This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init):

git config --global init.templatedir '~/.git-templates'

2 Create a directory to hold the global hooks:

mkdir -p ~/.git-templates/hooks

3 Write your hooks in ~/.git-templates/hooks. For example, here's a post-commit hook (located in ~/.git-templates/hooks/post-commit):


#!/bin/sh

# Copy last commit hash to clipboard on commit
git log -1 --format=format:%h | pbcopy


# Add other post-commit hooks 

4 Make sure the hook is executable.

chmod a+x ~/.git-templates/hooks/post-commit

5 Re-initialize git in each existing repo you'd like to use this in:

git init

NOTE if you already have a hook defined in your local git repo, this will not overwrite it.

#/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# forked by Gianluca Guarini
# forked by Tomasz Zwierzchon
root_dir="$(git rev-parse --show-toplevel)"
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --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)
if hash npm 2>/dev/null; then
check_run package.json "npm install && npm prune"
fi
# `bower install` and `bower prune` if the `bower.json` file gets changed
# to install all the frontend dependencies removing the unused packages ( not listed into the `bower.json` file )
if hash bower 2>/dev/null; then
check_run bower.json "bower install && bower prune"
fi
# `composer install` if the `composer.json` file gets changed
# to update all the php dependencies
if hash composer 2>/dev/null; then
check_run composer "sudo composer install"
fi
# `compass compile` to compil all the scss files when they get changed
if hash compass 2>/dev/null; then
check_run .scss "compass compile"
fi
# Magento cache clean using n98-magerun
if [ -f $root_dir/app/Mage.php ]; then
if hash n98-magerun.phar 2>/dev/null; then
n98-magerun.phar cache:clean
else
echo "Install n98-magerun!"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment