Created
July 20, 2021 06:36
-
-
Save muthuraj57/0c990940a557017f71fe61e4f980d0b0 to your computer and use it in GitHub Desktop.
Pre commit hook to apply Spotless for the changes files in staging area.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Ref: https://blog.jdriven.com/2020/11/formatting-in-pre-commit-hook/ | |
# command taken from https://github.com/JLLeitschuh/ktlint-gradle task addKtlintFormatGitPreCommitHook | |
filesToFormat="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.java|\.kt/ { print $2}')" | |
echo "files to format $filesToFormat" | |
for sourceFilePath in $filesToFormat; do | |
if [ -f "$sourceFilePath" ]; then | |
./gradlew spotlessApply -PspotlessIdeHook="$(pwd)/$sourceFilePath" | |
git add $sourceFilePath | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment