Fix ktfmt pre-commit hook only add staged files

This commit is contained in:
PhilKes 2024-10-29 17:47:29 +01:00
parent 014b4f5f11
commit edc7cc6812
2 changed files with 49 additions and 43 deletions

View file

@ -1,23 +1,23 @@
#!/bin/sh
# Count the number of staged Kotlin files
staged_files_count=$(git diff --name-only --cached --numstat -- '*.kt' | wc -l)
# Capture the list of initially staged Kotlin files
initial_staged_files=$(git diff --name-only --cached -- '*.kt')
# Format only if there are Kotlin files in git's index
if [ "$staged_files_count" -gt 0 ]; then
# Format the staged Kotlin files and remove the "app/" prefix
formatted_files=$(git diff --name-only --cached -- '*.kt' | sed 's|^app/||' | paste -sd ",")
./gradlew ktfmtPrecommit --include-only="$formatted_files"
# Check if the formatting command was successful
if [ $? -ne 0 ]; then
echo "Kotlin formatting failed. Please fix the issues."
exit 1
fi
# Add the formatted Kotlin files to the staging area
git add -A $(git diff --name-only -- '*.kt')
echo "Kotlin files formatted and changes staged."
if [ -z "$initial_staged_files" ]; then
echo "No Kotlin files staged for commit."
exit 0
fi
exit 0
formatted_files=$(echo "$initial_staged_files" | sed 's|^app/||' | paste -sd "," -)
echo "Formatting Kotlin files: $formatted_files"
./gradlew ktfmtPrecommit --include-only="$formatted_files"
if [ $? -ne 0 ]; then
echo "Kotlin formatting failed. Please fix the issues."
exit 1
fi
# Re-stage only the initially staged Kotlin files
echo "$initial_staged_files" | xargs git add
echo "Kotlin files formatted"

View file

@ -1,29 +1,35 @@
@echo off
setlocal enabledelayedexpansion
rem Count the number of staged Kotlin files
for /f %%i in ('git diff --name-only --cached --numstat -- "*.kt" ^| find /c /v ""') do set staged_files_count=%%i
rem Format only if there are Kotlin files in git's index
if %staged_files_count% gtr 0 (
rem Format the staged Kotlin files and remove the "app/" prefix
for /f "delims=" %%f in ('git diff --name-only --cached -- "*.kt" ^| sed "s|^app/||"') do (
set formatted_files=%%f
set formatted_files=!formatted_files!, %%f
)
rem Remove the trailing comma if necessary
set formatted_files=%formatted_files:~, -1%
call gradlew ktfmtPrecommit --include-only="%formatted_files%"
rem Check if the formatting command was successful
if errorlevel 1 (
echo Kotlin formatting failed. Please fix the issues.
exit /b 1
)
rem Add the formatted Kotlin files to the staging area
git add -A git diff --name-only -- "*.kt"
echo Kotlin files formatted and changes staged.
rem Capture the list of initially staged Kotlin files
set "initial_staged_files="
for /f "delims=" %%f in ('git diff --name-only --cached -- "*.kt"') do (
set "initial_staged_files=!initial_staged_files! %%f,"
)
rem Check if there are any staged Kotlin files
if "%initial_staged_files%"=="" (
echo No Kotlin files staged for commit.
exit /b 0
)
rem Remove the trailing comma from the list of formatted files
set "formatted_files=%initial_staged_files:~0,-1%"
echo Formatting Kotlin files: %formatted_files%
call gradlew ktfmtPrecommit --include-only="%formatted_files%"
rem Check if the formatting command was successful
if errorlevel 1 (
echo Kotlin formatting failed. Please fix the issues.
exit /b 1
)
rem Re-stage only the initially staged Kotlin files
for %%f in (%initial_staged_files%) do (
git add "%%f"
)
echo Kotlin files formatted
exit /b 0