-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetversion.sh
More file actions
executable file
·219 lines (192 loc) · 6.56 KB
/
setversion.sh
File metadata and controls
executable file
·219 lines (192 loc) · 6.56 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
#!/usr/bin/env bash
# setversion.sh
# Safely set project version (Maven multi-module aware) with semver checks
# Usage: ./setversion.sh <version> [--dry-run] [--force]
set -Eeuo pipefail
print_usage() {
cat <<EOF
This script sets the project version, commits the change, and optionally tags it.
Usage:
./setversion.sh <version> [--dry-run] [--force]
Options:
--dry-run Preview the changes without actually making them.
--force Force the change even if it looks like a downgrade or tag already exists.
Examples:
To create a new release:
./setversion.sh 1.0.0
To set a new SNAPSHOT version:
./setversion.sh 1.0.1-SNAPSHOT
To preview the changes:
./setversion.sh 1.0.0 --dry-run
After running the script:
git push && git push --tags
EOF
}
if [ "$#" -lt 1 ]; then
print_usage
exit 1
fi
VERSION=""
DRY_RUN=false
FORCE=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
--dry-run) DRY_RUN=true; shift ;;
--force) FORCE=true; shift ;;
-h|--help) print_usage; exit 0 ;;
*)
if [ -z "$VERSION" ]; then
VERSION="$1"
shift
else
echo "Unknown argument: $1" >&2
print_usage
exit 1
fi
;;
esac
done
# Basic semver validation (major.minor.patch) with optional -SNAPSHOT
if [[ ! "$VERSION" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(-SNAPSHOT)?$ ]]; then
echo "Error: Invalid version format. Use 'major.minor.patch' or 'major.minor.patch-SNAPSHOT'." >&2
exit 1
fi
# helper: choose mvn wrapper if present
MVN_CMD="mvn"
if [ -x "$(pwd)/mvnw" ]; then
MVN_CMD="$(pwd)/mvnw"
fi
# get current project version via maven (robust)
CURRENT_VERSION="$($MVN_CMD -q -DforceStdout help:evaluate -Dexpression=project.version 2>/dev/null || true)"
if [ -z "$CURRENT_VERSION" ]; then
echo "Error: Unable to determine current project version via Maven ($MVN_CMD)." >&2
exit 1
fi
echo "Current project version: $CURRENT_VERSION"
# parse semver parts
parse_semver() {
local v="$1"
if [[ "$v" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)(-SNAPSHOT)?$ ]]; then
echo "${BASH_REMATCH[1]} ${BASH_REMATCH[2]} ${BASH_REMATCH[3]} ${BASH_REMATCH[4]:-}" # major minor patch suffix
else
return 1
fi
}
compare_semver() {
# returns: 1 if a>b, 0 if a==b, -1 if a<b
local a=($1)
local b=($2)
for i in 0 1 2; do
if (( ${a[i]} > ${b[i]} )); then
echo 1; return 0
elif (( ${a[i]} < ${b[i]} )); then
echo -1; return 0
fi
done
# numeric parts equal; treat SNAPSHOT as lower than release
local sufA="${a[3]:-}"
local sufB="${b[3]:-}"
if [ "$sufA" = "$sufB" ]; then
echo 0
else
# if a has -SNAPSHOT and b doesn't, a < b
if [ "$sufA" = "-SNAPSHOT" ] && [ -z "$sufB" ]; then
echo -1
elif [ -z "$sufA" ] && [ "$sufB" = "-SNAPSHOT" ]; then
echo 1
else
# fallback equal
echo 0
fi
fi
}
CUR_PARTS=($(parse_semver "$CURRENT_VERSION")) || { echo "Error: current version $CURRENT_VERSION is not semver-compatible" >&2; exit 1; }
NEW_PARTS=($(parse_semver "$VERSION")) || { echo "Error: new version $VERSION is not semver-compatible" >&2; exit 1; }
cmp=$(compare_semver "${CUR_PARTS[*]}" "${NEW_PARTS[*]}") || true
if [ "$cmp" = "1" ]; then
echo "Error: New version $VERSION is lower than current version $CURRENT_VERSION (potential downgrade)." >&2
if [ "$FORCE" != "true" ]; then
echo "Use --force to override." >&2
exit 1
else
echo "--force provided: proceeding despite downgrade." >&2
fi
elif [ "$cmp" = "0" ]; then
echo "Warning: New version $VERSION is equal to current version $CURRENT_VERSION." >&2
if [ "$FORCE" != "true" ]; then
echo "Use --force to proceed anyway." >&2
exit 1
else
echo "--force provided: proceeding." >&2
fi
fi
# Branch check: allow master, release/*, or '<major>.x' (e.g. 1.x) unless --force
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)
MAJOR="${NEW_PARTS[0]}"
if [ -n "$CURRENT_BRANCH" ] && [ "$FORCE" != "true" ]; then
if [[ "$CURRENT_BRANCH" != "master" && "$CURRENT_BRANCH" != release/* && "$CURRENT_BRANCH" != "${MAJOR}.x" ]]; then
echo "Error: Releasing version $VERSION requires being on 'master', a 'release/*' branch, or '${MAJOR}.x', but current branch is '$CURRENT_BRANCH'." >&2
echo "If you really want to proceed from this branch, pass --force." >&2
exit 1
fi
fi
# Ensure git is configured
if ! git config user.name &>/dev/null || ! git config user.email &>/dev/null; then
echo "Error: Git user.name and user.email are not configured." >&2
exit 1
fi
# Ensure working tree is clean to avoid accidental overwrites (skip this check for --dry-run)
if [ "$DRY_RUN" != "true" ] && [ -n "$(git status --porcelain)" ] && [ "$FORCE" != "true" ]; then
echo "Error: Working tree is not clean. Commit or stash changes, or pass --force to override." >&2
git status --porcelain
exit 1
fi
# Check if tag already exists (for release versions)
if [[ "$VERSION" != *SNAPSHOT ]]; then
if git rev-parse -q --verify "refs/tags/$VERSION" >/dev/null; then
echo "Error: Git tag '$VERSION' already exists." >&2
if [ "$FORCE" != "true" ]; then
echo "Use --force to overwrite or choose a different version." >&2
exit 1
else
echo "--force provided: existing tag will be overwritten (old tag will be deleted and recreated)."
fi
fi
fi
# Dry-run summary
if [ "$DRY_RUN" = true ]; then
echo "Dry-run: The following actions would be performed:"
echo " - Run: $MVN_CMD versions:set -DnewVersion=$VERSION -DprocessAllModules=true -DgenerateBackupPoms=false"
if [[ "$VERSION" == *SNAPSHOT ]]; then
echo " - Commit with message: 'Bump version to $VERSION [skip ci]'"
else
echo " - Commit with message: 'Release version $VERSION'"
echo " - Create git tag: $VERSION"
fi
if [ "$FORCE" = true ]; then
echo " - (force mode enabled)"
fi
echo "No changes were made."
exit 0
fi
echo "Setting Maven version to $VERSION..."
$MVN_CMD versions:set -DnewVersion="$VERSION" -DprocessAllModules=true -DgenerateBackupPoms=false
# Commit changes
if [[ "$VERSION" == *SNAPSHOT ]]; then
echo "This is a SNAPSHOT version. Committing bump..."
git add -A
git commit -m "Bump version to $VERSION [skip ci]"
else
echo "This is a Release version. Committing and tagging..."
git add -A
git commit -m "Release version $VERSION"
# if tag exists and force, delete it first
if git rev-parse -q --verify "refs/tags/$VERSION" >/dev/null; then
if [ "$FORCE" = "true" ]; then
git tag -d "$VERSION" || true
git push --delete origin "$VERSION" || true
fi
fi
git tag "$VERSION"
fi
echo "Version set to $VERSION successfully."