Skip to content

Commit

Permalink
Fix ktfmt pre-commit hook only add staged files
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilKes committed Oct 29, 2024
1 parent 014b4f5 commit edc7cc6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
32 changes: 16 additions & 16 deletions .scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -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"
if [ -z "$initial_staged_files" ]; then
echo "No Kotlin files staged for commit."
exit 0
fi

# Check if the formatting command was successful
if [ $? -ne 0 ]; then
echo "Kotlin formatting failed. Please fix the issues."
exit 1
fi
formatted_files=$(echo "$initial_staged_files" | sed 's|^app/||' | paste -sd "," -)
echo "Formatting Kotlin files: $formatted_files"
./gradlew ktfmtPrecommit --include-only="$formatted_files"

# 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 [ $? -ne 0 ]; then
echo "Kotlin formatting failed. Please fix the issues."
exit 1
fi

exit 0
# Re-stage only the initially staged Kotlin files
echo "$initial_staged_files" | xargs git add

echo "Kotlin files formatted"
56 changes: 31 additions & 25 deletions .scripts/pre-commit.bat
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit edc7cc6

Please sign in to comment.