From 8722d01c4a1cfe39dac4ca2a4f87cd0cb5b08f46 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Wed, 28 Apr 2021 10:26:15 +1000 Subject: [PATCH 1/2] git hook: add version number and validation --- CHANGELOG.md | 2 +- .../org/jlleitschuh/gradle/ktlint/GitHook.kt | 16 +++++++++++++++- .../gradle/ktlint/tasks/BaseKtLintCheckTask.kt | 5 +++++ .../gradle/ktlint/GitHookTasksTest.kt | 11 +++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa60201d..025e5473 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [10.1.0-SNAPSHOT] Unreleased ### Added - - ? + - Added git hook update notifier ([#474](https://github.com/JLLeitschuh/ktlint-gradle/pull/474)) ### Changed - Updated Gradle to `6.8.3` version - Updated default KtLint version to `0.41.0` 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 732b2bff..72a05a4b 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt @@ -12,6 +12,19 @@ import javax.inject.Inject internal const val FILTER_INCLUDE_PROPERTY_NAME = "internalKtlintGitFilter" +/** + * The version of the git hook, + * + * When this value is passed to the gradle task it is validated to ensure users don't have outdated + * ktlint git hooks installed. + * + * This should be manually updated when changes are made to the git hook. + * + * TODO: Implement a CI/build step that will look for changes to either GitHook.kt or the git hook output and + * automatically increment this value + */ +internal const val hookVersion = "1" + @Language("Bash") internal val shShebang = """ @@ -32,7 +45,7 @@ private fun generateGradleCommand( } else { "./gradlew" } - return "$gradleCommand --quiet $taskName -P$FILTER_INCLUDE_PROPERTY_NAME=\"${'$'}CHANGED_FILES\"" + return "$gradleCommand --quiet $taskName -P$FILTER_INCLUDE_PROPERTY_NAME=\"${'$'}CHANGED_FILES\" -phookVersion=$hookVersion" } private fun generateGitCommand( @@ -82,6 +95,7 @@ internal fun generateGitHook( ${generateGradleCommand(taskName, gradleRootDirPrefix)} echo "Completed ktlint run." + ${postCheck(shouldUpdateCommit)} git stash pop diff --git a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt index ac8601e2..d59dcba1 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/tasks/BaseKtLintCheckTask.kt @@ -30,6 +30,7 @@ import org.jlleitschuh.gradle.ktlint.FILTER_INCLUDE_PROPERTY_NAME import org.jlleitschuh.gradle.ktlint.KOTLIN_EXTENSIONS import org.jlleitschuh.gradle.ktlint.applyGitFilter import org.jlleitschuh.gradle.ktlint.getEditorConfigFiles +import org.jlleitschuh.gradle.ktlint.hookVersion import org.jlleitschuh.gradle.ktlint.intermediateResultsBuildDir import org.jlleitschuh.gradle.ktlint.property import org.jlleitschuh.gradle.ktlint.worker.KtLintWorkAction @@ -89,6 +90,10 @@ abstract class BaseKtLintCheckTask @Inject constructor( init { if (project.hasProperty(FILTER_INCLUDE_PROPERTY_NAME)) { + // if FILTER_INCLUDE_PROPERTY_NAME exists then we are invoked from a git hook, check hook version + if (project.findProperty(hookVersion) != hookVersion) { + logger.warn("Your ktlint git hook is outdated, please update by running the addKtlint*GitPreCommitHook Gradle task.") + } applyGitFilter() } else { KOTLIN_EXTENSIONS.forEach { 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 ea4005dc..6e919779 100644 --- a/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt +++ b/plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt @@ -184,6 +184,17 @@ class GitHookTasksTest : AbstractPluginTest() { } } + @Test + internal fun `Git hook should send the hook version to gradle`() { + projectRoot.setupGradleProject() + val gitDir = projectRoot.initGit() + + build(":$INSTALL_GIT_HOOK_FORMAT_TASK").run { + assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS) + assertThat(gitDir.preCommitGitHook().readText()).contains("""-phookVersion=$hookVersion""") + } + } + private fun File.initGit(): File { val repo = RepositoryBuilder().setWorkTree(this).setMustExist(false).build() repo.create() From 5f93615a17041407b958aadbd03cc8643c16a750 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Wed, 28 Apr 2021 10:51:24 +1000 Subject: [PATCH 2/2] git hook: fix formatting issue --- plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt | 2 +- 1 file changed, 1 insertion(+), 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 72a05a4b..e80cce7e 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt @@ -65,7 +65,7 @@ private fun postCheck( git add ${'$'}file fi done - """.trimIndent() + """.trim('\n', ' ') } else { "" }