diff --git a/README.adoc b/README.adoc index 00c0bc3..975a71f 100644 --- a/README.adoc +++ b/README.adoc @@ -58,8 +58,9 @@ The `format` method has the following options: * `dirtySep: String = "-DIRTY"` - will be printed before the dirty hash if the local repository is in modified state * `dirtyHashDigits: Int = 8` - the number of digits to be used for the dirty hash * `tagModifier: String => String` - allows to modify the git tag (By default this strips a leading `v` if one exists. e.g. v1.2.3 -> 1.2.3) +* `untaggedSuffix: String = "" - will append the given string at the end of the version when the current revision is not tagged (e.g. use with `"-SNAPSHOT"` to publish to Maven Snapshot repositories) -When used with its defaults, the outcome is identical to the version scheme used by mill. +When used with its defaults, the outcome is identical to the version scheme used by Mill. == Download diff --git a/core/src/de/tobiasroeser/mill/vcs/version/VcsState.scala b/core/src/de/tobiasroeser/mill/vcs/version/VcsState.scala index 4d94705..96db8fb 100644 --- a/core/src/de/tobiasroeser/mill/vcs/version/VcsState.scala +++ b/core/src/de/tobiasroeser/mill/vcs/version/VcsState.scala @@ -17,17 +17,21 @@ case class VcsState( revHashDigits: Int = 6, dirtySep: String = "-DIRTY", dirtyHashDigits: Int = 8, - tagModifier: String => String = stripV + tagModifier: String => String = stripV, + untaggedSuffix: String = "" ): String = { val versionPart = tagModifier(lastTag.getOrElse(noTagFallback)) - val commitCountPart = if (lastTag.isEmpty || commitsSinceLastTag > 0) { + val isUntagged = lastTag.isEmpty || commitsSinceLastTag > 0 + + val commitCountPart = if (isUntagged) { s"$countSep${if (commitCountPad > 0) { (10000000000000L + commitsSinceLastTag).toString().substring(14 - commitCountPad, 14) - } else if (commitCountPad == 0) commitsSinceLastTag else ""}" + } else if (commitCountPad == 0) commitsSinceLastTag + else ""}" } else "" - val revisionPart = if (lastTag.isEmpty || commitsSinceLastTag > 0) { + val revisionPart = if (isUntagged) { s"$revSep${currentRevision.take(revHashDigits)}" } else "" @@ -36,7 +40,9 @@ case class VcsState( case Some(d) => dirtySep + d.take(dirtyHashDigits) } - s"$versionPart$commitCountPart$revisionPart$dirtyPart" + val snapshotSuffix = if (isUntagged) untaggedSuffix else "" + + s"$versionPart$commitCountPart$revisionPart$dirtyPart$snapshotSuffix" } /** @@ -52,6 +58,28 @@ case class VcsState( case t => t } + @deprecated("Binary compatibility shim. Use other overload instead.", "mill-vcs-verison after 0.2.0") + private[version] def format( + noTagFallback: String, + countSep: String, + commitCountPad: Byte, + revSep: String, + revHashDigits: Int, + dirtySep: String, + dirtyHashDigits: Int, + tagModifier: String => String + ): String = format( + noTagFallback = noTagFallback, + countSep = countSep, + commitCountPad = commitCountPad, + revSep = revSep, + revHashDigits = revHashDigits, + dirtySep = dirtySep, + dirtyHashDigits = dirtyHashDigits, + tagModifier = tagModifier, + untaggedSuffix = "" + ) + } object VcsState { diff --git a/core/test/src/de/tobiasroeser/mill/vcs/version/VcsStateSpec.scala b/core/test/src/de/tobiasroeser/mill/vcs/version/VcsStateSpec.scala index fe7446c..53d47c8 100644 --- a/core/test/src/de/tobiasroeser/mill/vcs/version/VcsStateSpec.scala +++ b/core/test/src/de/tobiasroeser/mill/vcs/version/VcsStateSpec.scala @@ -63,6 +63,22 @@ class VcsStateSpec extends AnyFreeSpec { ) } + "should append a -SNAPSHOT suffix" in { + assert( + state("0.7.3", 0, null, "61568ec80f2465f3f01ea2c7e92273f4fbf94b01") + .format(untaggedSuffix = "-SNAPSHOT") === "0.7.3" + ) + assert( + state("0.7.3", 4, "a6ea44d3726", "61568ec80f2465f3f01ea2c7e92273f4fbf94b01") + .format(untaggedSuffix = "-SNAPSHOT") === "0.7.3-4-61568e-DIRTYa6ea44d3-SNAPSHOT" + ) + assert( + state("0.7.3", 4, null, "61568ec80f2465f3f01ea2c7e92273f4fbf94b01") + .format(untaggedSuffix = "-SNAPSHOT") === "0.7.3-4-61568e-SNAPSHOT" + ) + } + + "Example format configs" - { "mill" in { assert(