Solvedspotless How to setup spotless for a git pre commit hook that only check changed files

I'm using spotless with an android kotlin project now. And I have spotless set up like this

apply plugin: 'com.diffplug.gradle.spotless'
spotless {
    kotlin {
        target "**/*.kt"

What I want to do is set up a git pre commit hook that apply gradlew spotlessCheck before the commit but only on the commited files. It's something like this eslint-pre-commit-check
I have search around the repo but can't figure out how to achieve this. Is there any way to do this? Thanks.

22 Answers

✔️Accepted Answer

Understood. Following is my workaround now. Hope it helps someone else who want to achieve this too.

# From gist at

echo '[git hook] executing gradle spotlessCheck before commit'

# stash any unstaged changes
git stash -q --keep-index

# run the spotlessCheck with the gradle wrapper
./gradlew spotlessCheck --daemon

# store the last exit code in a variable

# unstash the unstashed changes
git stash pop -q

# return the './gradlew spotlessCheck' exit code
exit $RESULT

Related Issues:

spotless How to setup spotless for a git pre commit hook that only check changed files
Understood Following is my workaround now Hope it helps someone else who want to achieve this too. ...
css loader Can't resolve 'fs' when bundle with webpack
Adding to webpack config file and the error is gone Not sure how it works It seems like webpack did ...
styled components [v4.0.0] Types: Could not find a declaration file for module 'styled-components/native'.
styled-components/native types moved to @types/styled-components-react-native. there are no types fo...
electron Requiring electron outside of main.js causes a TypeError
For anyone encountering this problem in the future and reading this thread Electron version: 1.3.5 O...
tailwindcss npx tailwindcss init not working
I had trouble generating the configuration file while using the compatibility package I updated my t...
electron Error while importing electron in react | import { ipcRenderer } from 'electron'
@MarshallOfSound my mistake I found the solution in issue #7300 if it can help anyone Please note th...
electron Failed to load resource: net::ERR_FILE_NOT_FOUND file:///D:/css/app.css
If this occur when having <base href=/> in the index.html just replace it by <base href=./>. ...
emotion TS2322: Property 'css' does not exist...
For Emotion 11 use the following configuration in your tsconfig.json until TypeScript 4.1 comes out:...
electron Error: Electron failed to install correctly, please delete node_modules/electron and try installing again
Try It should output a progress bar for the download 👍 Electron version: latest (That Try) Operatin...
autoprefixer Regression: -webkit-box-orient rules removed
Possibly helpful for anyone reading up on this thread and problem While upgrading my dependencies on...
styled components How do I provide props to styled elements in Typescript?
This is what I use at the moment: I looked at the docs and other than creating a whole empty compone...
css loader Cannot find module "!!./../node_modules/css-loader/ ...
I faced this problem also but this was because of the node-sass package I previously used Node V4.5....
styled components How do you use attrs with TypeScript?
If you need props inside your component and need to type those with a generic you can use this patte...
electron How do I open a url from <a> on default OS browser?
I found this code snippet on S.O.: Dropped it in my main index file it seems to be working as far as...
css loader Module build failed: Unknown word (5:1)
In my case the problem was the order of the loaders I changed this: to this: and it worked. ...
prettier Change HTML/JSX formatting to have one attribute/prop per line
The point I am trying to make is that the vertical space matters Wasting it (too much) has negative ...
esbuild Vue.js support
@yyx990803 I just added an API similar to what you proposed: It should now be possible to make use o...
electron 9.0.0 does not display local images
A recap for anyone else who's having this issue: Make sure you've enabled webSecurity from your Brow...
capacitor FileReader API not firing
You can try this. Description of the problem: In my project I'm trying to read and convert a Blob do...
ng packagr feat: peerDependencies and devDependencies auto whitelist.
@codegagan you can remove the warnings by adding the below to ng-packagr.json right next to lib prop...
css loader Unknown word error when loading plain CSS (not Sass or Less)
I seem to figure this out According to webpack docs there are multiple ways to use loaders If use re...
prettier [resolved] [HTML] allow corresponding option to jsxBracketSameLine
I don't think I've ever seen any markup language with the closing brackets on a separate line I'd li...
styled components Integrate with Animated
I know this is an old issue but to share a solution I've been using with latest version of styled-co...
electron The SUID sandbox helper binary was found, but is not configured correctly
CONFIG_USER_NS=y enables the user namespaces feature but they're still restricted to privileged user...
jupyter themes No module named 'jupyter_core'
+1 Edit: @OldBoots4 I did this: pip3 install --upgrade notebook and this: pip3 install lesscpy and i...
bootstrap Navbar .sticky-top doesn't work
My setup: Bootstrap@4.0.0-beta.2 macOS Sierra - Version 10.12.6 Google Chrome - Version 62.0.3202.62...
styled components Separate HTML attributes from styling props
Here’s my proposal It doesn’t seem to match where the library wants to go Background I am trying to ...
parcel "Name already registered with serializer" with parcel@next
I was getting this issue after trying to switch from parcel-v1 to parcel-v2 Deleting node_modules an...
ng packagr Please add a @NgModule annotation in AOT Compilation
Don't know if it helps but i had the same issue : Turns out my generated metadata [library].metadata...
styled jsx Typescript typings
I was able to fix it with: First of all awesome tool! We're using it together with TypeScript and it...
postcss import Throws "Error: PostCSS plugin postcss-import requires PostCSS 8" even when PostCSS 8 is installed
We ended up pinning postcss-import at 12.x while keeping up-to-date with postcss on the latest branc...
prettier Feature Request: breakBeforeElse
This feels like one of those I can't adopt prettier unless options that was mentioned in the 1.0 rel...
autoprefixer Browserslist: caniuse-lite is outdated
If you have a multitude of out of date packages the fastest way to update them is with npm-update-al...
prettier Add option to omit semicolons in output
I use standard.js which is a popular no-configuration style guide & linter They insist on a semicolo...
emotion Emotion v10 unusable with pure TypeScript
FYI having jsxFactory: jsx means that you would have to add import { jsx } from '@emotion/core'; to ...
parcel Cannot find module error when importing an static file using typescript
Create a .d.ts file in your project and put : It will instruct TypeScript to use any for all imports...
styled components Uncaught Error: Cannot create styled-component for component: undefined
I had the exact same issue and was able to get around it after I saw this comment on another repo Ba...
parcel Bug: React not defined (with TypeScript)
I encountered this as well recently Using import * as React from 'react' worked for me Maybe you can...
parcel 🐛dist/ directory isn't cleaned on repeated builds
In package.json add another script named prebuild: prebuild will run automatically before the build ...
css loader [2.0.0] ValidationError: CSS Loader Invalid Options - options should NOT have additional properties
Same issue here And works fine if remove minimize: true. Operating System: Linux workstation 4.15.0-...
js beautify Option to preserve or inline "short objects" on a single line
For people like me coming here looking for a way to make it work: it works and can be enabled with t...
prettier Space after function name in declarations
This is one of the reasons I've had to step away from adopting Prettier I would be quite happy with ...
parcel Uncaught ReferenceError: regeneratorRuntime is not defined
how to solve ? If you are targeting modern browsers 🐛 bug report When I started using generators in...
tailwindcss outline-none doesn't work on button with focus in chrome
You only need focus:outline-none for all browsers: Hi First of all a big thank's for your work on Ta...
electron autoUpdater does not work when have authenticated proxy
I finally got everything working so I just wanted to report back here quickly while I'm waiting for ...
capacitor iOS xcode Project - No such module found ‘Capacitor’
@vi354258 make sure you are opening the Xcode Workspace (App.xcworkspace instead of App.xcodeproj) ...
bootstrap Responsive Card Decks
I've managed to find a way of having cards in the responsive system while having equal heights just ...
prettier Ugly line break in a template string
Maybe templates should never be wrapped because it looks ugly most of time This input looks better t...
parcel Importing CSS from node_modules
I've been fighting with this for a while using .sassrc as a JSON object and yaml neither worked ...
prettier Space after function keyword in anonymous functions
+1 for space after function keyword Crockford supports this: If a function literal is anonymous ...