-
-
Save maniksurtani/667213 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 -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 | |
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 -f $lb | |
git pull -q origin $lb | |
git push ${BACKUP_REPO} $lb | |
done | |
git push ${BACKUP_REPO} --tags |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is licensed under Creative Commons so feel free to copy, edit and reuse.