-
Notifications
You must be signed in to change notification settings - Fork 18
/
versiontag.sh
executable file
·248 lines (201 loc) · 5.54 KB
/
versiontag.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env bash
set -e
# Initializes flags and other variables
action='' # the sub-command to execute
dry=false # Flag to run with executing commands
force=false # Flag to run without asking for confirmation
versionFile=false # Flag to generate the .semver file
last='' # Will contain the last version tag found
## Runs a command with arguments, taking care of the --dry flag
function execute {
cmd=("$@")
if [[ true == "$dry" ]];then
printf '%s \n' "DRY -no changes- ${cmd[*]}"
return
fi
printf '%s\n' "Executing ${cmd[*]} ..."
"${cmd[@]}" 2>&1
}
# Asks for confirmation returns yes|no
function confirm() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
# Generates the .semver file
function updateSemverFile() {
if [[ true == "$dry" || false == "$versionFile" ]]; then
return 0
fi
:> .semver
exec 3<> .semver
echo "---" >&3
echo ":major: $major" >&3
echo ":minor: $minor" >&3
echo ":patch: $patch" >&3
echo >&3
echo ":notes: $msg" >&3
echo >&3
exec 3>&-
}
function pushSemver() {
execute git add .semver
execute git commit -m "Update .semver file"
execute git push
}
# Removes last tag both local and remote
function removeTag() {
execute git tag -d "$last"
execute git push origin :"$last"
updateSemverFile
}
# Shows full help for the script
function showHelp() {
cat <<'TXT'
versiontag
==========
Automates version tag creation.
Commands: patch|minor|major 'Tag message'
remove
current
Options: -d|--dry: don't change things
-f|--force: don't ask for confirmation
-h|--help: show this help
-m|--message: annotate tag with a message
-s|--semver: generate .semver file
Examples:
---------
Show current version: versiontag current
Patch version: versiontag patch 'Fix broken view'
Minor version: versiontag minor --message'Add Customer filter by email'
Major version: versiontag major -m 'Blog module'
Remove last version: versiontag remove
Run without changes: versiontag --dry remove
TXT
}
# Shows the result of the command
function showSuccess() {
echo
echo "Tag v$major.$minor.$patch was created in local repository."
echo
echo "Push it:"
echo
echo " git push origin v$major.$minor.$patch"
echo
echo "Delete tag:"
echo
echo " git tag -d v$major.$minor.$patch"
echo
}
# Show current version
function showCurrentVersion() {
if [[ true == "$versionFile" ]]; then
updateSemverFile
printf '.semver file generated for: %s\n' "$last"
return
fi
printf 'Current version: %s\n' "$last"
}
# Get the last tag and breaks into parts
function getLastTag() {
# lasttag=`git tag -l --sort=-committerdate | head -1 2> /dev/null` || true
# ogi: changed to suite my needs (on OSX reverse comitterdate DOESN'T WORK, and we have to ignore the latest tag too)
lasttag=`git tag -l 'v*' | xargs -I@ git log --format=format:"%ai @%n" -1 @|sort -r| awk '{print $4}'| head -1 2> /dev/null` || true
if [[ -z "$lasttag" ]]; then
lasttag='v0.0.0'
fi
lasttag=${lasttag:1}
parts=(${lasttag//./ })
major=${parts[0]}
minor=${parts[1]}
patch=${parts[2]}
last="v$major.$minor.$patch"
}
# Get last tag from repo and process the desired command
getLastTag
# Process command options
for ((i = 1; i <= $#; i++ )); do
case ${!i} in
"-d"|"--dry")
dry=true
;;
"-f"|"--force")
force=true
;;
"-h"|"--help")
showHelp
exit 0
;;
"-m"|"--message")
let i++
msg="${!i}"
;;
"-s"|"--semver")
versionFile=true
;;
"patch"|"minor"|"major")
action="${!i}"
;;
"current")
action='current'
;;
"remove")
if [[ "yes" == $(confirm "Do you want to remove $last?") ]]; then
removeTag
fi
exit 0
;;
"help")
showHelp
exit 0
;;
*)
echo "${!i} option is not valid"
exit 1
;;
esac
done
case "$action" in
'current')
showCurrentVersion
exit 0;;
'patch')
patch=$((patch + 1))
;;
'minor')
minor=$((minor + 1))
patch=0
;;
'major')
major=$((major + 1))
minor=0
patch=0
;;
*)
echo "No mode selected"
exit 0
esac
# Shows information
printf "Current version : %s\n" "$last"
printf "New tag : %s.%s.%s\n\n" "v$major" "$minor" "$patch"
if [[ false == "$force" && "no" == $(confirm "Do you agree?") ]]; then
printf '\n%s' "No tag was created"
exit 0;
fi
if [[ -z $msg ]]; then
# execute git tag "v$major.$minor.$patch"
# ogi: changed to suite my needs: ALWAYS make an annotated tag!!
execute git tag -a "v$major.$minor.$patch" -m "releasing v$major.$minor.$patch"
else
printf 'Message : %s\n\n' "$msg"
execute git tag -a "v$major.$minor.$patch" -m "$msg ($action version)"
fi
updateSemverFile
if [[ false == "$force" && "no" == $(confirm "Do you want to push this tag right now?") ]]; then
printf '\n%s' "No tag was pushed"
exit 0;
fi
execute git push origin "v$major.$minor.$patch"
exit 0