X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=scripts%2Fgit-mirror.sh;h=fbb784931e89531094ccdae37c0e96569e069ae0;hb=9cf40f7bf1506d6bfcc744cff6a50461366d2a6a;hp=bf552c89a2bafe38d42b3d5b46158f5c95503d97;hpb=d6904539145bfd4e1938ce69050adeb0cce79011;p=infrastructure.git diff --git a/scripts/git-mirror.sh b/scripts/git-mirror.sh index bf552c8..fbb7849 100755 --- a/scripts/git-mirror.sh +++ b/scripts/git-mirror.sh @@ -8,13 +8,12 @@ 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 () { @@ -32,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 --ff 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 --ff 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 }