|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# library-api Version Validation Script |
| 4 | +# Ensures pom.xml version matches git tag |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 10 | +LIBRARY_API_DIR="$REPO_ROOT/library-api" |
| 11 | +POM_FILE="$LIBRARY_API_DIR/pom.xml" |
| 12 | + |
| 13 | +# Colors for output |
| 14 | +RED='\033[0;31m' |
| 15 | +GREEN='\033[0;32m' |
| 16 | +YELLOW='\033[1;33m' |
| 17 | +NC='\033[0m' # No Color |
| 18 | + |
| 19 | +print_error() { |
| 20 | + echo -e "${RED}ERROR: $1${NC}" >&2 |
| 21 | +} |
| 22 | + |
| 23 | +print_success() { |
| 24 | + echo -e "${GREEN}✓ $1${NC}" |
| 25 | +} |
| 26 | + |
| 27 | +print_warning() { |
| 28 | + echo -e "${YELLOW}⚠ $1${NC}" |
| 29 | +} |
| 30 | + |
| 31 | +# Check if we're in a git repository |
| 32 | +if ! git rev-parse --git-dir > /dev/null 2>&1; then |
| 33 | + print_error "Not in a git repository" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +# Check if pom.xml exists |
| 38 | +if [ ! -f "$POM_FILE" ]; then |
| 39 | + print_error "pom.xml not found at $POM_FILE" |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# Check if Maven is available |
| 44 | +if ! command -v mvn &> /dev/null; then |
| 45 | + print_error "Maven (mvn) not found. Please install Maven or use devbox." |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +# Extract version from pom.xml using Maven |
| 50 | +cd "$LIBRARY_API_DIR" |
| 51 | +POM_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout 2>/dev/null || echo "") |
| 52 | + |
| 53 | +if [ -z "$POM_VERSION" ]; then |
| 54 | + print_error "Could not extract version from pom.xml" |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +# Expected git tag based on pom.xml version |
| 59 | +EXPECTED_TAG="library-api-v${POM_VERSION}" |
| 60 | + |
| 61 | +# Check if we're being called in a pre-push hook context |
| 62 | +# In that case, check if a library-api-v* tag is being pushed |
| 63 | +if [ -n "$1" ] && [ "$1" = "--pre-push-hook" ]; then |
| 64 | + # Read stdin to get refs being pushed |
| 65 | + TAG_BEING_PUSHED="" |
| 66 | + while read local_ref local_sha remote_ref remote_sha; do |
| 67 | + # Check if it's a tag push matching library-api-v* |
| 68 | + if [[ "$remote_ref" =~ refs/tags/library-api-v.* ]]; then |
| 69 | + TAG_BEING_PUSHED="${remote_ref#refs/tags/}" |
| 70 | + |
| 71 | + # Validate the tag matches pom.xml version |
| 72 | + if [ "$TAG_BEING_PUSHED" != "$EXPECTED_TAG" ]; then |
| 73 | + echo "" |
| 74 | + print_error "Version mismatch detected!" |
| 75 | + echo "" |
| 76 | + echo " Tag being pushed: $TAG_BEING_PUSHED" |
| 77 | + echo " Expected tag: $EXPECTED_TAG" |
| 78 | + echo " pom.xml version: $POM_VERSION" |
| 79 | + echo "" |
| 80 | + echo "To fix this issue:" |
| 81 | + echo " 1. Delete the incorrect tag: git tag -d $TAG_BEING_PUSHED" |
| 82 | + echo " 2. Use the helper script: cd library-api && ./bin/tag-release $POM_VERSION" |
| 83 | + echo "" |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + |
| 87 | + print_success "Version validation passed: $TAG_BEING_PUSHED matches pom.xml version $POM_VERSION" |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | + done |
| 91 | + |
| 92 | + # No library-api tag being pushed, validation not needed |
| 93 | + exit 0 |
| 94 | +fi |
| 95 | + |
| 96 | +# Manual validation mode (not in pre-push hook) |
| 97 | +# Check if the expected tag exists locally or remotely |
| 98 | +TAG_EXISTS_LOCAL=$(git tag -l "$EXPECTED_TAG") |
| 99 | +TAG_EXISTS_REMOTE=$(git ls-remote --tags origin "$EXPECTED_TAG" 2>/dev/null | grep -c "$EXPECTED_TAG" || echo "0") |
| 100 | + |
| 101 | +if [ -n "$TAG_EXISTS_LOCAL" ]; then |
| 102 | + print_success "Local tag $EXPECTED_TAG exists and matches pom.xml version $POM_VERSION" |
| 103 | + exit 0 |
| 104 | +elif [ "$TAG_EXISTS_REMOTE" -gt 0 ]; then |
| 105 | + print_success "Remote tag $EXPECTED_TAG exists and matches pom.xml version $POM_VERSION" |
| 106 | + exit 0 |
| 107 | +else |
| 108 | + print_warning "No git tag found for current pom.xml version" |
| 109 | + echo "" |
| 110 | + echo " pom.xml version: $POM_VERSION" |
| 111 | + echo " Expected tag: $EXPECTED_TAG" |
| 112 | + echo "" |
| 113 | + echo "To create a release tag:" |
| 114 | + echo " cd library-api && ./bin/tag-release $POM_VERSION" |
| 115 | + echo "" |
| 116 | + exit 1 |
| 117 | +fi |
0 commit comments