f37 -> f39
[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 # a very old commit for tracking upstream repo in case we re-create this local repo
16 EPOCH=dc275f2
17
18 # drop generic for now
19 #SYNC_SPECS="master:senslab2 master:generic"
20 SYNC_SPECS="master:senslab2"
21
22 GIT_OPTIONS=""
23
24 function step () {
25     echo "====================" step "$@"
26 }
27 function msg () {
28     echo "==========" "$@"
29 }
30
31
32 function failure () {
33     echo Emergency exit -- Bailing out
34     exit 1
35 }
36
37 function runcd () { 
38     to=$1; shift
39     msg "Going to directory $to"
40     cd $to
41 }
42
43 function runcheckout () { 
44     branch=$1; shift
45     msg "Checking out branch $branch"
46     git checkout $branch
47 }
48
49 function run () {
50     if [ -z "$INTERACTIVE" ] ; then
51         "$@"
52     else 
53         echo -n "** Run" "$@ " "[y]/n/q ? "
54         read _answer
55         case x"$_answer" in 
56             x|xy*) echo Running "$@" ; "$@" ;;
57             xq*) echo Exiting ; exit 0 ;;
58             *) echo Skipped ; return ;;
59         esac
60     fi
61 }
62
63 function trash_workdir () {
64     if [ -d $WORKDIR ] ; then
65         step "trash_workdir"
66         msg "Cleaning up $WORKDIR"
67         run rm -rf $WORKDIR
68     fi
69 }
70
71 function check_or_create_workdir () {
72     [ -d $WORKDIR ] && return
73     step "check_or_create_workdir"
74     msg Restarting with a brand new workdir $WORKDIR
75     run git clone $FLABREPO $WORKDIR 
76     runcd $WORKDIR
77     run git remote rename origin flab
78     # create branch upstreammaster as the tracking branch for upstream
79     run git checkout -b upstreammaster $EPOCH
80     run git remote add -t upstreammaster -m master upstream $UPSTREAMREPO 
81 }
82
83 function update_local_master () {
84     step "update_local_master"
85     runcd $WORKDIR
86     # pull onelab master onto upstreammaster - should be fast-forward
87     runcheckout upstreammaster
88     msg "pulling upstreammaster from upstream"
89     run git pull $GIT_OPTIONS upstream refs/heads/master:refs/heads/upstreammaster
90     # pull flab master onto master -
91     runcheckout master
92     msg "pulling master from flab"
93     run git pull $GIT_OPTIONS flab refs/heads/master:refs/head/master
94     # merge upstream upon local
95     msg "locally merging upstream into (flab) master"
96     run git pull $GIT_OPTIONS . upstreammaster 
97     if [ -n "$PUSH" ] ; then
98         msg pushing master back to flab
99         run git push $GIT_OPTIONS flab refs/heads/master:refs/heads/master
100     fi
101 }
102
103 function merge_master_in_local_branches () {
104     step "merge_master_in_local_branches"
105
106     runcd $WORKDIR
107
108     # manage our branches: merge master into generic and generic into senslab
109     for merge in $SYNC_SPECS; do
110         # typically master
111         what=$(echo $merge | cut -d: -f1)
112         # typically senslab2
113         where=$(echo $merge | cut -d: -f2)
114         runcd $WORKDIR
115         # also pull the latest master in the mix - we already have it at hand
116         # this OTOH may not be fast forward
117         runcheckout $where
118         # update the local branch for changes from flab chaps
119         # this is expected to be fast-forward
120         msg pulling $where from flab
121         run git pull $GIT_OPTIONS flab refs/heads/$where:refs/heads/$where
122         msg locally merging $what in $where
123         run git pull $GIT_OPTIONS . $what
124         # push back
125         if [ -n "$PUSH" ] ; then
126             msg pushing back $where onto flab
127             run git push $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where
128         fi
129     done
130 }
131
132 function usage () {
133     echo "Usage: $COMMAND [options]"
134     echo "Options"
135     echo " -d dir : use this dir as a workdir for merging"
136     echo "         please use a *dedicated* space"
137     echo " -r : restart from a fresh workdir"
138     echo " -m : master only"
139     echo " -p : does pushes"
140     echo " -s : skip pushes"
141     echo " -i : interactive"
142     echo " -n : dry-run"
143     echo " -v : verbose"
144     exit 1
145 }
146
147 # default is not to push
148 PUSH=
149 function main () {
150     while getopts "d:rmspinv" opt; do
151         case $opt in 
152             d) WORKDIR=$OPTARG; shift ;;
153             r) RESTART_FROM_SCRATCH=true ;;
154             m) MASTER_ONLY=true ;;
155             s) PUSH=;;
156             p) PUSH=true;;
157             i) INTERACTIVE=true ;;
158             n) GIT_OPTIONS="$GIT_OPTIONS --dry-run" ;;
159             v) set -x ; GIT_OPTIONS="$GIT_OPTIONS --verbose" ;;
160             *) usage;;
161         esac
162     done
163     
164     [ -n "$WORKDIR" ] || WORKDIR=$DEFAULT_WORKDIR
165
166     trap failure ERR INT 
167
168     [ -n "$RESTART_FROM_SCRATCH" ] && trash_workdir
169     check_or_create_workdir
170     update_local_master
171     [ -n "$MASTER_ONLY" ] && return
172     merge_master_in_local_branches
173  
174 }
175
176 ###
177 main "$@"
178
179