From edc7cc6812a8ea89b6ad66242d136f825649a89f Mon Sep 17 00:00:00 2001 From: PhilKes Date: Tue, 29 Oct 2024 17:47:29 +0100 Subject: [PATCH] Fix ktfmt pre-commit hook only add staged files --- .scripts/pre-commit | 36 +++++++++++++------------- .scripts/pre-commit.bat | 56 +++++++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/.scripts/pre-commit b/.scripts/pre-commit index 2f60fd92..d80e53bd 100755 --- a/.scripts/pre-commit +++ b/.scripts/pre-commit @@ -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 \ No newline at end of file +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" diff --git a/.scripts/pre-commit.bat b/.scripts/pre-commit.bat index e68a053a..cf2e19d6 100644 --- a/.scripts/pre-commit.bat +++ b/.scripts/pre-commit.bat @@ -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," ) -exit /b 0 \ No newline at end of file +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