Skip to content

Commit

Permalink
git hook: add version number and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinIsland committed Apr 28, 2021
1 parent 51b2c4b commit c17ea02
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
15 changes: 14 additions & 1 deletion plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"""
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c17ea02

Please sign in to comment.