+function push_all_branches () {
+ local REPO_DIR=$1; shift
+ local NAME=$1; shift
+ local PUSH_TO=$1; shift
+ local PUSH_FROM=$1; shift
+
+ pushd $REPO_DIR > /dev/null
+ BRANCHES=$(git branch -r | grep $PUSH_FROM | grep -v HEAD | sed "s/.*\///g" | grep -v master)
+ HAS_MASTER=$(git branch -l | grep master)
+ popd > /dev/null
+
+ [ -n "$HAS_MASTER" ] && run ${REPO_DIR} git push $PUSH_TO master:master
+ for BRANCH in $BRANCHES ; do
+ run ${REPO_DIR} git push $PUSH_TO $BRANCH:$BRANCH
+ done
+}
+
+function mirror_repo () {
+ local repo=$1; shift
+ FAILED_CMDS=() # reset previous failure if any
+
+ NAME=$(basename ${repo} ".git")
+ GIT_NAME=${NAME}.git
+ REPO_DIR=${LOCAL_MIRROR_DIR}/${NAME}
+ REMOTE_REPO=${REMOTE_GIT}/${GIT_NAME}
+ MASTER_REPO=${MASTER_GIT}/${GIT_NAME}
+
+ # if the local master is a symlink (like /git/vserver-reference.git -> sliceref.git)
+ # then skip it
+ # we use this for either aliases (like vserver-reference and sliceimage) or
+ # for repos managed in other locations (like /git-slave) but where
+ # the symlink is needed so they get served by git-daemon
+ [ -h ${MASTER_REPO} ] && return
+
+ # if there is no remote repository it may be that we only have
+ # the repository locally and don't need to mirror
+ git ls-remote $REMOTE_REPO &> /dev/null || return
+
+ if [ -d ${REPO_DIR} ] ; then
+ msg "pulling from ${NAME}"
+ run ${REPO_DIR} git fetch origin --tags
+ run ${REPO_DIR} git fetch origin
+ merge_all_branches $REPO_DIR $NAME origin
+ if [ -n "$FAILED_CMDS" ]; then
+ # format mail body
+ body="Can not fetch from ${MASTER_REPO}\n\n------------\n FAILED COMMANDS:\n"
+ for line in "${FAILED_CMDS[@]}"; do body="$body$line\n"; done
+ notify $REPO_DIR "git-mirror.sh failed to merge remote with module ${NAME}" "$body"
+ return
+ fi
+ else
+ msg "mirroring ${NAME} for the first time"
+ run ${LOCAL_MIRROR_DIR} git clone ${REMOTE_REPO}
+ run ${REPO_DIR} git remote add local_master ${MASTER_REPO}
+ fi
+
+ msg "pushing ${NAME} to local master"
+ run ${REPO_DIR} git fetch local_master --tags
+ run ${REPO_DIR} git fetch local_master
+ merge_all_branches $REPO_DIR $NAME local_master
+ if [ -n "$FAILED_CMDS" ]; then
+ pushd ${REPO_DIR} > /dev/null
+ STATUS_OUT=$(git status)
+ popd > /dev/null
+ # format mail body
+ body="STATUS in ${REPO_DIR}:\n${STATUS_OUT} \n\n------------\n FAILED COMMANDS:\n"
+ for line in "${FAILED_CMDS[@]}"; do body="$body$line\n"; done
+ notify $REPO_DIR "git-mirror.sh failed on with module ${NAME}" "$body"
+ return
+ fi
+ run ${REPO_DIR} git push --tags local_master
+ push_all_branches $REPO_DIR $NAME local_master origin
+ if [ -n "$FAILED_CMDS" ]; then
+ # format mail body
+ body="FAILED COMMANDS:\n"
+ for line in "${FAILED_CMDS[@]}"; do body="$body$line\n"; done
+ notify $REPO_DIR "git-mirror.sh failed to push back with module ${NAME}" "$body"
+ return
+ fi
+ # success, remove previous check file if any
+ clear_notify $REPO_DIR
+ # touch a stamp so it's easier to figure out where/if things get stuck
+ touch ${REPO_DIR}/MIRRORED.stamp
+}
+
+function usage () {
+ echo "Usage $COMMAND [options] REPONAME*"
+ echo " [-a admin-mails] : provide space-separated admins emails"
+ echo " [-r remote-git-url] : e.g. -r git://git.onelab.eu/"
+ echo " [-q] quiet mode for running under cron"
+ echo " [-v] verbose mode"
+ echo " [-f] force mode, runs even if the lock file is present"
+ echo " see also manage-git-mirror.sh"
+ exit 1
+}
+
+while getopts "a:r:s:qvfh" opt; do
+ case $opt in
+ a) ADMINS=$OPTARG ;;
+ r) REMOTE_GIT=$OPTARG ;;
+ q) QUIET=true ;;
+ v) VERBOSE=true ;;
+ f) FORCE=true ;;
+ h) usage ;;
+ \?) echo "Invalid option: -$opt" >&2 ;;
+ esac
+done
+shift $((OPTIND-1))
+
+# skip this if force is set
+# the natural usage of force is, manage-git-mirror.sh touches the lock file
+# which prevents the cron jobs from running during an hour
+# then you can use with -f which leaves the lock file as it is
+if [ -z "$FORCE" ] ; then
+ # is the stamp older than an hour ?
+ GRACE=60
+ is_old=$(find $LOCKFILE -mmin +$GRACE 2> /dev/null)
+ if [ -n "$is_old" ] ; then
+ msg "$LOCKFILE is older than $GRACE minutes - removing"
+ rm -f $LOCKFILE
+ fi
+
+ if [ -f $LOCKFILE ] ; then
+ msg "Found $LOCKFILE - another git-mirror seems to be running. Aborting... "
+ exit 1
+ fi
+fi