Skip to content

Commit bd84aed

Browse files
author
Denis Jelovina
committed
Add git hooks for version check and installation script
1 parent 7ed4574 commit bd84aed

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

.githooks/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Git hooks for ALP repository
2+
=============================
3+
4+
This directory contains local git hook scripts used by maintainers to catch
5+
common mistakes before pushing. They are not automatically installed by git.
6+
7+
Recommended installation (one-time per clone):
8+
9+
./scripts/install-git-hooks.sh
10+
11+
This sets `git config core.hooksPath .githooks` for the repository and
12+
makes the hook scripts executable.
13+
14+
pre-push hook
15+
-------------
16+
`.githooks/pre-push` prevents pushing tags of the form `pyalp.v*` when the
17+
`pyalp/pyproject.toml` version does not match the tag version. This avoids
18+
accidentally uploading mismatched packages to TestPyPI/PyPI.
19+
20+
Bypass
21+
------
22+
To bypass the check for automation or deliberate overrides, set the
23+
environment variable `GIT_ALLOW_VERSION_MISMATCH=1` before pushing.

.githooks/pre-push

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

scripts/install-git-hooks.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
ROOT_DIR=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
5+
HOOK_DIR="$ROOT_DIR/.githooks"
6+
7+
if [ ! -d "$HOOK_DIR" ]; then
8+
echo "Hook directory $HOOK_DIR not found."
9+
exit 1
10+
fi
11+
12+
echo "Installing git hooks by setting core.hooksPath to '$HOOK_DIR'"
13+
git config core.hooksPath "$HOOK_DIR"
14+
15+
echo "Making hook scripts executable"
16+
chmod +x "$HOOK_DIR"/* || true
17+
18+
echo "Installed. To bypass the version check temporarily, set:"
19+
echo " export GIT_ALLOW_VERSION_MISMATCH=1"

0 commit comments

Comments
 (0)