GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add git pre push hook by lazer-dev · Pull Request #2553 · diffplug/spotless

Conversation

@lazer-dev

@lazer-dev lazer-dev commented Jul 12, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

What this PR does

This PR adds support for installing a Git pre-push hook that runs spotlessCheck — and if formatting issues are found, it automatically runs spotlessApply to fix them, preventing broken pushes and helping developers keep code clean.

✅ Gradle plugin
New task: spotlessInstallGitPrePushHook

✅ Maven plugin
New goal: spotless:install-git-pre-push-hook

🛠 Shared core logic
Introduced GitPrePushHookInstaller class in the lib module

Why I added this

Formatting errors from spotlessCheck often show up only in CI/CD, which is frustrating.
Many developers (like myself) work from the command line:

git commit -m "message"
git push

They don’t always install IDE plugins or pre-commit tools.

This feature provides a simple way to install a Git pre-push hook that ensures formatting is always correct before pushing — without any manual setup or IDE dependency.

How the hook works

  1. The installed hook:
  2. Runs spotlessCheck
  • If issues are found:
  • It runs spotlessApply automatically
  • Prints a message: "Spotless found problems, running apply; commit the result and re-push"
  • Aborts the push with exit code 1

This way, developers get formatting fixes automatically, and can just commit the result and re-push.
No more broken builds due to formatting errors!

Summary of changes

✅ Added core utility: GitPrePushHookInstaller.java
✅ Gradle: spotlessInstallGitPrePushHook task
✅ Maven: spotless:install-git-pre-push-hook goal
✅ Updated CHANGES.md in:

  • plugin-gradle/CHANGES.md
  • plugin-maven/CHANGES.md
  • root CHANGES.md

Next steps

Let me know if:

  • you'd prefer the hook to be pre-commit instead
  • you'd like to make hook behavior (e.g. apply on fail) configurable
  • or want this feature disabled by default and opt-in via plugin config

Thanks for your time and feedback!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

abhaysood reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@lazer-dev
lazer-dev force-pushed the git-pre-push-hook branch 2 times, most recently from efd37d5 to a9205b6 Compare July 14, 2025 06:28
@nedtwigg nedtwigg mentioned this pull request Jul 16, 2025
Proposal: Native Support or Integration for Pre-Commit Hook Workflow (Maven & Gradle) #2475
Closed
@nedtwigg

nedtwigg commented Jul 16, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Thanks very much for this excellent contribution!

  • pre-commit vs pre-push
    • pre-commit hook has some problems (and a solution) outlined in spotlessApply --staged #623
    • pre-push avoid the worst of this, I think the workflow described above in How the hook works is excellent
  • make hook behavior (e.g. apply on fail) configurable
    • i prefer to have fewer config options, if someone needs it so much they're willing to write the PR and docs for it then I'm okay with adding them later
  • want this feature disabled by default and opt-in via plugin config
    • If I read the code correctly, spotlessInstallGitPrePushHook will only run if you explicitly call it - it's not a dependency of check or anything like that. Am I correct?
    • to me, that is "disabled by default" enough. If someone really wants it they can add it as a dependency of check or whatever.

This still needs docs in the README.md for each plugin.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lazer-dev

lazer-dev commented Jul 17, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

If I read the code correctly, spotlessInstallGitPrePushHook will only run if you explicitly call it - it's not a dependency of check or anything like that. Am I correct?

Yes, that's correct — if you want this feature, you have to manually run the spotlessInstallGitPrePushHook command once.

to me, that is "disabled by default" enough. If someone really wants it they can add it as a dependency of check or whatever.

Yes, it's disabled by default — you're right. If someone wants it to run automatically, they can add it as a dependency to check, but I don't think we need to provide that functionality out of the box.

pre-commit hook has some problems (and a solution) outlined in #623

Yes, that's true. I'm not entirely sure the proposed solution works 100% reliably. In any case, that situation with pre-push happens quite rare, and I believe the shell script should stay as simple as possible. The edge case described in that issue doesn’t happen often enough to justify a more complex script that might introduce new bugs.

i prefer to have fewer config options, if someone needs it so much they're willing to write the PR and docs for it then

Hmm, if you really want this option, I’m happy to implement it. But just to consider — in 99.9999% of cases when spotlessCheck fails, the user will immediately run spotlessApply. Only in very rare cases does apply not fix everything and require manual edits. So in my opinion, the benefit of making this configurable is questionable. But again — if you want it, I’m happy to add it :)

This still needs docs in the README.md for each plugin.

Got it — could you please clarify what exactly is missing? Do you expect a short description of the hook task/goal, or also an example usage? Just want to make sure I include everything needed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lazer-dev added 2 commits July 16, 2025 20:59
@lazer-dev
add git pre push hook
e6e9a07
@lazer-dev
spotlessApply
6528b0b
@lazer-dev
lazer-dev force-pushed the git-pre-push-hook branch from a9205b6 to 6528b0b Compare July 17, 2025 04:00
cmoine-swi
cmoine-swi reviewed Jul 17, 2025
View reviewed changes
Comment thread lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

cmoine-swi
cmoine-swi reviewed Jul 17, 2025
View reviewed changes
Comment thread lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerMaven.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

ntwigg
Placeholders for docs.
b9f4880
@nedtwigg

nedtwigg commented Jul 17, 2025

Copy link
Copy Markdown
Member

docs ... clarify what exactly is missing

I just pushed up a commit with placeholders, your description at the open of the PR was a great start!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lazer-dev
maven wrapper support
a790659
@lazer-dev
lazer-dev requested review from cmoine-swi and nedtwigg July 18, 2025 05:45
@lazer-dev
lazer-dev force-pushed the git-pre-push-hook branch from 4ece095 to b8ce688 Compare July 18, 2025 06:02
@lazer-dev
reinstall support
525e70e
@lazer-dev
lazer-dev force-pushed the git-pre-push-hook branch from b8ce688 to 525e70e Compare July 18, 2025 06:09
ntwigg
Modify git-hook docs, with an eye towards a future where there is bot…
259ec38
…h a pre-push hook or a pre-commit hook, depending on what the user prefers.
nedtwigg
nedtwigg approved these changes Jul 18, 2025
View reviewed changes

@nedtwigg nedtwigg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is excellent. I tweaked the docs to be shorter and to look ahead to a future where users have a choice between a pre-push hook or a pre-commit hook. If you're happy with it @lazer-dev, I'll merge and release.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lazer-dev reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@lazer-dev

lazer-dev commented Jul 18, 2025

Copy link
Copy Markdown
Contributor Author

I think this is excellent. I tweaked the docs to be shorter and to look ahead to a future where users have a choice between a pre-push hook or a pre-commit hook. If you're happy with it @lazer-dev, I'll merge and release.

Just one question — if a user manually modifies the git-push hook file and breaks the formatting, the uninstall logic might not work properly. Should we handle this case somehow?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lazer-dev
lazer-dev commented Jul 18, 2025
View reviewed changes
Comment thread lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@lazer-dev
lazer-dev requested a review from nedtwigg July 18, 2025 20:39
@nedtwigg nedtwigg mentioned this pull request Jul 18, 2025
Remove support for Java 11 #2375
Closed
5 tasks done
@lazer-dev
lazer-dev force-pushed the git-pre-push-hook branch from 9fda21d to 957cb7a Compare July 19, 2025 04:02
@lazer-dev
uninstall fix
2dadeb6
@lazer-dev
lazer-dev force-pushed the git-pre-push-hook branch from 957cb7a to 2dadeb6 Compare July 19, 2025 04:20
@lazer-dev

lazer-dev commented Jul 20, 2025

Copy link
Copy Markdown
Contributor Author

@nedtwigg @cmoine-swi

Guys, if there are no further comments, I think we can merge?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nedtwigg
nedtwigg merged commit af40183 into diffplug:main Jul 20, 2025
@nedtwigg

nedtwigg commented Jul 20, 2025

Copy link
Copy Markdown
Member

Published in plugin-gradle 7.2.0 and plugin-maven 2.46.0

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tsaglam tsaglam mentioned this pull request Jul 21, 2025
Replace custom Git hook with Spotless pre-push hook jplag/JPlag#2500
Closed
@PierreLouisBertrand

PierreLouisBertrand commented Oct 14, 2025

Copy link
Copy Markdown

Hello !
There is an issue with the pre-push hook logic: when maven wrapper is used, it always goes for the mvnw file, even on Windows. But for Windows users, the mvnw file doesn't work (at least, it doesn't with powershell or cmd), we need to use mvnw.cmd.

I tried to used the pre-push hook maven goal, but when using Windows, the pre-push hook fails. 😥

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@MarcoMartins86

MarcoMartins86 commented Oct 21, 2025

Copy link
Copy Markdown

In Mac, there are also problems. The code below would return the relative path from the root of the project, not the absolute path. Using a git client like Fork, for example, pushing will run into problems since it can't find the executor.

if (mvnw.exists()) {
  return mvnw.getAbsolutePath();
}

Maybe adding a configuration property would be a better approach

<execution>
    <goals>
        <goal>install-git-pre-push-hook</goal>
    </goals>
    <phase>compile</phase>
    <configuration>
        <executor>mvn</executor>
    </configuration>
</execution>
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lazer-dev

lazer-dev commented Oct 23, 2025

Copy link
Copy Markdown
Contributor Author

Hello ! There is an issue with the pre-push hook logic: when maven wrapper is used, it always goes for the mvnw file, even on Windows. But for Windows users, the mvnw file doesn't work (at least, it doesn't with powershell or cmd), we need to use mvnw.cmd.

I tried to used the pre-push hook maven goal, but when using Windows, the pre-push hook fails. 😥

@PierreLouisBertrand What version of Spotless are you using? I recommend upgrading to the latest version, which now supports .cmd and .bat files.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lazer-dev lazer-dev mentioned this pull request Oct 24, 2025
git pre push hook, use absolute path to the executor #2701
Merged
@lazer-dev

lazer-dev commented Oct 24, 2025

Copy link
Copy Markdown
Contributor Author

In Mac, there are also problems. The code below would return the relative path from the root of the project, not the absolute path. Using a git client like Fork, for example, pushing will run into problems since it can't find the executor.

if (mvnw.exists()) {
  return mvnw.getAbsolutePath();
}

Maybe adding a configuration property would be a better approach

<execution>
    <goals>
        <goal>install-git-pre-push-hook</goal>
    </goals>
    <phase>compile</phase>
    <configuration>
        <executor>mvn</executor>
    </configuration>
</execution>

@MarcoMartins86 added absolute path here #2701

MarcoMartins86 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@clescot clescot mentioned this pull request Feb 25, 2026
setup code style in project LittleProxy/LittleProxy#705
Closed
@bzp99 bzp99 mentioned this pull request Jul 15, 2026
docs: add contribution guidelines, AGENTS.md, and community-health files LF-Decentralized-Trust-labs/hypernate#123
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@cmoine-swi cmoine-swi Awaiting requested review from cmoine-swi
@nedtwigg nedtwigg Awaiting requested review from nedtwigg

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@lazer-dev @nedtwigg @PierreLouisBertrand @MarcoMartins86 @cmoine-swi
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.