diff --git a/lib/git_ops/git.ex b/lib/git_ops/git.ex index 7eb2bf9..4a4be56 100644 --- a/lib/git_ops/git.ex +++ b/lib/git_ops/git.ex @@ -41,15 +41,20 @@ 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) + + 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..0564f34 100644 --- a/lib/git_ops/version.ex +++ b/lib/git_ops/version.ex @@ -8,7 +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.reverse() + |> Enum.reject(fn tag -> parse(prefix, tag) == :error end) |> 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"