blob: 3ed3cef54333527ab994427334a4788475da7aca [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
82RT_SRC=$LLVM_SRC/projects/compiler-rt
83if [ -d $RT_SRC ]; then
84 cd $RT_SRC
85 rt_branch=`get_branch`
86 rt_branches=`get_branches`
87fi
88
89CXX_SRC=$LLVM_SRC/projects/libcxx
90if [ -d $CXX_SRC ]; then
91 cd $CXX_SRC
92 cxx_branch=`get_branch`
93 cxx_branches=`get_branches`
94fi
95
96CXXABI_SRC=$LLVM_SRC/projects/libcxxabi
97if [ -d $CXXABI_SRC ]; then
98 cd $CXXABI_SRC
99 cxxabi_branch=`get_branch`
100 cxxabi_branches=`get_branches`
101fi
102
103UNW_SRC=$LLVM_SRC/projects/libunwind
104if [ -d $UNW_SRC ]; then
105 cd $UNW_SRC
106 unw_branch=`get_branch`
107 unw_branches=`get_branches`
108fi
109
110# Delete chosen branch
111DEL=''
112if [[ $1 = '-d' || $1 = '-D' ]]; then
113 DEL=$1
114 shift
115fi
116
117# No branch chosen, list all
118if [[ $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
148fi
149
150# Search for branch name
151branch=$1
152
153if [[ $DEL = 1 && $branch = 'master' ]]; then
154 echo "Cannot delete the master branch"
155 exit 1
156fi
157
158# Check which projects the branch is
159in_llvm=`has $branch $llvm_branches`
160in_clang=`has $branch $clang_branches`
161in_rt=`has $branch $rt_branches`
162in_cxx=`has $branch $cxx_branches`
163in_cxxabi=`has $branch $cxxabi_branches`
164in_unw=`has $branch $unw_branches`
165if [[ $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
169fi
170
171# DO IT
172switch $LLVM_SRC $branch $in_llvm
173switch $CLANG_SRC $branch $in_clang
174switch $RT_SRC $branch $in_rt
175switch $CXX_SRC $branch $in_cxx
176switch $CXXABI_SRC $branch $in_cxxabi
177switch $UNW_SRC $branch $in_unw