Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Catch plan citations at commit time

Juan Denisb1260f7ea5c8
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
index 2b0ab8fd..db67f8e1 100755
--- a/.githooks/pre-commit
+++ b/.githooks/pre-commit
@@ -1,13 +1,34 @@
#!/bin/sh
-# ServerKit pre-commit hook — lints staged frontend JS/JSX and blocks the commit
-# on ESLint ERRORS only (warnings are allowed). It exits early for commits that
-# touch no frontend JS/JSX (backend, SCSS, docs), so it never gets in the way of
-# those. Enable once per clone with:
+# ServerKit pre-commit hook — blocks internal plan citations in any staged app
+# file, then lints staged frontend JS/JSX and blocks the commit on ESLint
+# ERRORS only (warnings are allowed). The lint half exits early for commits
+# that touch no frontend JS/JSX (backend, SCSS, docs). Enable once per clone with:
#
# git config core.hooksPath .githooks
#
# (Or run frontend/setup-hooks.sh.)
+# Plan-citation guard: same patterns and allowlist as
+# backend/tests/test_no_plan_references.py, run on the staged blobs of every
+# file type (SCSS and Python included) so CI never has to catch it.
+# First interpreter that actually runs (Windows ships a python3 stub that
+# only opens the Store); none found means the CI test still covers it.
+py=""
+for candidate in python3 python; do
+ if "$candidate" -c 'import sys; sys.exit(sys.version_info < (3, 8))' 2>/dev/null; then
+ py=$candidate
+ break
+ fi
+done
+if [ -n "$py" ]; then
+ if ! "$py" backend/tests/test_no_plan_references.py --staged; then
+ echo ""
+ echo "✖ Commit blocked: remove the plan reference(s) above."
+ echo " To bypass for an emergency commit: git commit --no-verify"
+ exit 1
+ fi
+fi
+
files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^frontend/src/.*\.(js|jsx)$' || true)
[ -z "$files" ] && exit 0
diff --git a/backend/tests/test_no_plan_references.py b/backend/tests/test_no_plan_references.py
index df4ba244..d7b5b1a0 100644
--- a/backend/tests/test_no_plan_references.py
+++ b/backend/tests/test_no_plan_references.py
@@ -4,6 +4,10 @@ Plans live in a git-ignored folder, so "(plan 29 #9)" in a comment points
at nothing for anyone but its author — 680 of them had piled up, and one
reached users in a bottleneck hint. Say what the code does and why; the
history belongs in the commit message.
+
+The pre-commit hook runs this file directly (``--staged``) so a citation is
+caught at commit time, on any file type, instead of in a CI shard minutes
+after the push.
"""
import re
import subprocess
@@ -33,6 +37,11 @@ def _tracked():
return [p for p in out.decode().split('\0') if p and p not in EXCLUDED]
+def _hits(text):
+ return (len(PLAN_REF.findall(text)) + len(SPLIT_REF.findall(text))
+ + len(SECTION_REF.findall(text)))
+
+
def _counts():
found = {}
for rel in _tracked():
@@ -40,8 +49,27 @@ def _counts():
text = (REPO / rel).read_text(encoding='utf-8')
except (UnicodeDecodeError, FileNotFoundError):
continue
- hits = (len(PLAN_REF.findall(text)) + len(SPLIT_REF.findall(text))
- + len(SECTION_REF.findall(text)))
+ hits = _hits(text)
+ if hits:
+ found[rel] = hits
+ return found
+
+
+def _staged_counts():
+ """Hits in the staged (index) version of each staged file under ROOTS."""
+ out = subprocess.run(['git', 'diff', '--cached', '--name-only', '-z',
+ '--diff-filter=ACMR', '--', *ROOTS], cwd=REPO,
+ capture_output=True, check=True).stdout
+ found = {}
+ for rel in out.decode().split('\0'):
+ if not rel or rel in EXCLUDED:
+ continue
+ blob = subprocess.run(['git', 'show', f':{rel}'], cwd=REPO,
+ capture_output=True, check=True).stdout
+ try:
+ hits = _hits(blob.decode('utf-8'))
+ except UnicodeDecodeError:
+ continue
if hits:
found[rel] = hits
return found
@@ -58,3 +86,15 @@ def test_pending_list_is_not_stale():
counts = _counts()
stale = {rel: n for rel, n in PENDING.items() if counts.get(rel, 0) < n}
assert not stale, f'Lower or remove these PENDING entries: {stale} (now {counts})'
+
+
+if __name__ == '__main__':
+ import sys
+ counts = _staged_counts() if '--staged' in sys.argv else _counts()
+ over = {rel: n for rel, n in counts.items() if n > PENDING.get(rel, 0)}
+ for rel, n in sorted(over.items()):
+ print(f'{rel}: {n} internal plan reference(s), e.g. "plan 92" or a bare section sign')
+ if over:
+ print('Plans are git-ignored, so these point at nothing. Say what the code '
+ 'does instead; the history belongs in the commit message.')
+ sys.exit(1 if over else 0)