Skip to content
1 change: 1 addition & 0 deletions cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ mainClassName = "norm.cli.NormCliKt"
dependencies {
implementation project(":codegen")
implementation "com.github.ajalt:clikt:2.7.1"
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.11.0.202103091610-r'
}

14 changes: 13 additions & 1 deletion cli/src/main/kotlin/norm/cli/NormCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import norm.api.NormApi
import norm.fs.IO
import norm.fs.globSearch
import norm.util.withPgConnection
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import java.io.File


Expand Down Expand Up @@ -67,7 +69,9 @@ class NormCli : CliktCommand( // command name is inferred as norm-cli

// If dir is provided, relativize to itself
inputDir?.let { dir ->
globSearch(dir, "**.sql").forEach { sqlFile ->
val fileList =globSearch(dir, "**.sql")
val modifiedFiles = fileList.filter { modifiedFilesFromGitOnly().contains(it) }
modifiedFiles.forEach { sqlFile ->
IO(sqlFile, dir, outDir).process(normApi::generate)
}
}
Expand All @@ -77,6 +81,14 @@ class NormCli : CliktCommand( // command name is inferred as norm-cli
IO(sqlFile, basePath, outDir).process(normApi::generate)
}
}

fun modifiedFilesFromGitOnly(): List<String> {
val builder = FileRepositoryBuilder()
val repo = builder.setGitDir(File("." + "/.git")).setMustExist(true)
Comment thread
swanandvk marked this conversation as resolved.
Outdated
.build()
val git = Git(repo)
return git.diff().call().map { diffEntry -> diffEntry.newPath }
}
}