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

Use JGit to filter newly added/modified sql queries only for codegen #188

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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)
swanandvk marked this conversation as resolved.
Show resolved Hide resolved
.build()
val git = Git(repo)
return git.diff().call().map { diffEntry -> diffEntry.newPath }
}
}