Skip to content

Commit

Permalink
🧑‍💻 Bruk pre-commit hooken som gradle pluginen lager
Browse files Browse the repository at this point in the history
  • Loading branch information
chsko committed Apr 4, 2024
1 parent 296dca1 commit 8bb947d
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
#!/bin/bash
#!/bin/sh
######## KTLINT-GRADLE HOOK START ########

echo "Running git pre-commit hook"
CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $NF ~ /\.kts?$/ { print $NF }')"

./gradlew ktlintFormat
if [ -z "$CHANGED_FILES" ]; then
echo "No Kotlin staged files."
exit 0
fi;

ktlintFormatStatus=$?
echo "Running ktlint over these files:"
echo "$CHANGED_FILES"

if [[ $ktlintFormatStatus -ne 0 ]]; then
exit 1
else
exit 0
fi
diff=.git/unstaged-ktlint-git-hook.diff
git diff --color=never > $diff
if [ -s $diff ]; then
git apply -R $diff
fi

./gradlew --quiet ktlintFormat -PinternalKtlintGitFilter="$CHANGED_FILES"
gradle_command_exit_code=$?

echo "Completed ktlint run."

echo "$CHANGED_FILES" | while read -r file; do
if [ -f $file ]; then
git add $file
fi
done


if [ -s $diff ]; then
git apply --ignore-whitespace $diff
fi
rm $diff
unset diff

echo "Completed ktlint hook."
exit $gradle_command_exit_code
######## KTLINT-GRADLE HOOK END ########

0 comments on commit 8bb947d

Please sign in to comment.