-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemver_inc
executable file
·158 lines (142 loc) · 2.98 KB
/
semver_inc
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
#!/bin/bash
# @param $1 {string[]} input
# @param $2 {empty} withPre
_getLatest() {
if [ -z "$2" ]
then
local withPre=""
else
local withPre="(\-[0-9]+)?"
fi
{
echo "$1"
} | {
# Take the first satisfing entry //TODO readline?
egrep "^v?[0-9]+(\.[0-9]+){0,2}$withPre$" | head -1
} | {
# Strip 'v'-s
cut -d v -f2
}
}
# @param $1 {major|minor|patch|release} command
# @param $2 {empty} willBePre
_semver() {
command=$1
willBePre=$2
case $command in
'major')
if { [ -z "$prerelease" ] || [ ! -z "$willBePre" ] || [ "$patch" -ne "0" ] || [ "$minor" -ne "0" ]; }; then
major=$(($major + 1))
fi
minor=0
patch=0
prerelease=0
;;
'minor')
if { [ -z "$prerelease" ] || [ ! -z "$willBePre" ] || [ "$patch" -ne "0" ]; }; then
minor=$(($minor + 1))
fi
patch=0
prerelease=0
;;
'patch')
if { [ -z "$prerelease" ] || [ ! -z "$willBePre" ]; }; then
patch=$(($patch + 1))
fi
prerelease=0
;;
'release')
if [ -z "$willBePre" ]; then
echo "Unknown command '$release'"
exit 1
fi
if [ -z "$prerelease" ]
then
prerelease=0
patch=$(($patch + 1))
else
prerelease=$(($prerelease + 1))
fi
;;
*)
echo "Unknown command '$command'"
exit 1
;;
esac
}
command=$1
willBePre=""
if [[ $command == pre* ]]; then
command=$(echo "$command" | cut -c4-)
willBePre="1"
fi
case $command in
'product')
command="major"
own=1
;;
'feature')
command="minor"
own=1
;;
'hotfix')
command="patch"
own=1
;;
esac
if { [ -z "$own" ] || [ ! -z "$willBePre" ]; }; then
withPre="1"
fi
# Ways: $(</dev/stdin) $(cat) $(less <&0)
# Check from https://stackoverflow.com/a/28786207
# file=${1--} # POSIX-compliant; ${1:--} can be used either.
if [ ! -z "$2" ]
then
input="$2"
elif [ ! -t 0 ]
then
input=$(less <&0)
else
input=$(git tag --sort=-v:refname)
fi
latest=$(_getLatest "$input" "$withPre")
if [ -z "$latest" ] && { [ -z "$withPre" ]; }; then
# Attempt to find pre-less version
latest=$(_getLatest "$input" "1")
fi
if [ -z "$latest" ]; then
echo "No satisfied tag was found"
exit 1
fi
major=$(echo "$latest" | cut -d . -f1)
minor=$(echo "$latest" | cut -d . -f2)
patchAndPre=$(echo "$latest" | cut -d . -f3)
patch=$(echo "$patchAndPre" | cut -d - -f1)
prerelease=$(echo "$patchAndPre" | cut -s -d - -f2)
if { [ ! -z "$own" ] && [ ! -z "$willBePre" ] && [ ! -z "$prerelease" ]; }; then
case $command in
'major')
if { [ "$patch" -eq "0" ] && [ "$minor" -eq "0" ]; }; then
command="release"
fi
;;
'minor')
if [ "$patch" -eq '0' ]; then
command="release"
fi
;;
'patch')
command="release"
;;
esac
fi
_semver "$command" "$willBePre"
[ $? -ne "0" ] && exit 1
core="$major.$minor.$patch"
if [ -z "$willBePre" ]
then
echo "$core"
else
pre=${prerelease:-0}
echo "$core-$pre"
fi