|
| 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