|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Function to display error messages and exit |
| 6 | +error_exit() { |
| 7 | + echo "ERROR: $1" >&2 |
| 8 | + exit 1 |
| 9 | +} |
| 10 | + |
| 11 | +# Check if pyproject.toml exists |
| 12 | +if [[ ! -f pyproject.toml ]]; then |
| 13 | + error_exit "pyproject.toml not found in the current directory" |
| 14 | +fi |
| 15 | + |
| 16 | +# Check if git repo exists |
| 17 | +if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then |
| 18 | + error_exit "Not inside a git repository" |
| 19 | +fi |
| 20 | + |
| 21 | +echo "Checking if version needs to be bumped..." |
| 22 | + |
| 23 | +# Function to extract version from pyproject.toml |
| 24 | +get_version() { |
| 25 | + grep -E "^version\\s*=\\s*[\\"']" pyproject.toml | sed -E 's/version\\s*=\\s*["'\''](^["'\'']+)["'\''].*/\\1/' |
| 26 | +} |
| 27 | + |
| 28 | +# Check if pyproject.toml is staged |
| 29 | +if git diff --name-only --cached | grep -q "pyproject.toml"; then |
| 30 | + echo "pyproject.toml is staged. Checking if version has been modified..." |
| 31 | + |
| 32 | + # Get the current version in the working copy |
| 33 | + current_version=$(get_version) |
| 34 | + |
| 35 | + # Get the version from the last commit |
| 36 | + previous_version=$(git show HEAD:pyproject.toml 2>/dev/null | grep -E "^version\\s*=\\s*[\\"']" | sed -E 's/version\\s*=\\s*["'\''](^["'\'']+)["'\''].*/\\1/' || echo "") |
| 37 | +
|
| 38 | + if [[ "$current_version" != "$previous_version" ]]; then |
| 39 | + echo "Version has already been changed from $previous_version to $current_version. No need to bump version." |
| 40 | + exit 0 |
| 41 | + fi |
| 42 | +fi |
| 43 | +
|
| 44 | +# If we get here, we need to bump the version |
| 45 | +echo "Bumping patch version..." |
| 46 | +poetry version patch || error_exit "Failed to bump version with poetry" |
| 47 | +
|
| 48 | +new_version=$(get_version) |
| 49 | +echo "Version bumped to $new_version" |
| 50 | +
|
| 51 | +# Stage the changes to pyproject.toml |
| 52 | +git add pyproject.toml || error_exit "Failed to stage pyproject.toml" |
| 53 | +
|
| 54 | +echo "Successfully bumped version and staged pyproject.toml" |
| 55 | +exit 0 |
0 commit comments