commonMain.co.touchlab.skie.plugin.analytics.git.GitAnalytics.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-plugin-impl-jvm Show documentation
Show all versions of gradle-plugin-impl-jvm Show documentation
Internal implementation for SKIE Gradle plugin.
package co.touchlab.skie.plugin.analytics.git
import co.touchlab.skie.configuration.SkieConfigurationFlag
import co.touchlab.skie.plugin.analytics.AnalyticsProducer
import co.touchlab.skie.plugin.util.toPrettyJson
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.ListBranchCommand
import org.eclipse.jgit.lib.PersonIdent
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import java.io.File
import java.text.Normalizer
data class GitAnalytics(
val numberOfContributors: Int,
val numberOfCommits: Int,
val numberOfBranches: Int,
val numberOfTags: Int,
) {
class Producer(
private val gitRoot: Provider,
) : AnalyticsProducer {
override val configurationFlag: SkieConfigurationFlag = SkieConfigurationFlag.Analytics_Git
override val name: String = "git"
override fun produce(): String {
val git = gitRoot.get()?.let { Git.open(it) } ?: return """{ "error": "git not found" }"""
return GitAnalytics(
numberOfContributors = git.log().all().call().map { it.authorIdent.normalizedName }.distinct().size,
numberOfCommits = git.log().all().call().count(),
numberOfBranches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call().size,
numberOfTags = git.tagList().call().size,
).toPrettyJson()
}
}
}
private val PersonIdent.normalizedName: String
get() = Normalizer.normalize(name, Normalizer.Form.NFC)
fun Project.getGitRoot(): Provider = provider {
projectDir.findGitRoot()
}
private tailrec fun File.findGitRoot(): File? =
if (resolve(".git").exists()) this else parentFile?.findGitRoot()