run k22-f8-32 before k27
[infrastructure.git] / scripts / git-mirror.sh
index 2b46fdb..fbb7849 100755 (executable)
@@ -3,18 +3,17 @@
 MIRROR_GIT="git://git.planet-lab.org"
 MASTER_GIT="/git"
 LOCAL_MIRROR_DIR="/git-mirror"
-QUIET=1
+QUIET=0
 
 function msg () {
     if [ $QUIET -eq 0 ]
     then
-        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $1"
+        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $@"
     fi
 }
 
 function error () {
-    echo "[ERROR] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $1"
-    exit 1
+    echo "[ERROR] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $@"
 }
 
 function run () {
@@ -23,6 +22,7 @@ function run () {
         COMMAND="$1 &> /dev/null"
     else
         COMMAND="$1"
+        msg $COMMAND
     fi
     REPO=$2
 
@@ -31,41 +31,80 @@ function run () {
     popd > /dev/null
 }
 
+function merge_all_branches () {
+    NAME=$1
+    REMOTE=$2
+    REPO_DIR=$3
+
+    pushd $REPO_DIR > /dev/null
+    BRANCHES=$(git branch -r | grep $REMOTE | grep -v HEAD | sed "s/.*\///g" | grep -v master)
+    popd > /dev/null
+
+    run "git checkout master" ${REPO_DIR}
+    run "git merge --ff $REMOTE/master" ${REPO_DIR}
+    for BRANCH in $BRANCHES ; do
+        run "git branch $BRANCH $REMOTE/$BRANCH" ${REPO_DIR}
+        run "git checkout $BRANCH" ${REPO_DIR}
+        run "git merge --ff $REMOTE/$BRANCH" ${REPO_DIR}
+    done
+}
+
+function push_all_branches () {
+    NAME=$1
+    REMOTE=$2
+    REPO_DIR=$3
+
+    pushd $REPO_DIR > /dev/null
+    BRANCHES=$(git branch -r | grep $REMOTE | grep -v HEAD | sed "s/.*\///g" | grep -v master)
+    popd > /dev/null
+
+    run "git push $REMOTE master:master" ${REPO_DIR}
+    for BRANCH in $BRANCHES ; do
+        run "git push $REMOTE $BRANCH:$BRANCH" ${REPO_DIR}
+    done
+}
+
 function mirror () {
     for arg in "$@" ; do
-        NAME=$(echo ${arg} | sed 's/\/$//')
+        NAME=$(basename ${arg} | sed s/.git$//g)
         GIT_NAME=${NAME}.git
         REPO_DIR=${LOCAL_MIRROR_DIR}/${NAME}
         MIRROR_REPO=${MIRROR_GIT}/${GIT_NAME}
         MASTER_REPO=${MASTER_GIT}/${GIT_NAME}
 
-        if [ -d ${REPO_DIR} ]
+        # 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 $MIRROR_REPO &> /dev/null
+        if [ $? -eq 0 ]
         then
-           msg "pulling from ${REPO_NAME}"
-            run "git fetch origin --tags" ${REPO_DIR}
-           run "git fetch origin" ${REPO_DIR}
-            run "git merge origin/master" ${REPO_DIR}
-            if [ $? -ne 0 ]
+            if [ -d ${REPO_DIR} ]
             then
-                error "Can not fetch from ${MASTER_REPO}"
+               msg "pulling from ${NAME}"
+                run "git fetch origin --tags" ${REPO_DIR}
+               run "git fetch origin" ${REPO_DIR}
+                merge_all_branches $NAME origin $REPO_DIR
+                if [ $? -ne 0 ]
+                then
+                    error "Can not fetch from ${MASTER_REPO}"
+                fi
+            else
+                msg "mirroring ${NAME} for the first time"
+                run "git clone ${MIRROR_REPO}" ${LOCAL_MIRROR_DIR}
+                run "git remote add local_master ${MASTER_REPO}" ${REPO_DIR}
             fi
-        else
-            msg "mirroring in ${REPO_NAME} for the first time"
-            run "git clone ${MIRROR_REPO}" ${LOCAL_MIRROR_DIR}
-            run "git remote add local_master ${MASTER_REPO}" ${REPO_DIR}
-        fi
 
-        msg "pushing ${REPO_NAME} to local master"
-        run "git fetch local_master --tags" ${REPO_DIR}
-        run "git fetch local_master" ${REPO_DIR}
-        run "git merge local_master/master" ${REPO_DIR}
-        if [ $? -ne 0 ]
-        then
-            error "Can not fetch from ${MIRROR_REPO}"
+            msg "pushing ${NAME} to local master"
+            run "git fetch local_master --tags" ${REPO_DIR}
+            run "git fetch local_master" ${REPO_DIR}
+            merge_all_branches $NAME local_master $REPO_DIR
+            if [ $? -ne 0 ]
+            then
+                error "Can not fetch from ${MIRROR_REPO}"
+            else
+                run "git push --tags local_master" ${REPO_DIR}
+                push_all_branches $NAME local_master $REPO_DIR
+            fi
         fi
-
-        run "git push local_master" ${REPO_DIR}
-        run "git push --tags local_master" ${REPO_DIR}
     done
 }