diff --git a/CHANGELOG.md b/CHANGELOG.md index dfc54661..32c05615 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ### Fixed - Pre-commit hook causing conflicts ([issue: #443](https://github.com/JLLeitschuh/ktlint-gradle/issues/443)) ([#502](https://github.com/JLLeitschuh/ktlint-gradle/pull/502)) - `ktlintFormat` create empty directories in `src/` dir ([issue: #423](https://github.com/JLLeitschuh/ktlint-gradle/issues/423)) + - Add Git hook task breaks configuration cache ([issue: #505](https://github.com/JLLeitschuh/ktlint-gradle/issues/505)) ### Removed - ? 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 a439c813..904ed861 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt @@ -2,9 +2,12 @@ package org.jlleitschuh.gradle.ktlint import org.eclipse.jgit.lib.RepositoryBuilder import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.ProjectLayout import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.Property import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory import org.gradle.api.tasks.TaskAction import org.intellij.lang.annotations.Language import org.jlleitschuh.gradle.ktlint.tasks.BaseKtLintCheckTask @@ -133,7 +136,8 @@ private fun KtlintPlugin.PluginHolder.addInstallGitHookCheckTask() { } open class KtlintInstallGitHookTask @Inject constructor( - objectFactory: ObjectFactory + objectFactory: ObjectFactory, + projectLayout: ProjectLayout ) : DefaultTask() { @get:Input internal val taskName: Property = objectFactory.property(String::class.java) @@ -144,9 +148,19 @@ open class KtlintInstallGitHookTask @Inject constructor( @get:Input internal val hookName: Property = objectFactory.property(String::class.java) + @get:InputDirectory + internal val projectDir: DirectoryProperty = objectFactory.directoryProperty().apply { + set(projectLayout.projectDirectory) + } + + @get:InputDirectory + internal val rootDirectory: DirectoryProperty = objectFactory.directoryProperty().apply { + set(project.rootDir) + } + @TaskAction fun installHook() { - val repo = RepositoryBuilder().findGitDir(project.projectDir).setMustExist(false).build() + val repo = RepositoryBuilder().findGitDir(projectDir.get().asFile).setMustExist(false).build() if (!repo.objectDatabase.exists()) { logger.warn("No git folder was found!") return @@ -159,7 +173,7 @@ open class KtlintInstallGitHookTask @Inject constructor( gitHookFile.createNewFile() gitHookFile.setExecutable(true) } - val gradleRootDirPrefix = project.rootDir.relativeTo(repo.workTree).path + val gradleRootDirPrefix = rootDirectory.get().asFile.relativeTo(repo.workTree).path if (gitHookFile.length() == 0L) { gitHookFile.writeText( diff --git a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/ConfigurationCacheTest.kt b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/ConfigurationCacheTest.kt index d3b81c85..e412eeee 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/ConfigurationCacheTest.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/ConfigurationCacheTest.kt @@ -93,4 +93,54 @@ class ConfigurationCacheTest : AbstractPluginTest() { } } } + + @DisplayName("Should support configuration cache for git hook format install task") + @CommonTest + internal fun configurationCacheForGitHookFormatInstallTask(gradleVersion: GradleVersion) { + project(gradleVersion) { + projectPath.initGit() + + build( + configurationCacheFlag, + configurationCacheWarnFlag, + INSTALL_GIT_HOOK_FORMAT_TASK + ) { + assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + build( + configurationCacheFlag, + configurationCacheWarnFlag, + INSTALL_GIT_HOOK_FORMAT_TASK + ) { + assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(output).contains("Reusing configuration cache.") + } + } + } + + @DisplayName("Should support configuration cache for git hook check install task") + @CommonTest + internal fun configurationCacheForGitHookCheckInstallTask(gradleVersion: GradleVersion) { + project(gradleVersion) { + projectPath.initGit() + + build( + configurationCacheFlag, + configurationCacheWarnFlag, + INSTALL_GIT_HOOK_CHECK_TASK + ) { + assertThat(task(":$INSTALL_GIT_HOOK_CHECK_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + } + + build( + configurationCacheFlag, + configurationCacheWarnFlag, + INSTALL_GIT_HOOK_CHECK_TASK + ) { + assertThat(task(":$INSTALL_GIT_HOOK_CHECK_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(output).contains("Reusing configuration cache.") + } + } + } } 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 c09f4bc0..8ddda77d 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt @@ -1,7 +1,6 @@ package org.jlleitschuh.gradle.ktlint import org.assertj.core.api.Assertions.assertThat -import org.eclipse.jgit.lib.RepositoryBuilder import org.gradle.testkit.runner.TaskOutcome import org.gradle.util.GradleVersion import org.jlleitschuh.gradle.ktlint.testdsl.CommonTest @@ -219,12 +218,6 @@ class GitHookTasksTest : AbstractPluginTest() { } } - private fun File.initGit(): File { - val repo = RepositoryBuilder().setWorkTree(this).setMustExist(false).build() - repo.create() - return repo.directory - } - private fun File.preCommitGitHook(): File = gitHookFolder().resolve("pre-commit") private fun File.gitHookFolder(): File = resolve("hooks/") diff --git a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/TestsCommon.kt b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/TestsCommon.kt index 01f47ea7..5cc50478 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/TestsCommon.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/TestsCommon.kt @@ -1,5 +1,6 @@ package org.jlleitschuh.gradle.ktlint +import org.eclipse.jgit.lib.RepositoryBuilder import org.intellij.lang.annotations.Language import java.io.File @@ -38,3 +39,9 @@ fun File.kotlinPluginProjectSetup( """.trimIndent() ) } + +internal fun File.initGit(): File { + val repo = RepositoryBuilder().setWorkTree(this).setMustExist(false).build() + repo.create() + return repo.directory +}