From 4b312e16a85ab42811f600c77d685e75159b7794 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Thu, 3 Nov 2011 16:11:17 +0100 Subject: [PATCH] can handle divergences between upstream master and flab master generic left aside for now --- scripts/flab-sync.sh | 97 +++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 29 deletions(-) diff --git a/scripts/flab-sync.sh b/scripts/flab-sync.sh index b119095..a2366fb 100755 --- a/scripts/flab-sync.sh +++ b/scripts/flab-sync.sh @@ -12,12 +12,20 @@ COMMAND=$(basename $0) DEFAULT_WORKDIR=/Users/parmentelat/git/sfa-flab-sync UPSTREAMREPO=ssh://thierry@git.onelab.eu/git/sfa.git FLABREPO=ssh://thierry@git.f-lab.fr/git/sfa.git -AGAINST=master +# a very old commit for tracking upstream repo in case we re-create this local repo +EPOCH=dc275f2 + +# drop generic for now +#SYNC_SPECS="master:senslab2 master:generic" +SYNC_SPECS="master:senslab2" GIT_OPTIONS="" +function step () { + echo "====================" step "$@" +} function msg () { - echo "====================" "$@" + echo "==========" "$@" } @@ -32,11 +40,17 @@ function runcd () { cd $to } +function runcheckout () { + branch=$1; shift + msg "Checking out branch $branch" + git checkout $branch +} + function run () { if [ -z "$INTERACTIVE" ] ; then "$@" else - echo -n "Run" "$@ " "[y]/n/q ? " + echo -n "** Run" "$@ " "[y]/n/q ? " read _answer case x"$_answer" in x|xy*) echo Running "$@" ; "$@" ;; @@ -47,51 +61,71 @@ function run () { } function trash_workdir () { - [ -d $WORKDIR ] && run rm -rf $WORKDIR || : + if [ -d $WORKDIR ] ; then + step "trash_workdir" + msg "Cleaning up $WORKDIR" + run rm -rf $WORKDIR + fi } function check_or_create_workdir () { [ -d $WORKDIR ] && return + step "check_or_create_workdir" msg Restarting with a brand new workdir $WORKDIR run git clone $FLABREPO $WORKDIR runcd $WORKDIR run git remote rename origin flab - run git remote add -t master -m master onelab $UPSTREAMREPO + # create branch upstreammaster as the tracking branch for upstream + run git checkout -b upstreammaster $EPOCH + run git remote add -t upstreammaster -m master upstream $UPSTREAMREPO } -function merge_master_in_local_branches () { - # start with pulling the upstream master onto flab +function update_local_master () { + step "update_local_master" runcd $WORKDIR - # better safe than sorry - run git checkout master - msg pulling master from upstream/onelab - run git pull $GIT_OPTIONS $UPSTREAMREPO refs/heads/master:refs/heads/master - if [ -n "$PUSH_MASTER" ] ; then - msg pushing master to flab - run git push $GIT_OPTIONS $FLABREPO refs/heads/master:refs/heads/master + # pull onelab master onto upstreammaster - should be fast-forward + runcheckout upstreammaster + msg "pulling upstreammaster from upstream" + run git pull $GIT_OPTIONS upstream refs/heads/master:refs/heads/upstreammaster + # pull flab master onto master - + runcheckout master + msg "pulling master from flab" + run git pull $GIT_OPTIONS flab refs/heads/master:refs/head/master + # merge upstream upon local + msg "locally merging upstream into (flab) master" + run git pull $GIT_OPTIONS . upstreammaster + if [ -n "$PUSH" ] ; then + msg pushing master back to flab + run git push $GIT_OPTIONS flab refs/heads/master:refs/heads/master fi +} - [ -n "$MASTER_ONLY" ] && return +function merge_master_in_local_branches () { + step "merge_master_in_local_branches" + + runcd $WORKDIR # manage our branches: merge master into generic and generic into senslab - for merge in master:generic master:senslab2; do + for merge in $SYNC_SPECS; do + # typically master what=$(echo $merge | cut -d: -f1) + # typically senslab2 where=$(echo $merge | cut -d: -f2) runcd $WORKDIR # also pull the latest master in the mix - we already have it at hand # this OTOH may not be fast forward - run git checkout $where + runcheckout $where # update the local branch for changes from flab chaps # this is expected to be fast-forward msg pulling $where from flab - run git pull $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where + run git pull $GIT_OPTIONS flab refs/heads/$where:refs/heads/$where msg locally merging $what in $where - run git merge $GIT_OPTIONS $what + run git pull $GIT_OPTIONS . $what # push back - msg Avoiding the push back for now, run manually if needed - msg git push $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where - #msg pushing back $where onto flab - #run git push $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where + if [ -n "$PUSH" ] ; then + msg pushing back $where onto flab + run git push $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where + fi done } @@ -102,24 +136,27 @@ function usage () { echo " please use a *dedicated* space" echo " -r : restart from a fresh workdir" echo " -m : master only" - echo " -s : skip master push" + echo " -p : does pushes" + echo " -s : skip pushes" echo " -i : interactive" echo " -n : dry-run" echo " -v : verbose" exit 1 } -PUSH_MASTER=true +# default is not to push +PUSH= function main () { - while getopts "d:rmsinv" opt; do + while getopts "d:rmspinv" opt; do case $opt in d) WORKDIR=$OPTARG; shift ;; r) RESTART_FROM_SCRATCH=true ;; m) MASTER_ONLY=true ;; - s) PUSH_MASTER=;; + s) PUSH=;; + p) PUSH=true;; i) INTERACTIVE=true ;; - n) GIT_OPTIONS="$GIT_OPTIONS -n" ;; - v) set -x ; GIT_OPTIONS="$GIT_OPTIONS -v" ;; + n) GIT_OPTIONS="$GIT_OPTIONS --dry-run" ;; + v) set -x ; GIT_OPTIONS="$GIT_OPTIONS --verbose" ;; *) usage;; esac done @@ -130,6 +167,8 @@ function main () { [ -n "$RESTART_FROM_SCRATCH" ] && trash_workdir check_or_create_workdir + update_local_master + [ -n "$MASTER_ONLY" ] && return merge_master_in_local_branches } -- 2.43.0