-
Notifications
You must be signed in to change notification settings - Fork 176
Fix pre-commit hook path handling for subdirectories #996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix pre-commit hook path handling for subdirectories #996
Conversation
JLLeitschuh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good! It's going to require an addition to the CHANGELOG.md file in the root directory too though
When the Gradle project root differs from the Git repository root (e.g., Gradle is in a subdirectory like 'project/submodule/'), the pre-commit hooks generated by ktlint-gradle fail to properly format files because of path mismatches. This change fixes the issue by: - Using `git diff --relative=$gradleRootDirPrefix` to output paths relative to the Gradle project root - Adding proper file path prefix handling in the git add command when checking if files exist and staging them This ensures that when git outputs paths like "src/main/Foo.kt" (relative to project/submodule/), the hook correctly references them as "project/submodule/src/main/Foo.kt" when checking file existence and staging. Fixes JLLeitschuh#374
Added four new test cases to verify the fix for pre-commit hook path handling when Gradle projects are in subdirectories: 1. checkHookUsesRelativeFlagInSubdirectory: Verifies that check hooks use --relative flag when Gradle is in a subdirectory 2. formatHookUsesRelativeFlagInSubdirectory: Verifies that format hooks use --relative flag and correctly prefix file paths in git add commands 3. checkHookDoesNotUseRelativeFlagAtGitRoot: Verifies that check hooks do not use --relative flag when Gradle is at git root 4. formatHookDoesNotUseRelativeFlagAtGitRoot: Verifies that format hooks do not use --relative flag or path prefixes when Gradle is at git root
The gradleRootDirPrefix was using platform-specific path separators (backslash on Windows), but git hook scripts always require forward slashes. This caused tests to fail on Windows. Changed to always use forward slashes by replacing File.separator with "/" when constructing gradleRootDirPrefix. Fixes test failures on windows-latest CI runners.
1dbd94f to
4efc1a1
Compare
|
In 4efc1a1, I fixed the Windows path separator issue. Should this also be mentioned in CHANGELOG.md? |
JLLeitschuh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! @wakingrufus do you want to take a pass as well?
Fixes pre-commit hooks failing when the Gradle project is in a subdirectory of the Git repository.
Fixes #374