-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure deterministic results for SortBySemVer() (#559)
Signed-off-by: Kent <kent.rancourt@gmail.com>
- Loading branch information
Showing
3 changed files
with
36 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tag | ||
|
||
import "github.com/Masterminds/semver" | ||
|
||
// semverCollection is a replacement for semver.Collection that breaks version | ||
// comparison ties through a lexical comparison of the original version strings. | ||
// Using this, instead of semver.Collection, when sorting will yield | ||
// deterministic results that semver.Collection will not yield. | ||
type semverCollection []*semver.Version | ||
|
||
// Len returns the length of a collection. The number of Version instances | ||
// on the slice. | ||
func (s semverCollection) Len() int { | ||
return len(s) | ||
} | ||
|
||
// Less is needed for the sort interface to compare two Version objects on the | ||
// slice. If checks if one is less than the other. | ||
func (s semverCollection) Less(i, j int) bool { | ||
comp := s[i].Compare(s[j]) | ||
if comp != 0 { | ||
return comp < 0 | ||
} | ||
return s[i].Original() < s[j].Original() | ||
} | ||
|
||
// Swap is needed for the sort interface to replace the Version objects | ||
// at two different positions in the slice. | ||
func (s semverCollection) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters