3 ADMINS="Talip-Baris.Metin@inria.fr Thierry.Parmentelat@inria.fr"
4 MIRROR_GIT="git://git.planet-lab.org"
6 LOCAL_MIRROR_DIR="/git-mirror"
14 echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $@"
23 echo -e "$MSG" | mail -s "$SUBJECT" $admin
30 COMMAND="$1 &> /dev/null"
37 pushd ${REPO} > /dev/null
42 FAILED_CMDS="$FAILED_CMDS\n$COMMAND"
47 function run_ignore_errors () {
49 OLD_FAILED_CMDS=$FAILED_CMDS
52 FAILED_CMDS=$OLD_FAILED_CMDS
55 function merge_all_branches () {
60 pushd $REPO_DIR > /dev/null
61 BRANCHES=$(git branch -r | grep $REMOTE | grep -v HEAD | sed "s/.*\///g" | grep -v master)
64 run "git checkout master" ${REPO_DIR}
65 run "git merge --ff $REMOTE/master" ${REPO_DIR}
66 for BRANCH in $BRANCHES ; do
67 run_ignore_errors "git branch $BRANCH $REMOTE/$BRANCH" ${REPO_DIR}
68 run "git checkout $BRANCH" ${REPO_DIR}
69 run "git merge --ff $REMOTE/$BRANCH" ${REPO_DIR}
73 function push_all_branches () {
78 pushd $REPO_DIR > /dev/null
79 BRANCHES=$(git branch -r | grep $REMOTE | grep -v HEAD | sed "s/.*\///g" | grep -v master)
82 run "git push $REMOTE master:master" ${REPO_DIR}
83 for BRANCH in $BRANCHES ; do
84 run "git push $REMOTE $BRANCH:$BRANCH" ${REPO_DIR}
90 FAILED=0 # reset previous failure if any
92 NAME=$(basename ${arg} | sed s/.git$//g)
94 REPO_DIR=${LOCAL_MIRROR_DIR}/${NAME}
95 MIRROR_REPO=${MIRROR_GIT}/${GIT_NAME}
96 MASTER_REPO=${MASTER_GIT}/${GIT_NAME}
99 # if there is no remote repository it may be that we only have
100 # the repository locally and don't need to mirror
101 git ls-remote $MIRROR_REPO &> /dev/null
104 if [ -d ${REPO_DIR} ]
106 msg "pulling from ${NAME}"
107 run "git fetch origin --tags" ${REPO_DIR}
108 run "git fetch origin" ${REPO_DIR}
109 merge_all_branches $NAME origin $REPO_DIR
112 error "Can not fetch from ${MASTER_REPO}"
115 msg "mirroring ${NAME} for the first time"
116 run "git clone ${MIRROR_REPO}" ${LOCAL_MIRROR_DIR}
117 run "git remote add local_master ${MASTER_REPO}" ${REPO_DIR}
120 msg "pushing ${NAME} to local master"
121 run "git fetch local_master --tags" ${REPO_DIR}
122 run "git fetch local_master" ${REPO_DIR}
123 merge_all_branches $NAME local_master $REPO_DIR
126 pushd ${REPO_DIR} > /dev/null
127 STATUS_OUT=$(git status)
129 error "git-mirror.sh failed on ${MIRROR_REPO}" "STATUS:\n$STATUS_OUT \n\n------------\n FAILED COMMANDS:\n$FAILED_CMDS"
131 run "git push --tags local_master" ${REPO_DIR}
132 push_all_branches $NAME local_master $REPO_DIR
139 while getopts ":hq" opt
147 echo "USAGE: $0 [-q] REPONAME*"
151 echo "Invalid option: -$OPTARG" >&2