#!/bin/bash # increment_and_commit.sh # # Automates version bumping based on git tags. # Git tag is the authoritative source of truth. # # Usage: # ./increment_and_commit.sh "Your commit message here" # Bump patch (default) # ./increment_and_commit.sh --minor "Your commit message" # Bump minor # ./increment_and_commit.sh --major "Your commit message" # Bump major # # This script coordinates version updates across all files in the project set -e VERSION_FILE="www/js/version.json" BUMP_TYPE="patch" COMMIT_MSG="" # Parse arguments while [[ $# -gt 0 ]]; do case $1 in --minor) BUMP_TYPE="minor" shift ;; --major) BUMP_TYPE="major" shift ;; --patch) BUMP_TYPE="patch" shift ;; -*|--*) echo "Unknown option: $1" echo "Usage: $0 [--major|--minor|--patch] [commit message]" exit 1 ;; *) COMMIT_MSG="$1" shift ;; esac done # Get the latest git tag (authoritative version) LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") echo "Latest tag: $LATEST_TAG" # Parse version components VERSION=${LATEST_TAG#v} # Remove 'v' prefix MAJOR=$(echo "$VERSION" | cut -d. -f1) MINOR=$(echo "$VERSION" | cut -d. -f2) PATCH=$(echo "$VERSION" | cut -d. -f3) # Increment version based on bump type case $BUMP_TYPE in major) MAJOR=$((MAJOR + 1)) MINOR=0 PATCH=0 ;; minor) MINOR=$((MINOR + 1)) PATCH=0 ;; patch) PATCH=$((PATCH + 1)) ;; esac NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}" NEW_TAG="v${NEW_VERSION}" echo "New version: $NEW_TAG ($BUMP_TYPE bump)" # Prompt for commit message if not provided if [ -z "$COMMIT_MSG" ]; then echo "" echo "Enter commit message (or press Enter for default: \"Release $NEW_TAG\"):" read -r COMMIT_MSG if [ -z "$COMMIT_MSG" ]; then COMMIT_MSG="Release $NEW_TAG" fi fi echo "Commit message: $COMMIT_MSG" # Update version.json cat > "$VERSION_FILE" << EOF { "VERSION": "$NEW_TAG", "VERSION_NUMBER": "$NEW_VERSION", "BUILD_DATE": "$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")" } EOF echo "Updated $VERSION_FILE" # Stage all changes (version file + any other modified/new files) git add . # Commit with the provided message git commit -m "$COMMIT_MSG" # Create new tag git tag "$NEW_TAG" echo "" echo "✅ Committed and tagged: $NEW_TAG" echo "" # Auto-push to remote git push origin master --tags echo "✅ Pushed to remote"