summaryrefslogtreecommitdiff
path: root/hooks/pre-commit
diff options
context:
space:
mode:
authorAmit Kumar <amitkma@e.email>2021-04-27 22:21:34 +0530
committerAmit Kumar <amitkma@e.email>2021-04-27 23:23:49 +0530
commit0f69164b62c5df0374bc0431de62267e8f2e750a (patch)
tree8112c170d0cb06c4de52af126bd2551d7cc38b76 /hooks/pre-commit
parent05fca2863a198db5d58fd55db6c5d7b67fd46e58 (diff)
Make pre-commit executable
Diffstat (limited to 'hooks/pre-commit')
-rwxr-xr-x[-rw-r--r--]hooks/pre-commit50
1 files changed, 41 insertions, 9 deletions
diff --git a/hooks/pre-commit b/hooks/pre-commit
index 00d361b..6e0b167 100644..100755
--- a/hooks/pre-commit
+++ b/hooks/pre-commit
@@ -1,14 +1,46 @@
-#!/bin/sh
+#!/bin/bash
-./gradlew spotlessCheck -q
-EXIT_CODE=$?
-if [ $EXIT_CODE -ne 0 ]; then
- echo "❌ spotlessCheck failed, running spotlessApply for you..."
+filesToAddAfterFormatting=()
+containsSpotlessEnabledFormats=0
- ./gradlew spotlessApply -q
+# Collect all files currently in staging area, and check if there are any spotless enabled format file
+# We currently support only *.kt, *.gradle', '*.md', '.gitignore' files.
+for entry in $(git status --porcelain | sed -r 's/[ \t]+/-/g'); do
+ # entry can be for example:
+ # MM-src/main/java/net/project/MyController.java
+ # -M-src/main/java/net/project/AnotherController.java
- echo "Formatting done, please try your commit again! If the problem persists, apply fixes manually."
- exit $EXIT_CODE
+ if [[ $entry == M* ]]; then
+ filesToAddAfterFormatting+=(${entry:2}) # strips the prefix
+ fi
+
+ if [[ $entry == *.kt ]] || [[ $entry == *.gradle ]] || [[ $entry == *.md ]] || [[ $entry == .gitignore ]]; then
+ containsSpotlessEnabledFormats=1
+ fi
+done
+
+# If any java or kotlin files are found, run spotlessApply
+if [ "$containsSpotlessEnabledFormats" == "1" ]; then
+ echo '[git hook] executing gradle spotlessCheck before commit'
+ ./gradlew spotlessCheck
+ EXIT_CODE=$?
+ if [ $EXIT_CODE -ne 0 ]; then
+ echo "❌ [git hook] spotlessCheck failed, running spotlessApply for you..."
+
+ ./gradlew spotlessApply
+
+ echo "[git hook] Formatting done, please try your commit again! If the problem persists, apply fixes manually."
+ exit $EXIT_CODE
+ fi
+ echo "✔️ [git hook] Spotless: Everything looks clean!"
+else
+ echo "[git hook] Not running spotless"
fi
-exit 0 \ No newline at end of file
+# Add the files that were in the staging area
+for file in "${filesToAddAfterFormatting[@]}"; do
+ echo "re-adding $file after formatting"
+ git add "$file"
+done
+
+exit 0