-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ktfmt pre-commit hook only add staged files
- Loading branch information
Showing
2 changed files
with
47 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |