Skip to content

Instantly share code, notes, and snippets.

@akaszynski
Created June 10, 2025 17:41
Show Gist options
  • Save akaszynski/8b7636d52834ec078fb2c5c4f8af1b2c to your computer and use it in GitHub Desktop.
Save akaszynski/8b7636d52834ec078fb2c5c4f8af1b2c to your computer and use it in GitHub Desktop.
Automatically release a patch using pyproject.toml and git
function release() {
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "Working directory not clean. Commit or stash changes first."
return 1
fi
git checkout main
git pull
version_line=$(grep 'version =' pyproject.toml | head -n1)
if [[ $version_line =~ version\ =\ \"([0-9]+)\.([0-9]+)\.dev[0-9]+\" ]]; then
major=${BASH_REMATCH[1]}
next_minor=${BASH_REMATCH[2]}
minor=$((next_minor - 1))
else
echo "Invalid dev version format"
return 1
fi
target_branch="release/${major}.${minor}"
echo "Current development version: ${major}.${next_minor}.devX (from pyproject)"
echo "Assumed release branch: $target_branch"
if [[ "$1" =~ ^(patch|minor|major)$ ]]; then
release_type="$1"
else
read -p "Release type? [major|minor|patch]: " release_type
fi
if [[ "$release_type" != "patch" ]]; then
echo "Only patch release supported."
return 1
fi
git checkout $target_branch
git pull
git merge --quiet --no-edit main
release_version=$(grep 'version =' pyproject.toml | head -n1 | cut -d'"' -f2)
if [[ ! $release_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Invalid release version: $release_version"
return 1
fi
IFS='.' read -r maj min patch <<< "$release_version"
patch=$((patch + 1))
new_version="$maj.$min.$patch"
echo "Bumping to v$new_version"
if git rev-parse "v$new_version" >/dev/null 2>&1; then
echo "Tag v$new_version already exists"
return 1
fi
sed -i.bak "s/version = \".*\"/version = \"$new_version\"/" pyproject.toml
rm pyproject.toml.bak
git diff
if ! grep -q "version = \"$new_version\"" pyproject.toml; then
echo "Version update failed"
return 1
fi
git commit -am "release-$release_type: bump to v$new_version" --no-verify
git tag "v$new_version"
git push && git push --tags
# finally, checkout main again
git checkout main
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment