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 | |
| 82 | RT_SRC=$LLVM_SRC/projects/compiler-rt |
| 83 | if [ -d $RT_SRC ]; then |
| 84 | cd $RT_SRC |
| 85 | rt_branch=`get_branch` |
| 86 | rt_branches=`get_branches` |
| 87 | fi |
| 88 | |
| 89 | CXX_SRC=$LLVM_SRC/projects/libcxx |
| 90 | if [ -d $CXX_SRC ]; then |
| 91 | cd $CXX_SRC |
| 92 | cxx_branch=`get_branch` |
| 93 | cxx_branches=`get_branches` |
| 94 | fi |
| 95 | |
| 96 | CXXABI_SRC=$LLVM_SRC/projects/libcxxabi |
| 97 | if [ -d $CXXABI_SRC ]; then |
| 98 | cd $CXXABI_SRC |
| 99 | cxxabi_branch=`get_branch` |
| 100 | cxxabi_branches=`get_branches` |
| 101 | fi |
| 102 | |
| 103 | UNW_SRC=$LLVM_SRC/projects/libunwind |
| 104 | if [ -d $UNW_SRC ]; then |
| 105 | cd $UNW_SRC |
| 106 | unw_branch=`get_branch` |
| 107 | unw_branches=`get_branches` |
| 108 | fi |
| 109 | |
| 110 | # Delete chosen branch |
| 111 | DEL='' |
| 112 | if [[ $1 = '-d' || $1 = '-D' ]]; then |
| 113 | DEL=$1 |
| 114 | shift |
| 115 | fi |
| 116 | |
| 117 | # No branch chosen, list all |
| 118 | if [[ $1 = '' ]]; then |
| 119 | echo -n "LLVM branches: " |
| 120 | print_branches $llvm_branch $llvm_branches |
| 121 | echo |
| 122 | if [ -d $CLANG_SRC ]; then |
| 123 | echo -n "Clang branches: " |
| 124 | print_branches $clang_branch $clang_branches |
| 125 | echo |
| 126 | fi |
| 127 | if [ -d $RT_SRC ]; then |
| 128 | echo -n "Compiler-RT branches: " |
| 129 | print_branches $rt_branch $rt_branches |
| 130 | echo |
| 131 | fi |
| 132 | if [ -d $CXX_SRC ]; then |
| 133 | echo -n "LibC++ branches: " |
| 134 | print_branches $cxx_branch $cxx_branches |
| 135 | echo |
| 136 | fi |
| 137 | if [ -d $CXXABI_SRC ]; then |
| 138 | echo -n "LibC++ABI branches: " |
| 139 | print_branches $cxxabi_branch $cxxabi_branches |
| 140 | echo |
| 141 | fi |
| 142 | if [ -d $UNW_SRC ]; then |
| 143 | echo -n "LibUnwind branches: " |
| 144 | print_branches $unw_branch $unw_branches |
| 145 | echo |
| 146 | fi |
| 147 | exit |
| 148 | fi |
| 149 | |
| 150 | # Search for branch name |
| 151 | branch=$1 |
| 152 | |
| 153 | if [[ $DEL = 1 && $branch = 'master' ]]; then |
| 154 | echo "Cannot delete the master branch" |
| 155 | exit 1 |
| 156 | fi |
| 157 | |
| 158 | # Check which projects the branch is |
| 159 | in_llvm=`has $branch $llvm_branches` |
| 160 | in_clang=`has $branch $clang_branches` |
| 161 | in_rt=`has $branch $rt_branches` |
| 162 | in_cxx=`has $branch $cxx_branches` |
| 163 | in_cxxabi=`has $branch $cxxabi_branches` |
| 164 | in_unw=`has $branch $unw_branches` |
| 165 | if [[ $in_clang = 'no' && $in_llvm = 'no' && $in_rt = 'no' && \ |
| 166 | $in_cxx = 'no' && $in_cxxabi = 'no' && $in_unw = 'no' ]]; then |
| 167 | echo "Branch $branch doesn't exist on any repository" |
| 168 | exit 1 |
| 169 | fi |
| 170 | |
| 171 | # DO IT |
| 172 | switch $LLVM_SRC $branch $in_llvm |
| 173 | switch $CLANG_SRC $branch $in_clang |
| 174 | switch $RT_SRC $branch $in_rt |
| 175 | switch $CXX_SRC $branch $in_cxx |
| 176 | switch $CXXABI_SRC $branch $in_cxxabi |
| 177 | switch $UNW_SRC $branch $in_unw |