Skip to content

Instantly share code, notes, and snippets.

@maniksurtani
Created November 8, 2010 12:59
Show Gist options
  • Save maniksurtani/667673 to your computer and use it in GitHub Desktop.
Save maniksurtani/667673 to your computer and use it in GitHub Desktop.
A utility script that syncs a local clone of a personal fork with a remote upstream repo. This script assumes that all the branches that exist on your personal fork also exist on the upstream repo. The script pulls down changes from upstream into your l
#!/bin/bash
####################################################################################
#
# This script syncs your private, personal checkout with an upstream repository.
#
###################################################################################
# This is where your local, priave copy is checked out
LOCAL_REPO=${HOME}/Code/infinispan/my_repo
# The git path to the upstream repo. This is a read-only repo.
UPSTREAM_REPO="git://github.com/infinispan/infinispan.git"
# The refspec to the remote copy of your private repo. This is origin by default.
REMOTE_REPO=origin
#### ---- functions
need_stash() {
status=`git status --porcelain`
if [ "z$status" == "z" ] ; then
eval "${1}='FALSE'"
else
eval "${1}='TRUE'"
fi
}
sync() {
branch=${1}
git checkout -f ${branch}
stash=''
need_stash stash
if [ $stash == "TRUE" ] ; then
git stash
fi
git pull -q ${UPSTREAM_REPO} ${branch}
git push -q ${REMOTE_REPO} ${branch}
if [ $stash == "TRUE" ] ; then
git stash pop
fi
}
get_branches() {
flags=""
if [ ${2} == "remote" ] ; then
branches=`git branch -r | grep ${REMOTE_REPO} | sed -e "s/*//g" -e "/HEAD/d"`
else
branches=`git branch | sed -e "s/*//g"`
fi
i=0
for b in ${branches} ; do
cl=`echo ${b} | sed -e "s:${REMOTE_REPO}/::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 ${REMOTE_REPO}/$rb
else
echo "... no need to add local branch for $rb"
fi
fi
}
### --- end functions
cd ${LOCAL_REPO}
init_stash=''
need_stash init_stash
if [ $init_stash == "TRUE" ] ; then
git stash
fi
git fetch -q ${UPSTREAM_REPO}
git fetch -q ${UPSTREAM_REPO} --tags
get_branches local_branches local
get_branches remote_branches remote
for rb in ${remote_branches[*]} ; do
localize $rb "${local_branches[*]}"
done
unset local_branches
get_branches local_branches local
## Sync branches
for local_branch in ${local_branches[*]} ; do
sync $local_branch
done
## Sync tags
git push -q ${REMOTE_REPO} --tags
if [ $init_stash == "TRUE" ] ; then
git stash pop
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment