Repository navigation
Expand file tree
/
Copy pathrelease-cut.sh
More file actions
executable file
·183 lines (157 loc) · 3.79 KB
/
Copy pathrelease-cut.sh
File metadata and controls
executable file
·183 lines (157 loc) · 3.79 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
#!/usr/bin/env sh
set -eu
usage() {
echo "Usage: $0 <major|minor|patch> [branch]" >&2
echo "Example: $0 patch main" >&2
exit 1
}
BUMPTYPE="${1:-}"
BRANCH="${2:-main}"
[ -n "$BUMPTYPE" ] || usage
case "$BUMPTYPE" in
major | minor | patch)
;;
*)
echo "Invalid bump type: $BUMPTYPE" >&2
usage
;;
esac
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || {
echo "Not inside a git repository" >&2
exit 1
}
if ! git diff --quiet; then
echo "Working tree has unstaged changes" >&2
git status --short >&2
exit 1
fi
if ! git diff --cached --quiet; then
echo "Working tree has staged but uncommitted changes" >&2
git status --short >&2
exit 1
fi
tmp_file="$(mktemp)"
trap 'rm -f "$tmp_file"' EXIT
git fetch origin "$BRANCH"
git fetch --tags origin
if ! git merge-base --is-ancestor "refs/remotes/origin/$BRANCH" HEAD; then
echo "Current HEAD is behind or diverged from origin/$BRANCH" >&2
exit 1
fi
CURRENT_VERSION="$(
awk '
/^\[package\]$/ { in_package = 1; next }
/^\[/ && $0 != "[package]" { in_package = 0 }
in_package && /^version = "/ {
gsub(/^version = "/, "", $0)
gsub(/"$/, "", $0)
print
exit
}
' Cargo.toml
)"
case "$CURRENT_VERSION" in
'' | *[!0-9.]* | *.*.*.* | .* | *.)
echo "Invalid current version in Cargo.toml: $CURRENT_VERSION" >&2
exit 1
;;
esac
IFS=.
set -- $CURRENT_VERSION
IFS=' '
CURRENT_MAJOR="${1:-}"
CURRENT_MINOR="${2:-}"
CURRENT_PATCH="${3:-}"
[ -n "$CURRENT_MAJOR" ] || {
echo "Invalid current version in Cargo.toml: $CURRENT_VERSION" >&2
exit 1
}
[ -n "$CURRENT_MINOR" ] || {
echo "Invalid current version in Cargo.toml: $CURRENT_VERSION" >&2
exit 1
}
[ -n "$CURRENT_PATCH" ] || {
echo "Invalid current version in Cargo.toml: $CURRENT_VERSION" >&2
exit 1
}
case "$CURRENT_MAJOR:$CURRENT_MINOR:$CURRENT_PATCH" in
*[!0-9:]*)
echo "Invalid current version in Cargo.toml: $CURRENT_VERSION" >&2
exit 1
;;
esac
case "$BUMPTYPE" in
major)
NEXT_MAJOR=$((CURRENT_MAJOR + 1))
NEXT_MINOR=0
NEXT_PATCH=0
;;
minor)
NEXT_MAJOR=$CURRENT_MAJOR
NEXT_MINOR=$((CURRENT_MINOR + 1))
NEXT_PATCH=0
;;
patch)
NEXT_MAJOR=$CURRENT_MAJOR
NEXT_MINOR=$CURRENT_MINOR
NEXT_PATCH=$((CURRENT_PATCH + 1))
;;
esac
VERSION="$NEXT_MAJOR.$NEXT_MINOR.$NEXT_PATCH"
TAG="$VERSION"
if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null 2>&1; then
echo "Tag already exists: $TAG" >&2
exit 1
fi
if git ls-remote --tags --exit-code origin "refs/tags/$TAG" >/dev/null 2>&1; then
echo "Remote tag already exists on origin: $TAG" >&2
exit 1
fi
awk -v version="$VERSION" '
BEGIN {
in_package = 0
replaced = 0
}
/^\[package\]$/ {
in_package = 1
print
next
}
/^\[/ && $0 != "[package]" {
in_package = 0
}
in_package && !replaced && /^version = "/ {
print "version = \"" version "\""
replaced = 1
next
}
{
print
}
END {
if (!replaced) {
exit 1
}
}
' Cargo.toml > "$tmp_file"
mv "$tmp_file" Cargo.toml
if git diff --quiet -- Cargo.toml; then
echo "Cargo.toml already set to version $VERSION" >&2
exit 1
fi
cargo update --workspace >/dev/null
PREV_TAG="$(
git tag --sort=-v:refname \
| grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' \
| head -n 1
)"
[ -n "$PREV_TAG" ] || {
echo "No previous semver tag found" >&2
exit 1
}
./scripts/changelog-update.sh "$PREV_TAG" "$TAG"
git add Cargo.toml Cargo.lock CHANGELOG.md
git commit -m "$VERSION"
git tag -m "$TAG" "$TAG"
git push origin "HEAD:$BRANCH"
git push origin "$TAG"