Skip to content

Commit

Permalink
Merge pull request #526 from Tapchicoma/tapchicoma/505/git-hook-confi…
Browse files Browse the repository at this point in the history
…guration-cache

Fix install git hook task breaks configuration cache.
  • Loading branch information
Tapchicoma authored Sep 8, 2021
2 parents 5879f00 + d82899a commit d945407
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- ?
Expand Down
20 changes: 17 additions & 3 deletions plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String> = objectFactory.property(String::class.java)
Expand All @@ -144,9 +148,19 @@ open class KtlintInstallGitHookTask @Inject constructor(
@get:Input
internal val hookName: Property<String> = 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
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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/")
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
}

0 comments on commit d945407

Please sign in to comment.