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

fix: pre-commit file

parent 117cdfce
Loading
Loading
Loading
Loading
+21 −11
Changes for .githooks/pre-commit: 21 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -7,21 +7,31 @@ if [ -d "$GIT_DIR/rebase-merge" ] || [ -d "$GIT_DIR/rebase-apply" ] || [ -f "$GI
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

# 2. Get staged Java files (relative list for git add, absolute list for Spotless)
STAGED_RELATIVE=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.java$')
# 3. Get staged Java files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.java$')

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

# Convert to absolute paths separated by commas
STAGED_ABSOLUTE=$(echo "$STAGED_RELATIVE" | sed "s|^|$REPO_ROOT/|" | paste -sd, -)

# 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..."

# 3. Apply Spotless only to staged files
./mvnw spotless:apply -DspotlessFiles="$STAGED_ABSOLUTE"
# 4. Apply Spotless
$MVN_CMD spotless:apply -DspotlessFiles="$FILES_PARAM" -q
STATUS=$?

if [ $STATUS -ne 0 ]; then
@@ -29,7 +39,7 @@ if [ $STATUS -ne 0 ]; then
    exit 1
fi

# 4. Crucial: Re-stage the formatted files into the current commit
echo "$STAGED_RELATIVE" | xargs -r git add
# 5. Re-stage formatted files
echo "$STAGED_FILES" | xargs -r git add

exit 0
 No newline at end of file