Add pre-commit hook for autoformatting#2233
Conversation
Stored under scripts/git-hooks/ and installed via core.hooksPath. Hook runs `dune fmt --auto-promote` and re-stages any of the originally-staged files that the formatter touched, so commits never contain unformatted code. Files that weren't staged are left alone, even if the formatter modified them — so unrelated unstaged WIP doesn't get swept into the commit. Wired install-hooks into deps/dev so existing developers pick up the hook on their next make invocation.
Move install-hooks off make dev / dev-student — it only needs to run during initial setup. Add a Git Hooks section to CONTRIBUTING.md and a one-line pointer in README.md so contributors know what make deps is wiring up.
|
|
||
| if [ -n "$staged" ]; then | ||
| while IFS= read -r f; do | ||
| [ -e "$f" ] && git add -- "$f" |
There was a problem hiding this comment.
This is going to behave interestingly in the case where someone partially stages a file. Resulting in the entire file being committed.
There was a problem hiding this comment.
Having thought about this more my personal preference is probably to just abort the commit if there's reformatting. So I would probably stash the unstaged components run formatting (abort if there's changes) then commit then unstash.
There was a problem hiding this comment.
Alternatively we can have github reformat on the server
|
@Negabinary can you try this with vs code + windows. Not sure how it'll work there. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #2233 +/- ##
==========================================
- Coverage 50.42% 50.39% -0.04%
==========================================
Files 296 296
Lines 39452 39452
==========================================
- Hits 19894 19880 -14
- Misses 19558 19572 +14 🚀 New features to boost your workflow:
|
Summary
pre-commithook (underscripts/git-hooks/) that runsdune fmt --auto-promoteand re-stages any originally-staged files the formatter touched, so commits never contain unformatted code.make install-hookssetscore.hooksPathtoscripts/git-hooks. It's idempotent and is now a dependency ofdeps,dev, anddev-student, so anyone running setup or a normal build picks up the hook automatically (no manual opt-in needed).git commit --no-verifystill bypasses the hook for cases where it's necessary.Test plan
make install-hooks(first run installs, second run is silent — verified)git commit --no-verify— bypasses the hookmake depsonce on an existing clone to confirm hooks auto-install🤖 Generated with Claude Code