Skip to content

Commit

Permalink
[vergo:minor-release] Fix bug in tag prefix trimming (#12)
Browse files Browse the repository at this point in the history
The tag prefix trimming was using `strings.TrimLeft`, which gave an incorrect
result if the tag prefix happened to contain a digit that was also in the
version number.

For example: `myapp1-1.0.0` would be trimmed to `.0.0` rather than `1.0.0`.

The correct function to use in this case is `strings.TrimPrefix`.
  • Loading branch information
alangibson01 authored Nov 17, 2022
1 parent 9d2e3f5 commit 49aded0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [0.26.0] - 15-11-2022
Fixed bug in tag prefix trimming.

## [0.24.0] - 29-09-2022
Disable strict host checking using the global flag `--disable-strict-host-check` or `-d`.
This is only intended to be used on CI where known_hosts is not cached.
Expand Down
16 changes: 9 additions & 7 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package git
import (
"errors"
"fmt"
"net"
"os"
"regexp"
"sort"
"strings"

"github.com/Masterminds/semver/v3"
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
log "github.com/sirupsen/logrus"
"github.com/sky-uk/vergo/release"
cryptossh "golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"net"
"os"
"regexp"
"sort"
"strings"

"github.com/sky-uk/vergo/release"
)

type SortDirection string
Expand Down Expand Up @@ -248,7 +250,7 @@ func refsWithPrefix(repo *gogit.Repository, prefix string) ([]SemverRef, error)
err = tagRefs.ForEach(func(t *plumbing.Reference) error {
tag := t.Name().String()
if re.MatchString(tag) {
versionString := strings.TrimLeft(tag, tagPrefix)
versionString := strings.TrimPrefix(tag, tagPrefix)
if version, err := semver.NewVersion(versionString); err == nil {
versions = append(versions, SemverRef{Version: version, Ref: t})
} else {
Expand Down
16 changes: 9 additions & 7 deletions git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package git_test
import (
"errors"
"fmt"
"math"
"testing"
"time"

"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
"github.com/thoas/go-funk"
"go.uber.org/atomic"

"github.com/sky-uk/vergo/bump"
. "github.com/sky-uk/vergo/git"
. "github.com/sky-uk/vergo/internal-test"
"github.com/sky-uk/vergo/release"
"github.com/stretchr/testify/assert"
"github.com/thoas/go-funk"
"go.uber.org/atomic"
"math"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -234,7 +236,7 @@ func TestFindLatestTagSameCommitWithPrefix(t *testing.T) {
for patch := 1; patch < 5; patch++ {
versionSuffix := fmt.Sprintf("%d.%d.%d", major, minor, patch)
t.Run(versionSuffix, func(t *testing.T) {
tagPrefix1 := "app" + postfix
tagPrefix1 := "app1" + postfix
version1 := fmt.Sprintf("%d.%d.%d", major, minor, patch)
tag1 := fmt.Sprintf("%s%s", tagPrefix1, version1)
ref1 := r.CreateTag(tag1, r.Head().Hash())
Expand Down

0 comments on commit 49aded0

Please sign in to comment.