blob: 2a529fa52dd451138a1924bdcf7a237ef269e8ca [file] [log] [blame]
Renato Golin94cc1042016-04-26 11:02:23 +01001#!/usr/bin/env bash
2
3# The common script is only meant to be included from other LLVM helper scripts
4
Diana Picus72189fd2016-05-23 19:32:46 +03005# Verify the compulsory environment variables - these must be set for all the
6# helper scripts.
7if [[ $LLVM_ROOT = '' ]]; then
8 echo "Please, define \$LLVM_ROOT to point to the root"
9 echo "path where the worktree setup should be performed"
Renato Golin94cc1042016-04-26 11:02:23 +010010 exit -1
11fi
Diana Picus72189fd2016-05-23 19:32:46 +030012
Renato Golin94cc1042016-04-26 11:02:23 +010013if [[ $LLVM_GITRW = '' ]]; then
Diana Picus72189fd2016-05-23 19:32:46 +030014 echo "Please, define \$LLVM_GITRW to yes if you"
15 echo "want to set up Git-SVN and no otherwise"
Renato Golin94cc1042016-04-26 11:02:23 +010016 exit -1
17fi
Diana Picus600c05a2016-06-16 19:28:38 +030018if [[ $LLVM_GITRW = "yes" ]] && [[ $LLVM_GITUSER = '' ]]; then
Renato Golin7c10e6e2016-05-12 16:20:10 +010019 echo "Please, define \$LLVM_GITUSER to access Linaro's Git server"
20 exit 1
21fi
22if [[ $LLVM_GITRW = 'yes' ]] && [[ $LLVM_SVNUSER = '' ]]; then
23 echo "Please, define \$LLVM_SVNUSER when using GITRW=yes"
24 echo "SVNUSER is your upstream LLVM commit user"
25 exit 1
Renato Golin94cc1042016-04-26 11:02:23 +010026fi
27
Diana Picus72189fd2016-05-23 19:32:46 +030028# Verify the environment variables that should be set by llvm-env
29verify_env() {
30 if [[ $LLVM_SRC = '' ]]; then
31 echo "Please, define \$LLVM_SRC to point to the current LLVM"
32 echo "worktree directory, or run llvm-env to set it for you"
33 exit -1
34 fi
35 if [[ $LLVM_BLD = '' ]]; then
36 echo "Please, define \$LLVM_BLD to point to the current LLVM"
37 echo "build directory, or run llvm-env to set it for you"
38 exit -1
39 fi
40}
41
42has() {
43 if [[ $1 = '' || $2 = '' ]]; then
44 echo no
45 return
46 fi
47 local item=$1
48 shift
49 for each in $*; do
50 if [[ $item = $each ]]; then
51 echo yes
52 return
53 fi
54 done
55 echo no
56}
57
Renato Golin94cc1042016-04-26 11:02:23 +010058get_branch() {
59 branch=`git rev-parse --abbrev-ref HEAD`
60 if [[ $? != 0 ]]; then
61 local dir=`pwd`
62 echo "Source dir '$dir' is not in a git repository" 1>&2
63 exit -1
64 fi
65 echo $branch
66}
67
68get_branches() {
69 branches=`git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)'`
70 if [[ $? != 0 ]]; then
71 local dir=`pwd`
72 echo "Source dir '$dir' is not in a git repository" 1>&2
73 exit -1
74 fi
75 echo $branches
76}
77
Diana Picus72189fd2016-05-23 19:32:46 +030078has_branch() {
79 branch=$1
80 branches=`get_branches`
81 result=`has $branch $branches`
82 echo $result
83}
84
Renato Golin94cc1042016-04-26 11:02:23 +010085safe_run() {
Diana Picus416bc4f2016-04-22 16:47:57 +030086 "$@"
Renato Golin94cc1042016-04-26 11:02:23 +010087 if [[ $? != 0 ]]; then
Diana Picus416bc4f2016-04-22 16:47:57 +030088 echo "'$@' failed, bailing out"
Renato Golin94cc1042016-04-26 11:02:23 +010089 exit 1
90 fi
91}
92
93is_git() {
94 test -d `git rev-parse --show-toplevel`/.git
95}
96
97is_git_svn() {
98 test -f `git rev-parse --show-toplevel`/.git/svn/.metadata
99}
Diana Picus72189fd2016-05-23 19:32:46 +0300100
101# Quiet pushd & popd
102pushdq() {
103 pushd "$@" > /dev/null
104}
105
106popdq() {
107 popd "$@" > /dev/null
108}
109
110add_worktree() {
111 repo_dir=$1
112 worktree_dir=$2
113 branch=$3
114
115 pushdq $repo_dir
116
117 if [ `get_branch` != 'master' ]; then
118 echo "$repo_dir isn't on master, bailing out"
119 exit 1
120 fi
121
122 if [ `has_branch $branch` = "yes" ]; then
123 safe_run git worktree add $worktree_dir $branch
124 else
125 safe_run git worktree add -b $branch $worktree_dir
126 fi
127 popdq
128}
129
130remove_worktree() {
131 repo_dir=$1
132
133 if [ "$2" != "" ]; then
134 worktree_dir=$2
135 safe_run rm -rf $worktree_dir
136 fi
137
138 pushdq $repo_dir
139 safe_run git worktree prune
140 popdq
141}