Skip to content

Instantly share code, notes, and snippets.

@hacklschorsch
Created May 25, 2012 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hacklschorsch/2788752 to your computer and use it in GitHub Desktop.
Save hacklschorsch/2788752 to your computer and use it in GitHub Desktop.
GIT Hook-Chaining script
#!/bin/bash
#
# Author: orefalo
# @see http://stackoverflow.com/questions/8730514/chaining-git-hooks#8734391
#
# Changes:
# fs@it-agenten.com: Add [0-9] to file glob to exclude .sample files
#
# Usage:
#
# create file .git/hooks/hook-chain as follows
#
# Now link any hook that requires chaining, for instance
# ln -s hook-chain update
# ln -s hook-chain post-receive
#
# Finally, create the chains by renaming them as hookname.action.
# For instance, in the sample above, the update hook will run update.1acl
# followed by update.2github. The post-receive hook will run
# post-receive.1email followed by post-receive.2github.
hookname=`basename $0`
FILE=`mktemp`
trap 'rm -f $FILE' EXIT
cat - > $FILE
for hook in $GIT_DIR/hooks/$hookname.[0-9]*
do
if test -x "$hook"; then
# echo $hook
cat $FILE | $hook "$@"
status=$?
if test $status -ne 0; then
echo Hook $hook failed with error code $status
exit $status
fi
fi
done
@thomasbellio
Copy link

I am not clear on the purpose of the mktemp command? Can you explain that? Also I have modified this for my own purposes. It allows for all of the scripts to run, even if one of them fails. Here is the code:

hookname=`basename $0`
statusarray=()
my_path="`dirname \"$0\"`"

## For each hook  
for hook in $my_path/$hookname.[0-9]*
do
    if test -x "$hook"; then
        $hook "$@"
        status=$?
        statusarray+=($status)
    fi
done

# Now that we have run all of the hooks, we will now iterate over each of the statuses
# and if any of them are greater than 0 we will exit with that status
# echo $statusarray
for status in ${statusarray[@]}
do
    if test $status -ne 0; then
        exit $status
    fi
done

# If we get here then everything is hunky dorio and we need to exit with 0
exit 0

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