Commit 27a03907 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

refactor: introduce automatic pre-commit git hook for spotless:apply

parent 69ac46c7
Loading
Loading
Loading
Loading
Loading

.githooks/pre-commit

0 → 100755
+45 −0
Changes for .githooks/pre-commit: 45 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/bin/sh

# 1. Skip if Git is in the middle of a rebase, cherry-pick, or merge
GIT_DIR="$(git rev-parse --git-dir)"
if [ -d "$GIT_DIR/rebase-merge" ] || [ -d "$GIT_DIR/rebase-apply" ] || [ -f "$GIT_DIR/MERGE_HEAD" ]; then
    exit 0
fi

REPO_ROOT="$(git rev-parse --show-toplevel)"
cd "$REPO_ROOT" || exit 1

# 2. Locate mvnw (or fallback to mvn)
if [ -x "./mvnw" ]; then
    MVN_CMD="./mvnw"
elif command -v mvn >/dev/null 2>&1; then
    MVN_CMD="mvn"
else
    echo "⚠️ Neither ./mvnw nor system mvn found. Skipping Spotless formatting."
    exit 0
fi

# 3. Get staged Java files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.java$')

if [ -z "$STAGED_FILES" ]; then
    exit 0
fi

# Convert files to comma-separated list
# New (prefix with .*/ or regex escape):
FILES_PARAM=$(echo "$STAGED_FILES" | sed 's|^|.*/|' | paste -sd, -)
echo "✨ Auto-formatting staged Java files with Spotless..."
# 4. Apply Spotless
$MVN_CMD spotless:apply -DspotlessFiles="$FILES_PARAM" -q
STATUS=$?

if [ $STATUS -ne 0 ]; then
    echo "❌ Spotless apply failed. Aborting commit." >&2
    exit 1
fi

# 5. Re-stage formatted files
echo "$STAGED_FILES" | xargs -r git add

exit 0
 No newline at end of file
+2 −0
Changes for .gitignore: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -43,3 +43,5 @@ nb-configuration.xml
/.quarkus/cli/plugins/
# TLS Certificates
.certs/

/git-hook.log
+22 −0
Changes for pom.xml: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -139,6 +139,28 @@
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>configure-git-hooks</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>git</executable>
                            <arguments>
                                <argument>config</argument>
                                <argument>core.hooksPath</argument>
                                <argument>.githooks</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>