From c17ea027c810272b6749eb454164275d048d73c8 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Wed, 28 Apr 2021 10:26:15 +1000 Subject: [PATCH] git hook: add version number and validation --- CHANGELOG.md | 2 +- .../org/jlleitschuh/gradle/ktlint/GitHook.kt | 15 ++++++++++++++- .../gradle/ktlint/tasks/BaseKtLintCheckTask.kt | 4 ++++ .../jlleitschuh/gradle/ktlint/GitHookTasksTest.kt | 11 +++++++++++ 4 files changed, 30 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..658316e1 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( 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..53e3d607 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 @@ -88,6 +89,9 @@ abstract class BaseKtLintCheckTask @Inject constructor( } init { + if (project.findProperty(hookVersion)?.equals(hookVersion) == false) { + throw GradleException("Your ktlint git hook is outdated, please update by running the addKtlint*GitPreCommitHook Gradle task.") + } if (project.hasProperty(FILTER_INCLUDE_PROPERTY_NAME)) { applyGitFilter() } else { 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()