simpler
[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 AGAINST=master
16
17 GIT_OPTIONS=""
18
19 function msg () {
20     echo "====================" "$@"
21 }
22
23
24 function failure () {
25     echo Emergency exit -- Bailing out
26     exit 1
27 }
28
29 function runcd () { 
30     to=$1; shift
31     msg "Going to directory $to"
32     cd $to
33 }
34
35 function run () {
36     if [ -z "$INTERACTIVE" ] ; then
37         "$@"
38     else 
39         echo -n "Run" "$@ " "[y]/n/q ? "
40         read _answer
41         case x"$_answer" in 
42             x|xy*) echo Running "$@" ; "$@" ;;
43             xq*) echo Exiting ; exit 0 ;;
44             *) echo Skipped ; return ;;
45         esac
46     fi
47 }
48
49 function trash_workdir () {
50     [ -d $WORKDIR ] && run rm -rf $WORKDIR || :
51 }
52
53 function check_or_create_workdir () {
54     [ -d $WORKDIR ] && return
55     msg Restarting with a brand new workdir $WORKDIR
56     run git clone $FLABREPO $WORKDIR 
57     runcd $WORKDIR
58     run git remote rename origin flab
59     run git remote add -t master -m master onelab $UPSTREAMREPO 
60 }
61
62 function merge_master_in_local_branches () {
63     # start with pulling the upstream master onto flab
64     runcd $WORKDIR
65     # better safe than sorry
66     run git checkout master
67     msg pulling master from upstream/onelab
68     run git pull $GIT_OPTIONS $UPSTREAMREPO refs/heads/master:refs/heads/master
69     if [ -n "$PUSH_MASTER" ] ; then
70         msg pushing master to flab
71         run git push $GIT_OPTIONS $FLABREPO refs/heads/master:refs/heads/master
72     fi
73
74     [ -n "$MASTER_ONLY" ] && return
75
76     # manage our branches: merge master into generic and generic into senslab
77     for merge in master:generic master:senslab2; do
78         what=$(echo $merge | cut -d: -f1)
79         where=$(echo $merge | cut -d: -f2)
80         runcd $WORKDIR
81         # also pull the latest master in the mix - we already have it at hand
82         # this OTOH may not be fast forward
83         run git checkout $where
84         # update the local branch for changes from flab chaps
85         # this is expected to be fast-forward
86         msg pulling $where from flab
87         run git pull $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where
88         msg locally merging $what in $where
89         run git merge $GIT_OPTIONS $what
90         # push back
91         msg Avoiding the push back for now, run manually if needed
92         msg git push $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where
93         #msg pushing back $where onto flab
94         #run git push $GIT_OPTIONS $FLABREPO refs/heads/$where:refs/heads/$where
95     done
96 }
97
98 function usage () {
99     echo "Usage: $COMMAND [options]"
100     echo "Options"
101     echo " -d dir : use this dir as a workdir for merging"
102     echo "         please use a *dedicated* space"
103     echo " -r : restart from a fresh workdir"
104     echo " -m : master only"
105     echo " -s : skip master push"
106     echo " -i : interactive"
107     echo " -n : dry-run"
108     echo " -v : verbose"
109     exit 1
110 }
111
112 PUSH_MASTER=true
113 function main () {
114     while getopts "d:rmsinv" opt; do
115         case $opt in 
116             d) WORKDIR=$OPTARG; shift ;;
117             r) RESTART_FROM_SCRATCH=true ;;
118             m) MASTER_ONLY=true ;;
119             s) PUSH_MASTER=;;
120             i) INTERACTIVE=true ;;
121             n) GIT_OPTIONS="$GIT_OPTIONS -n" ;;
122             v) set -x ; GIT_OPTIONS="$GIT_OPTIONS -v" ;;
123             *) usage;;
124         esac
125     done
126     
127     [ -n "$WORKDIR" ] || WORKDIR=$DEFAULT_WORKDIR
128
129     trap failure ERR INT 
130
131     [ -n "$RESTART_FROM_SCRATCH" ] && trash_workdir
132     check_or_create_workdir
133     merge_master_in_local_branches
134  
135 }
136
137 ###
138 main "$@"
139
140