-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
version.sh
executable file
·166 lines (147 loc) · 4.65 KB
/
version.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
#!/bin/sh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
# Copyright 2023 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
#
# Files
version_file=VERSION
commit_file=VERSION_COMMIT
commit_depth_file=VERSION_COMMIT_DEPTH
release_file=RELEASE
# Defaults
version_major=4
version_minor=0
version_incrm=
version_prerelease=
commit=
commit_depth=
is_release=0
in_repo=
#
# Don't allow git to search in directories above this one, as we may
# be operating in an extracted version of the source and not a repo.
#
git="git --git-dir=.git"
#
# Check if we're operating in a repo
#
in_repo=$(${git} rev-parse > /dev/null 2>&1 && echo true || echo false)
usage()
{
echo "$(basename "$0") [-h|-d] [component [component...]] - Create a FreeRADIUS version string from one or more components"
echo
echo "component may be one of:"
echo "major Major version component from ${version_file}."
echo "minor Minor version component from ${version_file}."
echo "incrm Incremental version component from ${version_file}."
echo "prerelease Pre-release version component from ${version_file}."
echo "commit Short (8 hexit) commit hash."
echo "commit_depth How many commit since the last tag."
echo "is_release true if the current commit has been tagged as a release, else false."
echo "* Any other string. Will be echoed directly to stdout."
echo
echo "arguments:"
echo "-h Print helptext"
echo "-c Remove ${commit_file}, ${commit_depth_file} and ${release_file} files."
echo "-d Write commit, commit_depth, is_release to ${commit_file}, ${commit_depth_file}, ${release_file} respectively."
}
version_component()
{
for c in "$@"; do
case "$c" in
major)
out=$(cut -f1 -d~ 2>/dev/null < ${version_file} | cut -f1 -d.)
if [ -z "${out}" ]; then out="${version_major}"; fi
;;
minor)
out=$(cut -f1 -d~ 2>/dev/null < ${version_file} | cut -f2 -d.)
if [ -z "${out}" ]; then out="${version_minor}"; fi
;;
incrm)
out=$(cut -f1 -d~ 2>/dev/null < ${version_file} | cut -f3 -d.)
if [ -z "${out}" ]; then out="${version_incrm}"; fi
;;
prerelease)
out=$(cut -s -f2 -d~ 2>/dev/null < ${version_file})
if [ -z "${out}" ]; then out="$version_prerelease"; fi
;;
commit)
out=$(\
cat ${commit_file} 2> /dev/null || \
(${in_repo} && ${git} rev-parse --short=8 HEAD) || \
echo "${commit}"\
)
;;
commit_depth)
out=$(\
cat ${commit_depth_file} 2> /dev/null || \
(${in_repo} && ${git} describe --tags --match 'branch_*' --match 'release_*' | cut -s -d '-' -f 2) || \
echo "${commit_depth}" \
)
;;
# - If $RELEASE is set, that takes precedence
# - Otherwise if a RELEASE file is present, then release = 1.
# - If we're in a git repo
# - If there's a release_* tag matching the current commit,
# and there are no uncommented changes then release = 1, and if not release = 0
# - else release is 1
is_release)
out=$(\
([ -n "${RELEASE}" ] && echo "${RELEASE}" ) || \
([ -e "${release_file}" ] && echo 1) || \
(${in_repo} && ( (${git} describe --match='release_*' --exact-match > /dev/null 2>&1 && ${git} status > /dev/null && echo 1) || echo 0) ) || \
echo "${is_release}" \
)
;;
*)
out="$c"
;;
esac
printf '%s' "${out}" | tr -d '\n'
done
}
#
# Parse any arguments
#
while getopts "hcd" arg; do
case $arg in
h)
usage
;;
c)
[ ! -e "${commit_file}" ] || rm "${commit_file}"
[ ! -e "${commit_depth_file}" ] || rm "${commit_depth_file}"
[ ! -e "${release_file}" ] || rm "${release_file}"
exit 0
;;
d)
# Intermediary variables to quite shellcheck SC2005 as it doesn't
# seem to be able to differentiate between functions are other
# commands.
commit="$(version_component commit)"
echo "$commit" > "${commit_file}"
commit_depth="$(version_component commit_depth)"
echo "$commit_depth" > "${commit_depth_file}"
[ "$(version_component is_release)" -eq 1 ] && touch "${release_file}"
exit 0
;;
*)
exit 64
esac
done
shift $((OPTIND-1))
version_component "$@"
echo