-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-version.sh
More file actions
executable file
·151 lines (128 loc) · 2.92 KB
/
Copy pathgenerate-version.sh
File metadata and controls
executable file
·151 lines (128 loc) · 2.92 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
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
#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
set -e
_SH_SCRIPTS_DIR="$(cd -- "$(dirname -- "$0")" >/dev/null && pwd -P)"
SH_SCRIPTS_DIR="${SH_SCRIPTS_DIR:-"$_SH_SCRIPTS_DIR"}"
# shellcheck source=/dev/null
. "${SH_SCRIPTS_DIR}/_sh-common.sh"
if [ -n "$MESON_SUBDIR" ]; then
_MESON_SOURCE_DIR="${MESON_SOURCE_ROOT}/${MESON_SUBDIR}"
else
_MESON_SOURCE_DIR="$MESON_SOURCE_ROOT"
fi
SOURCE_DIR_DEFAULT="${_MESON_SOURCE_DIR:-.}"
VERSION_DEFAULT='v0.0.1'
parse_args() {
short_opts='s:,w,o:'
long_opts='source-dir:,write,output-dir:,no-dirty'
if ! getopt=$(getopt -o "$short_opts" --long "$long_opts" \
-n "$SH_SCRIPT_NAME" -- "$@"); then
sh_error 'Failed to parse args'
fi
eval set -- "$getopt"
write=0
output_dir=
no_dirty=0
while true; do
case "$1" in
'-s' | '--source-dir')
# Source directory
[ -z "$2" ] &&
sh_error "'$1' requires a non-empty string"
source_dir="$2"
shift 2
;;
'-w' | '--write')
# Write to file
write=1
shift 1
;;
'-o' | '--output-dir')
# Write to file in output directory
[ ! -d "$(dirname "$2")" ] &&
sh_error "'$1' directory does not exist"
write=1
output_dir="$2"
shift 2
;;
'--no-dirty')
# Do not append '-dirty' metadata
no_dirty=1
shift 1
;;
--)
shift
break
;;
*)
sh_error 'Internal error parsing args'
;;
esac
done
source_dir="${source_dir:-"$SOURCE_DIR_DEFAULT"}"
unset short_opts
unset long_opts
}
generate_version() {
dirty=
if git -C "$source_dir" status >/dev/null 2>&1 &&
[ -e "${source_dir}/.git" ]; then
if git -C "$source_dir" describe --tags --match "v[0-9]*" \
--exact-match HEAD >/dev/null 2>&1; then
version=$(git -C "$source_dir" tag --points-at HEAD |
grep -E '^v[0-9]' |
awk '{
tag = $0
if (match($0, /\+[^,]*/)) {
base = substr($0, 1, RSTART-1)
build = substr($0, RSTART+1, RLENGTH-1)
} else {
base = $0
build = ""
}
print base "," build "," tag
}' |
sort -V -t',' -k1,1 -k2,2 |
tail -1 |
cut -d',' -f3)
else
version=$(git -C "$source_dir" describe \
--abbrev=8 --tags --match "v[0-9]*" 2>/dev/null |
sed 's/+[^-]*//' ||
true)
fi
if [ -z "$version" ]; then
sha=$(git -C "$source_dir" describe --abbrev=8 --always)
commits=$(git -C "$source_dir" log --oneline | wc -l)
version="v0.0.1-${commits}-${sha}"
unset sha
unset commits
fi
if [ "$no_dirty" -eq 0 ]; then
dirty=$(git -C "$source_dir" diff --quiet ||
echo '-dirty')
fi
fi
version=$(echo "${version:-"$VERSION_DEFAULT"}${dirty}" |
sed -e 's/-g/-/' | cut -c 2-)
unset dirty
}
main() {
parse_args "$@"
version_file="${source_dir}/.version"
if [ -f "$version_file" ]; then
cat "$version_file"
exit 0
fi
generate_version
if [ "$write" -eq 0 ]; then
echo "$version"
exit 0
fi
if [ -z "$output_dir" ]; then
echo "$version" >"$version_file"
else
echo "$version" >"${output_dir}/.version"
fi
}
main "$@"