Skip to content

Instantly share code, notes, and snippets.

@bhaskarmelkani
Created August 20, 2021 18:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bhaskarmelkani/8baff88980f9cd90f8c44e07e433eb73 to your computer and use it in GitHub Desktop.
Save bhaskarmelkani/8baff88980f9cd90f8c44e07e433eb73 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# A precommit hook that uses spotless to format only staged files
# It also supports partially stage files using the following steps:
# 1. It stashed all the unstaged changes and then runs spotlessApply
# 2. After spotless apply is finished it applyes the stashed changes back on the code (that is also formatted/changed by spotless)
# 3. All the files that have conflicts due to the stash apply, it merges the conflict with the changes that are coming from the stash to not loose any new changes that were not staged
# stash any un staged changes
STASHED_HASH=$(git stash create)
if [ -n "$STASHED_HASH" ]
then
git stash store $STASHED_HASH;
git checkout -- .
fi
filesToFormat="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.java|\.kt/ { print $2}')"
printf "\n~~~ Spotless Starting ~~~"
printf "\nSpotless: Files to check"
for filePath in $filesToFormat
do
printf "\n* %s" "$filePath"
done
printf "\n\nSpotless: Starting to format dirty files\n\n"
for sourceFilePath in $filesToFormat
do
./gradlew spotlessApply -PspotlessIdeHook="$(pwd)/$sourceFilePath"
git add "$sourceFilePath"
done;
if [ -n "$STASHED_HASH" ]
then
echo "$(git stash apply -q)"
conflictedFiles="$(git diff --name-only --diff-filter=U)"
for conflictedFile in $conflictedFiles
do
git checkout --theirs "$(pwd)/$conflictedFile"
git restore --staged "$(pwd)/$conflictedFile"
done
git stash drop -q
fi
printf "\n~~~ Spotless Finished ~~~\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment