- 
                Notifications
    You must be signed in to change notification settings 
- Fork 489
add git pre push hook #2553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add git pre push hook #2553
Conversation
efd37d5    to
    a9205b6      
    Compare
  
    | Thanks very much for this excellent contribution! 
 This still needs docs in the README.md for each plugin. | 
| 
 Yes, that's correct — if you want this feature, you have to manually run the  
 Yes, it's disabled by default — you're right. If someone wants it to run automatically, they can add it as a dependency to  
 Yes, that's true. I'm not entirely sure the proposed solution works 100% reliably. In any case, that situation with  
 Hmm, if you really want this option, I’m happy to implement it. But just to consider — in 99.9999% of cases when  
 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. | 
a9205b6    to
    6528b0b      
    Compare
  
            
          
                lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerMaven.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | 
 I just pushed up a commit with placeholders, your description at the open of the PR was a great start! | 
4ece095    to
    b8ce688      
    Compare
  
    b8ce688    to
    525e70e      
    Compare
  
    …h a pre-push hook or a pre-commit hook, depending on what the user prefers.
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.
| 
 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? | 
9fda21d    to
    957cb7a      
    Compare
  
    957cb7a    to
    2dadeb6      
    Compare
  
    | Guys, if there are no further comments, I think we can merge? | 
| Published in  | 
| Hello ! I tried to used the pre-push hook maven goal, but when using Windows, the pre-push hook fails. 😥 | 
| 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> | 
| 
 @PierreLouisBertrand What version of Spotless are you using? I recommend upgrading to the latest version, which now supports .cmd and .bat files. | 
| 
 @MarcoMartins86 added absolute path here #2701 | 
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 runsspotlessApplyto 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
GitPrePushHookInstallerclass in the lib moduleWhy I added this
Formatting errors from
spotlessCheckoften show up only in CI/CD, which is frustrating.Many developers (like myself) work from the command line:
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
spotlessApplyautomaticallyThis 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:
spotlessInstallGitPrePushHooktask✅ Maven:
spotless:install-git-pre-push-hookgoal✅ Updated CHANGES.md in:
Next steps
Let me know if:
Thanks for your time and feedback!