/
find-previous-major-release-tag.sh
executable file
·52 lines (42 loc) · 1.27 KB
/
find-previous-major-release-tag.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
#!/usr/bin/env bash
set -ueo pipefail
verlte() {
# sort and get the first result
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
}
verlt() {
[ "$1" = "$2" ] && return 1 || verlte $1 $2
}
# This script finds the previous major tag from the current HEAD position.
closest_head_version="$(git describe --tags)"
sorted_tags="$(git tag --list --sort -version:refname)"
# this is ugly, but we use this approach to detect whether we are
# on a main branch, if that's the case, then we'll output the
# latest release version.
if [[ $closest_head_version =~ ^v1\.91\.0\-alpha ]]; then
for tag in $sorted_tags
do
if [[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo $tag
exit 0
fi
done
echo "did not find an appropriate release tag from main branch"
exit 1
fi
closest_zero_version="$(echo $closest_head_version | cut -d '.' -f 1-2).0"
IFS=$'\n'
for tag in $sorted_tags
do
if [[ ! "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
# it's not a proper version so ignore
continue
fi
# the first less than our closest head should be our latest version
if verlt $tag $closest_zero_version; then
echo $tag
exit 0
fi
done
echo "did not find an appropriate release tag"
exit 1