diff --git a/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/AllContributionCommits.kt b/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/AllContributionCommits.kt index 60cd856..c7f088a 100644 --- a/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/AllContributionCommits.kt +++ b/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/AllContributionCommits.kt @@ -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>> { - val tagList = listTags(workingDirectory) +fun Grgit.allContributionCommits(gitDigger: DiggerGitWrapper): List>> { + val tagList = gitDigger.listTags() return log().fold(emptyList()) { acc, commit -> val tag = tagList.find { it.commitId == commit.id } if (tag != null) { diff --git a/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/TagRef.kt b/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/TagRef.kt index dfc3ed7..7dc935a 100644 --- a/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/TagRef.kt +++ b/tools/digger-core/src/main/kotlin/com/zegreatrob/tools/digger/core/TagRef.kt @@ -4,9 +4,9 @@ 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 { @@ -14,59 +14,62 @@ fun currentCommitTag(workingDirectory: File): TagRef? { } } -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 { - 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 { + 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) diff --git a/tools/digger-plugin/src/main/kotlin/com/zegreatrob/tools/digger/DiggerExtension.kt b/tools/digger-plugin/src/main/kotlin/com/zegreatrob/tools/digger/DiggerExtension.kt index d6169e9..b748256 100644 --- a/tools/digger-plugin/src/main/kotlin/com/zegreatrob/tools/digger/DiggerExtension.kt +++ b/tools/digger-plugin/src/main/kotlin/com/zegreatrob/tools/digger/DiggerExtension.kt @@ -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 @@ -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( @@ -22,15 +24,17 @@ open class DiggerExtension( @Input var workingDirectory = objectFactory.property() + 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)