-
Notifications
You must be signed in to change notification settings - Fork 11k
/
Copy pathutil.sh
executable file
·284 lines (246 loc) · 7.56 KB
/
util.sh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
set -e -u
# Exits if there are uncommitted changes in the git repo.
function ensure_no_uncommitted_changes {
if ! git diff --quiet ; then
echo "Uncommitted changes found. Aborting." >&2
exit 1
fi
}
# Returns the current git branch, if any; the HEAD commit's SHA1 otherwise.
function current_git_ref {
branch=$(git rev-parse --abbrev-ref HEAD)
if [ $branch == "HEAD" ]; then
echo $(git rev-parse HEAD)
else
echo $branch
fi
}
# Returns the version of com.google.guava:guava at the current revision, pulled from Maven.
function guava_version {
mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate \
-Dexpression=project.version \
-pl guava 2> /dev/null \
| grep -Ev '(^\[|Download.+:)'
}
# Checks that a given tag exists in the repo.
function check_tag_exists {
tag=$1
if ! git show-ref -q --verify refs/tags/$tag; then
echo "Tag $tag does not exist" >&2
exit 1
fi
}
# Takes an input arg representing a version (as parsed in parse_version) and
# returns the git ref (tag or branch) that should be checked out to get that version.
function git_ref {
release=$1
if [[ $release == "snapshot" ]]; then
echo "master"
else
tag="v$release"
check_tag_exists $tag
echo $tag
fi
}
# Checks out the branch/ref with the given identifier.
# If the ref is the master branch, pulls to update it.
function git_checkout_ref {
ref=$1
echo -n "Checking out '$ref'..."
git checkout -q $ref
echo " Done."
# If we're on master, pull to get the latest
if [[ $ref == "master" ]]; then
echo -n "Pulling to get latest changes..."
git pull -q --ff-only
echo " Done."
fi
}
# Expands a release number into a numeric release number than can be directly
# compared to another expanded release number. Release numbers without a patch
# release version get expanded to have a ".0" patch release number. Non RC
# releases all get a final ".1" while "-rcN" releases get ".0N", to ensure that
# RCs are always considered lower than final versions.
#
# If a release ends in "-android" or "-jre", the result will also end in that.
#
# Examples:
#
# - 22.0 -> 22.0.0.1
# - 22.0.1 -> 22.0.1.1
# - 22.1-rc1 -> 22.1.0.01
# - 22.1-rc2 -> 22.1.0.02
function expand_release {
local release="$1"
if [[ "$release" == "snapshot" ]]; then
echo "$release"
return
fi
# strip -SNAPSHOT suffix if present
snapshot=""
if [[ "$release" =~ ^(.+)-SNAPSHOT$ ]]; then
release="${BASH_REMATCH[1]}"
snapshot="-SNAPSHOT"
fi
# strip -final suffix if present
final=""
if [[ "$release" =~ ^(.+)-final$ ]]; then
release="${BASH_REMATCH[1]}"
final="-final"
fi
# strip -flavor suffix if present
local flavor=""
if [[ "$release" =~ ^(.+)-(jre|android)$ ]]; then
release="${BASH_REMATCH[1]}"
flavor="-${BASH_REMATCH[2]}"
fi
rc="100"
if [[ "$release" =~ ^(.+)-rc([0-9]+)$ ]]; then
# strip and record rc number if present
release="${BASH_REMATCH[1]}"
rc="0${BASH_REMATCH[2]}"
fi
if [[ "$release" =~ ^[0-9]+\.[0-9]+$ ]]; then
release="$release.0"
fi
release="$release.$rc$final$flavor$snapshot"
echo "$release"
}
# Converts an expanded release number back to its default form (no patch
# version if the patch version is 0 and the major version is less than 32,
# RCs converted back to using -rcN).
function unexpand_release {
local release="$1"
if [[ "$release" == "snapshot" ]]; then
echo "$release"
return
fi
# strip -SNAPSHOT suffix if present
snapshot=""
if [[ "$release" =~ ^(.+)-SNAPSHOT$ ]]; then
release="${BASH_REMATCH[1]}"
snapshot="-SNAPSHOT"
fi
# strip -final suffix if present
final=""
if [[ "$release" =~ ^(.+)-final$ ]]; then
release="${BASH_REMATCH[1]}"
final="-final"
fi
# strip -android suffix if present
local flavor=""
if [[ "$release" =~ ^(.+)-(jre|android)$ ]]; then
release="${BASH_REMATCH[1]}"
flavor="-${BASH_REMATCH[2]}"
fi
local release_array
IFS='.' read -ra release_array <<< "$release"
local major="${release_array[0]}"
local minor="${release_array[1]}"
local patch="${release_array[2]}"
local rc="${release_array[3]}"
local result="$major.$minor"
if (( major >= 32 || patch != 0 )); then
result="$result.$patch"
fi
if [[ "$rc" != "100" ]]; then
rc="${rc:1}"
result="$result-rc$rc"
fi
result="$result$final$flavor$snapshot"
echo "$result"
}
function expand_releases {
while read release; do
expand_release "$release"
done
}
function unexpand_releases {
while read release; do
unexpand_release "$release"
done
}
platform="$(uname)"
if [[ "$platform" == "Linux" ]]; then
# GNU utils
extended="-r"
versionsort="--version-sort"
else
# BSD utils
extended="-E"
versionsort="-g"
fi
# Sorts all numeric releases given on stdin by version, from greatest version to
# least. This works as you'd expect, for example:
#
# 18.0.2 > 18.0.1 > 18.0 > 18.0-rc2 > 18.0-rc1 > 17.1 > 17.0.1 > 17.0
function sort_releases {
expand_releases | sort -r $versionsort | unexpand_releases
}
# Gets the major version part of a version number.
function major_version {
majorversion=$(echo "$1" | cut -d . -f 1)
if [[ ! $majorversion =~ ^[0-9]+$ ]]; then
echo "Invalid version number: $1" >&2
exit 1
fi
echo $majorversion
}
# Ceiling to use when looking for the previous version for a HEAD snapshot.
# This should be safe to ensure it's higher than any Guava version for a
# while. =)
readonly SNAPSHOT_CEILING="999.999.999"
# Prints the highest non-rc release from the sorted list of releases produced
# by sort_releases. If a release argument is provided, print the highest non-rc
# release that has a major or minor version that is lower than the given
# release. For example, given "16.0.1", return "15.0". Given "16.1", return
# "16.0.1".
#
# If the release argument is a "-android" release, only look at other
# "-android" releases.
function latest_release {
if [[ $# == 1 ]]; then
local ceiling="$1"
if [[ "$ceiling" =~ ^HEAD-(jre|android)-SNAPSHOT$ ]]; then
ceiling="${SNAPSHOT_CEILING}-${BASH_REMATCH[1]}"
fi
else
local ceiling="${SNAPSHOT_CEILING}"
fi
local non_rc_releases="$(ls releases | grep -v "rc" | grep -v "snapshot")"
if [[ "$ceiling" =~ ^.+-android(-SNAPSHOT)?$ ]]; then
# If the release we're looking at is an android release, only look at other
# android releases.
non_rc_releases="$(echo "$non_rc_releases" | grep -e "-android")"
else
# If it's not an android release, don't include android releases.
non_rc_releases="$(echo "$non_rc_releases" | grep -v -e "-android")"
fi
if [[ -z "$non_rc_releases" ]]; then
# There are no non-RC releases to compare against (or no android releases).
# Print nothing because there is no previous release.
return
fi
# Add the release we're looking for to the list, uniqueify, then sort
# according to our release sort order.
local releases="$((echo "$non_rc_releases" && echo "$ceiling") | sort -u | sort_releases)"
ceiling_expanded="$(expand_release "$ceiling")"
ceiling_major="$(cut -d. -f1 <<< "$ceiling_expanded")"
ceiling_minor="$(cut -d. -f2 <<< "$ceiling_expanded")"
local release
local seen_ceiling=1
for release in $releases; do
if [[ "$seen_ceiling" ]]; then
release_expanded="$(expand_release "$release")"
release_major="$(cut -d. -f1 <<< "$release_expanded")"
release_minor="$(cut -d. -f2 <<< "$release_expanded")"
if (( release_major < ceiling_major || release_minor < ceiling_minor )); then
echo "$release"
return
fi
elif [[ "$release" == "$ceiling" ]]; then
seen_ceiling=0
fi
done
}