Skip to content

Commit

Permalink
Support more readable git versions (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnaldo committed Feb 8, 2024
1 parent 1210ae1 commit 90a7daf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/result/spec-summary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- version: tags/es2023-candidate-2023-04^0 (d048f32e861c2ed4a26f59a50d392918f26da3ba)
- version: d048f32e861c2ed4a26f59a50d392918f26da3ba (es2023)
- grammar:
- productions: 359
- lexical: 147
Expand Down
6 changes: 6 additions & 0 deletions src/main/scala/esmeta/error/UtilError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ package esmeta.error

sealed abstract class UtilError(msg: String)
extends ESMetaError(msg, "UtilError")

case class InvalidGitVersion(msg: String)
extends UtilError(s"Invalid git version: $msg")

case class GitTagMismatch(hash: String, tagName: String)
extends UtilError(s"Git tag mismatch: $hash != $tagName")
27 changes: 20 additions & 7 deletions src/main/scala/esmeta/util/Git.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
package esmeta.util

import esmeta.error.*
import esmeta.util.BaseUtils.*
import esmeta.util.SystemUtils.*

/** git helpers */
abstract class Git(path: String, shortHashLength: Int = 16) { self =>

/** git versions */
case class Version(name: String, hash: String) {
case class Version(hash: String, tag: Option[String]) {

def git: Git = self

/** get short hashcode */
def shortHash: String = hash.take(shortHashLength)

/** conversion to string */
override def toString: String = s"$name ($hash)"
override def toString: String = hash + tag.fold("")(" (" + _ + ")")

/** hash-based equality check */
override def equals(that: Any): Boolean = that match
case that: Version => this.git == that.git && this.hash == that.hash
case _ => false
}
object Version:
val pattern = "(.+) \\((.+)\\)".r
val simplePattern = "([a-z0-9]+)".r
val tagPattern = "([a-z0-9]+) \\((.+)\\)".r
def apply(string: String): Version = string match
case pattern(name, hash) => Version(name, hash)
case simplePattern(hash) => Version(hash, None)
case tagPattern(hash, tag) => Version(hash, Some(tag))
case _ => throw InvalidGitVersion(string)

/** change git version */
def changeVersion(version: Version): Unit =
Expand All @@ -33,6 +39,15 @@ abstract class Git(path: String, shortHashLength: Int = 16) { self =>
def changeVersion(target: String): Unit =
executeCmd(s"git checkout $target", path)

/** get git hash */
def getHash(target: String): String =
executeCmd(s"git rev-list -n 1 $target", path).trim

/** get git tag */
def getTag(target: String): Option[String] = optional(
executeCmd(s"git describe --tags --exact-match $target", path).trim,
)

/** apply git patch */
def applyPatch(patch: String): Unit = executeCmd(s"patch -p1 < $patch", path)

Expand All @@ -45,9 +60,7 @@ abstract class Git(path: String, shortHashLength: Int = 16) { self =>

/** get git commit version */
def getVersion(target: String): Version =
val name = executeCmd(s"git name-rev --name-only $target", path).trim
val hash = executeCmd(s"git rev-list -n 1 $target", path).trim
Version(name, hash)
Version(getHash(target), getTag(target))

/** get git commit hash for the current version */
def currentVersion: Version = getVersion("HEAD")
Expand Down
6 changes: 4 additions & 2 deletions src/test/scala/esmeta/spec/StringifyTinyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ class StringifyTinyTest extends SpecTest {
// -------------------------------------------------------------------------
// Summary
// -------------------------------------------------------------------------
val version =
Spec.Version("d048f32e861c2ed4a26f59a50d392918f26da3ba", Some("es2023"))
checkParseAndStringify("Summary", Summary)(
Summary(
Some(Spec.Version("main", "2j3foijwo2")),
Some(version),
GrammarSummary(145, 16, 195, 28),
AlgorithmSummary(2258, 365),
StepSummary(18307, 754),
TypeSummary(5469, 439, 1543),
89,
58,
) ->
s"""- version: main (2j3foijwo2)
s"""- version: d048f32e861c2ed4a26f59a50d392918f26da3ba (es2023)
|- grammar:
| - productions: 356
| - lexical: 145
Expand Down

0 comments on commit 90a7daf

Please sign in to comment.