more reliable; hopefully it would not merge generic and senslab anymore
[infrastructure.git] / scripts / flab-sync.sh
1 #!/bin/bash
2
3 # implement the workflow between the various branches in flab
4 # basically keep the 2 branches generic and senslab 
5 # in sync with master from upstream
6
7 # this is expected to be a standalone directory *not* a workdir
8 # initialized with a clone from git.f-lab.fr/sfa.git
9
10 COMMAND=$(basename $0)
11
12 DEFAULT_WORKDIR=/Users/parmentelat/git/sfa-flab-sync
13 UPSTREAMREPO=ssh://thierry@git.onelab.eu/git/sfa.git
14 FLABREPO=ssh://thierry@git.f-lab.fr/git/sfa.git
15 TOTRACK="generic senslab"
16 AGAINST=master
17
18 GIT_OPTIONS=""
19
20 function msg () {
21     echo "====================" "$@"
22 }
23
24
25 function failure () {
26     echo Emergency exit -- Bailing out
27     exit 1
28 }
29
30 function check_or_create_workdir () {
31     [ -d $WORKDIR ] && return
32     msg Restarting with a brand new workdir $WORKDIR
33     git clone $FLABREPO $WORKDIR 
34 }
35
36 function merge_master_in_local_branches () {
37     # start with pulling the upstream master onto flab
38     cd $WORKDIR
39     # better safe than sorry
40     git checkout master
41     msg pulling master from onelab
42     git pull $GIT_OPTIONS $UPSTREAMREPO refs/heads/master:refs/heads/master
43     msg pushing master to flab
44     git push $GIT_OPTIONS $FLABREPO refs/heads/master:refs/heads/master
45
46     # manage our branches
47     for branch in $TOTRACK; do
48         cd $WORKDIR
49         # update the local branch for changes from flab chaps
50         # this is expected to be fast-forward
51         msg pulling $branch from flab
52         git pull $GIT_OPTIONS $FLABREPO refs/heads/$branch:refs/heads/$branch
53         # also pull the latest master in the mix - we already have it at hand
54         # this OTOH may not be fast forward
55         git checkout $branch
56         msg locally merging master in $branch
57         git merge $GIT_OPTIONS master
58         # push back
59         msg pushing back $branch onto flab
60         git push $GIT_OPTIONS $FLABREPO refs/heads/$branch:refs/heads/$branch
61     done
62 }
63
64 function usage () {
65     echo "Usage: $COMMAND [options]"
66     echo "Options"
67     echo "-d dir : use this dir as a workdir for merging"
68     echo "         please use a *dedicated* space"
69     echo " -n : dry-run"
70     echo " -v : verbose"
71     exit 1
72 }
73
74 function main () {
75     while getopts "d:nv" opt; do
76         case $opt in 
77             d) WORKDIR=$OPTARG; shift ;;
78             n) GIT_OPTIONS="$GIT_OPTIONS -n" ;;
79             v) set -x ; GIT_OPTIONS="$GIT_OPTIONS -v" ;;
80             *) usage;;
81         esac
82     done
83     
84     [ -n "$WORKDIR" ] || WORKDIR=$DEFAULT_WORKDIR
85
86     trap failure ERR INT 
87
88     check_or_create_workdir
89     merge_master_in_local_branches
90  
91 }
92
93 ###
94 main "$@"
95
96