Skip to content

Instantly share code, notes, and snippets.

@wookietreiber
Last active July 31, 2017 16:22
Show Gist options
  • Save wookietreiber/abad73d1c1811a1fed4a7019b34e9c5f to your computer and use it in GitHub Desktop.
Save wookietreiber/abad73d1c1811a1fed4a7019b34e9c5f to your computer and use it in GitHub Desktop.
git hook delegate
#!/bin/bash
GIT_HOOK_DIR="$0.d"
if [[ -d $GIT_HOOK_DIR ]] ; then
while IFS= read -r -d $'\0' hook ; do
"./$hook" || error="yes"
done < <(find "$GIT_HOOK_DIR" -mindepth 1 -print0)
[[ -n $error ]] && exit 1
fi
exit 0
@wookietreiber
Copy link
Author

This is a very simple delegate script for git hooks. The problem with git hooks is, that you can only have one per hook category, e.g. pre-commit. This delegate expects a directory, e.g. pre-commit.d and runs everything inside that directory.

Installation

To demonstrate the installation, we are going to check this repository itself using shellcheck. For this to work you will need to install shellcheck yourself using your package manager (pacman, yum, apt, ...). Once that is done:

# clone this repository
git clone https://gist.github.com/wookietreiber/abad73d1c1811a1fed4a7019b34e9c5f.git git-hook-delegate

# clone shellcheck hook repository
git clone https://gist.github.com/wookietreiber/3bf8621274caafed543fca6a3feab284.git git-hook-shellcheck

# symlink the delegate
cd git-hook-delegate/.git/hooks
ln -s ../../git-hook-delegate.sh pre-commit

# install shellcheck hook
mkdir pre-commit.d
cd pre-commit.d
ln -s ../../../../git-hook-shellcheck/git-hook-shellcheck.sh
cd ../../../

# change something that shellcheck will complain about
sed -i -e 's|find "$GIT_HOOK_DIR"|find $GIT_HOOK_DIR|' git-hook-delegate.sh

# try to commit
git commit -a

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