question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:28 (11 by maintainers)

github_iconTop GitHub Comments

24reactions
lowworcommented, Dec 14, 2017

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
5reactions
toefel18commented, Nov 28, 2020

I created a pre-commit hook that formats the code and adds the formatted files that were initially in the staging area back to the staging area in order to commit the actual formatted code. It leaves the other files as is.

you can install it by running this in your repository root:

curl -o .git/hooks/pre-commit https://gist.githubusercontent.com/toefel18/23c8927e28b7e7cf600cb5cdd4d980e1/raw/163337f2ae596d4b3a55936c2652b1e8227db5b7/pre-commit && chmod +x ./.git/hooks/pre-commit
#!/bin/bash
set -e

filesToAddAfterFormatting=()
containsJavaOrKotlin=0

# Collect all files currently in staging area, and check if there are any java or kotlin files
for entry in $(git status --porcelain | sed -r 's/[ \t]+/-/g')
do
  # entry can be for example:
  # MM-src/main/java/net/project/MyController.java
  # -M-src/main/java/net/project/AnotherController.java

 if [[ $entry == M* ]] ; then
    filesToAddAfterFormatting+=(${entry:2}) # strips the prefix
 fi

 if [[ $entry == *.java ]] || [[ $entry == *.kt ]] ; then
    containsJavaOrKotlin=1
 fi
done;


# If any java or kotlin files are found, run spotlessApply
if [ "$containsJavaOrKotlin" == "1" ] ; then
  echo "Kotlin and/or Java found in staging, running:  ./gradlew -PdisableSpotlessCheck spotlessApply"
  ./gradlew -PdisableSpotlessCheck spotlessApply
else
  echo "Not running spotlessApply"
fi

# Add the files that were in the staging area
for fileToAdd in $filesToAddAfterFormatting
do
  echo "re-adding $fileToAdd after formatting"
  git add "$fileToAdd"
done;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Apply Spotless formatting with Git pre-commit hook - Medium
Assign all staged files to a variable: This variable will contain the files, which the developer is about to commit. This is needed...
Read more >
Supported hooks - pre-commit
github.com/pre-commit/pre-commit-hooks. check-added-large-files - prevents giant files from being committed. check-ast - simply checks whether the files ...
Read more >
Spotless code with a git hook - NotesSensei's Blog - wissel.net
There are 3 steps involved for the Maven setup: Obtaining the formatting files, outlined here. Just make sure you are happy with the...
Read more >
Formatting in pre-commit hook - Git - JDriven Blog
This scripts tracks any of the java or kotlin files in the staging area. · Formats the code using spotless if there are...
Read more >
Git pre-hook: Setup pre-commit hook for Gradle project example
1. Create a pre-hook script, let's create pre-commit file inside a new scripts directory, and we want to run unit tests before code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found