Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,11 @@ check_tag() {

TAG=$1

# If a tag is specified, ensure that tag is present and checked out.
if [[ $TAG != $(git describe --abbrev=10) ]]; then
red "tag $TAG not checked out"
exit 1
fi

# Verify that it is signed if it is a real tag. If the tag looks like the
# output of "git describe" for an untagged commit, skip verification.
# The pattern is: <tag_name>-<number_of_commits>-g<abbreviated_commit_hash>
# Example: "v0.31.2-beta-122-g8c6b73c".
if [[ $TAG =~ -[0-9]+-g([0-9a-f]{10})$ ]]; then
if [[ $TAG =~ -[0-9]+-g([0-9a-f]{7,40})$ ]]; then
# This looks like a "git describe" output. Make sure the hash
# described is a prefix of the current commit.
DESCRIBED_HASH=${BASH_REMATCH[1]}
Expand All @@ -82,6 +76,25 @@ check_tag() {
return
fi

# Release tags must start with 'v' (for example v0.31.5-beta).
if [[ $TAG != v* ]]; then
red "tag $TAG must start with 'v'"
exit 1
fi

# Ensure the tag exists in the repository.
if ! git show-ref --verify --quiet "refs/tags/$TAG"; then
red "tag $TAG not found"
exit 1
fi

# Ensure the current commit is tagged with the requested tag, even when
# multiple tags point at HEAD.
if ! git tag --points-at HEAD | grep -Fxq "$TAG"; then
red "tag $TAG not checked out on the current commit"
exit 1
fi
Comment on lines +85 to +96

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These two checks for tag existence and for the tag pointing to the current commit can be combined for better efficiency. In the success case (which should be the common path for a release), this refactoring avoids an extra git process invocation. The suggested code first checks if the tag points to HEAD. If that fails, it then checks if the tag exists at all to provide a more specific error message to the user, preserving the current behavior but in a more optimized way.

Suggested change
# Ensure the tag exists in the repository.
if ! git show-ref --verify --quiet "refs/tags/$TAG"; then
red "tag $TAG not found"
exit 1
fi
# Ensure the current commit is tagged with the requested tag, even when
# multiple tags point at HEAD.
if ! git tag --points-at HEAD | grep -Fxq "$TAG"; then
red "tag $TAG not checked out on the current commit"
exit 1
fi
# Ensure the current commit is tagged with the requested tag, even when
# multiple tags point at HEAD.
if ! git tag --points-at HEAD | grep -Fxq "$TAG"; then
# If the tag is not on the current commit, check if it exists at all
# to provide a better error message.
if git show-ref --verify --quiet "refs/tags/$TAG"; then
red "tag $TAG not checked out on the current commit"
else
red "tag $TAG not found"
fi
exit 1
fi


if ! git verify-tag $TAG; then
red "tag $TAG not signed"
exit 1
Expand Down
Loading