forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionUtil.scala
44 lines (37 loc) · 1.26 KB
/
VersionUtil.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.eclipse.jgit.lib.{Constants, ObjectId, Ref, Repository}
import org.eclipse.jgit.revwalk.{RevCommit, RevWalk}
import java.text.SimpleDateFormat
import java.util.{Date, TimeZone}
object VersionUtil {
// Adapted from sbt-git
private class JGit(repo: Repository) {
def headCommit: ObjectId =
repo.exactRef(Constants.HEAD).getObjectId
def headCommitSha: String = headCommit.name
def headCommitDate: Date = {
val walk = new RevWalk(repo)
val commit = walk.parseCommit(headCommit)
val seconds = commit.getCommitTime.toLong
val millis = seconds * 1000L
new Date(millis)
}
}
private lazy val git = {
val repo = new FileRepositoryBuilder()
.setMustExist(true)
.findGitDir()
.build()
new JGit(repo)
}
/** Seven letters of the SHA hash is considered enough to uniquely identify a
* commit, albeit extremely large projects - such as the Linux kernel - need
* more letters to stay unique
*/
def gitHash: String = git.headCommitSha.substring(0, 7)
def commitDate: String = {
val format = new SimpleDateFormat("yyyyMMdd")
format.setTimeZone(TimeZone.getTimeZone("UTC"))
format.format(git.headCommitDate)
}
}