-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetectNewVersion.sh
More file actions
executable file
·245 lines (208 loc) · 8.12 KB
/
detectNewVersion.sh
File metadata and controls
executable file
·245 lines (208 loc) · 8.12 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
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
#!/usr/bin/env bash
# --------------------------------------------------------------------------------------------------
# detectNewVersion.sh
#
# Description
# Detects new version.
#
# --------------------------------------------------------------------------------------------------
# Source Detection
# --------------------------------------------------------------------------------------------------
sourced=0
if [ -n "$ZSH_EVAL_CONTEXT" ]; then
case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
elif [ -n "$BASH_VERSION" ]; then
(return 0 2>/dev/null) && sourced=1
else # All other shells: examine $0 for known shell binary filenames
# Detects `sh` and `dash`; add additional shell filenames as needed.
case ${0##*/} in sh|dash) sourced=1;; esac
fi
# --------------------------------------------------------------------------------------------------
# Help
# --------------------------------------------------------------------------------------------------
help="\
NAME
${0##*/}
SYNOPSIS
${0##*/} [-hv]
DESCRIPTION
Detects the new version for the repository by analyzing the gitflow branch history since the
previous version tag.
Prints only the new version to stdout by default.
The following options are available:
-h Print this menu.
-e Export detected new version to defined variable, if valid.
-f Forces a re-evaluation of the entire git history.
EXAMPLES
The following detects the new version for the repo.
./detectNewVersion.sh
The following detects the new version for the repo, and exports to the specified variable.
. ./detectNewVersion.sh -e fooVar
"
printHelp() {
echo -e "$help" >&2
}
# --------------------------------------------------------------------------------------------------
# Sanity (1/2)
# --------------------------------------------------------------------------------------------------
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo -e "\e[01;31mFATAL\e[00m: This is not a git repository!\n"
fi
# --------------------------------------------------------------------------------------------------
# Arguments
# --------------------------------------------------------------------------------------------------
OPTIND=1
while getopts "he:vf" opt; do
case $opt in
h)
printHelp
if [[ "$sourced" == 0 ]]; then
exit 0
else
return 0
fi
;;
e)
arg_e='set'
arg_e_val="$OPTARG"
;;
f)
arg_f='set'
;;
*)
echo -e "\e[01;31mERROR\e[00m: Invalid argument!"
printHelp
if [[ "$sourced" == 0 ]]; then
exit 0
else
return 0
fi
;;
esac
done
shift $((OPTIND-1))
# --------------------------------------------------------------------------------------------------
# Variables
# --------------------------------------------------------------------------------------------------
IFS_BAK=$IFS
tsCmd='date --utc +%FT%T.%3NZ'
relative_path="$(dirname "${BASH_SOURCE[0]}")"
dir="$(realpath "${relative_path}")"
lastVersion=$(/usr/bin/env bash -c "${dir}/detectPreviousVersion.sh")
lastVersionMajor=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p major $lastVersion")
lastVersionMinor=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p minor $lastVersion")
lastVersionPatch=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p patch $lastVersion")
lastVersionCommitHash=$(/usr/bin/env bash -c "${dir}/detectPreviousVersion.sh -c")
lastCommitHash=$(git rev-parse HEAD)
firstCommitHash=$(git rev-list --max-parents=0 HEAD)
ci_name=$("${dir}/detect-ci.sh")
origin=$(git config --get remote.origin.url)
#origin=${ci_name:-origin}
# Executes in ANY CI so long as repo origin is one of the following.
# Uncomment origin override to restrict this.
[[ "$origin" =~ "git@github.com"* || "$ci_name" == "github" ]] && origin_host=github
[[ "$origin" =~ "git@gitlab.com"* || "$ci_name" == "gitlab" ]] && origin_host=gitlab
[[ "$origin" =~ "git@bitbucket.com"* || "$ci_name" == "bitbucket" ]] && origin_host=bitbucket
case "$origin_host" in
github)
merge_string="Merge pull request #"
column=7
field=2
;;
gitlab)
merge_string="Merge branch"
column=4
field=1
;;
bitbucket)
merge_string="Merged in"
column=4
field=1
;;
*)
echo -e "\e[01;31mERROR\e[0m: Unsupported origin host."
exit 1
;;
esac
# --------------------------------------------------------------------------------------------------
# Sanity (2/2)
# --------------------------------------------------------------------------------------------------
if [[ -n $arg_e ]]; then
if [[ "$sourced" == 0 ]]; then
echo -e "[$(${tsCmd})] \e[01;31mERROR\e[00m: You must source this script when specifying an environment variable! Eg: '. ./${0##*/} -e foo_ver'\n"
exit 1
fi
fi
git log --pretty=oneline "$lastVersionCommitHash".."$lastCommitHash" | grep '+semver' | grep -q 'major\|breaking' && incrementMajor='true'
if [[ $incrementMajor != 'true' ]]; then
IFS=$'\r\n'
if [[ -n $arg_f ]]; then
for i in $(git log --pretty=oneline "${firstCommitHash}".."${lastCommitHash}" | awk -v s="$merge_string" -v c="$column" '$0 ~ s {print $c}' | awk -v f="$field" -F'/' '{print $f}' | tr -d "'" | grep -i '^enhancement$\|^feature$\|^fix$\|^hotfix$\|^bugfix$\|^ops$' | awk -F '\r' '{print $1}' | sort | uniq -c | sort -nr) ; do
varname=$(echo "$i" | awk '{print $2}')
varname=${varname,,}
value=$(echo "$i" | awk '{print $1}')
value=${value,,}
declare count_"$varname"="$value"
done
else
for i in $(git log --pretty=oneline "${lastVersionCommitHash}".."${lastCommitHash}" | awk -v s="$merge_string" -v c="$column" '$0 ~ s {print $c}' | awk -v f="$field" -F'/' '{print $f}' | tr -d "'" | grep -i '^enhancement$\|^feature$\|^fix$\|^hotfix$\|^bugfix$\|^ops$' | awk -F '\r' '{print $1}' | sort | uniq -c | sort -nr) ; do
varname=$(echo "$i" | awk '{print $2}')
varname=${varname,,}
value=$(echo "$i" | awk '{print $1}')
value=${value,,}
declare count_"$varname"="$value"
done
fi
IFS=$IFS_BAK
fi
# echo "enh: $count_enhancement"
# echo "feat: $count_feature"
# echo "fix: $count_fix"
# echo "bug: $count_bugfix"
# echo "hot: $count_hotfix"
# echo "ops: $count_ops"
# if [[ -n $arg_f ]]; then
# true
# else
# if [[ -z $incrementMajor && -z $count_feature && -z $count_enhancement && -z $count_fix && -z $count_bugfix && -z $count_hotfix && -z $count_ops ]]; then
# echo -e "\e[01;31mERROR\e[00m: No feature, enhancement, fix, bugfix, hotfix, or ops branches detected!"
# exit 1 # For GH Actions
# if [[ "$sourced" == 0 ]]; then
# exit 1
# else
# return 1
# fi
# fi
# fi
# --------------------------------------------------------------------------------------------------
# Main Operations
# --------------------------------------------------------------------------------------------------
if [[ $incrementMajor == 'true' ]]; then
newVersionMajor=$((lastVersionMajor + 1))
newVersionMinor='0'
newVersionPatch='0'
elif [[ -n $count_feature || -n $count_enhancement ]]; then
newVersionMajor=$lastVersionMajor
[[ -n $count_feature ]] && newVersionMinor=$((lastVersionMinor + count_feature))
[[ -n $count_enhancement ]] && newVersionMinor=$((newVersionMinor + count_enhancement))
newVersionPatch='0'
elif [[ -n $count_fix || -n $count_bugfix || -n $count_hotfix || -n $count_ops ]]; then
newVersionMajor=$lastVersionMajor
newVersionMinor=$lastVersionMinor
[[ -n $count_fix ]] && newVersionPatch=$((lastVersionPatch + count_fix))
[[ -n $count_bugfix ]] && newVersionPatch=$((newVersionPatch + count_bugfix))
[[ -n $count_hotfix ]] && newVersionPatch=$((newVersionPatch + count_hotfix))
[[ -n $count_ops ]] && newVersionPatch=$((newVersionPatch + count_ops))
else
newVersionMajor=$lastVersionMajor
newVersionMinor=$lastVersionMinor
newVersionPatch=$((lastVersionPatch + 1))
fi
newVersion=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -9p full $newVersionMajor.$newVersionMinor.$newVersionPatch")
if [[ -n $arg_e ]]; then
export_var="$arg_e_val"
eval "${export_var}=${newVersion}"
export export_var
else
echo "$newVersion"
fi