File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
9191func (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
Original file line number Diff line number Diff 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+
4862func 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" ))
You can’t perform that action at this time.
0 commit comments