-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-version
executable file
·194 lines (166 loc) · 4.84 KB
/
update-version
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# ------------
# Gets the current local tag
#
# @author Björn Hempel <[email protected]>
# @version 1.0 (2017-05-21)
# ------------
getCurrentTagFromGivenPath()
{
local gitDir="$1"
if [ "$gitDir" != "" ] && [ -d "$gitDir" ]; then
cd "$gitDir"
fi
local tagName=$(git describe 2>/dev/null | xargs | sed 's~-[0-9]\+-[A-Za-z0-9]\+$~~')
if [ "$tagName" == "" ]; then
echo "v0.0.0"
fi
echo "$tagName"
}
# ------------
# Gets the next revision tag.
#
# @author Björn Hempel <[email protected]>
# @version 1.0 (2017-06-06)
# ------------
getNextTagFromGivenPath()
{
local position=${2:-2}
local currentTag=$(getCurrentTagFromGivenPath "$1")
local searchReg="^([v]?)([0-9]+)\.([0-9]+)\.([0-9]+)"
if ! [[ "$currentTag" =~ $searchReg ]]; then
>&2 echo "Unable to parse the current version \"$currentTag\"."
return 1
fi
currentTag="${BASH_REMATCH[1]}${BASH_REMATCH[2]}.${BASH_REMATCH[3]}.${BASH_REMATCH[4]}"
local major=${BASH_REMATCH[2]}
local minor=${BASH_REMATCH[3]}
local revision=${BASH_REMATCH[4]}
local version="$major.$minor.$revision"
local prefix=$(echo "$currentTag" | sed "s/$version$//")
# increase the appropriate version number
case $position in
0)
major=$((major + 1))
minor=0
revision=0
;;
1)
minor=$((minor + 1))
revision=0
;;
*)
revision=$((revision + 1))
;;
esac
echo "$prefix$major.$minor.$revision"
}
# ------------
# Update and push given tag to given git repository.
#
# @author Björn Hempel <[email protected]>
# @version 1.0 (2017-06-10)
# ------------
getSetAndPushTag()
{
local nextTag="$1"
local gitDir="$2"
if [ "$gitDir" != "" ] && [ -d "$gitDir" ]; then
cd "$gitDir"
fi
git tag -a $nextTag -m "version $nextTag"
git push origin $nextTag
}
# ------------
# updateVersion method
#
# @author Björn Hempel <[email protected]>
# @version 1.0 (2017-06-06)
# ------------
updateVersion()
{
local pathRoot="$1"
# check the given path
if [ ! -d "$pathRoot/.git" ]; then
echo "The path \"$pathRoot\" is not a repository path. Abort." && exit
fi
# extract the name of the given path
name=$(basename `git rev-parse --show-toplevel`)
local currentTag=$(getCurrentTagFromGivenPath "$pathRoot")
local nextMajorTag=$(getNextTagFromGivenPath "$pathRoot" 0)
local nextMinorTag=$(getNextTagFromGivenPath "$pathRoot" 1)
local nextRevisionTag=$(getNextTagFromGivenPath "$pathRoot" 2)
echo "name: $name"
echo "directory: $pathRoot"
echo "current version: $currentTag"
echo
echo "Which new version number you want to use? (1) - $nextMajorTag, (2) - $nextMinorTag or (3) $nextRevisionTag"
echo
while true; do
read -n 1 -p "Choose (1), (2) or (3): " choice
case $choice in
# 1
1)
local nextTag="$nextMajorTag"
break
;;
# 2
2)
local nextTag="$nextMinorTag"
break
;;
# 3
3)
local nextTag="$nextRevisionTag"
break
;;
# escape
$'\e')
echo
echo
echo "The command was cancelled by user."
exit
;;
# other character
*)
echo
;;
esac
done
echo
echo
while true; do
read -n 1 -p "This will set the current version number $currentTag to $nextTag. Do you want to continue? Type (y)es or (no): " yn
case $yn in
# yes
[Yy]*)
break
;;
# no
[Nn]*)
echo
echo "The command was cancelled by user."
exit
;;
# escape
$'\e')
echo
echo "The command was cancelled by user."
exit
;;
# other character
*)
echo
;;
esac
done
# set and push new tag
echo
$(getSetAndPushTag "$nextTag" "$pathRoot")
echo "Done!"
}
# some configs and informations
scriptname="$0"
pathRoot="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# update version
updateVersion "$pathRoot"