@@ -60,51 +60,74 @@ func findLatestVersionTag(repo *git.Repository, majorVersionFilter int, includeP
60
60
}
61
61
62
62
cmd .Dir = worktree .Filesystem .Root () // Use the working directory for git commands
63
- output , err := cmd .Output ()
63
+ // We no longer need to capture the output of the command
64
+ _ , err = cmd .Output ()
64
65
65
66
if err != nil {
66
67
return nil , err
67
68
}
68
69
69
- latestTagHash := strings .TrimSpace (string (output ))
70
-
71
- // Retrieve the tag by its hash
70
+ // Retrieve the tags from the repository
72
71
tagIter , err := repo .Tags ()
73
72
if err != nil {
74
73
return nil , err
75
74
}
76
75
defer tagIter .Close ()
77
76
78
- var foundTag * plumbing.Reference
77
+ var foundTags []* plumbing.Reference
78
+
79
+ // Loop through all tags and filter those that match the semantic versioning format
79
80
for tag , err := tagIter .Next (); err != io .EOF ; tag , err = tagIter .Next () {
80
81
if err != nil {
81
82
return nil , err
82
83
}
83
84
84
- // Check if this tag matches the latest tag hash
85
- if tag .Hash ().String () == latestTagHash {
86
- foundTag = tag
87
- break
85
+ // Skip non-semantic version tags (i.e., tags that don't start with 'v')
86
+ if ! strings .HasPrefix (tag .Name ().Short (), "v" ) {
87
+ continue
88
+ }
89
+
90
+ // Convert the tag to a semver version
91
+ version , err := semver .ParseVersion (tag .Name ().Short ())
92
+ if err != nil {
93
+ // If parsing fails, skip this tag
94
+ continue
88
95
}
89
- }
90
96
91
- if foundTag == nil {
92
- return nil , errors .New ("no matching tag found" )
97
+ // Apply pre-release filter and major version filter
98
+ if (! includePreReleases && len (version .PreReleaseTag ) > 0 ) || (majorVersionFilter >= 0 && version .Major != majorVersionFilter ) {
99
+ continue
100
+ }
101
+
102
+ // Add the tag to the list of valid semantic version tags
103
+ foundTags = append (foundTags , tag )
93
104
}
94
105
95
- // Convert the tag to a semver version and apply filters
96
- version := tagNameToVersion (foundTag .Name ().Short ())
106
+ // If no valid tags were found
107
+ if len (foundTags ) == 0 {
108
+ return nil , errors .New ("no matching semantic version tags found" )
109
+ }
97
110
98
- if version == nil || (! includePreReleases && len (version .PreReleaseTag ) > 0 ) {
99
- return nil , nil
111
+ // Find the highest version tag
112
+ var latestTag * plumbing.Reference
113
+ var maxVersion * semver.Version
114
+ for _ , tag := range foundTags {
115
+ version , err := semver .ParseVersion (tag .Name ().Short ())
116
+ if err != nil {
117
+ // Skip invalid versions
118
+ continue
119
+ }
120
+ if maxVersion == nil || semver .CompareVersions (version , maxVersion ) > 0 {
121
+ maxVersion = version
122
+ latestTag = tag
123
+ }
100
124
}
101
125
102
- // Apply the major version filter
103
- if majorVersionFilter >= 0 && version .Major != majorVersionFilter {
104
- return nil , nil
126
+ if latestTag == nil {
127
+ return nil , errors .New ("no valid semantic version tag found" )
105
128
}
106
129
107
- return foundTag , nil
130
+ return latestTag , nil
108
131
}
109
132
110
133
func tagNameToVersion (tagName string ) * semver.Version {
0 commit comments