8000 Get commit SHA and date with JGit by szeiger · Pull Request #5733 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

Get commit SHA and date with JGit #5733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
szeiger committed Feb 28, 2017
commit 992a1135836dadb9494bdaa7c0363dffc3b965c8
38 changes: 28 additions & 10 deletions project/VersionUtil.scala
8000
Original file line number Diff line number Diff line change
Expand Up @@ -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._

8000 Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
22 changes: 0 additions & 22 deletions tools/get-scala-commit-date

This file was deleted.

9 changes: 0 additions & 9 deletions tools/get-scala-commit-date.bat

This file was deleted.

22 changes: 0 additions & 22 deletions tools/get-scala-commit-sha

This file was deleted.

9 changes: 0 additions & 9 deletions tools/get-scala-commit-sha.bat

This file was deleted.

0