-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetectPreviousVersion.sh
More file actions
executable file
·153 lines (125 loc) · 5.2 KB
/
detectPreviousVersion.sh
File metadata and controls
executable file
·153 lines (125 loc) · 5.2 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
#!/usr/bin/env bash
# --------------------------------------------------------------------------------------------------
# detectPreviousVersion.sh
#
# Description
# Detect previous version.
#
# --------------------------------------------------------------------------------------------------
# Help
# --------------------------------------------------------------------------------------------------
help="\
NAME
detectPreviousVersion.sh
SYNOPSIS
${0##*/} [-hvcnd]
DESCRIPTION
Detects most recent version tag of the repository.
Prints only the detected version to stdout by default.
Assumes version '0.0.0' if no previous versions are detected.
The following options are available:
-h Print this menu.
-v Verbose mode. Prints additional information to stdout if available.
-c Prints the commit hash instead of the detected version to stdout.
-n Enables mono-repo mode allowing the product name to match against tags.
EG: 'bob' would match tags like 'bob_1.2.3'.
TIP: dir names and product names should match. This arg exists in case they do not.
-d The directory of the product to version. EG: 'path/to/bob'.
EXAMPLES
Detects previous version, printing additional information if available.
./detectPreviousVersion.sh -v
NOTES
If most recent tag is not valid by semantic versioning standards, you will have to prime the
repository by tagging it appropriatley. Use '0.0.0' if in doubt. The version iteration
script will automatically derive the correct version for the repository based on gitflow
branch names.
"
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 "hv9cn:d:" opt; do
case $opt in
h)
printHelp
exit 0
;;
v)
arg_v='set'
;;
c)
arg_c='set'
;;
9)
arg_9='set'
;;
n)
arg_n='set'
arg_n_val="$OPTARG"
arg_opts="$arg_opts -n $OPTARG"
;;
d)
arg_d='set'
arg_d_val="$OPTARG"
arg_d_opt="--full-history"
arg_opts="$arg_opts -d $OPTARG"
;;
*)
echo -e "\e[01;31mERROR\e[00m: Invalid argument!"
printHelp
exit 1
;;
esac
done
shift $((OPTIND-1))
# --------------------------------------------------------------------------------------------------
# Variables
# --------------------------------------------------------------------------------------------------
tsCmd='date --utc +%FT%T.%3NZ'
if [[ -n $arg_9 ]]; then
semverRegex="^[v]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
[[ -n $arg_d ]] && semverRegex="([0-9A-Za-z]+)?[_-]?[v]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
else
semverRegex="^[v]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-([0-9A-Za-z]+))?(\\+((([1-9])|([1-9][0-9]+))))?$"
[[ -n $arg_d ]] && semverRegex="^([0-9A-Za-z]+)?[_-]?[v]?(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-([0-9A-Za-z]+))?(\\+((([1-9])|([1-9][0-9]+))))?$"
fi
relative_path="$(dirname "${BASH_SOURCE[0]}")"
dir="$(realpath "${relative_path}")"
lastVersion=$(git for-each-ref --sort=creatordate --format '%(refname:lstrip=2)' refs/tags | grep -E "$semverRegex" | tail -n 1)
# Support mono-repos where a product name is specified.
[[ -n $arg_n ]] && lastVersion=$(git for-each-ref --sort=creatordate --format '%(refname:lstrip=2)' refs/tags | grep "$arg_n_val" | grep -E "$semverRegex" | tail -n 1)
# --------------------------------------------------------------------------------------------------
# Sanity (2/2)
# --------------------------------------------------------------------------------------------------
if [[ "$lastVersion" == '' ]]; then
[[ -n $arg_v && -z $arg_n ]] && echo -e "[$(${tsCmd})] INFO: No previous version detected. Initializing at '0.0.0'.\n"
[[ -n $arg_v && -n $arg_n ]] && echo -e "[$(${tsCmd})] INFO: No previous version detected. Initializing at '${arg_n_val}_0.0.0'.\n"
lastVersion='0.0.0'
lastVersionCommitHash=$(git rev-list --max-parents=0 HEAD)
else
if ! bash -c "${dir}/validateSemver.sh -v9 $arg_opts $lastVersion"; then
exit 1
else
lastVersionCommitHash=$(git rev-list -n 1 "$lastVersion")
# Ensure lastVersion does not include a leading [vV]
lastVersion=$(bash -c "${dir}/validateSemver.sh -v9p full $arg_opts $lastVersion")
fi
fi
[[ -n $arg_n ]] && lastVersion="${arg_n_val}_${lastVersion}"
# --------------------------------------------------------------------------------------------------
# Main Operations
# --------------------------------------------------------------------------------------------------
if [[ -n $arg_c ]]; then
echo "$lastVersionCommitHash"
else
echo "$lastVersion"
fi