Skip to content

Instantly share code, notes, and snippets.

@emmanuelbernard
Created November 7, 2010 12:59

Revisions

  1. emmanuelbernard revised this gist Nov 7, 2010. 1 changed file with 23 additions and 19 deletions.
    42 changes: 23 additions & 19 deletions sync_repo.sh
    Original file line number Diff line number Diff line change
    @@ -35,34 +35,38 @@ localize() {
    if [ $rb == "HEAD" ] ; then
    echo "Not processing HEAD"
    else
    local=$2
    echo "Localizing $rb"
    found="FALSE"
    for l in ${local[*]} ; do
    if [ $found == "FALSE" ] ; then
    if [ $l == $rb ] ; then
    found="TRUE"
    local=$2
    echo "Localizing $rb"
    found="FALSE"
    for l in ${local[*]} ; do
    if [ $found == "FALSE" ] ; then
    if [ $l == $rb ] ; then
    found="TRUE"
    fi
    fi
    fi
    done
    done

    if [ $found == "FALSE" ] ; then
    echo "... adding a local branch for $rb"
    git checkout -q -b $rb origin/$rb
    else
    echo "... no need to add local branch for $rb"
    fi
    if [ $found == "FALSE" ] ; then
    echo "... adding a local branch for $rb"
    git checkout -q -f -b $rb origin/$rb
    else
    echo "... no need to add local branch for $rb"
    fi
    fi
    }

    git fetch origin
    git fetch --tags origin

    get_branches local_branches local
    get_branches remote_branches remote

    echo "Local: [${local_branches[*]}] and is of size ${#local_branches[*]}"
    echo "Remote: [${remote_branches[*]}] and is of size ${#remote_branches[*]}"

    for rb in ${remote_branches[*]} ; do
    localize $rb $local_branches
    argument=`echo ${local_branches[@]}`
    localize $rb "$argument"
    done

    unset local_branches
    @@ -72,9 +76,9 @@ echo "After localizing, local: [${local_branches[*]}] and is of size ${#local_br

    for lb in ${local_branches[*]} ; do
    echo "Updating $lb"
    git checkout -q $lb
    git pull -q origin
    git checkout -q -f $lb
    git pull -q origin $lb
    git push ${BACKUP_REPO} $lb
    done

    git push ${BACKUP_REPO} --tags
    git push ${BACKUP_REPO} --tags
  2. emmanuelbernard created this gist Nov 7, 2010.
    80 changes: 80 additions & 0 deletions sync_repo.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    #!/bin/bash
    #Synchronize all changes (incl new branches and tags) from repo A to repo B, where B is a bare repo.
    #B must be an bare clone of A (initial setup)
    #A' must be a clone of A where origin points to A
    #There are probably more optimized ways to do this. the advantage of this method is that no branch is deleted from the backup repo
    #so the source repo is not 100% trusted

    #make sure to define BACKUP_REPO and INTERMEDIARY_REPO

    #define where repo B resides (Git repo URL) eg file://${HOME}/path/to/repo.git
    BACKUP_REPO="file://${HOME}/path/to/repo.git"
    #A' repo location we will cd into
    INTERMEDIARY_REPO=${HOME}/repoAPrime

    #cd into the A' repo
    cd ${INTERMEDIARY_REPO}

    get_branches() {
    flags=""
    if [ $2 == "remote" ] ; then
    flags="-r"
    fi

    branches=`git branch $flags | sed -e "s/*//g"`
    i=0
    for b in $branches ; do
    cl=`echo $b | sed -e "s:\s*::g" -e "s:origin/::g"`
    eval "$1[$i]=$cl"
    let "i=$i + 1"
    done
    }

    localize() {
    rb=$1
    if [ $rb == "HEAD" ] ; then
    echo "Not processing HEAD"
    else
    local=$2
    echo "Localizing $rb"
    found="FALSE"
    for l in ${local[*]} ; do
    if [ $found == "FALSE" ] ; then
    if [ $l == $rb ] ; then
    found="TRUE"
    fi
    fi
    done

    if [ $found == "FALSE" ] ; then
    echo "... adding a local branch for $rb"
    git checkout -q -b $rb origin/$rb
    else
    echo "... no need to add local branch for $rb"
    fi
    fi
    }

    get_branches local_branches local
    get_branches remote_branches remote

    echo "Local: [${local_branches[*]}] and is of size ${#local_branches[*]}"
    echo "Remote: [${remote_branches[*]}] and is of size ${#remote_branches[*]}"

    for rb in ${remote_branches[*]} ; do
    localize $rb $local_branches
    done

    unset local_branches
    get_branches local_branches local

    echo "After localizing, local: [${local_branches[*]}] and is of size ${#local_branches[*]}"

    for lb in ${local_branches[*]} ; do
    echo "Updating $lb"
    git checkout -q $lb
    git pull -q origin
    git push ${BACKUP_REPO} $lb
    done

    git push ${BACKUP_REPO} --tags