From 5cf05f4e19fa3fae5eabbf4fb7df49ed3b7fa4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20B=C3=B8hmer?= Date: Wed, 7 Sep 2022 09:07:07 +0200 Subject: [PATCH 1/4] Improve execution in other then main worktree --- .../src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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..3f4403fd 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 From 9675a43a00160a12a955dff6f166226bb0edc681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20B=C3=B8hmer?= Date: Thu, 8 Sep 2022 09:51:25 +0200 Subject: [PATCH 2/4] Fix string interpolation --- .../src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 3f4403fd..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,8 +79,8 @@ internal fun generateGitHook( echo "Running ktlint over these files:" echo "${'$'}CHANGED_FILES" - GITDIR=$(git rev-parse --git-dir) - diff=$GITDIR/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 From d48ceb09dc6ef3942498a5ca9c261c9d8cd8ce6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20B=C3=B8hmer?= Date: Wed, 5 Oct 2022 13:34:16 +0200 Subject: [PATCH 3/4] Update CHANGELOG.md with #605 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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 From 2c5c401c13a0576d15f9b86032f5c3b9ee8e1e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20B=C3=B8hmer?= Date: Wed, 5 Oct 2022 13:50:36 +0200 Subject: [PATCH 4/4] Unit test for #605 --- .../kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt | 2 ++ 1 file changed, 2 insertions(+) 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") } } }