Skip to content

Commit 6d1cde4

Browse files
committed
Treat v-prefixed and non-prefixed semver tags as equivalent
1 parent 7ff4f44 commit 6d1cde4

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

internal/gitsemver/versioninfo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ func (vi *VersionInfo) GoPackage(repo, pkgName string) (retv string, err error)
8686
}
8787

8888
func (vi *VersionInfo) HasTag(tag string) bool {
89+
tagCore := strings.TrimPrefix(tag, "v")
90+
tagIsSemver := reMatchSemver.MatchString(tag)
8991
for _, gt := range vi.Tags {
9092
if gt.Tag == tag {
9193
return true
9294
}
95+
// Treat v-prefixed and non-prefixed semver tags as equivalent.
96+
if tagIsSemver && reMatchSemver.MatchString(gt.Tag) && strings.TrimPrefix(gt.Tag, "v") == tagCore {
97+
return true
98+
}
9399
}
94100
return false
95101
}

internal/gitsemver/versioninfo_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ func Test_VersionInfo_IncPatch_PrereleaseTag(t *testing.T) {
8686
}
8787
}
8888

89+
func Test_VersionInfo_IncPatch_AvoidsEquivalentVPrefixCollision(t *testing.T) {
90+
vi := &gitsemver.VersionInfo{
91+
Tag: "1.2.0",
92+
Tags: []gitsemver.GitTag{
93+
{Tag: "v1.2.1"},
94+
},
95+
}
96+
if got := vi.IncPatch(); got != "1.2.2" {
97+
t.Fatalf("expected 1.2.2, got %q", got)
98+
}
99+
}
100+
101+
func Test_VersionInfo_IncPatch_AvoidsEquivalentNoPrefixCollision(t *testing.T) {
102+
vi := &gitsemver.VersionInfo{
103+
Tag: "v1.2.0",
104+
Tags: []gitsemver.GitTag{
105+
{Tag: "1.2.1"},
106+
},
107+
}
108+
if got := vi.IncPatch(); got != "v1.2.2" {
109+
t.Fatalf("expected v1.2.2, got %q", got)
110+
}
111+
}
112+
89113
func Test_VersionInfo_IncPatch_InvalidTagNoLoop(t *testing.T) {
90114
vi := &gitsemver.VersionInfo{Tag: "not-a-semver-tag"}
91115
if got := vi.IncPatch(); got != "not-a-semver-tag" {

0 commit comments

Comments
 (0)