Skip to content

Commit

Permalink
Add support to custom version number ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
fcv committed Dec 6, 2021
1 parent c392e14 commit 4b1fff8
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ target/
/.cache
/.classpath
/.project

# BSP (Build Server Protocol)
# see https://www.scala-lang.org/blog/2020/10/27/bsp-in-sbt.html
# see https://build-server-protocol.github.io/
.bsp/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ You can begin to use git to control your project versions.
The git plugin will now autogenerate your version using the following rules, in order:

1. Looks at version-property setting (default to `project.version`), and checks the `sys.props` to see if this has a value. If so, use it.
2. Otherwise, looks at the project tags. The first to match the `gitTagToVersionNumberSetting` is used to assign the version. The default is to look for tags that begin with `v` and a number, and use the number as the version. If there are multiple version tags, it will pick the highest.
2. Otherwise, looks at the project tags. The first to match the `gitTagToVersionNumberSetting` is used to assign the version. The default is to look for tags that begin with `v` and a number, and use the number as the version. If there are multiple version tags, it will pick the highest based on `git.versionNumberOrdering`.
3. If no tags are found either, look at the head commit. We attach this to the `git.baseVersion` setting: "<base-version>.<git commit sha>"
4. If no head commit is present either (which means this is a brand-new repository with no commits yet), we append the current timestamp to the base version: "<base-version>.<timestamp>".

Expand Down
14 changes: 11 additions & 3 deletions src/main/scala/com/typesafe/sbt/SbtGit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.typesafe.sbt
import sbt._
import Keys._
import git.{ConsoleGitRunner, DefaultReadableGit, GitRunner, JGitRunner, ReadableGit}
import scala.math.Ordering
import sys.process.Process

/** This plugin has all the basic 'git' functionality for other plugins. */
Expand All @@ -28,6 +29,7 @@ object SbtGit {
val useGitDescribe = SettingKey[Boolean]("use-git-describe", "Get version by calling `git describe` on the repository")
val gitDescribePatterns = SettingKey[Seq[String]]("git-describe-patterns", "Patterns to `--match` against when using `git describe`")
val gitTagToVersionNumber = SettingKey[String => Option[String]]("git-tag-to-version-number", "Converts a git tag string to a version number.")
val versionNumberOrdering = SettingKey[Option[Ordering[String]]]("version-number-ordering", "The order to be applied when multiple versions are found on current HEAD")

// Component version strings. We use these when determining the actual version.
val formattedShaVersion = settingKey[Option[String]]("Completely formatted version string which will use the git SHA. Override this to change how the SHA version is formatted.")
Expand Down Expand Up @@ -186,6 +188,7 @@ object SbtGit {
val base = git.baseVersion.?.value
git.defaultFormatDateVersion(base, new java.util.Date)
},
versionNumberOrdering in ThisBuild := Some(git.defaultVersionNumberOrdering),
isSnapshot in ThisBuild := {
git.gitCurrentTags.value.isEmpty || git.gitUncommittedChanges.value
},
Expand All @@ -195,7 +198,8 @@ object SbtGit {
val uncommittedSuffix =
git.makeUncommittedSignifierSuffix(git.gitUncommittedChanges.value, git.uncommittedSignifier.value)
val releaseVersion =
git.releaseVersion(git.gitCurrentTags.value, git.gitTagToVersionNumber.value, uncommittedSuffix)
git.releaseVersion(git.gitCurrentTags.value, git.gitTagToVersionNumber.value,
git.versionNumberOrdering.value.getOrElse(Ordering.String.reverse), uncommittedSuffix)
val describedVersion =
git.flaggedOptional(git.useGitDescribe.value, git.describeVersion(git.gitDescribedVersion.value, uncommittedSuffix))
val datedVersion = formattedDateVersion.value
Expand Down Expand Up @@ -225,6 +229,7 @@ object SbtGit {
val gitCurrentBranch = GitKeys.gitCurrentBranch in ThisBuild
val gitTagToVersionNumber = GitKeys.gitTagToVersionNumber in ThisBuild
val baseVersion = GitKeys.baseVersion in ThisBuild
val versionNumberOrdering = GitKeys.versionNumberOrdering in ThisBuild
val versionProperty = GitKeys.versionProperty in ThisBuild
val gitUncommittedChanges = GitKeys.gitUncommittedChanges in ThisBuild
val uncommittedSignifier = GitKeys.uncommittedSignifier in ThisBuild
Expand All @@ -247,6 +252,9 @@ object SbtGit {
baseVersion.map(_ +"-").getOrElse("") + (df format (new java.util.Date))
}

val defaultVersionNumberOrdering: Ordering[String] =
Ordering.comparatorToOrdering(versionsort.VersionHelper.compare).reverse

def flaggedOptional(flag: Boolean, value: Option[String]): Option[String] =
if(flag) value
else None
Expand All @@ -258,14 +266,14 @@ object SbtGit {
gitDescribedVersion.map(_ + suffix)
}

def releaseVersion(currentTags: Seq[String], releaseTagVersion: String => Option[String], suffix: String): Option[String] = {
def releaseVersion(currentTags: Seq[String], releaseTagVersion: String => Option[String], versionNumberOrdering: Ordering[String], suffix: String): Option[String] = {
val releaseVersions =
for {
tag <- currentTags
version <- releaseTagVersion(tag)
} yield version + suffix
// NOTE - Selecting the last tag or the first tag should be an option.
releaseVersions.sortWith { versionsort.VersionHelper.compare(_, _) > 0 }.headOption
releaseVersions.sorted(versionNumberOrdering).headOption
}
def overrideVersion(versionProperty: String) = Option(sys.props(versionProperty))

Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import complete.DefaultParsers._

enablePlugins(GitVersioning)

git.baseVersion := "0.1"
git.versionProperty := "DUMMY_BUILD_VERSION"
git.versionNumberOrdering := None

val checkVersion = inputKey[Unit]("checks the version is the tag versino")
checkVersion := {
val Seq(v) = spaceDelimited("<arg>").parsed
val v2 = version.value
assert(v == v2, s"Expected version is wrong, found ${v2}, expected ${v}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % sys.props("project.version"))
15 changes: 15 additions & 0 deletions src/sbt-test/git-versioning/find-tag-newest-labeled/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
> git init
> git config user.email "[email protected]"
> git config user.name "Tester"
> git add README.md
> git commit -m "test"
> git tag v1.0.0
$ copy-file changes/build.sbt build.sbt
> reload
> checkVersion 1.0.0
> git tag v1.1.0-alpha
> reload
> checkVersion 1.1.0-alpha
> git tag v1.2.0
> reload
> checkVersion 1.2.0
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import complete.DefaultParsers._

enablePlugins(GitVersioning)

git.baseVersion := "0.1"
git.versionProperty := "DUMMY_BUILD_VERSION"

val checkVersion = inputKey[Unit]("checks the version is the tag versino")
checkVersion := {
val Seq(v) = spaceDelimited("<arg>").parsed
val v2 = version.value
assert(v == v2, s"Expected version is wrong, found ${v2}, expected ${v}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
> git init
> git config user.email "[email protected]"
> git config user.name "Tester"
> git add README.md
> git commit -m "test"
> git tag v1.0.0
$ copy-file changes/build.sbt build.sbt
> reload
> checkVersion 1.0.0
> git tag v1.2.0
> reload
> checkVersion 1.2.0
> git tag v1.10.0
> reload
> checkVersion 1.10.0

0 comments on commit 4b1fff8

Please sign in to comment.