Skip to content
Snippets Groups Projects
release.sh 1.34 KiB
#!/bin/bash
#
# Release script that will bump the project version and commit and push
# the version file.
#
# This supports a limited, but reasonable, set of version specifiers.
#
# ASSUMES:
#   1. You are using Hatch
#   2. You have `version` in your `project.dynamic` list
#
set -e

readonly DEFAULT_BRANCH=main

if [[ ! "$1" =~ ^(major|minor|patch|dev)$ ]]; then
    echo "USAGE: $0 <major|minor|patch|dev>"
    exit 1
fi

readonly branch="$(git branch --show-current)"
if [[ $branch != "main" ]]; then
  echo "Releases must be done on the default branch '$DEFAULT_BRANCH', not '$branch'"
  exit 1
fi

# Bumps the version, i.e., sets the version in the version file specified in
# the hatch version file config.
hatch version $1

readonly ver=$(hatch version)
if git tag -l | grep ${ver} &> /dev/null; then
    echo "Whoops!! Tag for version ${ver} already exists"
    echo "You may have to manually revert the version before running this script again."
    exit 1
fi

# Get the name of the file contain the version var __version__. This may need
# to be adjusted if version handing doesn't meet assumptions.
readonly verfile=$(find . -name \*.py | xargs grep -nE '^__version__ = ' | cut -d: -f1)
[[ -z ${verfile} ]] && echo "Failed to find version file" && exit 1
git commit -vm "bump version" "${verfile}"
git tag -am "bump to ${ver}" ${ver}
git push --follow-tags