reasonably useful and reliable, although interactive mode is advised
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 19 Oct 2011 15:22:55 +0000 (17:22 +0200)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Wed, 19 Oct 2011 15:22:55 +0000 (17:22 +0200)
scripts/flab-sync.sh

index c132ec9..05f23af 100755 (executable)
@@ -27,54 +27,91 @@ function failure () {
     exit 1
 }
 
+function runcd () { 
+    to=$1; shift
+    msg "Going to directory $to"
+    cd $to
+}
+
+function run () {
+    if [ -z "$INTERACTIVE" ] ; then
+       "$@"
+    else 
+       echo -n "Run" "$@ " "[y]/n/q ? "
+       read _answer
+       case x"$_answer" in 
+           x|xy*) echo Running "$@" ; "$@" ;;
+           xq*) echo Exiting ; exit 0 ;;
+           *) echo Skipped ; return ;;
+       esac
+    fi
+}
+
+function trash_workdir () {
+    [ -d $WORKDIR ] && run rm -rf $WORKDIR || :
+}
+
 function check_or_create_workdir () {
     [ -d $WORKDIR ] && return
     msg Restarting with a brand new workdir $WORKDIR
-    git clone $FLABREPO $WORKDIR 
+    run git clone $FLABREPO $WORKDIR 
+    runcd $WORKDIR
+    run git remote rename origin flab
+    run git remote add -t master -m master onelab $UPSTREAMREPO 
 }
 
 function merge_master_in_local_branches () {
     # start with pulling the upstream master onto flab
-    cd $WORKDIR
+    runcd $WORKDIR
     # better safe than sorry
-    git checkout master
-    msg pulling master from onelab
-    git pull $GIT_OPTIONS $UPSTREAMREPO refs/heads/master:refs/heads/master
+    run git checkout master
+    msg pulling master from upstream/onelab
+    run git pull $GIT_OPTIONS $UPSTREAMREPO refs/heads/master:refs/heads/master
     msg pushing master to flab
-    git push $GIT_OPTIONS $FLABREPO refs/heads/master:refs/heads/master
+    run git push $GIT_OPTIONS $FLABREPO refs/heads/master:refs/heads/master
 
-    # manage our branches
-    for branch in $TOTRACK; do
-       cd $WORKDIR
-        # update the local branch for changes from flab chaps
-        # this is expected to be fast-forward
-       msg pulling $branch from flab
-       git pull $GIT_OPTIONS $FLABREPO refs/heads/$branch:refs/heads/$branch
+    [ -n "$MASTER_ONLY" ] && return
+
+    # manage our branches: merge master into generic and generic into senslab
+    for merge in master:generic generic:senslab; do
+       what=$(echo $merge | cut -d: -f1)
+       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
-       git checkout $branch
-       msg locally merging master in $branch
-       git merge $GIT_OPTIONS master
+       run git checkout $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
+       msg locally merging $what in $where
+       run git merge $GIT_OPTIONS $what
         # push back
-       msg pushing back $branch onto flab
-       git push $GIT_OPTIONS $FLABREPO refs/heads/$branch:refs/heads/$branch
+       msg pushing back $where onto flab
+       run git push $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where
     done
 }
 
 function usage () {
     echo "Usage: $COMMAND [options]"
     echo "Options"
-    echo "-d dir : use this dir as a workdir for merging"
+    echo " -d dir : use this dir as a workdir for merging"
     echo "         please use a *dedicated* space"
+    echo " -r : restart from a fresh workdir"
+    echo " -m : master only"
+    echo " -i : interactive"
     echo " -n : dry-run"
     echo " -v : verbose"
     exit 1
 }
 
 function main () {
-    while getopts "d:nv" opt; do
+    while getopts "d:rminv" opt; do
        case $opt in 
            d) WORKDIR=$OPTARG; shift ;;
+           r) RESTART_FROM_SCRATCH=true ;;
+           m) MASTER_ONLY=true ;;
+           i) INTERACTIVE=true ;;
            n) GIT_OPTIONS="$GIT_OPTIONS -n" ;;
            v) set -x ; GIT_OPTIONS="$GIT_OPTIONS -v" ;;
             *) usage;;
@@ -85,6 +122,7 @@ function main () {
 
     trap failure ERR INT 
 
+    [ -n "$RESTART_FROM_SCRATCH" ] && trash_workdir
     check_or_create_workdir
     merge_master_in_local_branches