diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c63ed11..d79801a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +### Fixed + +- git `pre-commit` hook should now work in all worktrees [#605](https://github.com/JLLeitschuh/ktlint-gradle/pull/605) + ## [11.0.0] - 2022-08-24 ### Changed diff --git a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt index 607874ae..7fbde572 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt @@ -79,7 +79,9 @@ internal fun generateGitHook( echo "Running ktlint over these files:" echo "${'$'}CHANGED_FILES" - diff=.git/unstaged-ktlint-git-hook.diff + GITDIR=${'$'}(git rev-parse --git-dir) + diff=${'$'}GITDIR/unstaged-ktlint-git-hook.diff + git diff --color=never > ${'$'}diff if [ -s ${'$'}diff ]; then git apply -R ${'$'}diff diff --git a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt index d1046c59..b3f2d6b8 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt @@ -245,6 +245,8 @@ class GitHookTasksTest : AbstractPluginTest() { assertThat(hookText).contains("git apply -R \$diff") assertThat(hookText).contains("git apply --ignore-whitespace \$diff") assertThat(hookText).contains("rm \$diff") + assertThat(hookText).contains("GITDIR=\$(git rev-parse --git-dir)") + assertThat(hookText).contains("diff=\$GITDIR/unstaged-ktlint-git-hook.diff") } } }