#!/bin/bash

# manage local, gitolite-controlled, copies of read-only upstream repos.

repo=$2

url=$(gitolite git-config $repo gitolite-options.upstream.url)
[ -z "$url" ] && exit 0     # exit if no url was specified

cd $GL_REPO_BASE/$repo.git || exit 1

[ "$1" != "fetch" ] && {
    nice=$(gitolite git-config $repo gitolite-options.upstream.nice)
    [ -n "$nice" ] && find FETCH_HEAD -mmin -$nice | grep . >/dev/null && exit 0
}

git fetch -q "$url" '+refs/*:refs/*'

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

# FEATURES:
# * invokes upstream fetch on each local fetch
#   (unless the optional 'nice' setting is enabled)
# * can force a fetch (ignoring 'nice' value) from server CLI

# INSTRUCTIONS:
#
# * add 'upstream' to the PRE_GIT trigger list in the rc file.
# * add option lines to conf file.  For example:
#
#       repo git
#           R                       =   @all
#           RW+ my-company/         =   @developers
#
#           option  upstream.url    =   git://git.kernel.org/pub/scm/git/git.git
#           option  upstream.nice   =   120
#
# * to force a fetch on the server shell (or via cron), run this command:
#       gitolite ../triggers/upstream fetch reponame

# ADDITIONAL NOTES:
# * restrict local pushes to a namespace that the upstream won't use
#   (otherwise the next fetch will wipe them out)
# * if the upstream URL changes, just change the conf and push admin repo
# * the 'nice' setting is in minutes and is optional; it is the minimum
#   elapsed time between 2 upstream fetches.
