|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Get current branch name |
| 4 | +CURRENT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD) |
| 5 | + |
| 6 | +# Only check version bump on main branch |
| 7 | +if [ "$CURRENT_BRANCH" != "main" ]; then |
| 8 | + echo "ℹ️ Skipping version check (not on main branch)" |
| 9 | + exit 0 |
| 10 | +fi |
| 11 | + |
| 12 | +# Function to bump patch version |
| 13 | +bump_patch_version() { |
| 14 | + local current_version=$1 |
| 15 | + local major=$(echo "$current_version" | cut -d. -f1) |
| 16 | + local minor=$(echo "$current_version" | cut -d. -f2) |
| 17 | + local patch=$(echo "$current_version" | cut -d. -f3) |
| 18 | + local new_patch=$((patch + 1)) |
| 19 | + echo "$major.$minor.$new_patch" |
| 20 | +} |
| 21 | + |
| 22 | +# Determine if this is a pre-push or pre-commit hook |
| 23 | +# Check PRE_COMMIT_HOOK_TYPE environment variable set by pre-commit |
| 24 | +if [ "$PRE_COMMIT_FROM_REF" != "" ] || [ "$PRE_COMMIT_TO_REF" != "" ]; then |
| 25 | + # This is a pre-push hook - check if ANY files are being pushed |
| 26 | + REMOTE_REF="origin/main" |
| 27 | + |
| 28 | + # Check if there are any commits being pushed (any file changes) |
| 29 | + if ! git diff --quiet HEAD "$REMOTE_REF" 2>/dev/null; then |
| 30 | + # Get current version (what we're pushing) |
| 31 | + CURRENT_VERSION=$(git show HEAD:package.json | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/') |
| 32 | + |
| 33 | + # Get remote version (what's on origin) |
| 34 | + REMOTE_VERSION=$(git show "$REMOTE_REF":package.json 2>/dev/null | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/' || echo "0.0.0") |
| 35 | + |
| 36 | + # Extract patch versions |
| 37 | + CURRENT_PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3) |
| 38 | + REMOTE_PATCH=$(echo "$REMOTE_VERSION" | cut -d. -f3) |
| 39 | + |
| 40 | + if [ "$CURRENT_PATCH" -le "$REMOTE_PATCH" ]; then |
| 41 | + echo "❌ Error: patch version must be bumped before pushing to main" |
| 42 | + echo " Remote version: $REMOTE_VERSION" |
| 43 | + echo " Local version: $CURRENT_VERSION" |
| 44 | + echo "" |
| 45 | + echo " Cannot auto-bump during push. Please run:" |
| 46 | + echo " git reset --soft HEAD~1" |
| 47 | + echo " # Edit package.json to bump version" |
| 48 | + echo " git add package.json" |
| 49 | + echo " git commit" |
| 50 | + exit 1 |
| 51 | + else |
| 52 | + echo "✓ Patch version bumped: $REMOTE_VERSION → $CURRENT_VERSION" |
| 53 | + fi |
| 54 | + fi |
| 55 | +else |
| 56 | + # This is a pre-commit hook - check if ANY files are staged |
| 57 | + if ! git diff --cached --quiet; then |
| 58 | + # Get the version from package.json in working tree |
| 59 | + CURRENT_VERSION=$(cat package.json | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/') |
| 60 | + |
| 61 | + # Get the version from HEAD (last commit) |
| 62 | + HEAD_VERSION=$(git show HEAD:package.json 2>/dev/null | grep '"version"' | sed 's/.*"version": "\(.*\)".*/\1/' || echo "0.0.0") |
| 63 | + |
| 64 | + # Extract patch version (third number) |
| 65 | + CURRENT_PATCH=$(echo "$CURRENT_VERSION" | cut -d. -f3) |
| 66 | + HEAD_PATCH=$(echo "$HEAD_VERSION" | cut -d. -f3) |
| 67 | + |
| 68 | + # Check if patch version was bumped |
| 69 | + if [ "$CURRENT_PATCH" -le "$HEAD_PATCH" ]; then |
| 70 | + # Auto-bump the patch version |
| 71 | + NEW_VERSION=$(bump_patch_version "$HEAD_VERSION") |
| 72 | + |
| 73 | + echo "🔧 Auto-bumping patch version: $HEAD_VERSION → $NEW_VERSION" |
| 74 | + |
| 75 | + # Update package.json |
| 76 | + sed -i.bak "s/\"version\": \"$CURRENT_VERSION\"/\"version\": \"$NEW_VERSION\"/" package.json |
| 77 | + rm -f package.json.bak |
| 78 | + |
| 79 | + # Stage the updated package.json |
| 80 | + git add package.json |
| 81 | + |
| 82 | + echo "✓ Version bumped and staged" |
| 83 | + else |
| 84 | + echo "✓ Patch version already bumped: $HEAD_VERSION → $CURRENT_VERSION" |
| 85 | + fi |
| 86 | + fi |
| 87 | +fi |
| 88 | + |
| 89 | +exit 0 |
0 commit comments