Skip to content

Commit 7e6e599

Browse files
Merge pull request #89 from kevinbackhouse/version-tools
Tools for updating the version number
2 parents cb49ece + 37e557d commit 7e6e599

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

release_tools/version_bump.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
# SPDX-FileCopyrightText: 2025 GitHub
4+
# SPDX-License-Identifier: MIT
5+
6+
# Script for updating the version number. Call it like this:
7+
#
8+
# ./release_tools/version_bump.sh minor
9+
#
10+
# It uses `hatch version` to update the version number. Use
11+
# major/minor/micro to determine which part of the version number to
12+
# bump. It creates a new branch and commits the version number
13+
# change.
14+
#
15+
# This script does not push the change to GitHub, so you need to do
16+
# that manually.
17+
18+
if [[ $# -eq 0 ]] ; then
19+
echo 'usage: ./release_tools/version_bump.sh [ARG]'
20+
echo 'ARG is passed to the hatch version command to bump the version number.'
21+
echo 'ARG is usually "major", "minor", or "micro".'
22+
exit 0
23+
fi
24+
25+
# Check that the main branch is checked out.
26+
if [ "$(git rev-parse --abbrev-ref HEAD)" != "main" ] ; then
27+
echo "Please check out the main branch before running this command."
28+
exit 1
29+
fi
30+
31+
# Check no uncommitted changes.
32+
git update-index --refresh
33+
if ! git diff-index --quiet HEAD -- ; then
34+
echo "There are uncommitted file changes. Aborting."
35+
exit 1
36+
fi
37+
38+
# Bump version number
39+
if ! hatch version "$@" ; then
40+
echo "Failed to update version"
41+
exit 1
42+
fi
43+
44+
# Read new version number
45+
if ! NEW_VERSION_NUMBER=$(hatch version) ; then
46+
echo "Failed to retrieve version number."
47+
exit 1
48+
fi
49+
50+
# Create new branch
51+
if ! git checkout -b "version-$NEW_VERSION_NUMBER" ; then
52+
echo "Creating the branch failed."
53+
git restore .
54+
exit 1
55+
fi
56+
57+
# Commit the version number change.
58+
if ! git commit -a -m "Version $NEW_VERSION_NUMBER" ; then
59+
echo "git commit failed"
60+
exit 1
61+
fi
62+
63+
echo
64+
echo "I have updated the version number locally."
65+
echo "The branch is ready for you to push to GitHub and create a pull request."

release_tools/version_tag.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
# SPDX-FileCopyrightText: 2025 GitHub
4+
# SPDX-License-Identifier: MIT
5+
6+
# Create a signed tag for the new version number. This script is
7+
# intended to be run after you have created a new version number
8+
# (using `version_bump.sh` is recommended) and the change has been
9+
# merged into main.
10+
#
11+
# This script does not push the tag to GitHub, so you need to do
12+
# that manually.
13+
14+
# Check that the main branch is checked out.
15+
if [ "$(git rev-parse --abbrev-ref HEAD)" != "main" ] ; then
16+
echo "Please check out the main branch before running this command."
17+
exit 1
18+
fi
19+
20+
# Check no uncommitted changes.
21+
git update-index --refresh
22+
if ! git diff-index --quiet HEAD -- ; then
23+
echo "There are uncommitted file changes. Aborting."
24+
exit 1
25+
fi
26+
27+
# Check that this commit is signed by GitHub, to avoid
28+
# accidentally tagging a commit that only exists locally.
29+
if [ "$(git verify-commit HEAD --raw 2>&1 | grep -E "GOODSIG [A-F0-9]+ GitHub" -c)" -eq 0 ] ; then
30+
echo "This commit hasn't been signed by GitHub."
31+
echo "Please check that you are attempting to tag the correct commit."
32+
exit 1
33+
fi
34+
35+
# Check that this is a merge commit.
36+
if ! git rev-parse HEAD^2 >/dev/null 2>&1 ; then
37+
echo "This is not a merge commit."
38+
echo "Please check that you are attempting to tag the correct commit."
39+
exit 1
40+
fi
41+
42+
if ! PROJECT_NAME=$(hatch project metadata name) ; then
43+
echo "Failed to retrieve project name."
44+
exit 1
45+
fi
46+
if ! VERSION_NUMBER=$(hatch version) ; then
47+
echo "Failed to retrieve version number."
48+
exit 1
49+
fi
50+
TAG_NAME="v$VERSION_NUMBER"
51+
52+
# Create tag
53+
if ! git tag "$TAG_NAME" -s -m "Release $PROJECT_NAME version $VERSION_NUMBER." ; then
54+
echo "Failed to create the tag"
55+
exit 1
56+
fi
57+
58+
REMOTE_NAME=$(git for-each-ref --format='%(upstream:remotename)' refs/heads/main)
59+
if [ -z "$REMOTE_NAME" ]; then
60+
REMOTE_NAME="origin"
61+
fi
62+
63+
echo
64+
echo "I have created tag $TAG_NAME. You can push it to GitHub like this:"
65+
echo "git push \"$REMOTE_NAME\" tag \"$TAG_NAME\""

0 commit comments

Comments
 (0)