Commit 0474acb3 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

refactor: introduce .githooks/pre-commit script to call spotless:apply before commiting changes

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

# Plugin directory
/.quarkus/cli/plugins/

/git-hook.log
+24 −0
Changes for pom.xml: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -425,6 +425,30 @@
          </execution>
        </executions>
      </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>