new version of git-mirror uses fetch/merge and has a quite option for cron scripts.
authorBaris Metin <Talip-Baris.Metin@sophia.inria.fr>
Mon, 21 Jun 2010 10:30:31 +0000 (12:30 +0200)
committerBaris Metin <Talip-Baris.Metin@sophia.inria.fr>
Mon, 21 Jun 2010 10:30:31 +0000 (12:30 +0200)
scripts/git-mirror.sh

index de846f0..2b46fdb 100755 (executable)
@@ -1,45 +1,92 @@
 #!/bin/bash
 
-MIRROR="git://git.planet-lab.org"
-LOCAL="/git"
+MIRROR_GIT="git://git.planet-lab.org"
+MASTER_GIT="/git"
+LOCAL_MIRROR_DIR="/git-mirror"
+QUIET=1
+
+function msg () {
+    if [ $QUIET -eq 0 ]
+    then
+        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $1"
+    fi
+}
+
+function error () {
+    echo "[ERROR] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $1"
+    exit 1
+}
+
+function run () {
+    if [ $QUIET -eq 1 ]
+    then
+        COMMAND="$1 &> /dev/null"
+    else
+        COMMAND="$1"
+    fi
+    REPO=$2
+
+    pushd ${REPO} > /dev/null
+    eval $COMMAND
+    popd > /dev/null
+}
 
 function mirror () {
     for arg in "$@" ; do
-        REPO=${arg}
-        REPO_NAME=${REPO}.git
-        MIRROR_REPO=${MIRROR}/${REPO_NAME}
-        LOCAL_REPO=${LOCAL}/${REPO_NAME}
+        NAME=$(echo ${arg} | sed 's/\/$//')
+        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} ]
+        if [ -d ${REPO_DIR} ]
         then
-           echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pulling from ${REPO_NAME}"
-            pushd ${REPO}
-            git fetch origin --tags
-            git fetch origin
-            git rebase origin
-            popd
+           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 ]
+            then
+                error "Can not fetch from ${MASTER_REPO}"
+            fi
         else
-            echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx mirroring in ${REPO_NAME} for the first time"
-            git clone ${MIRROR_REPO}
-            pushd ${REPO}
-            git remote add local_master ${LOCAL_REPO}
-            popd
+            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
 
-        echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pushing ${REPO_NAME} to local master"
-        pushd ${REPO}
-        git fetch local_master --tags
-        git fetch local_master
-        git rebase local_master
+        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}"
+        fi
 
-        git push local_master
-        git push --tags local_master
-        popd
+        run "git push local_master" ${REPO_DIR}
+        run "git push --tags local_master" ${REPO_DIR}
     done
 }
 
-args="$@"
-[[ -z "$args" ]] && args=$(ls /svn)
 
-mirror $args
+while getopts ":hq" opt
+do
+  case $opt in
+      q)
+          QUIET=1
+          break
+          ;;
+      h)
+          echo "USAGE: $0 [-q] REPONAME*"
+          exit 1
+          ;;
+      \?)
+          echo "Invalid option: -$OPTARG" >&2
+          ;;
+  esac
+done
+
+shift $((OPTIND-1))
+mirror $@