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 |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 4 | # 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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 8 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 9 | # 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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 14 | |
| 15 | . llvm-common |
| 16 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 17 | safe_run verify_env |
| 18 | |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 19 | function print_branches() { |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 20 | # TODO: put a star or something next to branches that exist in other |
| 21 | # worktrees? (can be found by iterating through LLVM_ROOT) |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 22 | 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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 38 | function 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 Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 46 | if [[ $DELETE = '' ]]; then |
| 47 | echo " + Checking out $branch in $SRC" |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 48 | safe_run git checkout $branch |
| 49 | else |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 50 | echo " + Deleting $branch in $SRC" |
| 51 | safe_run git branch $DELETE $branch |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 52 | fi |
| 53 | else |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 54 | if [[ $DELETE = '' ]]; then |
| 55 | echo " + Creating $branch in $SRC" |
| 56 | safe_run git checkout -b $branch |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 57 | fi |
| 58 | fi |
| 59 | fi |
| 60 | } |
| 61 | |
| 62 | # Gather info |
| 63 | cd $LLVM_SRC |
| 64 | llvm_branch=`get_branch` |
| 65 | llvm_branches=`get_branches` |
| 66 | |
| 67 | CLANG_SRC=$LLVM_SRC/tools/clang |
| 68 | if [ -d $CLANG_SRC ]; then |
| 69 | cd $CLANG_SRC |
| 70 | clang_branch=`get_branch` |
| 71 | clang_branches=`get_branches` |
| 72 | fi |
| 73 | |
| 74 | RT_SRC=$LLVM_SRC/projects/compiler-rt |
| 75 | if [ -d $RT_SRC ]; then |
| 76 | cd $RT_SRC |
| 77 | rt_branch=`get_branch` |
| 78 | rt_branches=`get_branches` |
| 79 | fi |
| 80 | |
| 81 | CXX_SRC=$LLVM_SRC/projects/libcxx |
| 82 | if [ -d $CXX_SRC ]; then |
| 83 | cd $CXX_SRC |
| 84 | cxx_branch=`get_branch` |
| 85 | cxx_branches=`get_branches` |
| 86 | fi |
| 87 | |
| 88 | CXXABI_SRC=$LLVM_SRC/projects/libcxxabi |
| 89 | if [ -d $CXXABI_SRC ]; then |
| 90 | cd $CXXABI_SRC |
| 91 | cxxabi_branch=`get_branch` |
| 92 | cxxabi_branches=`get_branches` |
| 93 | fi |
| 94 | |
| 95 | UNW_SRC=$LLVM_SRC/projects/libunwind |
| 96 | if [ -d $UNW_SRC ]; then |
| 97 | cd $UNW_SRC |
| 98 | unw_branch=`get_branch` |
| 99 | unw_branches=`get_branches` |
| 100 | fi |
| 101 | |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame] | 102 | LLD_SRC=$LLVM_SRC/tools/lld |
| 103 | if [ -d $LLD_SRC ]; then |
| 104 | cd $LLD_SRC |
| 105 | lld_branch=`get_branch` |
| 106 | lld_branches=`get_branches` |
| 107 | fi |
| 108 | |
| 109 | LLDB_SRC=$LLVM_SRC/tools/lldb |
| 110 | if [ -d $LLDB_SRC ]; then |
| 111 | cd $LLDB_SRC |
| 112 | lldb_branch=`get_branch` |
| 113 | lldb_branches=`get_branches` |
| 114 | fi |
| 115 | |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 116 | # Delete chosen branch |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 117 | DELETE='' |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 118 | if [[ $1 = '-d' || $1 = '-D' ]]; then |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 119 | DELETE=$1 |
| 120 | shift |
| 121 | fi |
| 122 | |
| 123 | # Force creation |
| 124 | CREATE='' |
| 125 | if [[ $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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 132 | shift |
| 133 | fi |
| 134 | |
| 135 | # No branch chosen, list all |
| 136 | if [[ $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 Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame] | 165 | 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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 175 | exit |
| 176 | fi |
| 177 | |
| 178 | # Search for branch name |
| 179 | branch=$1 |
| 180 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 181 | if [[ $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 Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 194 | 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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 218 | fi |
| 219 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 220 | # Check which projects have the branch |
Renato Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 221 | in_llvm=`has $branch $llvm_branches` |
| 222 | in_clang=`has $branch $clang_branches` |
| 223 | in_rt=`has $branch $rt_branches` |
| 224 | in_cxx=`has $branch $cxx_branches` |
| 225 | in_cxxabi=`has $branch $cxxabi_branches` |
| 226 | in_unw=`has $branch $unw_branches` |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame] | 227 | in_lld=`has $branch $lld_branches` |
| 228 | in_lldb=`has $branch $lldb_branches` |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 229 | if [[ $CREATE = '' ]]; then |
Renato Golin | 000477d | 2018-03-01 18:03:55 +0000 | [diff] [blame] | 230 | if [[ $in_clang = 'no' && $in_llvm = 'no' && \ |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 231 | $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 Golin | 94cc104 | 2016-04-26 11:02:23 +0100 | [diff] [blame] | 237 | fi |
| 238 | |
| 239 | # DO IT |
| 240 | switch $LLVM_SRC $branch $in_llvm |
| 241 | switch $CLANG_SRC $branch $in_clang |
| 242 | switch $RT_SRC $branch $in_rt |
| 243 | switch $CXX_SRC $branch $in_cxx |
| 244 | switch $CXXABI_SRC $branch $in_cxxabi |
| 245 | switch $UNW_SRC $branch $in_unw |
Diana Picus | 3124fb8 | 2016-04-25 15:37:25 +0300 | [diff] [blame] | 246 | switch $LLD_SRC $branch $in_lld |
| 247 | switch $LLDB_SRC $branch $in_lldb |