aboutsummaryrefslogtreecommitdiff
path: root/helpers/llvm-common
blob: 2a529fa52dd451138a1924bdcf7a237ef269e8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash

# The common script is only meant to be included from other LLVM helper scripts

# Verify the compulsory environment variables - these must be set for all the
# helper scripts.
if [[ $LLVM_ROOT = '' ]]; then
  echo "Please, define \$LLVM_ROOT to point to the root"
  echo "path where the worktree setup should be performed"
  exit -1
fi

if [[ $LLVM_GITRW = '' ]]; then
  echo "Please, define \$LLVM_GITRW to yes if you"
  echo "want to set up Git-SVN and no otherwise"
  exit -1
fi
if [[ $LLVM_GITRW = "yes" ]] && [[ $LLVM_GITUSER = '' ]]; then
  echo "Please, define \$LLVM_GITUSER to access Linaro's Git server"
  exit 1
fi
if [[ $LLVM_GITRW = 'yes' ]] && [[ $LLVM_SVNUSER = '' ]]; then
  echo "Please, define \$LLVM_SVNUSER when using GITRW=yes"
  echo "SVNUSER is your upstream LLVM commit user"
  exit 1
fi

# Verify the environment variables that should be set by llvm-env
verify_env() {
  if [[ $LLVM_SRC = '' ]]; then
    echo "Please, define \$LLVM_SRC to point to the current LLVM"
    echo "worktree directory, or run llvm-env to set it for you"
    exit -1
  fi
  if [[ $LLVM_BLD = '' ]]; then
    echo "Please, define \$LLVM_BLD to point to the current LLVM"
    echo "build directory, or run llvm-env to set it for you"
    exit -1
  fi
}

has() {
  if [[ $1 = '' || $2 = '' ]]; then
    echo no
    return
  fi
  local item=$1
  shift
  for each in $*; do
    if [[ $item = $each ]]; then
      echo yes
      return
    fi
  done
  echo no
}

get_branch() {
  branch=`git rev-parse --abbrev-ref HEAD`
  if [[ $? != 0 ]]; then
    local dir=`pwd`
  	echo "Source dir '$dir' is not in a git repository" 1>&2
	  exit -1
  fi
  echo $branch
}

get_branches() {
  branches=`git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short)'`
  if [[ $? != 0 ]]; then
    local dir=`pwd`
  	echo "Source dir '$dir' is not in a git repository" 1>&2
	  exit -1
  fi
  echo $branches
}

has_branch() {
  branch=$1
  branches=`get_branches`
  result=`has $branch $branches`
  echo $result
}

safe_run() {
  "$@"
  if [[ $? != 0 ]]; then
	  echo "'$@' failed, bailing out"
    exit 1
  fi
}

is_git() {
  test -d `git rev-parse --show-toplevel`/.git
}

is_git_svn() {
  test -f `git rev-parse --show-toplevel`/.git/svn/.metadata
}

# Quiet pushd & popd
pushdq() {
  pushd "$@" > /dev/null
}

popdq() {
  popd "$@" > /dev/null
}

add_worktree() {
  repo_dir=$1
  worktree_dir=$2
  branch=$3

  pushdq $repo_dir

  if [ `get_branch` != 'master' ]; then
    echo "$repo_dir isn't on master, bailing out"
    exit 1
  fi

  if [ `has_branch $branch` = "yes" ]; then
    safe_run git worktree add $worktree_dir $branch
  else
    safe_run git worktree add -b $branch $worktree_dir
  fi
  popdq
}

remove_worktree() {
  repo_dir=$1

  if [ "$2" != "" ]; then
    worktree_dir=$2
    safe_run rm -rf $worktree_dir
  fi

  pushdq $repo_dir
  safe_run git worktree prune
  popdq
}