|
1 | | -import scala.sys.process.Process |
| 1 | +import org.eclipse.jgit.storage.file.FileRepositoryBuilder |
| 2 | +import org.eclipse.jgit.lib.{Constants, ObjectId, Ref, Repository} |
| 3 | +import org.eclipse.jgit.revwalk.{RevCommit, RevWalk} |
| 4 | + |
| 5 | +import java.text.SimpleDateFormat |
| 6 | +import java.util.Date |
2 | 7 |
|
3 | 8 | object VersionUtil { |
4 | | - def executeScript(scriptName: String) = { |
5 | | - val cmd = |
6 | | - if (System.getProperty("os.name").toLowerCase.contains("windows")) |
7 | | - s"cmd.exe /c project\\scripts\\build\\$scriptName.bat -p" |
8 | | - else s"project/scripts/build/$scriptName" |
9 | | - Process(cmd).lineStream.head.trim |
| 9 | + |
| 10 | + // Adapted from sbt-git |
| 11 | + private class JGit(repo: Repository) { |
| 12 | + def headCommit: ObjectId = |
| 13 | + repo.exactRef(Constants.HEAD).getObjectId |
| 14 | + |
| 15 | + def headCommitSha: String = headCommit.name |
| 16 | + |
| 17 | + def headCommitDate: Date = { |
| 18 | + val walk = new RevWalk(repo) |
| 19 | + val commit = walk.parseCommit(headCommit) |
| 20 | + val seconds = commit.getCommitTime.toLong |
| 21 | + val millis = seconds * 1000L |
| 22 | + new Date(millis) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + private lazy val git = { |
| 27 | + val repo = new FileRepositoryBuilder() |
| 28 | + .setMustExist(true) |
| 29 | + .findGitDir() |
| 30 | + .build() |
| 31 | + new JGit(repo) |
10 | 32 | } |
11 | 33 |
|
12 | 34 | /** Seven letters of the SHA hash is considered enough to uniquely identify a |
13 | 35 | * commit, albeit extremely large projects - such as the Linux kernel - need |
14 | 36 | * more letters to stay unique |
15 | 37 | */ |
16 | | - def gitHash = executeScript("get-scala-commit-sha").substring(0, 7) |
17 | | - def commitDate = executeScript("get-scala-commit-date") |
| 38 | + def gitHash: String = git.headCommitSha.substring(0, 7) |
| 39 | + def commitDate: String = { |
| 40 | + val format = new SimpleDateFormat("yyyyMMdd") |
| 41 | + format.format(git.headCommitDate) |
| 42 | + } |
18 | 43 | } |
0 commit comments