Skip to content

Commit

Permalink
[patch] factoring digger git wrapper into a new class
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Jul 30, 2024
1 parent 5ac6265 commit 1297961
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package com.zegreatrob.tools.digger.core

import org.ajoberstar.grgit.Commit
import org.ajoberstar.grgit.Grgit
import java.io.File

fun Grgit.allContributionCommits(workingDirectory: File): List<Pair<TagRef?, List<Commit>>> {
val tagList = listTags(workingDirectory)
fun Grgit.allContributionCommits(gitDigger: DiggerGitWrapper): List<Pair<TagRef?, List<Commit>>> {
val tagList = gitDigger.listTags()
return log().fold(emptyList()) { acc, commit ->
val tag = tagList.find { it.commitId == commit.id }
if (tag != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,72 @@ import kotlinx.datetime.Instant
import java.io.File
import java.nio.charset.Charset

fun currentCommitTag(workingDirectory: File): TagRef? {
val firstTag = listTags(workingDirectory).maxByOrNull { it.dateTime }
val headCommitId = headCommitId(workingDirectory)
fun DiggerGitWrapper.currentCommitTag(): TagRef? {
val firstTag = listTags().maxByOrNull { it.dateTime }
val headCommitId = headCommitId()
return if (firstTag?.commitId != headCommitId) {
null
} else {
firstTag
}
}

fun headCommitId(workingDirectory: File): String {
val process =
ProcessBuilder(
listOf(
"git",
"--no-pager",
"rev-parse",
"HEAD",
),
)
.directory(workingDirectory)
.start()
val outputText = process.inputStream.readAllBytes().toString(Charset.defaultCharset())
val error = process.errorStream.readAllBytes().toString(Charset.defaultCharset())
process.waitFor()
if (error.isNotEmpty()) {
throw Error(error)
}
return outputText.trim()
}
class DiggerGitWrapper(private val workingDirectory: File) {

fun listTags(workingDirectory: File): List<TagRef> {
val process =
ProcessBuilder(
listOf(
"git",
"--no-pager",
"tag",
"--list",
"--format=%(refname:strip=2),%(*objectname),%(creatordate:iso-strict)",
),
)
.directory(workingDirectory)
.start()
val outputText = process.inputStream.readAllBytes().toString(Charset.defaultCharset())
val error = process.errorStream.readAllBytes().toString(Charset.defaultCharset())
process.waitFor()
val output = outputText.split("\n").mapNotNull {
val commaSplit = it.split(",")
if (commaSplit.size >= 3) {
TagRef(
name = commaSplit.subList(0, commaSplit.size - 2).joinToString(""),
commitId = commaSplit.reversed()[1],
dateTime = Instant.parse(commaSplit.last()),
fun headCommitId(): String {
val process =
ProcessBuilder(
listOf(
"git",
"--no-pager",
"rev-parse",
"HEAD",
),
)
} else {
null
.directory(workingDirectory)
.start()
val outputText = process.inputStream.readAllBytes().toString(Charset.defaultCharset())
val error = process.errorStream.readAllBytes().toString(Charset.defaultCharset())
process.waitFor()
if (error.isNotEmpty()) {
throw Error(error)
}
return outputText.trim()
}
if (error.isNotEmpty()) {
throw Error(error)

fun listTags(): List<TagRef> {
val process =
ProcessBuilder(
listOf(
"git",
"--no-pager",
"tag",
"--list",
"--format=%(refname:strip=2),%(*objectname),%(creatordate:iso-strict)",
),
)
.directory(workingDirectory)
.start()
val outputText = process.inputStream.readAllBytes().toString(Charset.defaultCharset())
val error = process.errorStream.readAllBytes().toString(Charset.defaultCharset())
process.waitFor()
val output = outputText.split("\n").mapNotNull {
val commaSplit = it.split(",")
if (commaSplit.size >= 3) {
TagRef(
name = commaSplit.subList(0, commaSplit.size - 2).joinToString(""),
commitId = commaSplit.reversed()[1],
dateTime = Instant.parse(commaSplit.last()),
)
} else {
null
}
}
if (error.isNotEmpty()) {
throw Error(error)
}
return output
}
return output
}

data class TagRef(val name: String, val commitId: String, val dateTime: Instant)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zegreatrob.tools.digger

import com.zegreatrob.tools.digger.core.DiggerGitWrapper
import com.zegreatrob.tools.digger.core.TagRef
import com.zegreatrob.tools.digger.core.allContributionCommits
import com.zegreatrob.tools.digger.core.contribution
Expand All @@ -10,6 +11,7 @@ import org.ajoberstar.grgit.gradle.GrgitServiceExtension
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Input
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.provideDelegate
import java.io.File

open class DiggerExtension(
Expand All @@ -22,15 +24,17 @@ open class DiggerExtension(
@Input
var workingDirectory = objectFactory.property<File>()

private val gitDigger by lazy { DiggerGitWrapper(workingDirectory.get()) }

fun allContributionData() =
grgitServiceExtension.service.get().grgit
.allContributionCommits(workingDirectory.get())
.allContributionCommits(gitDigger)
.map { range -> range.first to range.second.toList().contribution() }
.map { (tag, contributions) -> contributions.copyWithLabelAndTag(tag) }

fun currentContributionData() =
with(grgitServiceExtension.service.get().grgit) {
val currentCommitTag = currentCommitTag(workingDirectory.get())
val currentCommitTag = gitDigger.currentCommitTag()
currentContributionCommits()
.contribution()
.copyWithLabelAndTag(currentCommitTag)
Expand Down

0 comments on commit 1297961

Please sign in to comment.