about summary refs log tree commit diff homepage
path: root/scripts/build/common-functions
blob: c964419f604ab12bf371ef075e828f1192969127 (plain) (blame)
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
# Defines common function and variables used by the scripts

function git_clone_or_update() {
  local url="$1"
  local destination="$2"
  local branch="$3"

  if [[ ! -e "${destination}/.git" ]]; then
    git clone $([ "${branch}x" != "x" ] && echo "--depth 1 -b ${branch}" || echo "") "$url" "$destination"
  else
    cd "$destination"
    git pull
    if [[ ! -z "$branch" ]]; then
      git checkout "${branch}"
    fi
  fi
}

function get_git_hash() {
  local url="$1"
  local branch="$2"
  local commit_id=""
  
  # List remote git branches and get the commit id of the branch
  commit_id="$(git ls-remote -h "$url" |grep "$branch" | cut -d$'\t' -f 1)"
  
  # If that doesn't work use the branch instead of the commit id
  if [[ -z "${commit_id}" ]]; then 
    echo "${branch}"
  else
    echo "${commit_id:0:7}"
  fi
}

function with_sudo() {
    echo "Checking sudo $@"
    if [[ $(whoami) != "root" ]]; then
        sudo "$@"
    else
        "$@"
    fi
}