-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathcheck_version.sh
More file actions
executable file
·89 lines (80 loc) · 2.11 KB
/
check_version.sh
File metadata and controls
executable file
·89 lines (80 loc) · 2.11 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
#!/bin/sh
# If we have a Git repository then check whether our hard-coded version
# in CMakeLists.txt matches our tags.
usage () {
echo "Check or update the CMakeLists.txt version against Git tags."
echo "Runs a version check when no flags are specified."
echo
echo "usage: $(basename "$0") [--update <vX.Y>]"
echo "options:"
echo " -u|--update Update CMakeLists.txt"
exit 0
}
# Update CMakeLists.txt by passing the "--update" flag
main () {
case "$1" in
-u|--update)
update "$2"
;;
-h|--help)
usage
;;
*)
check_version
;;
esac
}
git_version () {
git describe --first-parent --always HEAD
}
strip_version () {
# Transform "v?A.B.C-abc" into "A.B"
sed -e 's/^v//' | cut -d . -f 1,2
}
format_version () {
# Transform "A.B" into "vA.B.X"
read stdin_version
stdin_version=$(echo "$stdin_version" | strip_version)
printf 'v%s.X' "$stdin_version"
}
check_version () {
if git rev-parse HEAD >/dev/null
then
version=$(git_version | format_version)
if ! grep "$version" CMakeLists.txt >/dev/null
then
if test "$quiet" != 1
then
simple_version=$(git_version | strip_version)
echo 1>&2
echo 1>&2 error: CMakeLists.txt does not contain "$version"
echo 1>&2 fixit: "$0" --update "$simple_version"
echo 1>&2
fi
return 1
else
echo "CMakeLists.txt is already up to date."
return 0
fi
fi
# This test is skipped if git is unavailable
echo 1>&2 "warning: $(basename "$0") was skipped because git is unavailable."
return 0
}
update () {
quiet=1
if ! check_version
then
if test -n "$1"
then
version=$(echo "$1" | format_version)
else
version=$(git_version | format_version)
fi
perl -p -i -e "s/v\\d+\\.\\d+\\.X/$version/" CMakeLists.txt &&
echo "CMakeLists.txt updated." &&
git diff -- CMakeLists.txt
fi
exit 0
}
main "$@"