#!/bin/sh
# Git post-commit hook: auto-tag when version changes

# Get version from current commit
CURRENT=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')

# Get version from previous commit (if exists)
PREVIOUS=$(git show HEAD^:Cargo.toml 2>/dev/null | grep '^version = ' | head -1 | sed 's/version = "\(.*\)"/\1/')

# If version changed and tag doesn't exist, create it
if [ -n "$CURRENT" ] && [ "$CURRENT" != "$PREVIOUS" ]; then
    TAG="v$CURRENT"
    if ! git tag -l | grep -q "^$TAG$"; then
        echo "Version changed: $PREVIOUS -> $CURRENT"
        git tag "$TAG"
        echo "Created tag: $TAG"
        echo ""
        echo "To push: git push origin main --tags"
    fi
fi
