Proposal: Native Support or Integration for Pre-Commit Hook Workflow (Maven & Gradle)
Hi there! First off, I just want to say how much I appreciate Spotless. It has become a critical part of my workflow, and I really value the consistency and flexibility it brings to code formatting.
One area where I’ve felt a bit of friction, though, is around formal support for pre-commit workflows. To address this, I built separate solutions for both Maven and Gradle that wire up Spotless to Git pre-commit hooks via shell script files that then call Spotless via Maven or Gradle. They work well and are quite robust, but admittedly require a fair amount of setup and documentation, which can be a barrier, especially for teams or developers trying to onboard quickly.
After stepping back from these projects, I wondered whether this kind of functionality might be a candidate for inclusion directly in Spotless itself, or possibly as an officially supported companion module. You may have considered something like this before, and if so, I’d love to hear your thoughts or learn from any past discussions.
I’m opening this issue to start the conversation and gauge whether there's interest in exploring this further. I’d be happy to share what I’ve built and work toward something that fits well with the existing design and philosophy of Spotless.
Thanks for your time, and again, I appreciate the work you all do on this project!
formal support for pre-commit workflows
This has been a common request, and we've never supported it well out-of-the-box. Part of this has been about staged files - when the staged content matches the unstaged content, it's easy to do something. But what if the staged content does not match the unstaged?
We have a long-open PR to allow Spotless to apply itself to the staged content, ignoring the disk
- https://github.com/diffplug/spotless/issues/623
Probably the best place to start is documentation. A draft PR which updates the root README.md for the Gradle and Maven plugins with getting-started instructions for your scripts. If those docs would meet the community's requirements, then we can close out these issues and pull your stuff upstream.
I think what @mrlonis is asking is slightly different from https://github.com/diffplug/spotless/issues/623, which discusses an issue when running commands in a particular order, if I understand correctly. It also mentions a nice project (captain-hook), which is unfortunatly only available for Gradle.
The idea is to provide a Gradle & Maven command to set up a precommit hook out of the box, such as gradle spotlessSetupPreCommitHook and mvn spotless:step-pre-commit-hook. Maybe this could be a starting point. I can help with a PR if needed 😉
- This is happening in #2553, if you have feedback now's your chance :)
Published in plugin-gradle 7.2.0 and plugin-maven 2.46.0