#!/bin/sh

# run some command on every new repo created via the conf file
# ----------------------------------------------------------------------

# BACKGROUND
#   The POST_CREATE trigger (see doc/triggers.mkd) only works for repos that a
#   user creates (see doc/wild.mkd).  It does NOT work for repos created
#   normally using the conf file.  The POST_COMPILE trigger sequence should
#   normally be used to trigger anything to happen after a compile.
#
#   I have not seen a sane use case for a POST_CREATE trigger when a new repo
#   is created via the conf file.  (I do not consider "all new repos should
#   have a default set of branches" to be a sane requirement).  However, on
#   the off-chance that something turns up at some future time, here's how you
#   can do this without touching the core gitolite code.

# INSTRUCTIONS FOR USE
# - (optional) rename this to whatever you want
# - change the 'post_create_normal' function below to do whatever you want it
#   to do, or make it call some other command to do it
# - make the script executable
# - add 'new-normal-repos' (or whatever you renamed it to) to the PRE_GIT and
#   the POST_COMPILE trigger lists in the rc file

post_create_normal() {
    echo "post_create_normal called with repo '$1'" >&2
}

# tempfile prefix; if you really care, change this also, otherwise leave it alone
tfp=$HOME/.tmp.$GL_USER.list-repos

# ----------------------------------------------------------------------

if [ "$1" = "PRE_GIT" ]
then
    # unless someone is pushing to the admin repo, we don't care
    [ "$2" = "gitolite-admin" ] || exit 0
    [ "$4" = "W" ] || exit 0

    # save the list of repos
    gitolite list-repos > $tfp.1
elif [ "$1" = "POST_COMPILE" ]
then
    # get the new list of repos and compare with the one PRE_GIT created
    gitolite list-repos > $tfp.2
    for repo in `grep -x -f $tfp.1 -v $tfp.2`
    do

        post_create_normal "$repo"
    done
else
    # if you edited your rc file correctly, this line should never be reached
    echo "ignoring call to" `basename $0` "with arg1 = '$1'" >&2
fi
