feat: add git pre-commit hook for automatic code formatting#23384
feat: add git pre-commit hook for automatic code formatting#23384
Conversation
Adds a pre-commit hook that runs spotless:apply on staged files, eliminating the need to manually format code before commits. - scripts/git-hooks/pre-commit: the hook that formats staged files - scripts/install-git-hooks.sh: installs hooks, backs up existing ones - pom.xml: runs install script during Maven initialize phase Existing pre-commit hooks are backed up to pre-commit.local and called after formatting completes.
- Run spotless:apply on whole project (multi-module compatible) - Use POSIX-compatible commands only (works on Linux and Mac) - Remove unnecessary variables and function abstractions
33834c8 to
405ba20
Compare
|
How would you integrate husky into the Maven build? |
|
Still with |
|
The problem with this is that once it copies hooks into |
|
I guess this solution would also not work for Windows users |
|
Let's see how it would look with husky. It would assume you have node installed |
Replace custom shell-based git hooks with Husky for better branch switching support. The .husky/ directory is version-controlled so hooks update automatically when switching branches without manual reinstallation.
Use npx husky@9.1.7 directly instead of relying on package.json.
|
Looks quite a lot simpler now |
|
should also work on windows |
Move Husky installation to a profile activated by the absence of .husky/_, so it runs only once instead of on every Maven execution.
|
@claude review this |
|
Works on macOS, anyone able to test it on Windows? |
|
@caalador maybe |
|
Does work on windows also. |
caalador
left a comment
There was a problem hiding this comment.
Else ok, but could have a mention in the README that one should run the install-git-hooks profile once for this to start working.
|
The whole point is that you should not need that |
|
Well just doing a commit without once running the profile did not work. |
|
Now it doesn't format at all when committing. |
|
Adding |
How does that work? Does it compare with local |
With this it compares to local main, otherwise we should have
Right, and we will forget. Maybe the reference branch can be inferred with some git command? |
|
When I try this on Windows in a virtual machine, it does activate the profile but it does not install the hook because on windows the command is Edit: This does not sound correct either. Might be something wrong with the node installation |
|
I had a broken Node installation. Now with a new Node 24, I see on the first |
|
Also initially and no longer shows that after |
Husky runs hooks with `sh -e`, so grep returning 1 (no matches) killed the script before reaching the empty-check guard. Also removed GNU-only `xargs -r` flag for Windows compatibility.
…mmit hook Compute the merge-base between HEAD and the upstream tracking branch, and pass it to Spotless via -Dspotless.ratchetFrom so only files changed since the branch diverged are formatted. This avoids running Spotless on the entire codebase on every commit. The property defaults to empty so plain `mvn spotless:apply` still formats everything.
When HEAD is detached (e.g. during rebase), there is no upstream
tracking branch so @{u} fails. Exit gracefully and let the commit
proceed without formatting.
Is this broken for parallel builds? |
Use a lock directory (mkdir is atomic) to ensure only one process runs npx husky in parallel Maven builds. Move the logic to a dedicated script in scripts/.
heruan
left a comment
There was a problem hiding this comment.
Added some comments from Claude. Starts feeling like this could have some unexpected site-effects, would it be performance or committing unwanted changes.
If we decide to go on and merge, I'd suggest to communicate this change and ask feedback.
- Fix installHusky.js lock cleanup: use finally block so the lock directory is always removed even if npx throws, preventing stuck locks - Fix pre-commit partial staging: stash unstaged changes before formatting and restore after, so `git add -p` workflows are preserved
|
heruan
left a comment
There was a problem hiding this comment.
LGTM now! I suggest to document this hook in CONTRIBUTING.md or where else appropriate.



Adds a pre-commit hook that runs spotless:apply on staged files, eliminating the need to manually format code before commits.
Existing pre-commit hooks are backed up to pre-commit.local and called after formatting completes.