Skip to content

Instantly share code, notes, and snippets.

@jasny
Last active Jan 2, 2016
Embed
What would you like to do?
Define git hooks within your repository. Allows defining multiple scripts for one hook.
#!/bin/bash
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"
fi
# 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"
done
[ -f "$HOOK_DIR/env" ] || \
echo "# Place environment variable here. These are only for your local system.
export APPLICATION_ENV=prod" > "$HOOK_DIR/env"
#!/bin/bash
set -e
. .git/hooks/env
if [ -x $0.local ]; then
$0.local "$@"
fi
if [ -d "bin/git-hooks/$(basename $0)" ]; then
for SCRIPT in bin/git-hooks/$(basename $0)/*; do
[ -x "$SCRIPT" ] || continue
"$SCRIPT" "$@"
done
fi
@jasny
Copy link
Author

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