|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Git pre-push hook to prevent pushing tags of the form pyalp.v* when |
| 3 | +# pyalp/pyproject.toml version hasn't been updated accordingly. |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +# Allow bypass via environment variable (useful for automation or deliberate overrides) |
| 7 | +if [ "${GIT_ALLOW_VERSION_MISMATCH:-}" = "1" ] || [ "${SKIP_PYALP_VERSION_CHECK:-}" = "1" ]; then |
| 8 | + exit 0 |
| 9 | +fi |
| 10 | + |
| 11 | +REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo "") |
| 12 | +if [ -n "$REPO_ROOT" ]; then |
| 13 | + cd "$REPO_ROOT" |
| 14 | +fi |
| 15 | + |
| 16 | +readarray -t LINES || true |
| 17 | +if [ ${#LINES[@]} -eq 0 ]; then |
| 18 | + # No refs being pushed |
| 19 | + exit 0 |
| 20 | +fi |
| 21 | + |
| 22 | +py_version="" |
| 23 | +if [ -f pyalp/pyproject.toml ]; then |
| 24 | + py_version=$(grep -E '^version\s*=\s*"' pyalp/pyproject.toml | head -n1 | sed -E 's/^version\s*=\s*"([^\"]+)".*/\1/' || true) |
| 25 | +fi |
| 26 | + |
| 27 | +err=0 |
| 28 | +for line in "${LINES[@]}"; do |
| 29 | + # input format: <local_ref> <local_sha> <remote_ref> <remote_sha> |
| 30 | + read -r local_ref local_sha remote_ref remote_sha <<<"$line" |
| 31 | + if [[ "$remote_ref" =~ ^refs/tags/pyalp\.v(.+)$ ]]; then |
| 32 | + tag_ver="${BASH_REMATCH[1]}" |
| 33 | + if [ -z "$py_version" ]; then |
| 34 | + echo "ERROR: pushing tag 'pyalp.v${tag_ver}' but could not determine pyalp/pyproject.toml version." >&2 |
| 35 | + echo "Please update pyalp/pyproject.toml (version = \"${tag_ver}\") or set GIT_ALLOW_VERSION_MISMATCH=1 to bypass." >&2 |
| 36 | + err=1 |
| 37 | + elif [ "$tag_ver" != "$py_version" ]; then |
| 38 | + echo "ERROR: Tag version mismatch: pushing 'pyalp.v${tag_ver}' but pyalp/pyproject.toml contains version '${py_version}'." >&2 |
| 39 | + echo "Update pyalp/pyproject.toml to match the tag, or set GIT_ALLOW_VERSION_MISMATCH=1 to bypass." >&2 |
| 40 | + err=1 |
| 41 | + else |
| 42 | + echo "OK: tag pyalp.v${tag_ver} matches pyalp/pyproject.toml" |
| 43 | + fi |
| 44 | + fi |
| 45 | +done |
| 46 | + |
| 47 | +if [ "$err" -ne 0 ]; then |
| 48 | + echo "Push aborted due to version mismatch." >&2 |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +exit 0 |
0 commit comments