Skip to content

Instantly share code, notes, and snippets.

Last active November 11, 2022 09:45
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Gradle task that runs ktlint only over changed files
configurations { ktlint }
dependencies { ktlint "com.pinterest:ktlint:$ktlintVersion" }
task ktlintCi(type: JavaExec, group: "verification") {
description = "Run Kotlin linter on changed files."
group = "CI"
main = "com.pinterest.ktlint.Main"
classpath = configurations.ktlint
doFirst {
def changedFilesList = getDiffedFilesFromBranch("main")
def ignoredFiles = "!**/src/**/*Test.kt"
if (changedFilesList.isEmpty()) {
println("No kotlin files changed! Ignoring all files...")
ignoredFiles = "!**/src/**"
} else {
println("Running ktlint on the changed files:")
changedFilesList.each { println(it) }
def reportDir = "$buildDir/reports/ktlint"
def params = [
private static List<String> getDiffedFilesFromBranch(String branch) {
def cmd = "git diff --diff-filter=d --name-only origin/$branch --relative | grep '\\.kt\\?\$'"
def outputStream = new ByteArrayOutputStream()
['sh', '-c', cmd].execute().waitForProcessOutput(outputStream, System.err)
return mapOutputToStringList(outputStream)
//get double quoted, comma separated list of files
private static List<GString> mapOutputToStringList(ByteArrayOutputStream outputStream) {
if (outputStream.toString().isEmpty()) return []
return outputStream.toString()
.collect { "${it}.kt" }
Copy link


Copy link

iurysza commented Sep 12, 2021


@artur-eiji-ifood ❤️

Copy link

@iurysza which classpath do you use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment