@@ -51,50 +51,59 @@ func extractPatchAndRC(tag string) (string, string, error) {
5151 return patch , rc , nil
5252}
5353
54- func findPreviousTag (proposedTag string , tags []string ) (string , error ) {
55- var previousTag string
56- proposedMinor := semver .MajorMinor (proposedTag )
57-
58- proposedPatch , proposedRC , err := extractPatchAndRC (proposedTag )
59- if err != nil {
60- return "" , err
54+ func removeInvalidTags (tags []string ) []string {
55+ var validTags []string
56+ for _ , tag := range tags {
57+ if _ , _ , err := extractPatchAndRC (tag ); err == nil {
58+ validTags = append (validTags , tag )
59+ }
6160 }
61+ return validTags
62+ }
6263
64+ func removeNewerOrEqualTags (proposedTag string , tags []string ) []string {
65+ var validTags []string
6366 for _ , tag := range tags {
64- // If this tag is newer than the proposed tag, skip it.
65- if semver .Compare (tag , proposedTag ) > 0 {
66- continue
67+ if semver .Compare (tag , proposedTag ) < 0 {
68+ validTags = append (validTags , tag )
6769 }
70+ }
71+ return validTags
72+ }
6873
69- // If this tag is older than a tag we've already decided is a candidate, skip it.
70- if semver .Compare (tag , previousTag ) <= 0 {
71- continue
72- }
73- tagPatch , tagRC , err := extractPatchAndRC (tag )
74- if err != nil {
75- continue
74+ func removeTagsFromSameMinorSeries (proposedTag string , tags []string ) []string {
75+ var validTags []string
76+ proposedMinor := semver .MajorMinor (proposedTag )
77+ for _ , tag := range tags {
78+ if semver .MajorMinor (tag ) != proposedMinor {
79+ validTags = append (validTags , tag )
7680 }
81+ }
82+ return validTags
83+ }
7784
78- // If it's a non-RC release...
79- if proposedRC == "0" {
80- if proposedPatch == "0" {
81- // ...and we're cutting the first patch of a new minor release series, don't consider tags in the same
82- // minor release series.
83- if semver .MajorMinor (tag ) != proposedMinor {
84- previousTag = tag
85- }
86- } else {
87-
88- previousTag = tag
89- }
90- } else {
91- if tagRC != "0" && tagPatch == proposedPatch {
92- previousTag = tag
93- } else if tagRC == "0" {
94- previousTag = tag
95- }
85+ func getMostRecentTag (tags []string ) string {
86+ var mostRecentTag string
87+ for _ , tag := range tags {
88+ if mostRecentTag == "" || semver .Compare (tag , mostRecentTag ) > 0 {
89+ mostRecentTag = tag
9690 }
9791 }
92+ return mostRecentTag
93+ }
94+
95+ func findPreviousTag (proposedTag string , tags []string ) (string , error ) {
96+ tags = removeInvalidTags (tags )
97+ tags = removeNewerOrEqualTags (proposedTag , tags )
98+
99+ proposedPatch , proposedRC , _ := extractPatchAndRC (proposedTag ) // Ignore the error, we already filtered out invalid tags.
100+ if proposedRC == "0" && proposedPatch == "0" {
101+ // If we're cutting the first patch of a new minor release series, don't consider tags in the same minor release
102+ // series. We want to compare to the latest tag in the previous minor release series.
103+ tags = removeTagsFromSameMinorSeries (proposedTag , tags )
104+ }
105+
106+ previousTag := getMostRecentTag (tags )
98107 if previousTag == "" {
99108 return "" , fmt .Errorf ("no matching tag found for tags: " + strings .Join (tags , ", " ))
100109 }
0 commit comments