#!/usr/bin/env bash # This script helps you check which branches are checked out on git and make # sure all repos linked in the current worktree are on the same branch (either # by checking it out, or by creating it if it doesn't exist). The branch must # exist in at least one of the repos. If it doesn't you can force its creation # with -b. # Syntax: llvm-branch [-d|-b] [branch] # -d: delete chosen branch # -b: create branch in all repos # branch: checkout or create branch on all repos linked in the current tree # Without arguments, the script lists all branches and highlights the current one . llvm-common safe_run verify_env function print_branches() { # TODO: put a star or something next to branches that exist in other # worktrees? (can be found by iterating through LLVM_ROOT) local current=$1 shift local space=0 for each in $*; do if [[ $space = 1 ]]; then echo -n " " fi if [[ $each = $current ]]; then echo -n "[$each]" else echo -n $each fi space=1 done } function switch() { SRC=$1 branch=$2 in=$3 if [ -d $SRC ]; then cd $SRC pwd if [[ $in = 'yes' ]]; then if [[ $DELETE = '' ]]; then echo " + Checking out $branch in $SRC" safe_run git checkout $branch else echo " + Deleting $branch in $SRC" safe_run git branch $DELETE $branch fi else if [[ $DELETE = '' ]]; then echo " + Creating $branch in $SRC" safe_run git checkout -b $branch fi fi fi } # Gather info cd $LLVM_SRC llvm_branch=`get_branch` llvm_branches=`get_branches` CLANG_SRC=$LLVM_SRC/tools/clang if [ -d $CLANG_SRC ]; then cd $CLANG_SRC clang_branch=`get_branch` clang_branches=`get_branches` fi CLANG_EXTRA_SRC=$LLVM_SRC/tools/clang/tools/extra if [ -d $CLANG_EXTRA_SRC ]; then cd $CLANG_EXTRA_SRC clang_extra_branch=`get_branch` clang_extra_branches=`get_branches` fi RT_SRC=$LLVM_SRC/projects/compiler-rt if [ -d $RT_SRC ]; then cd $RT_SRC rt_branch=`get_branch` rt_branches=`get_branches` fi CXX_SRC=$LLVM_SRC/projects/libcxx if [ -d $CXX_SRC ]; then cd $CXX_SRC cxx_branch=`get_branch` cxx_branches=`get_branches` fi CXXABI_SRC=$LLVM_SRC/projects/libcxxabi if [ -d $CXXABI_SRC ]; then cd $CXXABI_SRC cxxabi_branch=`get_branch` cxxabi_branches=`get_branches` fi UNW_SRC=$LLVM_SRC/projects/libunwind if [ -d $UNW_SRC ]; then cd $UNW_SRC unw_branch=`get_branch` unw_branches=`get_branches` fi LLD_SRC=$LLVM_SRC/tools/lld if [ -d $LLD_SRC ]; then cd $LLD_SRC lld_branch=`get_branch` lld_branches=`get_branches` fi LLDB_SRC=$LLVM_SRC/tools/lldb if [ -d $LLDB_SRC ]; then cd $LLDB_SRC lldb_branch=`get_branch` lldb_branches=`get_branches` fi # Delete chosen branch DELETE='' if [[ $1 = '-d' || $1 = '-D' ]]; then DELETE=$1 shift fi # Force creation CREATE='' if [[ $1 = '-b' ]]; then if [[ $DELETE != '' ]]; then echo "Can't create and delete branch at the same time" exit 1 fi CREATE=$1 shift fi # No branch chosen, list all if [[ $1 = '' ]]; then echo -n "LLVM branches: " print_branches $llvm_branch $llvm_branches echo if [ -d $CLANG_SRC ]; then echo -n "Clang branches: " print_branches $clang_branch $clang_branches echo fi if [ -d $CLANG_EXTRA_SRC ]; then echo -n "Clang tools extra branches: " print_branches $clang_extra_branch $clang_extra_branches echo fi if [ -d $RT_SRC ]; then echo -n "Compiler-RT branches: " print_branches $rt_branch $rt_branches echo fi if [ -d $CXX_SRC ]; then echo -n "LibC++ branches: " print_branches $cxx_branch $cxx_branches echo fi if [ -d $CXXABI_SRC ]; then echo -n "LibC++ABI branches: " print_branches $cxxabi_branch $cxxabi_branches echo fi if [ -d $UNW_SRC ]; then echo -n "LibUnwind branches: " print_branches $unw_branch $unw_branches echo fi if [ -d $LLD_SRC ]; then echo -n "LLD branches: " print_branches $lld_branch $lld_branches echo fi if [ -d $LLDB_SRC ]; then echo -n "LLDB branches: " print_branches $lldb_branch $lldb_branches echo fi exit fi # Search for branch name branch=$1 if [[ $DELETE != '' ]]; then if [[ $branch = 'master' ]]; then echo "Cannot delete the master branch" exit 1 fi if [[ $branch = $llvm_branch ]]; then echo "Cannot delete $branch - it is checked out in llvm" exit 2 fi if [[ $branch = $clang_branch ]]; then echo "Cannot delete $branch - it is checked out in clang" exit 2 fi if [[ $branch = $clang_extra_branch ]]; then echo "Cannot delete $branch - it is checked out in clang tools extra" exit 2 fi if [[ $branch = $rt_branch ]]; then echo "Cannot delete $branch - it is checked out in compiler rt" exit 2 fi if [[ $branch = $cxx_branch ]]; then echo "Cannot delete $branch - it is checked out in libcxx" exit 2 fi if [[ $branch = $cxxabi_branch ]]; then echo "Cannot delete $branch - it is checked out in libcxxabi" exit 2 fi if [[ $branch = $unw_branch ]]; then echo "Cannot delete $branch - it is checked out in libunwind" exit 2 fi if [[ $branch = $lld_branch ]]; then echo "Cannot delete $branch - it is checked out in lld" exit 2 fi if [[ $branch = $lldb_branch ]]; then echo "Cannot delete $branch - it is checked out in lldb" exit 2 fi fi # Check which projects have the branch in_llvm=`has $branch $llvm_branches` in_clang=`has $branch $clang_branches` in_clang_extra=`has $branch $clang_extra_branches` in_rt=`has $branch $rt_branches` in_cxx=`has $branch $cxx_branches` in_cxxabi=`has $branch $cxxabi_branches` in_unw=`has $branch $unw_branches` in_lld=`has $branch $lld_branches` in_lldb=`has $branch $lldb_branches` if [[ $CREATE = '' ]]; then if [[ $in_clang = 'no' && $in_clang_extra && $in_llvm = 'no' && \ $in_rt = 'no' && $in_cxx = 'no' && $in_cxxabi = 'no' && \ $in_unw = 'no' && $in_lld = 'no' && $in_lldb = 'no' ]]; then echo "Branch $branch doesn't exist on any repository" echo "Force its creation with -b" exit 1 fi fi # DO IT switch $LLVM_SRC $branch $in_llvm switch $CLANG_SRC $branch $in_clang switch $CLANG_EXTRA_SRC $branch $in_clang_extra switch $RT_SRC $branch $in_rt switch $CXX_SRC $branch $in_cxx switch $CXXABI_SRC $branch $in_cxxabi switch $UNW_SRC $branch $in_unw switch $LLD_SRC $branch $in_lld switch $LLDB_SRC $branch $in_lldb