0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 20:03:05 +02:00
Anki-Android/pre-commit
2021-10-23 15:21:16 -05:00

33 lines
1.0 KiB
Bash
Executable File

#!/bin/sh
set -e
CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2 }')"
if [ -z "$CHANGED_FILES" ]; then
echo "No Kotlin staged files. Hence, skipping pre-commit Ktlint run."
exit 0
fi;
echo "Running Ktlint over these files:"
echo "$CHANGED_FILES"
# -q removes noise from the output if it fails.
# TODO: -w is better, but https://github.com/JLLeitschuh/ktlint-gradle/issues/457 adds noise
# -w should display: "CriticalExceptionTest.kt:19:1 Wildcard import (cannot be auto-corrected)"
# Cleaning and retrying only if the ktlint fails
if ! ./gradlew -q ktlintFormat ; then
echo "Cleaning the project and retrying."
# Clean is required because of https://github.com/ankidroid/Anki-Android/issues/9521
./gradlew clean
./gradlew -q ktlintFormat
fi;
echo "Completed ./gradlew ktlintFormat run."
echo "$CHANGED_FILES" | while read -r file; do
if [ -f "$file" ]; then
git add "$file"
fi
done
echo "Completed the pre-commit hook."