summaryrefslogtreecommitdiff
path: root/project
diff options
context:
space:
mode:
authorStefan Zeiger <szeiger@novocode.com>2017-02-23 15:47:52 +0100
committerStefan Zeiger <szeiger@novocode.com>2017-02-28 13:11:58 +0100
commit992a1135836dadb9494bdaa7c0363dffc3b965c8 (patch)
tree1974f70215225bc5b9c63cc5e119f2fc5caa0637 /project
parent920bc4e31c5415d98c1a7f26aebc790250aafe4a (diff)
downloadscala-992a1135836dadb9494bdaa7c0363dffc3b965c8.tar.gz
scala-992a1135836dadb9494bdaa7c0363dffc3b965c8.tar.bz2
scala-992a1135836dadb9494bdaa7c0363dffc3b965c8.zip
Get commit SHA and date with JGit
In order to get the SHA and date we used to run shell scripts and parse the output of `git` commands. On Windows we even ran a batch file that looked for `bash.exe` and then ran the shell script in bash. Using JGit should be more robust than the old Rube Goldberg implementation. The values produced are the same, except for the time zone. Previously the timestamp was formatted with the local timezone, now we use UTC.
Diffstat (limited to 'project')
-rw-r--r--project/VersionUtil.scala38
-rw-r--r--project/plugins.sbt5
2 files changed, 33 insertions, 10 deletions
diff --git a/project/VersionUtil.scala b/project/VersionUtil.scala
index ebc2488345..6215d54c59 100644
--- a/project/VersionUtil.scala
+++ b/project/VersionUtil.scala
@@ -2,8 +2,10 @@ package scala.build
import sbt._
import Keys._
-import java.util.Properties
+import java.util.{Date, Locale, Properties, TimeZone}
import java.io.{File, FileInputStream}
+import java.text.SimpleDateFormat
+
import scala.collection.JavaConverters._
import BuildSettings.autoImport._
@@ -64,6 +66,7 @@ object VersionUtil {
* suffix is used for releases. All other suffix values are treated as RC / milestone builds. The special suffix
* value "SPLIT" is used to split the real suffix off from `baseVersion` instead and then apply the usual logic. */
private lazy val versionPropertiesImpl: Def.Initialize[Versions] = Def.setting {
+ val log = sLog.value
val (base, suffix) = {
val (b, s) = (baseVersion.value, baseVersionSuffix.value)
@@ -74,16 +77,31 @@ object VersionUtil {
} else (b, s)
}
- def executeTool(tool: String) = {
- val cmd =
- if (System.getProperty("os.name").toLowerCase.contains("windows"))
- s"cmd.exe /c tools\\$tool.bat -p"
- else s"tools/$tool"
- Process(cmd).lines.head
+ val (dateObj, sha) = {
+ try {
+ // Use JGit to get the commit date and SHA
+ import org.eclipse.jgit.storage.file.FileRepositoryBuilder
+ import org.eclipse.jgit.revwalk.RevWalk
+ val db = new FileRepositoryBuilder().findGitDir.build
+ val head = db.resolve("HEAD")
+ if(head eq null) {
+ log.info("No git HEAD commit found -- Using current date and 'unknown' SHA")
+ (new Date, "unknown")
+ } else {
+ val commit = new RevWalk(db).parseCommit(head)
+ (new Date(commit.getCommitTime.toLong * 1000L), commit.getName.substring(0, 7))
+ }
+ } catch { case ex: Exception =>
+ log.error("Could not determine commit date + SHA: "+ex)
+ log.trace(ex)
+ (new Date, "unknown")
+ }
+ }
+ val date = {
+ val df = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.ENGLISH)
+ df.setTimeZone(TimeZone.getTimeZone("UTC"))
+ df.format(dateObj)
}
-
- val date = executeTool("get-scala-commit-date")
- val sha = executeTool("get-scala-commit-sha").substring(0, 7)
val (canonicalV, mavenSuffix, osgiV, release) = suffix match {
case "SNAPSHOT" => (s"$base-$date-$sha", s"-SNAPSHOT", s"$base.v$date-$sha", false)
diff --git a/project/plugins.sbt b/project/plugins.sbt
index ca80c88a9e..58e2d6cdad 100644
--- a/project/plugins.sbt
+++ b/project/plugins.sbt
@@ -21,6 +21,11 @@ buildInfoPackage := "scalabuild"
libraryDependencies += "com.typesafe" %% "mima-reporter" % "0.1.13"
+libraryDependencies ++= Seq(
+ "org.eclipse.jgit" % "org.eclipse.jgit" % "4.6.0.201612231935-r",
+ "org.slf4j" % "slf4j-nop" % "1.7.23"
+)
+
concurrentRestrictions in Global := Seq(
Tags.limitAll(1) // workaround for https://github.com/sbt/sbt/issues/2970
)