From 992a1135836dadb9494bdaa7c0363dffc3b965c8 Mon Sep 17 00:00:00 2001 From: Stefan Zeiger Date: Thu, 23 Feb 2017 15:47:52 +0100 Subject: 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. --- project/VersionUtil.scala | 38 ++++++++++++++++++++++++++++---------- project/plugins.sbt | 5 +++++ 2 files changed, 33 insertions(+), 10 deletions(-) (limited to 'project') 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 ) -- cgit v1.2.3