Gitorious: Per-repository post-receive hooks

Gitorious version 2.3 brought official support for server-wide custom post-receive hooks, which means you do not to use hacks anymore. The custom hooks are used for all repositories, and do not offer per-repository hooks. This article shows you how to do it.


At first, create the custom-post-receive hook file in $gitorious-install-dir/data/hooks/ and make it executable:

#!/bin/sh
# run all post-receive.* hooks in custom-hooks/
 
if [ ! -d custom-hooks ]; then
    exit
fi
 
FILE="`mktemp`"
cat - > "$FILE"
for i in custom-hooks/post-receive.*; do
    [ -x "$i" ] && cat "$FILE" | "$i"
done
rm "$FILE"

Now, in each of the repositories that need a post-receive hook, create a directory custom-hooks and copy or symlink a post-receive hook into it:

$ cd /var/data/git/repositories/project/demo.git/
$ mkdir custom-hooks
$ echo '#!/bin/sh
echo Current date: `date`' > custom-hooks/post-receive.date
$ chmod +x custom-hooks/post-receive.date

When pushing to this repository, you will get the following output:

...
Writing objects: 100% (3/3), 336 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: => Syncing Gitorious... Current date: Sat Dec 31 17:42:23 CEST 2012
[OK]
...
  

Written by Christian Weiske.

Comments? Please send an e-mail.