mirror of
https://github.com/sciwhiz12/Janitor.git
synced 2024-11-14 15:31:26 +00:00
55 lines
1.9 KiB
Groovy
55 lines
1.9 KiB
Groovy
|
import org.eclipse.jgit.api.Git
|
||
|
import org.eclipse.jgit.lib.PersonIdent
|
||
|
import org.eclipse.jgit.revwalk.RevCommit
|
||
|
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
|
||
|
|
||
|
import java.time.OffsetDateTime
|
||
|
import java.time.format.DateTimeFormatter
|
||
|
|
||
|
buildscript {
|
||
|
repositories {
|
||
|
mavenCentral()
|
||
|
}
|
||
|
|
||
|
dependencies {
|
||
|
classpath "org.eclipse.jgit:org.eclipse.jgit:5.11.0.202103091610-r"
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ext {
|
||
|
versionInfo = [] as HashMap
|
||
|
}
|
||
|
|
||
|
Git git = null
|
||
|
try {
|
||
|
git = Git.wrap(new FileRepositoryBuilder().readEnvironment().findGitDir(project.projectDir).setMustExist(true).build())
|
||
|
Iterator<RevCommit> it = git.log().call().iterator()
|
||
|
if (it.hasNext()) {
|
||
|
RevCommit commit = it.next() // HEAD commit
|
||
|
PersonIdent ident = commit.committerIdent
|
||
|
OffsetDateTime date = OffsetDateTime.ofInstant(ident.when.toInstant(), ident.timeZone.toZoneId())
|
||
|
versionInfo.put("timestamp", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date))
|
||
|
versionInfo.put("hash", commit.name)
|
||
|
}
|
||
|
|
||
|
def describe = git.describe().setTags(true).setLong(true).call()
|
||
|
|
||
|
if (describe == null) { // no candidates found
|
||
|
versionInfo.put("version", "0.0.0-SNAPSHOT" + (versionInfo.containsKey("hash") ? "+" + versionInfo.get("hash") : ""))
|
||
|
} else {
|
||
|
def split = describe.split("-")
|
||
|
def prefix = split[0].startsWith("v") ? split[0].substring(1) : split[0]
|
||
|
int commitCount = Integer.parseInt(split[1])
|
||
|
boolean dirty = !git.status().call().clean
|
||
|
if (commitCount == 0) {
|
||
|
versionInfo.put("version", prefix + (dirty ? "-SNAPSHOT" : ""))
|
||
|
} else {
|
||
|
versionInfo.put("version", prefix + "-SNAPSHOT+" + split[2] + (dirty ? ".dirty" : "") )
|
||
|
}
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
println(e)
|
||
|
logger.info("Exception while getting version info from Git", e)
|
||
|
} finally {
|
||
|
git?.close()
|
||
|
}
|