Skip to content
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

New format option untaggedSuffix #66

Merged
merged 5 commits into from
Sep 1, 2022
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
3 changes: 2 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 33 additions & 5 deletions core/src/de/tobiasroeser/mill/vcs/version/VcsState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""

Expand All @@ -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"
}

/**
Expand All @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a naive question, but is this needed? With the new change it won't break compat for existing users right because it has a default value?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically, you're right, as most Mill plugins are used in build scripts, hence their use site is always recompiled, when the version changes.

But with your new mill-ci-release plugin, we started to have downstream users who depend on binary published artifacts. And there is a high change of really breaking the build, when the user is also explicitly importing both plugins.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, so you're saying it's my fault! 😆

Kidding, makes sense.

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 {
Expand Down
16 changes: 16 additions & 0 deletions core/test/src/de/tobiasroeser/mill/vcs/version/VcsStateSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down