Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 1 | #!/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 | |
| 15 | function 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 | |
| 32 | function 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 | |
| 48 | function 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 |
| 71 | cd $LLVM_SRC |
| 72 | llvm_branch=`get_branch` |
| 73 | llvm_branches=`get_branches` |
| 74 | |
| 75 | CLANG_SRC=$LLVM_SRC/tools/clang |
| 76 | if [ -d $CLANG_SRC ]; then |
| 77 | cd $CLANG_SRC |
| 78 | clang_branch=`get_branch` |
| 79 | clang_branches=`get_branches` |
| 80 | fi |
| 81 | |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 82 | CLANG_EXTRA_SRC=$LLVM_SRC/tools/clang/tools/extra |
| 83 | if [ -d $CLANG_EXTRA_SRC ]; then |
| 84 | cd $CLANG_EXTRA_SRC |
| 85 | clang_extra_branch=`get_branch` |
| 86 | clang_extra_branches=`get_branches` |
| 87 | fi |
| 88 | |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 89 | RT_SRC=$LLVM_SRC/projects/compiler-rt |
| 90 | if [ -d $RT_SRC ]; then |
| 91 | cd $RT_SRC |
| 92 | rt_branch=`get_branch` |
| 93 | rt_branches=`get_branches` |
| 94 | fi |
| 95 | |
| 96 | CXX_SRC=$LLVM_SRC/projects/libcxx |
| 97 | if [ -d $CXX_SRC ]; then |
| 98 | cd $CXX_SRC |
| 99 | cxx_branch=`get_branch` |
| 100 | cxx_branches=`get_branches` |
| 101 | fi |
| 102 | |
| 103 | CXXABI_SRC=$LLVM_SRC/projects/libcxxabi |
| 104 | if [ -d $CXXABI_SRC ]; then |
| 105 | cd $CXXABI_SRC |
| 106 | cxxabi_branch=`get_branch` |
| 107 | cxxabi_branches=`get_branches` |
| 108 | fi |
| 109 | |
| 110 | UNW_SRC=$LLVM_SRC/projects/libunwind |
| 111 | if [ -d $UNW_SRC ]; then |
| 112 | cd $UNW_SRC |
| 113 | unw_branch=`get_branch` |
| 114 | unw_branches=`get_branches` |
| 115 | fi |
| 116 | |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 117 | LLD_SRC=$LLVM_SRC/tools/lld |
| 118 | if [ -d $LLD_SRC ]; then |
| 119 | cd $LLD_SRC |
| 120 | lld_branch=`get_branch` |
| 121 | lld_branches=`get_branches` |
| 122 | fi |
| 123 | |
| 124 | LLDB_SRC=$LLVM_SRC/tools/lldb |
| 125 | if [ -d $LLDB_SRC ]; then |
| 126 | cd $LLDB_SRC |
| 127 | lldb_branch=`get_branch` |
| 128 | lldb_branches=`get_branches` |
| 129 | fi |
| 130 | |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 131 | # Delete chosen branch |
| 132 | DEL='' |
| 133 | if [[ $1 = '-d' || $1 = '-D' ]]; then |
| 134 | DEL=$1 |
| 135 | shift |
| 136 | fi |
| 137 | |
| 138 | # No branch chosen, list all |
| 139 | if [[ $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 Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 148 | 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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 153 | 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 Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 173 | 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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 183 | exit |
| 184 | fi |
| 185 | |
| 186 | # Search for branch name |
| 187 | branch=$1 |
| 188 | |
| 189 | if [[ $DEL = 1 && $branch = 'master' ]]; then |
| 190 | echo "Cannot delete the master branch" |
| 191 | exit 1 |
| 192 | fi |
| 193 | |
| 194 | # Check which projects the branch is |
| 195 | in_llvm=`has $branch $llvm_branches` |
| 196 | in_clang=`has $branch $clang_branches` |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 197 | in_clang_extra=`has $branch $clang_extra_branches` |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 198 | in_rt=`has $branch $rt_branches` |
| 199 | in_cxx=`has $branch $cxx_branches` |
| 200 | in_cxxabi=`has $branch $cxxabi_branches` |
| 201 | in_unw=`has $branch $unw_branches` |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 202 | in_lld=`has $branch $lld_branches` |
| 203 | in_lldb=`has $branch $lldb_branches` |
| 204 | if [[ $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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 207 | echo "Branch $branch doesn't exist on any repository" |
| 208 | exit 1 |
| 209 | fi |
| 210 | |
| 211 | # DO IT |
| 212 | switch $LLVM_SRC $branch $in_llvm |
| 213 | switch $CLANG_SRC $branch $in_clang |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 214 | switch $CLANG_EXTRA_SRC $branch $in_clang_extra |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 215 | switch $RT_SRC $branch $in_rt |
| 216 | switch $CXX_SRC $branch $in_cxx |
| 217 | switch $CXXABI_SRC $branch $in_cxxabi |
| 218 | switch $UNW_SRC $branch $in_unw |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame^] | 219 | switch $LLD_SRC $branch $in_lld |
| 220 | switch $LLDB_SRC $branch $in_lldb |