-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.sh
More file actions
115 lines (89 loc) · 2.17 KB
/
quickstart.sh
File metadata and controls
115 lines (89 loc) · 2.17 KB
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
#!/bin/bash
# Allow QS_REPO and QS_GIT_REPO
[[ -z "${QS_GIT_REPO}" ]] && QS_GIT_REPO="${QS_REPO}"
REPO="${QS_GIT_REPO%%/tree/*}"
# Allow QS_BRANCH and QS_GIT_BRANCH
[[ -z "${QS_GIT_BRANCH}" ]] && QS_GIT_BRANCH="${QS_BRANCH}"
if [[ -n "${QS_GIT_BRANCH}" ]] ; then
# if branch explicitly set, it takes precedence
BRANCH="${QS_GIT_BRANCH}"
elif [[ "${QS_GIT_REPO}" == *"/tree/"* ]] ; then
# Try to get branch from URL
BRANCH="${QS_GIT_REPO##*/tree/}"
fi
# If branch was never set, default to main
[[ -z "${BRANCH}" ]] && BRANCH=main
# Temp working space
TMPDIR=$(mktemp -d)
DEBUG=1
# FUNCTIONS
cleanup() {
[[ $DEBUG -gt 0 ]] && set -x
rm -rf "$TMPDIR"
}
die() {
echo "Error: ${*}" >&2
cleanup
exit 2
}
check_git() {
[[ $DEBUG -gt 0 ]] && set -x
which git || die "Can't find 'git'. Is 'git' installed and on your path?"
}
validate_repo() {
[[ $DEBUG -gt 0 ]] && set -x
[[ -z "$REPO" ]] && die "QS_GIT_REPO not defined."
git ls-remote "$REPO" &>/dev/null \
|| die "Invalid or Inaccessible repository: '$REPO'"
}
validate_branch() {
[[ $DEBUG -gt 0 ]] && set -x
[[ -z "$BRANCH" ]] && die "QS_GIT_BRANCH not defined."
local _is_valid
_is_valid=no
if is_remote_branch_valid "$BRANCH" ; then
_is_valid=yes
else
if [[ "$BRANCH" == "main" ]] ; then
# try for master branch (instead of main)
if is_remote_branch_valid "master" ; then
BRANCH="master"
_is_valid=yes
fi
fi
fi
if [[ "$_is_valid" == "no" ]] ; then
die "Invalid branch: '$BRANCH'"
fi
}
is_remote_branch_valid() {
[[ $DEBUG -gt 0 ]] && set -x
local _branch
_branch="$1"
[[ -z "$_branch" ]] && die "Refusing to test empty branch name"
git ls-remote --heads "$REPO" "$_branch" 2>/dev/null \
| head -1 \
| grep -q "refs/heads/$_branch"
}
clone_repo() {
[[ $DEBUG -gt 0 ]] && set -x
git clone \
--single-branch \
--branch "$BRANCH" \
"$REPO" \
$TMPDIR
}
run_setup() {
[[ $DEBUG -gt 0 ]] && set -x
local _setup
_setup=$TMPDIR/setup.sh
[[ -f "$_setup" ]] || die "Setup script not found: '$_setup'"
"$_setup"
}
[[ $DEBUG -gt 0 ]] && set -x
check_git
validate_repo
validate_branch
clone_repo
run_setup
cleanup