forked from telefonicaid/sigfox-iotagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_version_string.sh
executable file
·147 lines (140 loc) · 4.9 KB
/
get_version_string.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
#!/bin/bash
#
# Copyright 2016 Telefonica Investigacion y Desarrollo, S.A.U
#
# This file is part of the iotagent-sigfox
#
# the iotagent-sigfox is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# the iotagent-sigfox 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 Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with the iotagent-sigfox. If not, see http://www.gnu.org/licenses/.
#
# For those usages not covered by this license please contact with
# iot_support at tid dot es
#
#
# Bash lib to know the RPM version and revision from a GitHub repository
# Call method get_rpm_version_string to obtain them for rpmbuild
#
shopt -s extglob
get_branch()
{
git rev-parse --abbrev-ref HEAD
}
## Specific functions according the TID workflow
get_branch_type()
{
local branch="$(get_branch)"
case $branch in
feature/*|bug/*|hotfix/*) echo "unstable";;
release/*) echo "release";;
+(+([[:digit:]])\.)+([[:digit:]]) ) echo "release" ;;
develop) echo "develop";;
master) echo "stable";;
*) echo "other";;
esac
}
get_version_string()
{
if [[ $(is_pdi_compliant) -eq 0 ]]; then # Not TID compliant, return a dummy version
echo "HEAD-0-g$(git log --pretty=format:'%h' -1)"
return
fi
local branch describe_all describe_tags version ancestor
describe_all="$(git describe --all --long)"
describe_tags="$(git describe --tags --long 2>/dev/null)"
[[ "${describe_tags}" == "${describe_all#*/}" ]] && version="${describe_tags%/*}" || version="${version#*/}"
case $(get_branch_type) in
stable)
# If we are on stable branch get last tag as the version, but transform to x.x.x-x-SHA1
version="${describe_tags%-*-*}"
echo "${version%.*}-${version#*.*.*.}-g$(git log --pretty=format:'%h' -1)"
;;
develop)
## if we are in develop use the total count of commits
version=$(git describe --tags --long --match *-KO)
echo "${version%/*}-${version#*KO-}"
;;
release)
version=$(get_branch)
version=$(git describe --tags --long --match ${version#release/*}-KO)
echo "${version%-KO*}-${version#*KO-}"
;;
other)
## We are in detached mode, use the last KO tag
version=$(git describe --tags --long --match *-KO)
echo "${version%/*}-${version#*KO-}"
;;
*)
# RMs don't stablish any standard here, we use branch name as version
version=$(get_branch)
# Using always develop as parent branch does not describe correctly the number of revision
# for branches not starting there, but works as an incremental rev
ancestor="$(git merge-base $version develop)"
version=${version#*/}
local res="$(git log --oneline ${ancestor}.. --pretty='format:%h')"
## wc alone does not get the last line when there's no new line
[[ -z $res ]] && rel=0 || rel=$(echo "$res" | wc -l | tr -d ' ')
echo "${version}-${rel}-g$(git log --pretty=format:'%h' -1)"
esac
}
## Parse the version string and sanitize it
## to use it you can do, for example:
## ># read ver rel < <(get_rpm_version_string)
get_rpm_version_string() {
local version_string ver rel
version_string="$(get_version_string)"
ver="${version_string%-*-*}"
rel="${version_string:$((${#ver}+1))}"
echo "${ver//[[:space:]-\/#]}" "${rel//[-]/.}"
}
## DEPRECATED: use get_rpm_version_string instead
get_pdi_version_string()
{
get_rpm_version_string
}
is_pdi_compliant()
{
case $(get_branch_type) in
"other")
# Maybe we are on detached mode but also are compliant
# See if there's a tag (annotated or not) describing a Kick Off
git describe --tags --match *-KO >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
echo 1
else
echo 0
fi
;;
"release")
ver=$(get_branch)
# remove the leading release/ if necessary
ver=${ver#release/*}
# see if there's a tag (annotated or not) describing its Kick Off
git describe --tags --match ${ver}-KO >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
echo 1
else
echo 0
fi
;;
"develop")
# see if there's a tag (annotated or not) describing a Kick Off
git describe --tags --match *-KO >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
echo 1
else
echo 0
fi
;;
*) echo 1 ;;
esac
}