blob: 534391473e2a24490a33d5252e0c2aad2d55894b [file] [log] [blame]
Renato Golin94cc1042016-04-26 11:02:23 +01001#!/usr/bin/env bash
2
3# This script helps you check which branches are checked out on git and make
Diana Picus72189fd2016-05-23 19:32:46 +03004# sure all repos linked in the current worktree are on the same branch (either
5# by checking it out, or by creating it if it doesn't exist). The branch must
6# exist in at least one of the repos. If it doesn't you can force its creation
7# with -b.
Renato Golin94cc1042016-04-26 11:02:23 +01008
Diana Picus72189fd2016-05-23 19:32:46 +03009# Syntax: llvm-branch [-d|-b] [branch]
10# -d: delete chosen branch
11# -b: create branch in all repos
12# branch: checkout or create branch on all repos linked in the current tree
13# Without arguments, the script lists all branches and highlights the current one
Renato Golin94cc1042016-04-26 11:02:23 +010014
15. llvm-common
16
Diana Picus72189fd2016-05-23 19:32:46 +030017safe_run verify_env
18
Renato Golin94cc1042016-04-26 11:02:23 +010019function print_branches() {
Diana Picus72189fd2016-05-23 19:32:46 +030020 # TODO: put a star or something next to branches that exist in other
21 # worktrees? (can be found by iterating through LLVM_ROOT)
Renato Golin94cc1042016-04-26 11:02:23 +010022 local current=$1
23 shift
24 local space=0
25 for each in $*; do
26 if [[ $space = 1 ]]; then
27 echo -n " "
28 fi
29 if [[ $each = $current ]]; then
30 echo -n "[$each]"
31 else
32 echo -n $each
33 fi
34 space=1
35 done
36}
37
Renato Golin94cc1042016-04-26 11:02:23 +010038function switch() {
39 SRC=$1
40 branch=$2
41 in=$3
42 if [ -d $SRC ]; then
43 cd $SRC
44 pwd
45 if [[ $in = 'yes' ]]; then
Diana Picus72189fd2016-05-23 19:32:46 +030046 if [[ $DELETE = '' ]]; then
47 echo " + Checking out $branch in $SRC"
Renato Golin94cc1042016-04-26 11:02:23 +010048 safe_run git checkout $branch
49 else
Diana Picus72189fd2016-05-23 19:32:46 +030050 echo " + Deleting $branch in $SRC"
51 safe_run git branch $DELETE $branch
Renato Golin94cc1042016-04-26 11:02:23 +010052 fi
53 else
Diana Picus72189fd2016-05-23 19:32:46 +030054 if [[ $DELETE = '' ]]; then
55 echo " + Creating $branch in $SRC"
56 safe_run git checkout -b $branch
Renato Golin94cc1042016-04-26 11:02:23 +010057 fi
58 fi
59 fi
60}
61
62# Gather info
63cd $LLVM_SRC
64llvm_branch=`get_branch`
65llvm_branches=`get_branches`
66
67CLANG_SRC=$LLVM_SRC/tools/clang
68if [ -d $CLANG_SRC ]; then
69 cd $CLANG_SRC
70 clang_branch=`get_branch`
71 clang_branches=`get_branches`
72fi
73
Diana Picus3124fb82016-04-25 15:37:25 +030074CLANG_EXTRA_SRC=$LLVM_SRC/tools/clang/tools/extra
75if [ -d $CLANG_EXTRA_SRC ]; then
76 cd $CLANG_EXTRA_SRC
77 clang_extra_branch=`get_branch`
78 clang_extra_branches=`get_branches`
79fi
80
Renato Golin94cc1042016-04-26 11:02:23 +010081RT_SRC=$LLVM_SRC/projects/compiler-rt
82if [ -d $RT_SRC ]; then
83 cd $RT_SRC
84 rt_branch=`get_branch`
85 rt_branches=`get_branches`
86fi
87
88CXX_SRC=$LLVM_SRC/projects/libcxx
89if [ -d $CXX_SRC ]; then
90 cd $CXX_SRC
91 cxx_branch=`get_branch`
92 cxx_branches=`get_branches`
93fi
94
95CXXABI_SRC=$LLVM_SRC/projects/libcxxabi
96if [ -d $CXXABI_SRC ]; then
97 cd $CXXABI_SRC
98 cxxabi_branch=`get_branch`
99 cxxabi_branches=`get_branches`
100fi
101
102UNW_SRC=$LLVM_SRC/projects/libunwind
103if [ -d $UNW_SRC ]; then
104 cd $UNW_SRC
105 unw_branch=`get_branch`
106 unw_branches=`get_branches`
107fi
108
Diana Picus3124fb82016-04-25 15:37:25 +0300109LLD_SRC=$LLVM_SRC/tools/lld
110if [ -d $LLD_SRC ]; then
111 cd $LLD_SRC
112 lld_branch=`get_branch`
113 lld_branches=`get_branches`
114fi
115
116LLDB_SRC=$LLVM_SRC/tools/lldb
117if [ -d $LLDB_SRC ]; then
118 cd $LLDB_SRC
119 lldb_branch=`get_branch`
120 lldb_branches=`get_branches`
121fi
122
Renato Golin94cc1042016-04-26 11:02:23 +0100123# Delete chosen branch
Diana Picus72189fd2016-05-23 19:32:46 +0300124DELETE=''
Renato Golin94cc1042016-04-26 11:02:23 +0100125if [[ $1 = '-d' || $1 = '-D' ]]; then
Diana Picus72189fd2016-05-23 19:32:46 +0300126 DELETE=$1
127 shift
128fi
129
130# Force creation
131CREATE=''
132if [[ $1 = '-b' ]]; then
133 if [[ $DELETE != '' ]]; then
134 echo "Can't create and delete branch at the same time"
135 exit 1
136 fi
137
138 CREATE=$1
Renato Golin94cc1042016-04-26 11:02:23 +0100139 shift
140fi
141
142# No branch chosen, list all
143if [[ $1 = '' ]]; then
144 echo -n "LLVM branches: "
145 print_branches $llvm_branch $llvm_branches
146 echo
147 if [ -d $CLANG_SRC ]; then
148 echo -n "Clang branches: "
149 print_branches $clang_branch $clang_branches
150 echo
151 fi
Diana Picus3124fb82016-04-25 15:37:25 +0300152 if [ -d $CLANG_EXTRA_SRC ]; then
153 echo -n "Clang tools extra branches: "
154 print_branches $clang_extra_branch $clang_extra_branches
155 echo
156 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100157 if [ -d $RT_SRC ]; then
158 echo -n "Compiler-RT branches: "
159 print_branches $rt_branch $rt_branches
160 echo
161 fi
162 if [ -d $CXX_SRC ]; then
163 echo -n "LibC++ branches: "
164 print_branches $cxx_branch $cxx_branches
165 echo
166 fi
167 if [ -d $CXXABI_SRC ]; then
168 echo -n "LibC++ABI branches: "
169 print_branches $cxxabi_branch $cxxabi_branches
170 echo
171 fi
172 if [ -d $UNW_SRC ]; then
173 echo -n "LibUnwind branches: "
174 print_branches $unw_branch $unw_branches
175 echo
176 fi
Diana Picus3124fb82016-04-25 15:37:25 +0300177 if [ -d $LLD_SRC ]; then
178 echo -n "LLD branches: "
179 print_branches $lld_branch $lld_branches
180 echo
181 fi
182 if [ -d $LLDB_SRC ]; then
183 echo -n "LLDB branches: "
184 print_branches $lldb_branch $lldb_branches
185 echo
186 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100187 exit
188fi
189
190# Search for branch name
191branch=$1
192
Diana Picus72189fd2016-05-23 19:32:46 +0300193if [[ $DELETE != '' ]]; then
194 if [[ $branch = 'master' ]]; then
195 echo "Cannot delete the master branch"
196 exit 1
197 fi
198 if [[ $branch = $llvm_branch ]]; then
199 echo "Cannot delete $branch - it is checked out in llvm"
200 exit 2
201 fi
202 if [[ $branch = $clang_branch ]]; then
203 echo "Cannot delete $branch - it is checked out in clang"
204 exit 2
205 fi
206 if [[ $branch = $clang_extra_branch ]]; then
207 echo "Cannot delete $branch - it is checked out in clang tools extra"
208 exit 2
209 fi
210 if [[ $branch = $rt_branch ]]; then
211 echo "Cannot delete $branch - it is checked out in compiler rt"
212 exit 2
213 fi
214 if [[ $branch = $cxx_branch ]]; then
215 echo "Cannot delete $branch - it is checked out in libcxx"
216 exit 2
217 fi
218 if [[ $branch = $cxxabi_branch ]]; then
219 echo "Cannot delete $branch - it is checked out in libcxxabi"
220 exit 2
221 fi
222 if [[ $branch = $unw_branch ]]; then
223 echo "Cannot delete $branch - it is checked out in libunwind"
224 exit 2
225 fi
226 if [[ $branch = $lld_branch ]]; then
227 echo "Cannot delete $branch - it is checked out in lld"
228 exit 2
229 fi
230 if [[ $branch = $lldb_branch ]]; then
231 echo "Cannot delete $branch - it is checked out in lldb"
232 exit 2
233 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100234fi
235
Diana Picus72189fd2016-05-23 19:32:46 +0300236# Check which projects have the branch
Renato Golin94cc1042016-04-26 11:02:23 +0100237in_llvm=`has $branch $llvm_branches`
238in_clang=`has $branch $clang_branches`
Diana Picus3124fb82016-04-25 15:37:25 +0300239in_clang_extra=`has $branch $clang_extra_branches`
Renato Golin94cc1042016-04-26 11:02:23 +0100240in_rt=`has $branch $rt_branches`
241in_cxx=`has $branch $cxx_branches`
242in_cxxabi=`has $branch $cxxabi_branches`
243in_unw=`has $branch $unw_branches`
Diana Picus3124fb82016-04-25 15:37:25 +0300244in_lld=`has $branch $lld_branches`
245in_lldb=`has $branch $lldb_branches`
Diana Picus72189fd2016-05-23 19:32:46 +0300246if [[ $CREATE = '' ]]; then
247 if [[ $in_clang = 'no' && $in_clang_extra && $in_llvm = 'no' && \
248 $in_rt = 'no' && $in_cxx = 'no' && $in_cxxabi = 'no' && \
249 $in_unw = 'no' && $in_lld = 'no' && $in_lldb = 'no' ]]; then
250 echo "Branch $branch doesn't exist on any repository"
251 echo "Force its creation with -b"
252 exit 1
253 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100254fi
255
256# DO IT
257switch $LLVM_SRC $branch $in_llvm
258switch $CLANG_SRC $branch $in_clang
Diana Picus3124fb82016-04-25 15:37:25 +0300259switch $CLANG_EXTRA_SRC $branch $in_clang_extra
Renato Golin94cc1042016-04-26 11:02:23 +0100260switch $RT_SRC $branch $in_rt
261switch $CXX_SRC $branch $in_cxx
262switch $CXXABI_SRC $branch $in_cxxabi
263switch $UNW_SRC $branch $in_unw
Diana Picus3124fb82016-04-25 15:37:25 +0300264switch $LLD_SRC $branch $in_lld
265switch $LLDB_SRC $branch $in_lldb