Skip to content

Instantly share code, notes, and snippets.

@muthuraj57
Last active July 20, 2021 06:36
Show Gist options
  • Save muthuraj57/d0de2f52bfcfeb7750bcca046753359e to your computer and use it in GitHub Desktop.
Save muthuraj57/d0de2f52bfcfeb7750bcca046753359e to your computer and use it in GitHub Desktop.
#!/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