blob: 495214150072c6ce76d9d66aa0b5766f675b95d6 [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
74RT_SRC=$LLVM_SRC/projects/compiler-rt
75if [ -d $RT_SRC ]; then
76 cd $RT_SRC
77 rt_branch=`get_branch`
78 rt_branches=`get_branches`
79fi
80
81CXX_SRC=$LLVM_SRC/projects/libcxx
82if [ -d $CXX_SRC ]; then
83 cd $CXX_SRC
84 cxx_branch=`get_branch`
85 cxx_branches=`get_branches`
86fi
87
88CXXABI_SRC=$LLVM_SRC/projects/libcxxabi
89if [ -d $CXXABI_SRC ]; then
90 cd $CXXABI_SRC
91 cxxabi_branch=`get_branch`
92 cxxabi_branches=`get_branches`
93fi
94
95UNW_SRC=$LLVM_SRC/projects/libunwind
96if [ -d $UNW_SRC ]; then
97 cd $UNW_SRC
98 unw_branch=`get_branch`
99 unw_branches=`get_branches`
100fi
101
Diana Picus3124fb82016-04-25 15:37:25 +0300102LLD_SRC=$LLVM_SRC/tools/lld
103if [ -d $LLD_SRC ]; then
104 cd $LLD_SRC
105 lld_branch=`get_branch`
106 lld_branches=`get_branches`
107fi
108
109LLDB_SRC=$LLVM_SRC/tools/lldb
110if [ -d $LLDB_SRC ]; then
111 cd $LLDB_SRC
112 lldb_branch=`get_branch`
113 lldb_branches=`get_branches`
114fi
115
Renato Golin94cc1042016-04-26 11:02:23 +0100116# Delete chosen branch
Diana Picus72189fd2016-05-23 19:32:46 +0300117DELETE=''
Renato Golin94cc1042016-04-26 11:02:23 +0100118if [[ $1 = '-d' || $1 = '-D' ]]; then
Diana Picus72189fd2016-05-23 19:32:46 +0300119 DELETE=$1
120 shift
121fi
122
123# Force creation
124CREATE=''
125if [[ $1 = '-b' ]]; then
126 if [[ $DELETE != '' ]]; then
127 echo "Can't create and delete branch at the same time"
128 exit 1
129 fi
130
131 CREATE=$1
Renato Golin94cc1042016-04-26 11:02:23 +0100132 shift
133fi
134
135# No branch chosen, list all
136if [[ $1 = '' ]]; then
137 echo -n "LLVM branches: "
138 print_branches $llvm_branch $llvm_branches
139 echo
140 if [ -d $CLANG_SRC ]; then
141 echo -n "Clang branches: "
142 print_branches $clang_branch $clang_branches
143 echo
144 fi
145 if [ -d $RT_SRC ]; then
146 echo -n "Compiler-RT branches: "
147 print_branches $rt_branch $rt_branches
148 echo
149 fi
150 if [ -d $CXX_SRC ]; then
151 echo -n "LibC++ branches: "
152 print_branches $cxx_branch $cxx_branches
153 echo
154 fi
155 if [ -d $CXXABI_SRC ]; then
156 echo -n "LibC++ABI branches: "
157 print_branches $cxxabi_branch $cxxabi_branches
158 echo
159 fi
160 if [ -d $UNW_SRC ]; then
161 echo -n "LibUnwind branches: "
162 print_branches $unw_branch $unw_branches
163 echo
164 fi
Diana Picus3124fb82016-04-25 15:37:25 +0300165 if [ -d $LLD_SRC ]; then
166 echo -n "LLD branches: "
167 print_branches $lld_branch $lld_branches
168 echo
169 fi
170 if [ -d $LLDB_SRC ]; then
171 echo -n "LLDB branches: "
172 print_branches $lldb_branch $lldb_branches
173 echo
174 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100175 exit
176fi
177
178# Search for branch name
179branch=$1
180
Diana Picus72189fd2016-05-23 19:32:46 +0300181if [[ $DELETE != '' ]]; then
182 if [[ $branch = 'master' ]]; then
183 echo "Cannot delete the master branch"
184 exit 1
185 fi
186 if [[ $branch = $llvm_branch ]]; then
187 echo "Cannot delete $branch - it is checked out in llvm"
188 exit 2
189 fi
190 if [[ $branch = $clang_branch ]]; then
191 echo "Cannot delete $branch - it is checked out in clang"
192 exit 2
193 fi
Diana Picus72189fd2016-05-23 19:32:46 +0300194 if [[ $branch = $rt_branch ]]; then
195 echo "Cannot delete $branch - it is checked out in compiler rt"
196 exit 2
197 fi
198 if [[ $branch = $cxx_branch ]]; then
199 echo "Cannot delete $branch - it is checked out in libcxx"
200 exit 2
201 fi
202 if [[ $branch = $cxxabi_branch ]]; then
203 echo "Cannot delete $branch - it is checked out in libcxxabi"
204 exit 2
205 fi
206 if [[ $branch = $unw_branch ]]; then
207 echo "Cannot delete $branch - it is checked out in libunwind"
208 exit 2
209 fi
210 if [[ $branch = $lld_branch ]]; then
211 echo "Cannot delete $branch - it is checked out in lld"
212 exit 2
213 fi
214 if [[ $branch = $lldb_branch ]]; then
215 echo "Cannot delete $branch - it is checked out in lldb"
216 exit 2
217 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100218fi
219
Diana Picus72189fd2016-05-23 19:32:46 +0300220# Check which projects have the branch
Renato Golin94cc1042016-04-26 11:02:23 +0100221in_llvm=`has $branch $llvm_branches`
222in_clang=`has $branch $clang_branches`
223in_rt=`has $branch $rt_branches`
224in_cxx=`has $branch $cxx_branches`
225in_cxxabi=`has $branch $cxxabi_branches`
226in_unw=`has $branch $unw_branches`
Diana Picus3124fb82016-04-25 15:37:25 +0300227in_lld=`has $branch $lld_branches`
228in_lldb=`has $branch $lldb_branches`
Diana Picus72189fd2016-05-23 19:32:46 +0300229if [[ $CREATE = '' ]]; then
Renato Golin000477d2018-03-01 18:03:55 +0000230 if [[ $in_clang = 'no' && $in_llvm = 'no' && \
Diana Picus72189fd2016-05-23 19:32:46 +0300231 $in_rt = 'no' && $in_cxx = 'no' && $in_cxxabi = 'no' && \
232 $in_unw = 'no' && $in_lld = 'no' && $in_lldb = 'no' ]]; then
233 echo "Branch $branch doesn't exist on any repository"
234 echo "Force its creation with -b"
235 exit 1
236 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100237fi
238
239# DO IT
240switch $LLVM_SRC $branch $in_llvm
241switch $CLANG_SRC $branch $in_clang
242switch $RT_SRC $branch $in_rt
243switch $CXX_SRC $branch $in_cxx
244switch $CXXABI_SRC $branch $in_cxxabi
245switch $UNW_SRC $branch $in_unw
Diana Picus3124fb82016-04-25 15:37:25 +0300246switch $LLD_SRC $branch $in_lld
247switch $LLDB_SRC $branch $in_lldb