Skip to content

Commit 94b2d81

Browse files
committed
Use JGit in the build instead of shell and bat scripts
1 parent 801c761 commit 94b2d81

File tree

6 files changed

+36
-61
lines changed

6 files changed

+36
-61
lines changed

project/VersionUtil.scala

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
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
27

38
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)
1032
}
1133

1234
/** Seven letters of the SHA hash is considered enough to uniquely identify a
1335
* commit, albeit extremely large projects - such as the Linux kernel - need
1436
* more letters to stay unique
1537
*/
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+
}
1843
}

project/build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Used by VersionUtil to get gitHash and commitDate
2+
libraryDependencies += "org.eclipse.jgit" % "org.eclipse.jgit" % "4.11.0.201803080745-r"

project/scripts/build/get-scala-commit-date

Lines changed: 0 additions & 16 deletions
This file was deleted.

project/scripts/build/get-scala-commit-date.bat

Lines changed: 0 additions & 9 deletions
This file was deleted.

project/scripts/build/get-scala-commit-sha

Lines changed: 0 additions & 18 deletions
This file was deleted.

project/scripts/build/get-scala-commit-sha.bat

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)