X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=scripts%2Fgit-mirror.sh;h=fbb784931e89531094ccdae37c0e96569e069ae0;hb=b901fc0580316df1260eafe3fbbce65baa167be3;hp=434b7e8d565bfc92c30f47bd3c7ae7a023b2feb9;hpb=a88407b43212fc319e5a1b87cdfbec5340ac52b6;p=infrastructure.git diff --git a/scripts/git-mirror.sh b/scripts/git-mirror.sh old mode 100644 new mode 100755 index 434b7e8..fbb7849 --- a/scripts/git-mirror.sh +++ b/scripts/git-mirror.sh @@ -1,40 +1,131 @@ #!/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=0 + +function msg () { + if [ $QUIET -eq 0 ] + then + echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $@" + fi +} + +function error () { + echo "[ERROR] xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx $@" +} + +function run () { + if [ $QUIET -eq 1 ] + then + COMMAND="$1 &> /dev/null" + else + COMMAND="$1" + msg $COMMAND + fi + REPO=$2 + + pushd ${REPO} > /dev/null + eval $COMMAND + 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 - REPO=${arg} - REPO_NAME=${REPO}.git - MIRROR_REPO=${MIRROR}/${REPO_NAME} - LOCAL_REPO=${LOCAL}/${REPO_NAME} + 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} ] + # 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 - echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pulling from ${REPO_NAME}" - pushd ${REPO} - git pull --tags - git pull - popd - 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 - fi + if [ -d ${REPO_DIR} ] + then + 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 - echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pushing ${REPO_NAME} to local master" - pushd ${REPO} - git push local_master - git push --tags local_master - popd + 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 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 $@