This is a simple Gradle plugin for installing a custom ktlint Git pre-commit hook script.
The advantages over using ktlint's built-in installGitPreCommitHook
command are:
-
The
ktlint
JAR is downloaded by the plugin from the Maven Central repository, which means that the JAR doesn't need to be pre-installed on the system -
The custom hook script will first stash away any unstaged changes before running
ktlint
, so that only the changes that will actually be committed will be inspected (the officialgit-stash
command is not used for this) -
If committing via IntelliJ IDEA / Android Studio, then absolute paths are printed instead of relative ones. This results in those paths being clickable in the IDE
This plugin's custom hook script was greatly inspired by JLLeitschuh/ktlint-gradle.
The main advantages/fixes of this plugin's hook script are:
-
The script doesn't execute Gradle, it executes the
ktlint
JAR directly, which (depending on the project's size) can potentially shave off multiple seconds of the hook's execution time -
The script handles stashing binary files correctly
-
The script supports the Git config
diff.noprefix = true
Note that the hook script requires at least Git version
2.35.0
.
Check your installed Git version with:git --version
The minimum supported ktlint version is 0.50.0
.
Kotlin script & plugins DSL
plugins {
id("io.github.mfederczuk.ktlint") version "0.1.0-indev09"
}
repositories {
// make sure that Maven Central is configured as an artifact repository
mavenCentral()
}
ktlint {
version.set("1.2.1") // sets the version of ktlint
// the following configuration properties are optional
flags.limit.set(5) // adds the --limit=5 flag to ktlint
gitPreCommitHook {
disableAbortCommitOnErrors() // disables aborting the commit on lint errors
enableInstallOnBuild() // automatically installs the hook every time a build is started
}
}
Kotlin script & legacy plugin application
buildscript {
repositories {
maven("https://plugins.gradle.org/m2/")
}
dependencies {
classpath("io.github.mfederczuk:ktlint-gradle-plugin:0.1.0-indev09")
}
}
apply(plugin = "io.github.mfederczuk.ktlint")
repositories {
// make sure that Maven Central is configured as an artifact repository
mavenCentral()
}
ktlint {
version.set("1.2.1") // sets the version of ktlint
// the following configuration properties are optional
flags.limit.set(5) // adds the --limit=5 flag to ktlint
gitPreCommitHook {
disableAbortCommitOnErrors() // disables aborting the commit on lint errors
enableInstallOnBuild() // automatically installs the hook every time a build is started
}
}
Groovy & plugins DSL
plugins {
id 'io.github.mfederczuk.ktlint' version '0.1.0-indev09'
}
repositories {
// make sure that Maven Central is configured as an artifact repository
mavenCentral()
}
ktlint {
version = '1.2.1' // sets the version of ktlint
// the following configuration properties are optional
flags.limit = 5 // adds the --limit=5 flag to ktlint
gitPreCommitHook {
disableAbortCommitOnErrors() // disables aborting the commit on lint errors
enableInstallOnBuild() // automatically installs the hook every time a build is started
}
}
Groovy & legacy plugin application
buildscript {
repositories {
maven { url 'https://plugins.gradle.org/m2/' }
}
dependencies {
classpath 'io.github.mfederczuk:ktlint-gradle-plugin:0.1.0-indev09'
}
}
apply plugin: 'io.github.mfederczuk.ktlint'
repositories {
// make sure that Maven Central is configured as an artifact repository
mavenCentral()
}
ktlint {
version = '1.2.1' // sets the version of ktlint
// the following configuration properties are optional
flags.limit = 5 // adds the --limit=5 flag to ktlint
gitPreCommitHook {
disableAbortCommitOnErrors() // disables aborting the commit on lint errors
enableInstallOnBuild() // automatically installs the hook every time a build is started
}
}
- Configuration property to add the
--reporter=<reporterConfiguration>
flag toktlint
- Configuration property to add the
--ruleset=<rulesetJarPaths>
flag toktlint
- Configuration property to add the
--editorconfig=<editorConfigPath>
flag toktlint
- Configuration property to add the
--baseline
flag toktlint
- Configuration property to add the
--log-level=<minLogLevel>
flag toktlint
- Gradle task to run ktlint manually
- Configuration property to run ktlint over all Kotlin files, not just the ones that are staged
Read through the Contribution Guidelines if you want to contribute to this project.
ktlint GitHub Gradle Plugin is licensed under both the Mozilla Public License 2.0 AND
the Apache License 2.0.
For more information about copying and licensing, see the COPYING.txt
file.
(note that this project is not affiliated with ktlint or Pinterest Inc.)