mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-03 15:08:18 +01:00
Port remaining groovy build logic to kotlin
Co-authored-by: Jason <11360596+jpenilla@users.noreply.github.com>
This commit is contained in:
parent
1509cf8978
commit
0983167740
@ -1,6 +1,5 @@
|
||||
plugins {
|
||||
`kotlin-dsl`
|
||||
`groovy-gradle-plugin`
|
||||
}
|
||||
|
||||
repositories {
|
||||
|
@ -1,51 +0,0 @@
|
||||
import net.kyori.indra.git.IndraGitExtension
|
||||
import org.eclipse.jgit.lib.Ref
|
||||
import org.eclipse.jgit.lib.Repository
|
||||
import org.eclipse.jgit.revwalk.RevWalk
|
||||
import org.gradle.api.Project
|
||||
|
||||
final class GitUtil {
|
||||
private GitUtil() {
|
||||
}
|
||||
|
||||
static int commitsSinceLastTag(Project project) {
|
||||
def indraGit = project.extensions.findByType(IndraGitExtension.class)
|
||||
if (indraGit == null || !indraGit.isPresent() || indraGit.tags().isEmpty()) {
|
||||
return -1
|
||||
}
|
||||
def tags = indraGit.tags()
|
||||
def depth = 0
|
||||
def walk = new RevWalk(indraGit.git().getRepository())
|
||||
def commit = walk.parseCommit(indraGit.commit())
|
||||
while (true) {
|
||||
for (tag in tags) {
|
||||
if (walk.parseCommit(tag.getLeaf().getObjectId()) == commit) {
|
||||
walk.dispose()
|
||||
return depth
|
||||
}
|
||||
}
|
||||
depth++
|
||||
commit = walk.parseCommit(commit.getParents()[0])
|
||||
}
|
||||
}
|
||||
|
||||
static String headBranchName(Project project) {
|
||||
if (System.getenv("GITHUB_HEAD_REF") != null && !System.getenv("GITHUB_HEAD_REF").isEmpty()) {
|
||||
return System.getenv("GITHUB_HEAD_REF")
|
||||
} else if (System.getenv("GITHUB_REF") != null && !System.getenv("GITHUB_REF").isEmpty()) {
|
||||
return System.getenv("GITHUB_REF").replaceFirst("refs/heads/", "")
|
||||
}
|
||||
|
||||
def indraGit = project.extensions.findByType(IndraGitExtension.class)
|
||||
if (!indraGit.isPresent()) {
|
||||
return "detached-head"
|
||||
}
|
||||
|
||||
Ref ref = indraGit.git().getRepository().exactRef('HEAD')?.target
|
||||
if (ref == null) {
|
||||
return "detached-head"
|
||||
}
|
||||
|
||||
return Repository.shortenRefName(ref.name)
|
||||
}
|
||||
}
|
46
build-logic/src/main/kotlin/GitUtil.kt
Normal file
46
build-logic/src/main/kotlin/GitUtil.kt
Normal file
@ -0,0 +1,46 @@
|
||||
import net.kyori.indra.git.IndraGitExtension
|
||||
import org.eclipse.jgit.lib.Repository
|
||||
import org.eclipse.jgit.revwalk.RevWalk
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.kotlin.dsl.findByType
|
||||
|
||||
object GitUtil {
|
||||
@JvmStatic
|
||||
fun commitsSinceLastTag(project: Project): Int? {
|
||||
val indraGit = project.extensions.findByType(IndraGitExtension::class)?.takeIf {
|
||||
it.isPresent && it.tags().isNotEmpty()
|
||||
} ?: return null
|
||||
val git = indraGit.git() ?: return null
|
||||
|
||||
val tags = indraGit.tags()
|
||||
var depth = 0
|
||||
val walk = RevWalk(git.repository)
|
||||
var commit = walk.parseCommit(indraGit.commit())
|
||||
while (true) {
|
||||
for (tag in tags) {
|
||||
if (walk.parseCommit(tag.leaf.objectId) == commit) {
|
||||
walk.dispose()
|
||||
return depth
|
||||
}
|
||||
}
|
||||
depth++
|
||||
commit = walk.parseCommit(commit.parents[0])
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun headBranchName(project: Project): String {
|
||||
System.getenv("GITHUB_HEAD_REF")?.takeIf { it.isNotEmpty() }
|
||||
?.let { return it }
|
||||
System.getenv("GITHUB_REF")?.takeIf { it.isNotEmpty() }
|
||||
?.let { return it.replaceFirst("refs/heads/", "") }
|
||||
|
||||
val indraGit = project.extensions.findByType(IndraGitExtension::class)
|
||||
?.takeIf { it.isPresent }
|
||||
|
||||
val ref = indraGit?.git()?.repository?.exactRef("HEAD")?.target
|
||||
?: return "detached-head"
|
||||
|
||||
return Repository.shortenRefName(ref.name)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user