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 #!/bin/sh
# Count the number of staged Kotlin files # Capture the list of initially staged Kotlin files
staged_files_count=$(git diff --name-only --cached --numstat -- '*.kt' | wc -l) initial_staged_files=$(git diff --name-only --cached -- '*.kt')
# Format only if there are Kotlin files in git's index if [ -z "$initial_staged_files" ]; then
if [ "$staged_files_count" -gt 0 ]; then echo "No Kotlin files staged for commit."
# Format the staged Kotlin files and remove the "app/" prefix exit 0
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."
fi 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 @echo off
setlocal enabledelayedexpansion
rem Count the number of staged Kotlin files rem Capture the list of initially staged Kotlin files
for /f %%i in ('git diff --name-only --cached --numstat -- "*.kt" ^| find /c /v ""') do set staged_files_count=%%i set "initial_staged_files="
for /f "delims=" %%f in ('git diff --name-only --cached -- "*.kt"') do (
rem Format only if there are Kotlin files in git's index set "initial_staged_files=!initial_staged_files! %%f,"
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.
) )
exit /b 0 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