Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add git hook version #474

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [10.3.0-SNAPSHOT] - Unreleased
### Added
- Added git hook update notifier ([#474](https://github.com/JLLeitschuh/ktlint-gradle/pull/474))
- ?

### Changed
Expand Down
18 changes: 16 additions & 2 deletions plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -34,7 +47,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 All @@ -54,7 +67,7 @@ private fun postCheck(
git add ${'$'}file
fi
done
""".trimIndent()
""".trim('\n', ' ')
} else {
""
}
Expand Down Expand Up @@ -90,6 +103,7 @@ internal fun generateGitHook(
$gradleCommandExitCode=$?

echo "Completed ktlint run."

${postCheck(shouldUpdateCommit)}

if [ -s ${'$'}diff ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 @@ -96,6 +97,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,23 @@ 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""")
}
}
Comment on lines +274 to +283
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update this test to use the @CommonTest format above?


private fun File.initGit(): File {
val repo = RepositoryBuilder().setWorkTree(this).setMustExist(false).build()
repo.create()
return repo.directory
}

private fun File.preCommitGitHook(): File = gitHookFolder().resolve("pre-commit")

private fun File.gitHookFolder(): File = resolve("hooks/")
Expand Down