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