Skip to content

Instantly share code, notes, and snippets.

Last active Jan 2, 2016
What would you like to do?
Define git hooks within your repository. Allows defining multiple scripts for one hook.
HOOK_NAMES="applypatch-msg pre-applypatch post-applypatch pre-commit prepare-commit-msg commit-msg post-commit pre-rebase post-checkout post-merge pre-receive update post-receive post-update pre-auto-gc"
HOOK_DIR=$(git rev-parse --show-toplevel)/.git/hooks
DIR=$(dirname "$0")
for hook in $HOOK_NAMES; do
# If the hook already exists, is executable, and is not a symlink
if [ ! -h "$HOOK_DIR/$hook" -a -x "$HOOK_DIR/$hook" ]; then
mv "$HOOK_DIR/$hook" "$HOOK_DIR/$hook.local"
# create the symlink, overwriting the file if it exists
# probably the only way this would happen is if you're using an old version of git
# -- back when the sample hooks were not executable, instead of being named ____.sample
ln -s -f ../../bin/git-hooks/wrapper "$HOOK_DIR/$hook"
[ -f "$HOOK_DIR/env" ] || \
echo "# Place environment variable here. These are only for your local system.
export APPLICATION_ENV=prod" > "$HOOK_DIR/env"
set -e
. .git/hooks/env
if [ -x $0.local ]; then
$0.local "$@"
if [ -d "bin/git-hooks/$(basename $0)" ]; then
for SCRIPT in bin/git-hooks/$(basename $0)/*; do
[ -x "$SCRIPT" ] || continue
"$SCRIPT" "$@"
Copy link

jasny commented Jan 11, 2014

- bin
  - install-git-hooks
  - git-hooks
    - wrapper
    - post-merge
      - some_script
      - another_script
    - post-commit
      - ya_script

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