+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
+}