blob: 98a95047db7fc861fbe678d6e7154bf555f274d6 [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
4# sure they're coherent with the feature you're developing. For instance,
5# if a feature spans across both LLVM and Clang, you could name the branch on
6# each repo the same and use this script to seamlessly move between them.
7
8# Syntax: llvm-branch [-d] [branch]
9# -d: delete chosen branch
10# branch: find on llvm and clang and check it out
11# Without arguments, the script lists all branches and highlight the current one
12
13. llvm-common
14
15function print_branches() {
16 local current=$1
17 shift
18 local space=0
19 for each in $*; do
20 if [[ $space = 1 ]]; then
21 echo -n " "
22 fi
23 if [[ $each = $current ]]; then
24 echo -n "[$each]"
25 else
26 echo -n $each
27 fi
28 space=1
29 done
30}
31
32function has() {
33 if [[ $1 = '' || $2 = '' ]]; then
34 echo no
35 return
36 fi
37 local item=$1
38 shift
39 for each in $*; do
40 if [[ $item = $each ]]; then
41 echo yes
42 return
43 fi
44 done
45 echo no
46}
47
48function switch() {
49 SRC=$1
50 branch=$2
51 in=$3
52 if [ -d $SRC ]; then
53 cd $SRC
54 pwd
55 if [[ $in = 'yes' ]]; then
56 if [[ $DEL = '' ]]; then
57 safe_run git checkout $branch
58 else
59 safe_run git checkout master
60 safe_run git branch $DEL $branch
61 fi
62 else
63 if [[ $DEL = '' ]]; then
64 safe_run git checkout master
65 fi
66 fi
67 fi
68}
69
70# Gather info
71cd $LLVM_SRC
72llvm_branch=`get_branch`
73llvm_branches=`get_branches`
74
75CLANG_SRC=$LLVM_SRC/tools/clang
76if [ -d $CLANG_SRC ]; then
77 cd $CLANG_SRC
78 clang_branch=`get_branch`
79 clang_branches=`get_branches`
80fi
81
Diana Picus3124fb82016-04-25 15:37:25 +030082CLANG_EXTRA_SRC=$LLVM_SRC/tools/clang/tools/extra
83if [ -d $CLANG_EXTRA_SRC ]; then
84 cd $CLANG_EXTRA_SRC
85 clang_extra_branch=`get_branch`
86 clang_extra_branches=`get_branches`
87fi
88
Renato Golin94cc1042016-04-26 11:02:23 +010089RT_SRC=$LLVM_SRC/projects/compiler-rt
90if [ -d $RT_SRC ]; then
91 cd $RT_SRC
92 rt_branch=`get_branch`
93 rt_branches=`get_branches`
94fi
95
96CXX_SRC=$LLVM_SRC/projects/libcxx
97if [ -d $CXX_SRC ]; then
98 cd $CXX_SRC
99 cxx_branch=`get_branch`
100 cxx_branches=`get_branches`
101fi
102
103CXXABI_SRC=$LLVM_SRC/projects/libcxxabi
104if [ -d $CXXABI_SRC ]; then
105 cd $CXXABI_SRC
106 cxxabi_branch=`get_branch`
107 cxxabi_branches=`get_branches`
108fi
109
110UNW_SRC=$LLVM_SRC/projects/libunwind
111if [ -d $UNW_SRC ]; then
112 cd $UNW_SRC
113 unw_branch=`get_branch`
114 unw_branches=`get_branches`
115fi
116
Diana Picus3124fb82016-04-25 15:37:25 +0300117LLD_SRC=$LLVM_SRC/tools/lld
118if [ -d $LLD_SRC ]; then
119 cd $LLD_SRC
120 lld_branch=`get_branch`
121 lld_branches=`get_branches`
122fi
123
124LLDB_SRC=$LLVM_SRC/tools/lldb
125if [ -d $LLDB_SRC ]; then
126 cd $LLDB_SRC
127 lldb_branch=`get_branch`
128 lldb_branches=`get_branches`
129fi
130
Renato Golin94cc1042016-04-26 11:02:23 +0100131# Delete chosen branch
132DEL=''
133if [[ $1 = '-d' || $1 = '-D' ]]; then
134 DEL=$1
135 shift
136fi
137
138# No branch chosen, list all
139if [[ $1 = '' ]]; then
140 echo -n "LLVM branches: "
141 print_branches $llvm_branch $llvm_branches
142 echo
143 if [ -d $CLANG_SRC ]; then
144 echo -n "Clang branches: "
145 print_branches $clang_branch $clang_branches
146 echo
147 fi
Diana Picus3124fb82016-04-25 15:37:25 +0300148 if [ -d $CLANG_EXTRA_SRC ]; then
149 echo -n "Clang tools extra branches: "
150 print_branches $clang_extra_branch $clang_extra_branches
151 echo
152 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100153 if [ -d $RT_SRC ]; then
154 echo -n "Compiler-RT branches: "
155 print_branches $rt_branch $rt_branches
156 echo
157 fi
158 if [ -d $CXX_SRC ]; then
159 echo -n "LibC++ branches: "
160 print_branches $cxx_branch $cxx_branches
161 echo
162 fi
163 if [ -d $CXXABI_SRC ]; then
164 echo -n "LibC++ABI branches: "
165 print_branches $cxxabi_branch $cxxabi_branches
166 echo
167 fi
168 if [ -d $UNW_SRC ]; then
169 echo -n "LibUnwind branches: "
170 print_branches $unw_branch $unw_branches
171 echo
172 fi
Diana Picus3124fb82016-04-25 15:37:25 +0300173 if [ -d $LLD_SRC ]; then
174 echo -n "LLD branches: "
175 print_branches $lld_branch $lld_branches
176 echo
177 fi
178 if [ -d $LLDB_SRC ]; then
179 echo -n "LLDB branches: "
180 print_branches $lldb_branch $lldb_branches
181 echo
182 fi
Renato Golin94cc1042016-04-26 11:02:23 +0100183 exit
184fi
185
186# Search for branch name
187branch=$1
188
189if [[ $DEL = 1 && $branch = 'master' ]]; then
190 echo "Cannot delete the master branch"
191 exit 1
192fi
193
194# Check which projects the branch is
195in_llvm=`has $branch $llvm_branches`
196in_clang=`has $branch $clang_branches`
Diana Picus3124fb82016-04-25 15:37:25 +0300197in_clang_extra=`has $branch $clang_extra_branches`
Renato Golin94cc1042016-04-26 11:02:23 +0100198in_rt=`has $branch $rt_branches`
199in_cxx=`has $branch $cxx_branches`
200in_cxxabi=`has $branch $cxxabi_branches`
201in_unw=`has $branch $unw_branches`
Diana Picus3124fb82016-04-25 15:37:25 +0300202in_lld=`has $branch $lld_branches`
203in_lldb=`has $branch $lldb_branches`
204if [[ $in_clang = 'no' && $in_clang_extra && $in_llvm = 'no' && \
205 $in_rt = 'no' && $in_cxx = 'no' && $in_cxxabi = 'no' && \
206 $in_unw = 'no' && $in_lld = 'no' && $in_lldb = 'no' ]]; then
Renato Golin94cc1042016-04-26 11:02:23 +0100207 echo "Branch $branch doesn't exist on any repository"
208 exit 1
209fi
210
211# DO IT
212switch $LLVM_SRC $branch $in_llvm
213switch $CLANG_SRC $branch $in_clang
Diana Picus3124fb82016-04-25 15:37:25 +0300214switch $CLANG_EXTRA_SRC $branch $in_clang_extra
Renato Golin94cc1042016-04-26 11:02:23 +0100215switch $RT_SRC $branch $in_rt
216switch $CXX_SRC $branch $in_cxx
217switch $CXXABI_SRC $branch $in_cxxabi
218switch $UNW_SRC $branch $in_unw
Diana Picus3124fb82016-04-25 15:37:25 +0300219switch $LLD_SRC $branch $in_lld
220switch $LLDB_SRC $branch $in_lldb