Debian Codegraph @

release

raw
1 #!/usr/bin/env bash
2 #
3 # Usage: release <version>
4 #
5 # Prepare a release: bump version in code and changelog, commit, tag, and
6 # push. CI then runs checks, creates the GitHub Release, and publishes to PyPI.
7 #
8 # The changelog must have a '# Unreleased' or '# Unreleased: <title>' line
9 # before running. That line is renamed to '# <version>' or '# <version>: <title>'.
10 #
11 # Arguments:
12 # <version> Version to release in MAJOR.MINOR.PATCH format
13 #
14 # Options:
15 # -h, --help Show this help
16 #
17 set -euo pipefail
18 trap "exit" INT
19
20 THIS_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
21
22 ask_yes_no() {
23 local prompt="$1"
24 local choice=
25 while [[ "${choice}" != "y" ]]; do
26 read -r -p "${prompt} [y/n] " choice
27 case "${choice}" in
28 y|Y) return 0 ;;
29 n|N) printf "Aborted.\n"; exit 0 ;;
30 *) printf "Type 'y' or 'n'.\n" ;;
31 esac
32 done
33 }
34
35 main() {
36 show_help() {
37 sed -n '2,${/^#/!q; s/^# \?//p}' "${BASH_SOURCE[0]}" | sed "s|\$0|${0##*/}|g"
38 }
39
40 [[ "${1:-}" =~ ^(-h|--help)$ ]] && { show_help; exit 0; }
41
42 if [[ $# -ne 1 ]]; then
43 show_help >&2
44 exit 1
45 fi
46
47 local version="$1"
48 local tag="v${version}"
49
50 printf "Pre-check: version format... "
51 if ! [[ "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
52 printf "\nInvalid version '%s': expected MAJOR.MINOR.PATCH\n" "${version}" >&2
53 exit 1
54 fi
55 printf "✅\n"
56
57 printf "Pre-check: required tools (git, python3)... "
58 for tool in git python3; do
59 if ! command -v "${tool}" &>/dev/null; then
60 printf "\nRequired tool not found: %s\n" "${tool}" >&2
61 exit 1
62 fi
63 done
64 printf "✅\n"
65
66 printf "Pre-check: working tree is clean (except changelog.md)... "
67 if ! git -C "${THIS_DIR}" diff --quiet -- ':!changelog.md' \
68 || ! git -C "${THIS_DIR}" diff --cached --quiet -- ':!changelog.md'; then
69 printf "\nWorking tree has uncommitted changes outside changelog.md. Commit or stash before releasing.\n" >&2
70 exit 1
71 fi
72 printf "✅\n"
73
74 printf "Pre-check: on master branch... "
75 local branch
76 branch=$(git -C "${THIS_DIR}" rev-parse --abbrev-ref HEAD)
77 if [[ "${branch}" != "master" ]]; then
78 printf "\nNot on master branch (current: %s)\n" "${branch}" >&2
79 exit 1
80 fi
81 printf "✅\n"
82
83 printf "Pre-check: not behind origin/master... "
84 git -C "${THIS_DIR}" fetch origin --quiet
85 if ! git -C "${THIS_DIR}" merge-base --is-ancestor origin/master HEAD; then
86 printf "\nLocal master is behind or has diverged from origin/master. Pull first.\n" >&2
87 exit 1
88 fi
89 printf "✅\n"
90
91 printf "Pre-check: tag %s does not exist on origin... " "${tag}"
92 if git -C "${THIS_DIR}" ls-remote --tags origin | grep -q "refs/tags/${tag}$"; then
93 printf "\nTag %s already exists on origin\n" "${tag}" >&2
94 exit 1
95 fi
96 printf "✅\n"
97
98 printf "Pre-check: changelog has '# Unreleased' section... "
99 if ! grep -qE "^# Unreleased" "${THIS_DIR}/changelog.md"; then
100 printf "\nchangelog.md does not have a '# Unreleased' section.\n" >&2
101 printf "Add '# Unreleased: <title>' above the previous release and retry.\n" >&2
102 exit 1
103 fi
104 printf "✅\n"
105
106 printf "Setting version in treetime/__init__.py to %s... " "${version}"
107 sed -i "s/^version = '.*'/version = '${version}'/" "${THIS_DIR}/treetime/__init__.py"
108 if ! grep -q "^version = '${version}'" "${THIS_DIR}/treetime/__init__.py"; then
109 printf "\nFailed to set version in treetime/__init__.py\n" >&2
110 exit 1
111 fi
112 printf "✅\n"
113
114 printf "Updating changelog.md: '# Unreleased' -> '# %s'... " "${version}"
115 # Preserves any trailing ': <title>' on the Unreleased line.
116 sed -i "s/^# Unreleased/# ${version}/" "${THIS_DIR}/changelog.md"
117 if ! grep -qE "^# ${version}" "${THIS_DIR}/changelog.md"; then
118 printf "\nFailed to update changelog.md\n" >&2
119 exit 1
120 fi
121 printf "✅\n"
122
123 printf "\nChanges staged for release commit:\n\n"
124 git -C "${THIS_DIR}" --no-pager diff --unified=3 -- treetime/__init__.py changelog.md
125 printf "\n"
126
127 printf "Committing... "
128 git -C "${THIS_DIR}" add treetime/__init__.py changelog.md
129 git -C "${THIS_DIR}" commit -m "chore: release ${version}" --quiet
130 printf "✅\n"
131
132 printf "Creating tag %s... " "${tag}"
133 git -C "${THIS_DIR}" tag "${tag}"
134 printf "✅\n"
135
136 printf "\nRelease %s is ready locally.\n" "${version}"
137 ask_yes_no "Push commit and tag to origin?"
138
139 git -C "${THIS_DIR}" push origin master --quiet
140 git -C "${THIS_DIR}" push origin "${tag}" --quiet
141 printf "Tag %s pushed. CI will run checks, create the GitHub Release, and publish to PyPI.\n" "${tag}"
142 }
143
144 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
145 main "$@"
146 fi