Fix pre-commit hook path handling for subdirectories - #996
Open
hey-cube wants to merge 4 commits into
Open
Conversation
JLLeitschuh
requested changes
Nov 10, 2025
JLLeitschuh
left a comment
Owner
There was a problem hiding this comment.
This looks good! It's going to require an addition to the CHANGELOG.md file in the root directory too though
hey-cube
marked this pull request as draft
November 11, 2025 07:25
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.
hey-cube
force-pushed
the
fix/pre-commit-hook-relative-path
branch
from
November 11, 2025 07:30
1dbd94f to
4efc1a1
Compare
Author
Author
|
In 4efc1a1, I fixed the Windows path separator issue. Should this also be mentioned in CHANGELOG.md? |
hey-cube
marked this pull request as ready for review
November 11, 2025 07:41
JLLeitschuh
approved these changes
Nov 12, 2025
JLLeitschuh
left a comment
Owner
There was a problem hiding this comment.
This looks good to me! @wakingrufus do you want to take a pass as well?
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes pre-commit hooks failing when the Gradle project is in a subdirectory of the Git repository.
Fixes #374