From 5d45a2dc6269367e434c214e3ccb327cb2328e38 Mon Sep 17 00:00:00 2001 From: Rafal Harabien Date: Fri, 15 Oct 2021 20:34:42 +0200 Subject: [PATCH] Do not mark entire root directory as input for git hook task --- .../org/jlleitschuh/gradle/ktlint/GitHook.kt | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 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 904ed861..0748c81f 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt @@ -2,12 +2,10 @@ 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 @@ -79,24 +77,24 @@ internal fun generateGitHook( echo "Running ktlint over these files:" echo "${'$'}CHANGED_FILES" - + diff=.git/unstaged-ktlint-git-hook.diff git diff --color=never > ${'$'}diff if [ -s ${'$'}diff ]; then git apply -R ${'$'}diff fi - + ${generateGradleCommand(taskName, gradleRootDirPrefix)} echo "Completed ktlint run." ${postCheck(shouldUpdateCommit)} - + if [ -s ${'$'}diff ]; then git apply --ignore-whitespace ${'$'}diff fi rm ${'$'}diff unset diff - + echo "Completed ktlint hook." """.trimIndent() @@ -148,19 +146,13 @@ 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) - } + private val projectDir = projectLayout.projectDirectory.asFile - @get:InputDirectory - internal val rootDirectory: DirectoryProperty = objectFactory.directoryProperty().apply { - set(project.rootDir) - } + private val rootDirectory = project.rootDir @TaskAction fun installHook() { - val repo = RepositoryBuilder().findGitDir(projectDir.get().asFile).setMustExist(false).build() + val repo = RepositoryBuilder().findGitDir(projectDir).setMustExist(false).build() if (!repo.objectDatabase.exists()) { logger.warn("No git folder was found!") return @@ -173,7 +165,7 @@ open class KtlintInstallGitHookTask @Inject constructor( gitHookFile.createNewFile() gitHookFile.setExecutable(true) } - val gradleRootDirPrefix = rootDirectory.get().asFile.relativeTo(repo.workTree).path + val gradleRootDirPrefix = rootDirectory.relativeTo(repo.workTree).path if (gitHookFile.length() == 0L) { gitHookFile.writeText(