Repository navigation
Expand file tree
/
Copy pathbump.sh
More file actions
executable file
·94 lines (81 loc) · 2.72 KB
/
Copy pathbump.sh
File metadata and controls
executable file
·94 lines (81 loc) · 2.72 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
#!/usr/bin/env bash
# Starts a stable release: sets the version in mygo.json, commits it and
# tags v<version>; with -p it pushes the branch and the tag, and the tag
# runs ci.yml and release.yml, which publishes the release once both pass.
# Usage: bump.sh -v 1.0.2 [-p]
set -euo pipefail
usage() {
printf 'Usage: %s -v 1.0.2 [-p]\n' "$(basename "$0")" >&2
exit 2
}
VERSION=""
PUSH=0
while getopts ":v:p" opt; do
case "$opt" in
v) VERSION="$OPTARG" ;;
p) PUSH=1 ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
[[ $# -eq 0 ]] || usage
[[ -n "$VERSION" ]] || usage
[[ "$VERSION" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]] || {
printf 'Version must use x.y.z format, got: %s\n' "$VERSION" >&2
exit 2
}
ROOT="$(git rev-parse --show-toplevel)"
cd "$ROOT"
CONFIG="mygo.json"
TAG="v$VERSION"
if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
printf 'Local tag already exists: %s\n' "$TAG" >&2
exit 1
fi
if git remote get-url origin >/dev/null 2>&1 &&
git ls-remote --exit-code --tags origin "refs/tags/$TAG" >/dev/null 2>&1; then
printf 'Remote tag already exists: %s\n' "$TAG" >&2
exit 1
fi
# Only the version line changes, keeping the rest of mygo.json as written.
# Updates are offered only to a higher version, so it must grow.
CURRENT="$(python3 - "$CONFIG" "$VERSION" <<'PY'
import json, re, sys
path, version = sys.argv[1:]
with open(path) as f:
text = f.read()
current = json.loads(text)["version"]
if current == version:
raise SystemExit(f"{path} is already at {version}.")
def parts(v):
return tuple(int(n) for n in v.split("."))
try:
if parts(version) <= parts(current):
raise SystemExit(f"{version} is not above the current version {current}.")
except ValueError:
raise SystemExit(f"{path} has a version that is not x.y.z: {current}")
new, n = re.subn(r'("version"\s*:\s*")[^"]*(")', r"\g<1>" + version + r"\g<2>", text, count=1)
if n != 1 or json.loads(new)["version"] != version:
raise SystemExit(f"Could not set the version in {path}.")
with open(path, "w") as f:
f.write(new)
print(current)
PY
)"
# cliff.toml skips this exact subject, keeping it out of the release notes
# Leave unrelated working-tree edits and staged changes as they are.
git commit --only -m "chore(release): bump version to $VERSION" -- "$CONFIG"
git tag -a "$TAG" -m "Release $TAG"
if [[ "$PUSH" -eq 1 ]]; then
BRANCH="$(git branch --show-current)"
[[ -n "$BRANCH" ]] || {
printf 'Cannot push from a detached HEAD.\n' >&2
exit 1
}
git push origin "$BRANCH"
git push origin "$TAG"
fi
printf 'Bumped %s to %s, committed and tagged %s.\n' "$CURRENT" "$VERSION" "$TAG"
if [[ "$PUSH" -eq 0 ]]; then
printf 'Push with: git push origin HEAD && git push origin %s\n' "$TAG"
fi