Commit 330d7450 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

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

parent ab0ebd01
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
@@ -41,3 +41,5 @@ nb-configuration.xml

# Plugin directory
/.quarkus/cli/plugins/

/git-hook.log
+28 −1
Changes for pom.xml: 28 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@

    <groupId>org.etsi.osl.hypo.core</groupId>
    <artifactId>common</artifactId>
    <version>1.6.3</version>
    <version>1.8.0</version>

    <properties>
        <!-- Quarkus Platform (MUST use io.quarkus.platform for full BOM extension resolution) -->
@@ -94,6 +94,10 @@

    <build>
        <plugins>
            <plugin>
                <groupId>com.diffplug.spotless</groupId>
                <artifactId>spotless-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-maven-plugin</artifactId>
@@ -131,6 +135,29 @@
                    </annotationProcessorPaths>
                </configuration>
            </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>
            <!-- other plugins... -->
        </plugins>
    </build>