Skip to content

Feature/pre commit hooks - #857

Open
jurretanjamx wants to merge 15 commits into
MendixMau:masterfrom
jurretanjamx:feature/pre-commit-hooks
Open

jurretanjamx wants to merge 15 commits into
MendixMau:masterfrom
jurretanjamx:feature/pre-commit-hooks

Conversation

@jurretanjamx

Copy link
Copy Markdown

Here's the PR description draft:


Title: Run a project's git hooks through pre-commit, installed for you

Summary

Projects that use the toolkit get their git hooks from bin/install-project-hooks.sh:

  • the model-verification check on commit;
  • the full build before push;
  • the Studio Pro revision notes;
  • the restatement-ledger marker.

Until now these were plain scripts in .git/hooks. A team's own hook was moved aside to .pre-mxtk and chained, and there was no standard place to add more hooks.

With this PR, the hooks run through pre-commit (https://pre-commit.com):

  • One standard config. The toolkit's hooks are a # mxtk-managed block of local hooks in the project's .pre-commit-config.yaml. Teams put their own hooks (linters, formatters, secret scanners) in the same file, outside the block.
  • Installed automatically. The installer finds pre-commit or installs it: brew on macOS, then pipx, uv, and pip --user, which it skips for an externally managed Python. It never uses sudo. It asks [Y/n] when run in a terminal, and doesn't ask agents or CI. MXTK_INSTALL_PRECOMMIT=0 opts out.
  • Older projects move over on their next sync-project.sh. --check fails while a clone could use pre-commit, so sync runs the installer, the same way it already refreshes stale hooks.
  • Existing configs are respected. A missing config is created. In an existing one, the block goes into the repos: list at the file's own indent. The file is only written if pre-commit validate-config accepts the result, and later runs replace only the block. If the result would be invalid, the file isn't touched, the snippet is printed, and plain hooks are written.
  • Team hooks keep running. A team's own hook becomes pre-commit's .legacy. The exception is post-rewrite: pre-commit gives a legacy post-rewrite hook no input, so that type stays a plain chain.
  • No pre-commit, no change. Where pre-commit can't be installed, the plain hooks are written byte-identical to master's.

How it works

  • All six hook types (pre-commit, pre-push, post-commit, post-rewrite, post-merge, post-checkout) are installed with pre-commit install --allow-missing-config, so an older branch without the config can still commit.
  • The YAML entries call install-project-hooks.sh --run , which runs the same hook text as the plain hooks. The hook code has one source, and no new file needs syncing.
  • pre-commit passes on only part of git's input, so --run rebuilds what the hooks read:
    • pre-push: pre-commit passes only the first pushed branch. The hook now checks every local branch instead. That's a superset of the push, so a full build that's due is never skipped.
    • post-rewrite: pre-commit doesn't pass the list of rewritten commits. After an amend the hook notes HEAD. After a rebase it notes the commits in ORIG_HEAD..HEAD that have no revision note yet.
    • post-merge / post-checkout: these map directly onto PRE_COMMIT_IS_SQUASH_MERGE, PRE_COMMIT_FROM_REF, PRE_COMMIT_TO_REF and PRE_COMMIT_CHECKOUT_TYPE.
  • A clone on pre-commit stays on it. If pre-commit goes missing, --check fails and says so, and commits are refused until it's reinstalled. Nothing switches back to plain hooks.

Known risks and limits

  • pre-commit stashes unstaged changes while hooks run. That includes a .mpr or mprcontents/ that Studio Pro may have open. The README says to save in Studio Pro and stage the model before committing from the command line.
  • Studio Pro may not run git hooks at all. It commits through libgit2. This was already true before this PR, and it's logged in bug-logs/open-question-studio-pro-git-hooks.md.
  • Claude Code sandbox. pre-commit writes to ~/.cache/pre-commit. A sandbox that only allows writes inside the project could make an agent's commit fail. Not tested.

Also in this PR

  • Drops the earlier change on this branch that set up pre-commit for the toolkit's own clone: the root .pre-commit-config.yaml, the bin/install-hooks.sh rewrite, its fixture and the README badge.
  • templates/pre-commit-config.yaml: an example config, generated with --print-config.
  • README: a "Git hooks" item in "What it tests in your app".
  • Seven fixtures that check the plain hooks (test-model-stamp, test-restatement-git-hooks, test-mx-metadata-note, test-build-tree, test-exec-gate-kind, test-module-close-cadence, test-sync-only-bin) and tests/wave2/run-all.sh export MXTK_INSTALL_PRECOMMIT=0 MXTK_PRECOMMIT_BIN=none, so pre-commit stays out of test runs.
  • A CHANGELOG line under Unreleased.

Testing

  • By hand on macOS with pre-commit 4.6.2, in scratch repos:
    • a project on the old plain hooks switches over, and a team post-merge hook keeps running as .legacy;
    • commit, amend, merge, file checkout, rebase and push each run the right hook with the right input;
    • adding to an existing YAML with a ci: block after repos: validates, and a re-run changes nothing;
    • an invalid YAML is never edited;
    • a clone whose pre-commit is gone fails --check, and the installer refuses with a message.
  • Installed on two repos by a toolkit user: one with no pre-commit config, one with its own.
  • The plain hooks are byte-identical to master's where pre-commit isn't available.
  • check-scripts, check-portability, check-no-private-citations, the leak guard and render-routing --check pass. The baseline is unchanged at 64,164 of 80,000 words, because only README.md changed and it isn't part of the baseline.

🤖 Generated with Claude Code (https://claude.com/claude-code)

jurretanjamx and others added 7 commits December 1, 2026 12:01
Added pre-commit.ci status badge to README.
Added clarification on README purpose and project overview.
…talled for you

install-project-hooks.sh finds or installs pre-commit (brew, pipx, uv, pip --user;
never sudo; opt out with MXTK_INSTALL_PRECOMMIT=0) and writes a "# mxtk-managed" block
into .pre-commit-config.yaml, so teams can keep their own hooks next to the toolkit's in
one standard file. An older project moves over on its next sync-project.sh. Where
pre-commit is not available, the plain hooks are written byte-identical to before.

Drops the earlier toolkit-clone change on this branch (root .pre-commit-config.yaml,
bin/install-hooks.sh rewrite, its fixture, the README badge): the target is the
projects that use the toolkit, not the toolkit's own clone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MendixMau

Copy link
Copy Markdown
Owner

Outside PR marker: DISCUSS — a new third-party dependency, auto-installed on every synced project, and the default hook install path changes

Review: HOLD — --check now fails on every project without pre-commit (project-bin/install-project-hooks.sh:625), so every sync-project.sh (bin/sync-project.sh:1414) retries a network install, unprompted for agents and CI, and never reports clean.

Value
The aim is sound: one config file for team hooks, and one source for the hook bodies (emit_*/--run). Safety:

  • No hidden or bidi characters.
  • Every hook is repo: local/language: system, so pre-commit fetches no hook repos (templates/pre-commit-config.yaml:6-56).
  • No eval of outside input, no CI change, no sudo.

Overbuilt for an opt-in feature: a four-way installer (:439-458) plus an awk YAML splicer (:498-530).

Regression risk

  • Not idempotent. Where pre-commit cannot be installed, the install falls back to plain hooks, but --check still exits 1 (:625). Every sync retries the install, and session-check.sh:68 prints a ✗ every session.
  • Silent install. With no terminal, it runs pip install --user or brew install of an unpinned pre-commit (:441-455).
  • Weaker guard.
    • --allow-missing-config (:548) silently skips model verification on a branch with no config, or once the managed block is deleted from the committed YAML.
    • SKIP=mxtk-pre-commit becomes a new bypass.
  • Fail-closed when pre-commit goes missing (:633-635): every commit is refused.
  • Studio Pro. pre-commit stashes unstaged .mpr/mprcontents/ while Studio Pro has them open. The PR calls this "accepted risk"; it is a model-corruption path.
  • pre-push now treats every local branch as new (:591), so a full build can fire for a branch that is not being pushed.

Correctness

  • The 7 fixtures and run-all.sh only add MXTK_PRECOMMIT_BIN=none. No assertion is weakened, but no fixture covers the pre-commit path, the awk splice or run_hook.
  • No captured validate-config output or real pre-commit hook file. The # File generated by pre-commit marker (:409) is assumed.
  • Run on macOS only, "not yet run on Linux or Git Bash" — Shipping rules 1, 3 and 4.
  • The CHANGELOG credit ("a toolkit user") names no person, but no project either.

Context cost
No routed skill changed, and README is not baseline: 0 always-loaded words.

Time cost
Every hook, post-checkout included, now pays pre-commit's Python startup plus a fresh _common.sh source via --run. Likely hundreds of ms per git operation.

Exact fixes (for option 2)

  1. Default MXTK_INSTALL_PRECOMMIT=0. Use pre-commit only when the user opts in, and never install without a terminal.
  2. --check must not fail only because pre-commit is absent or present (:624-625).
  3. Drop --allow-missing-config, or keep a plain pre-commit body as the fallback.
  4. Add a fixture with a stub pre-commit, plus a captured real hook file and validate-config output. Cover the awk splice at both indents.
  5. Field-run on Linux and Git Bash, and cite the project.

Options (DISCUSS)

  1. Merge as is. Not recommended (see Regression risk).
  2. Make pre-commit opt-in and keep plain hooks as the default, with fixes 1–5. Recommended.
  3. Hold until the Linux/Git Bash runs and the Studio Pro stash risk are settled.

Generated by Claude Code

Require an existing pre-commit installation for opt-in use, document the setup, and cover captured hook/config behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jurretanjamx

jurretanjamx commented Dec 3, 2026 •

Copy link
Copy Markdown
Author

Toolkit hooks are plain Git hooks by default. To opt in to pre-commit, provision it in the developer/agent environment and set MXTK_INSTALL_PRECOMMIT=1 for init-project.sh, sync-project.sh, or install-project-hooks.sh. The toolkit does not install packages. If pre-commit is unavailable, plain hooks remain in use.

The installer validates its managed config block before writing it and preserves team hooks outside that block. --check does not fail solely because pre-commit is absent, but still fails for missing managed hooks or config. pre-commit stashes unstaged changes while hooks run; save and stage model files before command-line commits.

Validation: test-model-stamp.sh passed (52/52); Linux field run with pre-commit 4.7.0 passed. Git Bash validation remains outstanding.

Keep managed hook validation failures visible when pre-commit is unavailable, and document its unstaged-file stash behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@MendixMau

Copy link
Copy Markdown
Owner

Thanks for this. One config file for team hooks and a single source for the hook bodies are both real improvements. We'd like to take it, but as an opt-in, because the toolkit runs on many projects, often driven by agents or CI with no person at the terminal.

Changes we need before merge:

  1. Opt-in, never a silent install. Make MXTK_INSTALL_PRECOMMIT=0 the default and keep plain hooks as the default path. Install pre-commit only when a person runs the installer in a terminal and answers yes. Never install from an agent or CI run (today pip install --user / brew install can run unprompted, unpinned).
  2. --check must not fail only because pre-commit is absent or present (project-bin/install-project-hooks.sh:622-625). Today every sync-project.sh retries the install and every session shows a ✗ even when the plain hooks are correct.
  3. Don't refuse every commit when pre-commit goes missing (:633-635). Fall back to the plain hooks with a warning instead; a guard must not block the action that resolves it.
  4. Keep the model-verification guard where there is no config. --allow-missing-config (:548) silently skips it on a branch without the config, or if the managed block is removed. Keep a plain pre-commit body as the fallback, and say in the README that SKIP=mxtk-… bypasses it.
  5. Studio Pro stash risk. pre-commit stashes unstaged .mpr/mprcontents/ while hooks run, possibly while Studio Pro has the model open. Please exclude the model paths from what the framework stashes, or refuse to run with unstaged model changes, rather than relying on a README note.
  6. pre-push should check only the branches being pushed (or say clearly why checking all local branches is needed). Today a full build can fire for a branch nobody is pushing.
  7. Tests and field proof (our CLAUDE.md "Shipping an instrument" rules):
    • add a fixture with a stub pre-commit that covers --run, the YAML splice at both indents, and the fallback;
    • commit a captured real hook file and validate-config output beside it;
    • do one run each on Linux and Git Bash (Windows), and name the project in the commit.
  8. CHANGELOG credit: name the source project or team, not "a toolkit user".

Optional: a quick timing of one commit with and without pre-commit, so we can state the per-operation cost.

Happy to re-review once these are in. Thanks again!


Generated by Claude Code

@MendixMau

Copy link
Copy Markdown
Owner

Outside PR marker: HOLD — the install is now opt-in, but a clone that loses pre-commit is still locked out and sync no longer repairs it; the pre-push and stash issues remain.

Review: HOLD — in pre-commit mode with pre-commit gone, --check passes (project-bin/install-project-hooks.sh:555-557; asserted by tests/wave2/test-model-stamp.sh:250). The generated hooks still exit 1 (captured hook file). bin/sync-project.sh:1414 installs only when --check fails, so the fallback at :607-612 never runs and every commit is refused.

Safety: no package installs, network fetches, CI edits, or hidden or bidi characters. No existing assertion was weakened.

# Item Status
1 --check retried on every sync Partly fixed. Plain hooks are the default (:615). In pre-commit mode, --check is green while the hooks are broken.
2 Silent install Fixed. No install command remains.
3 --allow-missing-config Partly fixed. The flag is dropped (:525), so there is no silent skip. But a branch with no config now refuses every commit, there is no plain fallback, and the SKIP= bypass is undocumented.
4 Studio Pro stash Not fixed. README note only (README.md:226-228).
5 Fixtures and field runs Partly fixed. Real hook and validate-config output are captured. There is no --run or pre-push coverage. :246 greps the marker, not the indent. :271 is a no-op. :277 checks a file the test just wrote, not that plain hooks returned. The Linux run skipped push. Git Bash has not been run.
6 CHANGELOG credit Not fixed. It credits "toolkit maintainers (policy)" (CHANGELOG.md:21).
new pre-push input Likely regression. input="$(cat)" (:568) is likely empty, because pre-commit reads pre-push stdin itself and does not forward it to hooks. The module-close-commit trigger would then be dropped silently.

Exact fixes remaining

  1. When fw_active is true and pc_find fails, --check exits 1. Flip test-model-stamp.sh:250.
  2. Build the pre-push input from the PRE_COMMIT_LOCAL_BRANCH, PRE_COMMIT_REMOTE_BRANCH, PRE_COMMIT_FROM_REF and PRE_COMMIT_TO_REF variables. Add a --run pre-push fixture.
  3. Make --run pre-commit refuse unstaged model changes, or exclude the model paths. Document SKIP=.
  4. On a branch with no config, fall back to a plain body.
  5. Fixtures: assert the four-space indent and the restored plain hooks, and delete :271.
  6. Do a Git Bash run and a push run, and cite the project.
  7. Credit the source project or team.
  8. Minor: after opting in, MXTK_INSTALL_PRECOMMIT=0 is ignored (:607).

Generated by Claude Code

jurretanjamx and others added 5 commits December 4, 2026 09:48
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Addresses the second MendixMau#857 review (HOLD). Each .git/hooks/<type> is now a
small toolkit wrapper that calls pre-commit hook-impl, not pre-commit's
generated file, so git's own input reaches the toolkit first:

- pre-push checks every pushed ref, and post-rewrite notes the real
  rewritten commits (saved stdin, MXTK_HOOK_INPUT), not the first ref or
  an ORIG_HEAD..HEAD guess.
- No pre-commit, no config on the branch, or no managed block: the plain
  toolkit hook runs with one warning. Never locked out.
- --check fails while pre-commit is missing, so sync restores plain hooks.
- MXTK_INSTALL_PRECOMMIT=0 switches an opted-in clone back.
- pre-commit stashes in every hook stage: each wrapper compares the
  unstaged .mpr/mprcontents diff before and after, and on a difference
  prints the git apply command; commit and push are stopped.
- --run skips _common.sh (hook-call cost).

Plain hooks stay byte-identical to master's. T12 rewritten against a
stub pre-commit (67/67); tests/retests/field-precommit-hooks.sh part A
17/17 on macOS with pre-commit 4.6.1. Linux, Git Bash and the Studio
Pro part B are still to run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mmit hooks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jurretanjamx

Copy link
Copy Markdown
Author

Thanks for the second review. The main change since it: .git/hooks/<type> is now a small toolkit wrapper, not pre-commit's generated file. git hands its input to the hook file, so the wrapper sees it before pre-commit does. That one change fixes items 1, 2 and 4. The wrapper calls pre-commit hook-impl with the same arguments as pre-commit's own file (tests/wave2/fixtures/pre-commit-real/hook-pre-commit.txt).

# Item Now
1 --check green while the hooks are broken fw_check fails when pre-commit is missing, so sync-project.sh re-runs the installer, which restores plain hooks; --check is then green (T12e asserts both). At commit time the wrapper falls back to the plain hook with one warning, so nobody is locked out between syncs.
2 pre-push input empty under pre-commit The wrapper saves git's stdin and passes it to hook-impl and, through MXTK_HOOK_INPUT, to --run pre-push. A push of 2 branches gives the toolkit both refs (T12b, field run A5). We did not use the PRE_COMMIT_* variables, because they only describe the first ref. post-rewrite gets the real old/new list the same way, so the ORIG_HEAD..HEAD guess is gone. With no saved input (pre-commit run --hook-stage pre-push by hand), --run warns and checks every local branch.
3 Studio Pro stash No refusal up front (maintainer decision). pre-commit stashes in every hook stage, not just pre-commit, and never deletes its patch files (staged_files_only.py). So every wrapper hashes the unstaged .mpr/mprcontents/ diff before and after hook-impl. On a difference it prints a ✗ with the patch path and git apply command; commit and push are stopped, post-* hooks report only. Covered by T12d and field run A4, which forces a failed restore with real pre-commit. The Studio Pro case itself is field run part B: on Windows (ARM64, Git Bash, pre-commit 4.7.0), with Studio Pro open on an MPR v2 app and an unstaged model change, pre-commit set the open .mpr and mprcontents/ aside and put them back 3 times (post-checkout, pre-commit, post-commit) with no file-lock error; the change came back byte-identical, Studio Pro's next save reached the same file, both changes survived a close and reopen, and Studio Pro showed no dialog (9/9). SKIP= is documented in the README.
4 Branch with no config The wrapper runs the plain toolkit hook, which still refuses an unverified model. The same applies to a config without the managed block: the toolkit hook runs outside pre-commit, then the team hooks run (T12c).
5 Fixtures T12 rewritten, in its own scratch repo with a stub pre-commit whose hook-impl reads stdin and runs --run <type> like the real one. It asserts the exact two-space and four-space indents, the restored plain hooks, --check both ways, the 2-branch push, every fallback, the stash check, =0 opt-out, and migrating pre-commit's own hook files and .legacy. The :271 no-op is gone.
6 Git Bash and push runs tests/retests/field-precommit-hooks.sh (part A automatic, part B guided with Studio Pro open). macOS: 17/17 with pre-commit 4.6.1, push included. Windows Git Bash (ARM64, git 2.56.1, pre-commit 4.7.0, toolkit dfe7445): part A 17/17, push included; part B 9/9 with Studio Pro 10.24.3. Linux (Ubuntu 24.04 container, aarch64, pre-commit 4.7.0, toolkit 364a1c3): part A 17/17, push included. Part B (Studio Pro) runs on Windows and macOS only; macOS part B was not run.
7 CHANGELOG credit Community and Collaboration team.
8 =0 ignored after opting in MXTK_INSTALL_PRECOMMIT=0 now switches back to plain hooks and keeps the config (T12e).

Time cost. One hook call costs about 1 s with pre-commit vs about 0.1 s plain on macOS, and about 2.9 s vs 0.55 s on Windows (20 checkouts, field run A7). Almost all of it is pre-commit's own startup and stash check. --run no longer loads _common.sh. We accept this cost for an opt-in mode.

Regression risk for existing projects. Default mode is unchanged. Plain hooks are byte-identical to master's, and no clone switches without MXTK_INSTALL_PRECOMMIT=1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants