Repository navigation
Expand file tree
/
Copy pathrelease
More file actions
executable file
·85 lines (72 loc) · 2.67 KB
/
Copy pathrelease
File metadata and controls
executable file
·85 lines (72 loc) · 2.67 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
#!/usr/bin/env bash
set -euo pipefail
# Tags and pushes a release. The latest beta release should always be a
# superset of the latest stable release, so this script ensures that is
# the case before tagging and pushing.
#
# Usage: ./release v2.3.0 (stable, auto-creates beta if needed)
# ./release v2.3.0-beta.1 (beta, pushed directly)
usage() {
cat <<'EOF'
Usage: release <tag>
Tags and pushes a release. The "v" prefix is added automatically if omitted.
release 2.3.0 Stable release. Auto-creates a beta tag at HEAD
first if no existing beta already covers it.
release 2.3.0-beta.1 Beta release. Pushed directly.
The latest beta release should always be a superset of the latest stable
release, so stable releases create a companion beta when needed.
EOF
}
if [[ $# -eq 0 || "${1-}" == "-h" || "${1-}" == "--help" ]]; then
usage
exit 0
fi
tag="$1"
# Prepend a "v" to the tag if it doesn't have one
[[ "$tag" == v* ]] || tag="v$tag"
# Pick up any tags pushed from other machines or CI
git fetch origin --tags --quiet
# Bail if the tag already exists locally or on the remote
if git rev-parse "$tag" >/dev/null 2>&1; then
echo "Error: tag $tag already exists." >&2
exit 1
fi
# A hyphen in a semver tag means it's a prerelease (e.g. v2.3.0-beta.1).
# Stable tags have no hyphen, and need the beta-first check.
if [[ "$tag" != *-* ]]; then
# Strip the "v" prefix to get the bare version (e.g. "2.3.0")
base_version="${tag#v}"
# Check whether any existing beta tag is at HEAD or ahead of it.
# If so, beta users already have this code and we can skip creating one.
# "is-ancestor HEAD beta_tag" is true when HEAD <= beta_tag.
needs_beta=true
for beta_tag in $(git tag --list 'v*-*' --sort=-v:refname); do
if git merge-base --is-ancestor HEAD "$beta_tag" 2>/dev/null; then
echo "Beta $beta_tag already covers HEAD."
needs_beta=false
break
fi
done
if $needs_beta; then
# Find the highest existing beta number for this version so we don't
# collide (e.g. if beta.1 and beta.2 exist, we create beta.3).
latest_num=0
for t in $(git tag --list "v${base_version}-beta.*"); do
# Extract the number after "beta." (e.g. "v2.3.0-beta.2" -> "2")
num="${t##*beta.}"
if (( num > latest_num )); then
latest_num=$num
fi
done
next_num=$(( latest_num + 1 ))
auto_beta="v${base_version}-beta.${next_num}"
echo "No beta covers HEAD. Tagging $auto_beta first."
git tag "$auto_beta"
git push origin "$auto_beta"
echo "Pushed $auto_beta."
fi
fi
# Tag and push the requested release (stable or beta)
git tag "$tag"
git push origin "$tag"
echo "Pushed $tag."