hand the pre-push rules over to invisible-core

This repo's hook was 257 lines of shell, 207 of which were identical to the
manager's. It is a 55-line stub now: find an interpreter, hand over stdin.
The rules live in invisible_core.hooks, which this repo pins exactly, so
there is one implementation instead of three copies that drifted nine ways.

What this repo wants gated is declared in pyproject under
[tool.invisible.hooks] - the suite, the core pin, and v* as the release tag
prefix. The block is required rather than defaulted.

scripts/install_hooks.py is new here. It lived only in the core, so this
repo shipped a hook that nothing armed and nothing checked was armed: a
fresh clone pushed with every gate inert and said nothing about it. A test
now fails in an unarmed clone.

test_the_pre_push_hook_gates_release_tags used to read the hook's text for
a module name and a tag pattern, which made it a copy of the shell it was
checking - red once when the hook swapped a glob for a regex, red again
when the policy moved out of the shell, both times against a correct hook.
It drives the real policy now and checks the gate ran for THIS project.

.gitattributes pins the hook to LF so a Windows clone cannot produce a
shell script its own shebang dies on.
This commit is contained in:
feder-cr committed 2026-07-27 21:02:43 +02:00
1 parent b7bfb36452
commit 195c962dde
5 files changed
+134 -257

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
# The pre-push hook is a shell script, and git must never hand it to a POSIX
# shell with CRLF line endings: `/bin/sh^M: bad interpreter` is how a gate stops
# running on a machine where nothing else looks wrong. This repo is developed on
# Windows with core.autocrlf=true, so without this line a fresh Windows clone
# gets CRLF in the working tree - and Git-for-Windows' sh is the one that runs it.
#
# The index has always stored LF, so a Linux clone was never affected. That is
# exactly why this was worth pinning rather than leaving to a default that
# happens to be right today.
.githooks/** text eol=lf
# The publish ledger is compared byte for byte across an autocrlf checkout and a
# Linux one. The gate folds CRLF vs LF deliberately, but there is no reason to
# make it do that work.
PUBLISHED.json text eol=lf
+46 -248
View File
@@ -1,257 +1,55 @@
#!/bin/sh
# Pre-push hook: blocks push if the test suite isn't fully green.
# Pre-push gate. THE POLICY IS NOT IN THIS FILE - it is `invisible_core.hooks`,
# and this stub exists only to find an interpreter and hand over stdin.
#
# Enable once with:
# It used to be here, in three repositories, as 743 lines of shell that were 207
# identical lines out of 211 between two of them. Nine of the eleven differences
# were accidental: the core ran no pytest, the core set a variable three times
# that only the other two ever read, the core resolved an interpreter and then
# called bare `python` anyway. Every fix landed in whichever copy was open.
#
# What this repository wants gated is declared in its own pyproject, under
# [tool.invisible.hooks]. That is the only per-repo part, and it is data.
#
# Install once with:
# git config core.hooksPath .githooks
#
# Bypass for a known-broken WIP push (NOT for releases):
# git push --no-verify
# The --no-verify flag is the only escape hatch. Use it sparingly and never
# for branches that feed into a release.
# A test in this repo fails in an uninstalled checkout, so nobody has to
# remember. Bypass a known-broken WIP push with `git push --no-verify`; that is
# the only escape hatch, and never for a branch that feeds a release.
set -e
# No `set -e`: exit codes here have to be READ and explained, not turned into a
# silent abort. A gate that dies without a message is the worst shape of all.
echo "[pre-push] running unit + integration tests before push..."
# Run from the repository root regardless of where `git push` was invoked.
cd "$(dirname "$0")/.." || exit 1
# Run from this script's directory so it works regardless of where the user
# invoked git push from.
cd "$(dirname "$0")/.."
# Default pyproject addopts skip slow/e2e. That's the gate we want for every
# push, fast feedback. e2e is reserved for explicit release runs.
if ! python -m pytest -q --tb=short; then
echo ""
echo "[pre-push] TESTS FAILED - push aborted."
echo "[pre-push] Either fix the failure or use 'git push --no-verify' if"
echo "[pre-push] you really know what you're doing (NOT for release branches)."
python_bin=""
for candidate in "$INVISIBLE_GATE_PYTHON" python3 python py; do
[ -n "$candidate" ] || continue
if command -v "$candidate" >/dev/null 2>&1; then
python_bin="$candidate"
break
fi
done
if [ -z "$python_bin" ]; then
echo "[pre-push] REFUSED - no python on PATH, so no gate can run." >&2
echo "[pre-push] Nothing must be pushed past a hook that could not check it." >&2
echo "[pre-push] Set INVISIBLE_GATE_PYTHON to an interpreter and push again." >&2
exit 1
fi
# The invisible-core== specifier in pyproject.toml must name the version the
# core checkout actually builds. The core's version is derived from its release
# seal, so it moves on its own when a new engine is rolled in; this pin does
# not. Checked here because a wrong pin is not visible to the test suite: it
# only shows up at install time, on someone else's machine.
#
# The checker lives in the local workbench, one level above this checkout, and
# is not part of this repository. It used to be wrapped in a bare
# `if [ -f "$PIN_CHECK" ]`, which skipped with NO output and then fell through
# to a line reading "all tests green - push proceeding". Renaming or moving the
# workbench, or pushing from a worktree at a different depth, disarmed the gate
# and printed something that looks exactly like success. A gate that cannot run
# is now a refusal, and the only way past it is to say so out loud.
PIN_CHECK="${INVISIBLE_PIN_CHECK:-../../scripts/sync_core_pin.py}"
PIN_SKIPPED=""
if [ "$INVISIBLE_PIN_CHECK" = "skip" ]; then
echo ""
echo "[pre-push] ###################################################################"
echo "[pre-push] # WARNING: INVISIBLE_PIN_CHECK=skip #"
echo "[pre-push] # The invisible-core pin was NOT compared against the core. If it #"
echo "[pre-push] # is stale, every install of this package resolves a core it was #"
echo "[pre-push] # never tested against, and no test downstream can see it. #"
echo "[pre-push] ###################################################################"
echo ""
PIN_SKIPPED=yes
elif [ ! -f "$PIN_CHECK" ]; then
echo "" >&2
echo "[pre-push] REFUSED - the invisible-core pin gate is not reachable." >&2
echo "[pre-push] looked for: $PIN_CHECK" >&2
echo "[pre-push] from: $(pwd)" >&2
echo "[pre-push] The workbench moved, or this is a clone/worktree at another" >&2
echo "[pre-push] depth. Point INVISIBLE_PIN_CHECK at sync_core_pin.py, or push" >&2
echo "[pre-push] with INVISIBLE_PIN_CHECK=skip to state on the record that the" >&2
echo "[pre-push] pin is going out unchecked." >&2
exit 1
else
# The gate's own known-bad cases, before the gate's verdict is trusted. They
# live next to it and take about a second. Nothing else in either repo runs
# them, so without this they are 18 cases that execute when a human
# remembers - which is a runbook note wearing a gate's clothes, the same
# shape as the silent skip above.
PIN_TESTS="$(dirname "$PIN_CHECK")/test_sync_core_pin.py"
if [ ! -f "$PIN_TESTS" ]; then
echo "" >&2
echo "[pre-push] REFUSED - $PIN_TESTS is missing, so the pin gate is" >&2
echo "[pre-push] unverified. A gate that has only ever printed PASS is not a" >&2
echo "[pre-push] gate; do not trust this one until its own cases run." >&2
exit 1
fi
echo "[pre-push] running the pin gate's own known-bad cases..."
if ! PIN_TEST_OUT="$(python "$PIN_TESTS" 2>&1)"; then
echo "$PIN_TEST_OUT" >&2
echo "" >&2
echo "[pre-push] REFUSED - the pin gate's own test suite is RED, so its" >&2
echo "[pre-push] verdict on the pin means nothing. Fix $PIN_TESTS first." >&2
exit 1
fi
echo "[pre-push] $(echo "$PIN_TEST_OUT" | tail -n 1)"
echo "[pre-push] checking the invisible-core pin..."
if ! python "$PIN_CHECK" --check; then
echo ""
echo "[pre-push] the invisible-core pin does not match the core - push aborted."
echo "[pre-push] Fix it with: python $PIN_CHECK"
exit 1
fi
fi
# ── forbidden names ────────────────────────────────────────────────────
# Naming a protection vendor or a target site in a public repo is a legal and
# credibility problem, and no test in this package covers it. It cannot be a
# test here either: the scanner needs a word list that deliberately lives
# outside every public repo.
#
# THREE cases, kept apart on purpose. The first cut of this had only two and
# refused whenever the script was not at the default path - which meant every
# clone of the PUBLIC repo was blocked on every push with no way to comply
# except switching the gate off. That is the same "red by default, so people
# route around it" failure the guard was moved out of the test suite to avoid.
#
# configured (INVISIBLE_NAME_CHECK set) but unreachable -> REFUSE. Somebody
# meant this to run and it did not.
# present at the default path -> RUN, refuse on any
# non-zero: the script exits 2 rather than 0 when it could not scan.
# neither -> this is not the
# workbench; there is no word list here and nothing to enforce. Say so in
# one line and carry on. Silence would be the actual failure.
NAME_CHECK="${INVISIBLE_NAME_CHECK:-../../scripts/check_forbidden_names.py}"
NAME_SKIPPED=""
if [ "$INVISIBLE_NAME_CHECK" = "skip" ]; then
echo ""
echo "[pre-push] ###################################################################"
echo "[pre-push] # WARNING: INVISIBLE_NAME_CHECK=skip #"
echo "[pre-push] # Nothing checked this push for vendor or target-site names. If #"
echo "[pre-push] # one is in the diff it is public the moment this lands, and a #"
echo "[pre-push] # force-push does not remove it - GitHub keeps the object. #"
echo "[pre-push] ###################################################################"
echo ""
NAME_SKIPPED=yes
elif [ -n "$INVISIBLE_NAME_CHECK" ] && [ ! -f "$NAME_CHECK" ]; then
echo "" >&2
echo "[pre-push] REFUSED - INVISIBLE_NAME_CHECK is set but points at nothing." >&2
echo "[pre-push] looked for: $NAME_CHECK" >&2
echo "[pre-push] from: $(pwd)" >&2
echo "[pre-push] Fix the path, or set INVISIBLE_NAME_CHECK=skip to state on the" >&2
echo "[pre-push] record that this push is going out unscanned." >&2
exit 1
elif [ ! -f "$NAME_CHECK" ]; then
echo "[pre-push] no forbidden-name word list here, so nothing scanned this push."
echo "[pre-push] (that scan is a maintainer gate; it lives outside this repo.)"
NAME_SKIPPED=yes
else
# git feeds the refs being pushed on stdin, one line per ref:
# "<local ref> <local sha> <remote ref> <remote sha>"
# Read here because this is the only gate that needs it, and guarded on a
# tty so that running the hook by hand does not hang waiting for input.
if [ -t 0 ]; then
PUSH_REFS=""
else
PUSH_REFS="$(cat)"
fi
# An all-zero REMOTE sha means the remote has never seen this branch, so
# there is no old..new to take: everything not already on a remote is new.
# An all-zero LOCAL sha is a branch deletion, which pushes no content.
RANGE=""
if [ -n "$PUSH_REFS" ]; then
RANGE=$(printf '%s
' "$PUSH_REFS" | awk '
$2 ~ /^0+$/ { next }
$4 ~ /^0+$/ { print $2 " --not --remotes"; exit }
{ print $4 ".." $2; exit }')
fi
run_name_check() {
if [ -n "$RANGE" ]; then
python "$NAME_CHECK" . --range "$RANGE"
else
python "$NAME_CHECK" .
fi
}
# The gate on the gate. Nothing tested the scanner until 2026-07-27, and it
# was returning a clean verdict over three of the four surfaces a public
# repo actually publishes - commit messages among them, which is where ten
# of our own names went out.
NAME_TEST="$(dirname "$NAME_CHECK")/test_check_forbidden_names.py"
if [ -f "$NAME_TEST" ] && ! python "$NAME_TEST"; then
echo "" >&2
echo "[pre-push] REFUSED - the name scanner's own cases are red, so its" >&2
echo "[pre-push] verdict on this push would mean nothing." >&2
exit 1
fi
if [ -n "$RANGE" ]; then
echo "[pre-push] scanning files + the commit messages in $RANGE ..."
else
echo "[pre-push] scanning files for names we do not publish."
echo "[pre-push] NOTE: no push range on stdin, so COMMIT MESSAGES were not"
echo "[pre-push] scanned. A message cannot be reworded once it is pushed."
fi
if ! run_name_check; then
echo "" >&2
echo "[pre-push] REFUSED - see above. The offending files are named; the" >&2
echo "[pre-push] tokens are not, so this log is safe to paste." >&2
exit 1
fi
fi
# ── publish gate (release tags only) ───────────────────────────────────
# Until 2026-07-27 this repo had NO gate: a release was a bare `twine upload`
# of whatever was in a directory. That is how 0.4.4 reached the index built
# from a tree that predated the fix it was supposed to carry - and a PyPI
# filename is never re-uploaded, so it is wrong forever.
#
# The gate lives in the core, which this package pins exactly, so it is always
# importable. `publish` builds and uploads WHAT IT JUST BUILT; that is the
# property that makes the failure above impossible.
#
# Only release tags are gated: the gate builds the project twice, and a hook
# that costs a minute on every push is a hook people delete.
release_tag=""
if [ -n "$PUSH_REFS" ]; then
release_tag=$(printf '%s
' "$PUSH_REFS" | awk '
$3 ~ /^refs\/tags\/v/ { print $3; exit }')
fi
if [ -n "$release_tag" ]; then
echo "[pre-push] $release_tag is a release tag, running the publish gate..."
# --project-root is a TOP-LEVEL option, before the subcommand. Putting it
# after made argparse exit 2, which this block read as a refusal - so the
# hook printed REFUSED without the gate ever having run, and the first
# "verification" of this wiring was itself wrong.
# `if` rather than a bare call: these hooks run under `set -e`, so a command
# exiting non-zero aborts the script BEFORE `$?` can be read - and exit 5
# here is a NO-OP, not a failure. The result was a push that died with no
# message at all, which is the worst shape a gate can fail in.
if python -m invisible_core.release --project-root . check --verify-index; then
gate_rc=0
else
gate_rc=$?
fi
# 5 is ALREADY PUBLISHED and byte-identical: nothing to do, not a refusal.
# It is what re-pushing or back-filling a release tag legitimately produces,
# and the gate gives it its own code precisely so a caller can tell it from
# "the content changed under an unmoved version", which must stay fatal.
if [ "$gate_rc" -ne 0 ] && [ "$gate_rc" -ne 5 ]; then
echo "" >&2
echo "[pre-push] REFUSED (gate exit $gate_rc) - see above. A version whose" >&2
echo "[pre-push] content moved needs a NEW version number: a PyPI filename is" >&2
echo "[pre-push] never re-uploaded, so a wrong artifact stays wrong forever." >&2
exit 1
fi
fi
# The closing line never claims more than what actually ran.
if [ -n "$PIN_SKIPPED" ]; then
echo "[pre-push] tests green, PIN GATE SKIPPED - push proceeding (see the warning above)."
else
if [ -n "$NAME_SKIPPED" ]; then
echo "[pre-push] tests green, pin matches, NAME SCAN SKIPPED - push proceeding."
else
echo "[pre-push] all tests green, pin matches, no forbidden names - push proceeding."
fi
fi
exit 0
# `src` first so a checkout gates itself with the policy in the tree being
# pushed. os.getcwd() rather than $PWD on purpose: under git-bash on Windows
# $PWD is /c/... and Python does not resolve that mount.
# exec, so stdin (the refs git is pushing) reaches the policy untouched.
exec "$python_bin" -c 'import os, sys
sys.path.insert(0, os.path.join(os.getcwd(), "src"))
try:
from invisible_core.hooks import main
except ImportError as exc:
sys.exit("[pre-push] REFUSED - cannot import invisible_core.hooks (%s).\n"
"[pre-push] The pre-push policy lives in invisible-core. This repo is\n"
"[pre-push] either that package (so src/ is not where it should be) or\n"
"[pre-push] pins it exactly (so install it). Either way, nothing is\n"
"[pre-push] pushed past a hook that could not check it." % exc)
sys.exit(main())'
+13
View File
@@ -120,3 +120,16 @@ include = ["src/invisible_playwright", "tests", "README.md", "LICENSE", "pyproje
# public repo, so as a shipped test it handed every user three red tests they
# could not make green. It is a workbench gate now, run by the pre-push hook.
exclude = ["tests/playwright-upstream"]
# Which pre-push gates this repository wants. `.githooks/pre-push` is a stub;
# the policy is invisible_core.hooks, and this block is the only per-repo part
# of it. Declared rather than defaulted on purpose: the wrong default silently
# skips a gate, and a skipped gate reads exactly like a passed one.
#
# `pin = true`: the invisible-core== specifier below must name the version the
# core checkout actually builds. No test here can see a stale pin - it only
# shows up at install time, on someone else's machine.
[tool.invisible.hooks]
pytest = true
pin = true
release_tags = ["v"]
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
"""Point this clone's git hooks at .githooks/, so the pre-push gates are armed.
Back-compat shim. The implementation is `invisible_core.hooks.install_main`,
moved there on 2026-07-27 because it existed here and therefore existed for ONE
of the three repositories: the other two shipped a hook that nothing armed, and
nothing checked was armed, so a fresh clone of either had no gates at all and no
way to notice. Same asymmetry, and the same fix, as the publish gate.
python scripts/install_hooks.py # arm this clone
python scripts/install_hooks.py --check # exit 1 if it is not armed
Nobody has to remember: a test in each repo fails in a checkout whose hooksPath
is not set, and prints this command. The suite is the reminder, and it runs on
its own.
"""
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
# No src insert: this repo pins invisible-core exactly, so the shared
# implementation is simply importable.
from invisible_core.hooks import install_main # noqa: E402
if __name__ == "__main__":
raise SystemExit(install_main(sys.argv[1:], root=ROOT))
+33 -9
View File
@@ -49,15 +49,23 @@ def test_the_gate_recognises_THIS_project_and_not_the_core():
@pytest.mark.skipif(not _HOOK.exists(), reason="not a source checkout")
def test_the_pre_push_hook_gates_release_tags():
text = _HOOK.read_text(encoding="utf-8")
assert "invisible_core.release" in text, (
"the pre-push hook does not run the publish gate, so a release tag can "
"be pushed - and published - with no check at all")
# The slashes are escaped inside the awk pattern, so strip backslashes
# before looking. Asserting the raw form failed against a hook that was
# perfectly correct - a test reading the implementation's punctuation.
assert "/tags/v" in text.replace("\\", ""), (
"the hook does not recognise a release tag")
"""Driven, not read.
This used to grep the hook for `invisible_core.release` and for a tag
pattern, and it was wrong twice for the same reason: it was a copy of the
shell it was checking. It went red when the hook swapped a glob for an awk
regex, and again when the policy moved out of the shell into
`invisible_core.hooks` - both times against a hook that was correct, and
both times leaving the actual property unasserted.
The helper pushes a release tag through the real policy with a recording
runner and checks the gate was invoked FOR THIS PROJECT, then pushes a
branch and checks it was not. Its write-up lives once, in the core, because
all three repos make the same claim.
"""
from invisible_core.testing import assert_pre_push_policy_is_wired
assert_pre_push_policy_is_wired(_REPO)
@pytest.mark.skipif(not (_REPO / "PUBLISHED.json").exists(),
@@ -76,3 +84,19 @@ def test_the_ledger_records_what_actually_shipped():
f"entry {entry['version']} names a wheel from another version: "
f"{entry['wheel_filename']}")
assert entry["wheel"]["files"], f"{entry['version']}: no file table"
# ------------------------------------------------------- the clone is armed
def test_the_hooks_are_armed_in_this_clone():
"""A checked-in hook directory does nothing until core.hooksPath points at
it, and git will not do that for you.
Until 2026-07-27 both the installer and this reminder lived in the core, so
THIS repo shipped a hook that nothing armed and nothing checked was armed:
a fresh clone pushed with the tests, the pin, the name scan and the publish
gate all skipped, and said nothing about it.
"""
from invisible_core.testing import assert_hooks_are_armed
assert_hooks_are_armed(_REPO)