Skip to content

Commit 4681dbd

Browse files
retronymadriaanm
authored andcommitted
Resort to old-school git HEAD detection in the build
1 parent 6bcf47d commit 4681dbd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

project/VersionUtil.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import Keys._
55
import java.util.{Date, Locale, Properties, TimeZone}
66
import java.io.{File, FileInputStream}
77
import java.text.SimpleDateFormat
8+
import java.time.Instant
9+
import java.time.format.DateTimeFormatter
10+
import java.time.temporal.{TemporalAccessor, TemporalQueries, TemporalQuery}
811

912
import scala.collection.JavaConverters._
1013
import BuildSettings.autoImport._
@@ -71,8 +74,21 @@ object VersionUtil {
7174
val db = new FileRepositoryBuilder().findGitDir.build
7275
val head = db.resolve("HEAD")
7376
if (head eq null) {
74-
log.info("No git HEAD commit found -- Using current date and 'unknown' SHA")
75-
(new Date, "unknown")
77+
import scala.sys.process._
78+
try {
79+
// Workaround lack of git worktree support in JGit https://bugs.eclipse.org/bugs/show_bug.cgi?id=477475
80+
val sha = List("git", "rev-parse", "HEAD").!!.trim
81+
val commitDateIso = List("git", "log", "-1", "--format=%cI", "HEAD").!!.trim
82+
val date = java.util.Date.from(DateTimeFormatter.ISO_DATE_TIME.parse(commitDateIso, new TemporalQuery[Instant] {
83+
override def queryFrom(temporal: TemporalAccessor): Instant = Instant.from(temporal)
84+
}))
85+
(date, sha.substring(0, 7))
86+
} catch {
87+
case ex: Exception =>
88+
ex.printStackTrace()
89+
log.info("No git HEAD commit found -- Using current date and 'unknown' SHA")
90+
(new Date, "unknown")
91+
}
7692
} else {
7793
val commit = new RevWalk(db).parseCommit(head)
7894
(new Date(commit.getCommitTime.toLong * 1000L), commit.getName.substring(0, 7))

0 commit comments

Comments
 (0)