Skip to content

Commit a46104f

Browse files
committed
Resolve issues with finding the latest tag
Signed-off-by: Andrew Brandt <[email protected]>
1 parent 362b974 commit a46104f

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

latest/latest.go

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,51 +60,74 @@ func findLatestVersionTag(repo *git.Repository, majorVersionFilter int, includeP
6060
}
6161

6262
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()
6465

6566
if err != nil {
6667
return nil, err
6768
}
6869

69-
latestTagHash := strings.TrimSpace(string(output))
70-
71-
// Retrieve the tag by its hash
70+
// Retrieve the tags from the repository
7271
tagIter, err := repo.Tags()
7372
if err != nil {
7473
return nil, err
7574
}
7675
defer tagIter.Close()
7776

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
7980
for tag, err := tagIter.Next(); err != io.EOF; tag, err = tagIter.Next() {
8081
if err != nil {
8182
return nil, err
8283
}
8384

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
8895
}
89-
}
9096

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)
93104
}
94105

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+
}
97110

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+
}
100124
}
101125

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")
105128
}
106129

107-
return foundTag, nil
130+
return latestTag, nil
108131
}
109132

110133
func tagNameToVersion(tagName string) *semver.Version {

0 commit comments

Comments
 (0)