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"
        ktlint(versions.ktlint)
    }
}

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.

#!/bin/sh
# From gist at https://gist.github.com/chadmaughan/5889802

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
RESULT=$?

# unstash the unstashed changes
git stash pop -q

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

Related Issues:

17
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. ...
972
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 ...
782
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...
506
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...
324
tailwindcss npx tailwindcss init not working
I had trouble generating the configuration file while using the compatibility package I updated my t...
236
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...
229
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=./>. ...
220
emotion TS2322: Property 'css' does not exist...
For Emotion 11 use the following configuration in your tsconfig.json until TypeScript 4.1 comes out:...
204
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...
194
autoprefixer Regression: -webkit-box-orient rules removed
Possibly helpful for anyone reading up on this thread and problem While upgrading my dependencies on...
187
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...
186
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....
183
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...
175
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...
174
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. ...
167
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 ...
154
esbuild Vue.js support
@yyx990803 I just added an API similar to what you proposed: It should now be possible to make use o...
139
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...
126
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...
109
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...
107
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...
105
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...
104
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...
103
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...
101
jupyter themes No module named 'jupyter_core'
+1 Edit: @OldBoots4 I did this: pip3 install --upgrade notebook and this: pip3 install lesscpy and i...
101
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...
99
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 ...
97
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...
96
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...
96
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...
94
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...
90
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...
89
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...
87
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...
86
emotion Emotion v10 unusable with pure TypeScript
FYI having jsxFactory: jsx means that you would have to add import { jsx } from '@emotion/core'; to ...
84
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...
82
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...
78
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...
77
parcel 🐛dist/ directory isn't cleaned on repeated builds
In package.json add another script named prebuild: prebuild will run automatically before the build ...
76
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-...
74
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...
72
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 ...
71
parcel Uncaught ReferenceError: regeneratorRuntime is not defined
how to solve ? If you are targeting modern browsers 🐛 bug report When I started using generators in...
71
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...
70
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 ...
70
capacitor iOS xcode Project - No such module found ‘Capacitor’
@vi354258 make sure you are opening the Xcode Workspace (App.xcworkspace instead of App.xcodeproj) ...
70
bootstrap Responsive Card Decks
I've managed to find a way of having cards in the responsive system while having equal heights just ...
69
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...
66
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 ...
63
prettier Space after function keyword in anonymous functions
+1 for space after function keyword Crockford supports this: If a function literal is anonymous ...