From 713d121a0353991764c22b4fc54bccad7716ac03 Mon Sep 17 00:00:00 2001 From: Andrew Summers Date: Wed, 19 Feb 2020 16:31:10 -0600 Subject: [PATCH 1/2] fix: Fix tag order from git tag function. This list of tags comes in ABC sorted according to git, whereas last_valid_non_rc_version function assumed it would come in tag revision order. This manifests itself in finding e.g. 9.9.0 as the latest revision when 10.0.0 is in the list of tags, as well. Because this tag is used later to calculate the diff, it winds up double counting commits and therefor doing bumps it should not be. --- lib/git_ops/git.ex | 14 ++++++++++---- lib/git_ops/version.ex | 1 - test/version_test.exs | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/git_ops/git.ex b/lib/git_ops/git.ex index 7eb2bf9..cd9bfbd 100644 --- a/lib/git_ops/git.ex +++ b/lib/git_ops/git.ex @@ -41,15 +41,21 @@ defmodule GitOps.Git do def tags(repo) do tags = repo - |> Git.tag!() - |> String.split("\n") + |> Git.rev_list!(["--tags"]) + |> String.split("\n", trim: true) - if Enum.empty?(tags) do + semver_tags = + repo + |> Git.describe!(["--always", "--abbrev=0", "--tags"] ++ tags) + |> String.split("\n", trim: true) + |> Enum.reject(fn tag -> Version.parse(tag) == :error end) + + if Enum.empty?(semver_tags) do raise """ Could not find an appropriate semver tag in git history. Ensure that you have initialized the project and commited the result. """ else - tags + semver_tags end end diff --git a/lib/git_ops/version.ex b/lib/git_ops/version.ex index ebfc3b9..0109f5d 100644 --- a/lib/git_ops/version.ex +++ b/lib/git_ops/version.ex @@ -8,7 +8,6 @@ defmodule GitOps.Version do @spec last_valid_non_rc_version([String.t()], String.t()) :: String.t() | nil def last_valid_non_rc_version(versions, prefix) do versions - |> Enum.reverse() |> Enum.find(fn version -> match?({:ok, %{pre: []}}, parse(prefix, version)) end) diff --git a/test/version_test.exs b/test/version_test.exs index a5c294b..a68cab5 100644 --- a/test/version_test.exs +++ b/test/version_test.exs @@ -167,14 +167,14 @@ defmodule GitOps.Test.VersionTest do end test "last valid non rc is found correctly without prefixes" do - versions = ["0.0.1", "0.1.0-rc0", "0.1.0", "0.2.0-alpha"] + versions = ["0.2.0-alpha", "0.1.0", "0.1.0-rc0", "0.0.1"] last_rc = Version.last_valid_non_rc_version(versions, "") assert last_rc == "0.1.0" end test "last valid non rc is found correctly with prefixes" do - versions = ["v0.0.1", "v0.1.0-rc0", "0.1.0", "0.2.0-alpha"] + versions = ["0.2.0-alpha", "0.1.0", "v0.1.0-rc0", "v0.0.1"] last_rc = Version.last_valid_non_rc_version(versions, "v") assert last_rc == "v0.0.1" From 96a1758f887cc18844629e91dd2ba8b7d3c2c615 Mon Sep 17 00:00:00 2001 From: Andrew Summers Date: Wed, 19 Feb 2020 18:00:04 -0600 Subject: [PATCH 2/2] fix: Move version tag parsing logic --- lib/git_ops/git.ex | 1 - lib/git_ops/version.ex | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git_ops/git.ex b/lib/git_ops/git.ex index cd9bfbd..4a4be56 100644 --- a/lib/git_ops/git.ex +++ b/lib/git_ops/git.ex @@ -48,7 +48,6 @@ defmodule GitOps.Git do repo |> Git.describe!(["--always", "--abbrev=0", "--tags"] ++ tags) |> String.split("\n", trim: true) - |> Enum.reject(fn tag -> Version.parse(tag) == :error end) if Enum.empty?(semver_tags) do raise """ diff --git a/lib/git_ops/version.ex b/lib/git_ops/version.ex index 0109f5d..0564f34 100644 --- a/lib/git_ops/version.ex +++ b/lib/git_ops/version.ex @@ -8,6 +8,7 @@ defmodule GitOps.Version do @spec last_valid_non_rc_version([String.t()], String.t()) :: String.t() | nil def last_valid_non_rc_version(versions, prefix) do versions + |> Enum.reject(fn tag -> parse(prefix, tag) == :error end) |> Enum.find(fn version -> match?({:ok, %{pre: []}}, parse(prefix, version)) end)