Skip to content

Commit 1d669a1

Browse files
committed
support build suffixes
1 parent 82e70ca commit 1d669a1

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

pkg/versioninfo.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,30 @@ func (vi *VersionInfo) HasTag(tag string) bool {
8989

9090
// IncPatch increments the patch level of the version, returning the new tag.
9191
func (vi *VersionInfo) IncPatch() string {
92+
baseTag := vi.Tag
93+
// Ignore prerelease/build suffixes when incrementing the patch level.
94+
if idx := strings.IndexAny(baseTag, "-+"); idx > -1 {
95+
if core := baseTag[:idx]; reMatchSemver.MatchString(core) {
96+
baseTag = core
97+
}
98+
}
99+
if !reMatchSemver.MatchString(baseTag) {
100+
vi.SameTree = true
101+
return vi.Tag
102+
}
103+
vi.Tag = baseTag
92104
for strings.Count(vi.Tag, ".") < 2 {
93105
vi.Tag += ".0"
94106
}
95107
for {
96108
patchindex := strings.LastIndexByte(vi.Tag, '.') + 1
97-
if patchlevel, err := strconv.Atoi(vi.Tag[patchindex:]); err == nil {
98-
vi.Tag = vi.Tag[:patchindex] + strconv.Itoa(patchlevel+1)
99-
if !vi.HasTag(vi.Tag) {
100-
break
101-
}
109+
patchlevel, err := strconv.Atoi(vi.Tag[patchindex:])
110+
if err != nil {
111+
break
112+
}
113+
vi.Tag = vi.Tag[:patchindex] + strconv.Itoa(patchlevel+1)
114+
if !vi.HasTag(vi.Tag) {
115+
break
102116
}
103117
}
104118
vi.SameTree = true

pkg/versioninfo_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ func Test_VersionInfo_IncPatch(t *testing.T) {
4545
}
4646
}
4747

48+
func Test_VersionInfo_IncPatch_PrereleaseTag(t *testing.T) {
49+
vi := &gitsemver.VersionInfo{Tag: "v1.2.3-rc.1"}
50+
if got := vi.IncPatch(); got != "v1.2.4" {
51+
t.Fatalf("expected v1.2.4, got %q", got)
52+
}
53+
}
54+
55+
func Test_VersionInfo_IncPatch_InvalidTagNoLoop(t *testing.T) {
56+
vi := &gitsemver.VersionInfo{Tag: "not-a-semver-tag"}
57+
if got := vi.IncPatch(); got != "not-a-semver-tag" {
58+
t.Fatalf("expected unchanged tag, got %q", got)
59+
}
60+
}
61+
4862
func Test_CleanBranch(t *testing.T) {
4963
isEqual(t, "branch-with-dots", gitsemver.CleanBranch("-branch.with..dots"))
5064
isEqual(t, "gitlab-branch", gitsemver.CleanBranch("gitlab---branch"))

0 commit comments

Comments
 (0)