-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧑💻 Bruk pre-commit hooken som gradle pluginen lager
- Loading branch information
Showing
1 changed file
with
36 additions
and
9 deletions.
There are no files selected for viewing
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
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 ######## |