Compare commits

...

2 Commits

Author SHA1 Message Date
Pierre Tellier
e3393c8834 test
All checks were successful
Format / formatting (push) Successful in 6s
Build / build (push) Successful in 28s
CI / build (push) Successful in 10s
2025-03-09 21:20:20 +01:00
Pierre Tellier
5f8fe4a374 feat: precommit hook for google java format 2025-03-09 21:19:00 +01:00
2 changed files with 26 additions and 0 deletions

2
hooks/README.md Normal file
View File

@ -0,0 +1,2 @@
# Useful hooks in this project
To use, just add the content of the wanted hook in `.git/hook/pre-commit`. You may need to use `chmod +x pre-commit`

24
hooks/google-java-format Executable file
View File

@ -0,0 +1,24 @@
#!/bin/bash
# Path to the Google Java Formatter JAR
FORMATTER_JAR="$HOME/.local/share/java/google-java-format.jar"
# Download the Google Java Formatter JAR if it doesn't exist
if [ ! -f "$FORMATTER_JAR" ]; then
echo "Downloading Google Java Formatter..."
mkdir -p "$(dirname "$FORMATTER_JAR")"
curl -L -o "$FORMATTER_JAR" https://github.com/google/google-java-format/releases/download/v1.20.0/google-java-format-1.20.0-all-deps.jar
fi
# Format all staged Java files
STAGED_JAVA_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "\.java$")
if [ -n "$STAGED_JAVA_FILES" ]; then
echo "Formatting Java files..."
java -jar "$FORMATTER_JAR" --skip-sorting-imports --skip-reflowing-long-strings --aosp --replace $STAGED_JAVA_FILES
# Re-stage the formatted files
git add $STAGED_JAVA_FILES
fi
exit 0