Repository navigation
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·60 lines (55 loc) · 2.72 KB
/
Copy pathpre-commit
File metadata and controls
executable file
·60 lines (55 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
#
# Pre-commit hook:
# 1. Runs PostgreSQL migration compatibility tests when migration-related
# files are staged.
# 2. Keeps the VERSION file in sync with the README version badge so the
# About page in the app never lags behind a release commit.
#
# Install: ln -sf ../../scripts/pre-commit .git/hooks/pre-commit
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM)
# ---------------------------------------------------------------------------
# 1. Migration compatibility check
# ---------------------------------------------------------------------------
if echo "$STAGED_FILES" | grep -qE '^(src/init_db\.py|src/utils/database\.py|src/models/)'; then
echo "Migration files changed — running PostgreSQL compatibility checks..."
python tests/test_migration_compatibility.py
if [ $? -ne 0 ]; then
echo ""
echo "Pre-commit hook FAILED: PostgreSQL migration compatibility issues found."
echo "Fix the issues above before committing."
exit 1
fi
echo "All migration compatibility checks passed."
fi
# ---------------------------------------------------------------------------
# 2. VERSION-file sync
# ---------------------------------------------------------------------------
# Source of truth: the version inside the README shields.io badge
# https://img.shields.io/badge/version-X.Y.Z[-suffix]-<colour>.svg
# When that badge moves (typical release commit edit), or when somebody
# touches VERSION directly, we make sure both files end up with the same
# value and re-stage them so the commit captures it.
if echo "$STAGED_FILES" | grep -qE '^(README\.md|VERSION)$'; then
if [ -f README.md ] && [ -f VERSION ]; then
# Extract version from README badge. Accepts X.Y.Z or X.Y.Z-suffix.
# A literal dash in the version (e.g. the "-alpha" suffix) is escaped
# as "--" in the shields.io badge so the badge renders instead of
# 404ing; collapse it back to a single dash for the VERSION file.
README_VER=$(grep -oE 'shields\.io/badge/version-[0-9]+\.[0-9]+\.[0-9]+[A-Za-z0-9.-]*-[a-z]+\.svg' README.md \
| head -1 \
| sed -E 's|shields\.io/badge/version-([0-9]+\.[0-9]+\.[0-9]+[A-Za-z0-9.-]*)-[a-z]+\.svg|\1|' \
| sed 's|--|-|g')
if [ -z "$README_VER" ]; then
echo "Version-sync skipped: could not parse version from README badge."
else
EXPECTED="v${README_VER}"
CURRENT=$(tr -d '[:space:]' < VERSION)
if [ "$CURRENT" != "$EXPECTED" ]; then
echo "Syncing VERSION file: $CURRENT → $EXPECTED (from README badge)"
printf '%s\n' "$EXPECTED" > VERSION
git add VERSION
fi
fi
fi
fi