Skip to content

Commit

Permalink
Do not mark entire root directory as input for git hook task
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafal Harabien committed Oct 15, 2021
1 parent ac312e3 commit 5d45a2d
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -148,19 +146,13 @@ 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)
}
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
Expand All @@ -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(
Expand Down

0 comments on commit 5d45a2d

Please sign in to comment.