Skip to content

Conversation

@hey-cube
Copy link

@hey-cube hey-cube commented Nov 5, 2025

Fixes pre-commit hooks failing when the Gradle project is in a subdirectory of the Git repository.

Fixes #374

Copy link
Owner

@JLLeitschuh JLLeitschuh left a 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

@hey-cube hey-cube requested a review from JLLeitschuh November 11, 2025 07:19
@hey-cube 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 hey-cube force-pushed the fix/pre-commit-hook-relative-path branch from 1dbd94f to 4efc1a1 Compare November 11, 2025 07:30
@hey-cube
Copy link
Author

hey-cube commented Nov 11, 2025

I rebased because CHANGELOG.md was modified on the main branch.
ecbfb0e and cfc1aa1 remain unchanged.

@hey-cube
Copy link
Author

In 4efc1a1, I fixed the Windows path separator issue. Should this also be mentioned in CHANGELOG.md?
Or would it be better to split this commit into a separate pull request?

@hey-cube hey-cube marked this pull request as ready for review November 11, 2025 07:41
Copy link
Owner

@JLLeitschuh JLLeitschuh left a 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gradlew and .git is not in the same root folder- addKtlintCheckGitPreCommitHook always successfully even i broke the rule [pre-commit]

2 participants