Skip to content

Commit

Permalink
Update pre-commit to run for certain files
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinstardust committed Feb 26, 2024
1 parent 3f842f1 commit 68c542c
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
echo "***********************************************"
echo "Running git pre-commit hook to lint and format."
echo "***********************************************"
echo ""

# Gather the staged files - to make sure changes are saved only for these files.
stagedFiles=$(git diff --staged --name-only)
# The git diff and grep part is re-used to prevent a bug which leads to 0 files having a count of 1
stagedFilesCount=$(git diff --staged --name-only | grep -E '(library|demo)' | wc -l | awk '{$1=$1};1')

if [[ "$stagedFilesCount" -eq "0" ]]; then
echo "No staged demo/ or library/ files found when running git diff --staged | grep -E '(library|demo)'"
echo "***********************************************"
exit 0
fi

echo "Found $stagedFilesCount staged files."
echo git diff --staged --name-only | grep -E '(library|demo)'
echo ""
echo "Running ktFormat to fix any auto-fixable issues"

./gradlew ktFormat
Expand All @@ -22,13 +30,17 @@ echo ""

./gradlew ktLint

error_count=$(grep "<error" build/reports/ktlint/main-lint.xml | wc -l | awk '{$1=$1};1')
errorCount=$(grep "<error" build/reports/ktlint/main-lint.xml | wc -l | awk '{$1=$1};1')

if [[ "$error_count" -ne "0" ]]; then
if [[ "$errorCount" -ne "0" ]]; then
echo ""
echo "***********************************************"
echo "ktLint failed with a count of $error_count errors."
echo "For more info, check build/reports/ktlint/main-lint.xml"
echo "Here's the first 20 lines of that lint report"
echo ""
head -20 build/reports/ktlint/main-lint.xml
echo "***********************************************"
exit 1
fi

Expand Down

0 comments on commit 68c542c

Please sign in to comment.