Commit dae427e8 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

refactor: introduce pre-commit spotless:apply

parent 558ffe85
Loading
Loading
Loading
Loading
Loading

.githooks/pre-commit

0 → 100755
+46 −0
Changes for .githooks/pre-commit: 46 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
FILES_PARAM=$(echo "$STAGED_FILES" | 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
+25 −0
Changes for pom.xml: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -227,6 +227,31 @@
        <artifactId>spotless-maven-plugin</artifactId>
      </plugin>

        <!-- 2. Exec Plugin (this is the one that has the 'exec' goal) -->
        <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>


        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
      </plugin>